m4.5d

Polynomial weight model model

using DynamicHMCModels

ProjDir = rel_path_d("..", "scripts", "04")
cd(ProjDir)

Import the dataset.

howell1 = CSV.read(rel_path("..", "data", "Howell1.csv"), delim=';')
df = convert(DataFrame, howell1);

544 rows × 4 columns

heightweightagemale
Float64Float64Float64Int64
1151.76547.825663.01
2139.736.485863.00
3136.52531.864865.00
4156.84553.041941.01
5145.41541.276951.00
6163.8362.992635.01
7149.22538.243532.00
8168.9155.4827.01
9147.95534.869919.00
10165.154.487754.01
11154.30549.895147.00
12151.1341.220266.01
13144.7836.032273.00
14149.947.720.00
15150.49533.849365.30
16163.19548.562736.01
17157.4842.325844.01
18143.94238.356931.00
19121.9219.617912.01
20105.4113.9488.00
2186.3610.48936.50
22161.2948.987939.01
23156.2142.722729.00
24129.5423.586813.01
25109.2215.98917.00
26146.435.493656.01
27148.5937.903345.00
28147.3235.465219.00
29137.1627.328917.01
30125.7322.679616.00
31114.317.860211.01
32147.95540.31329.01
33161.92555.111430.01
34146.0537.506424.00
35146.0538.498635.00
36152.70546.606633.00
37142.87538.838827.00
38142.87535.578632.00
39147.95547.400436.00
40160.65547.882324.01
41151.76549.413230.01
42162.86549.384824.01
43171.4556.557352.01
44147.3239.122342.00
45147.95549.895119.00
46144.7828.803117.00
47121.9220.41168.01
48128.90523.3612.00
4997.7913.26765.00
50154.30541.248555.01
51143.5138.555343.00
52146.742.420.01
53157.4844.650518.01
54127.022.010613.01
55110.4915.42219.00
5697.7912.75735.00
57165.73558.598442.01
58152.446.7244.00
59141.60544.225260.00
60158.850.920.00
61155.57554.317637.00
62164.46545.897850.01
63151.76548.024150.00
64161.2952.219831.01
65154.30547.627225.00
66145.41545.642723.00
67145.41542.410952.00
68152.436.485879.31
69163.8355.933635.01
70144.14537.194527.00
71129.5424.550713.01
72129.5425.627914.00
73153.6748.307538.01
74142.87537.336339.00
75146.0529.596912.00
76167.00547.173630.01
77158.4247.28724.00
7891.4412.92740.61
79165.73557.549551.01
80149.8637.931646.00
81147.95541.900617.00
82137.79527.584112.00
83154.9447.201922.00
84160.9643.204629.01
85161.92550.263738.01
86147.95539.377530.00
87113.66517.46336.01
88159.38550.68945.01
89148.5939.434247.00
90136.52536.287479.00
91158.11546.266445.01
92144.7842.269154.00
93156.84547.627231.01
94179.0755.706823.01
95118.74518.82419.00
96170.1848.562741.01
97146.0542.807723.00
98147.3235.068336.00
99113.0317.88855.01
100162.5656.755730.00
101133.98527.442312.01
102152.451.255934.00
103160.0247.230344.01
104149.8640.936743.00
105142.87532.715373.30
106167.00557.067538.01
107159.38542.977843.01
108154.9439.944433.00
109148.5932.460216.00
110111.12517.123111.01
111111.7616.49946.01
112162.5645.954535.01
113152.441.106829.00
114124.4618.257112.00
115111.7615.08199.01
11686.3611.48157.61
117170.1847.598858.01
118146.0537.506453.00
119159.38545.01951.01
120151.1342.269148.00
121160.65554.856329.01
122169.54553.523941.01
123158.7552.191481.751
12474.2959.752231.01
125149.8642.410935.00
126153.03549.583346.00
12796.5213.09755.01
128161.92541.730529.01
129162.5656.018642.01
130149.22542.155727.00
131116.8419.39118.00
132100.07615.08196.01
133163.19553.098622.01
134161.92550.235343.01
135145.41542.524353.00
136163.19549.101343.01
137151.1338.498641.00
138150.49549.810150.00
139141.60529.313415.01
140170.81559.760733.01
14191.4411.70833.00
142157.4847.93962.01
143152.439.292449.00
144149.22538.130117.01
145129.5421.999212.00
146147.3236.882722.00
147145.41542.127429.00
148121.9219.7888.00
149113.66516.78295.01
150157.4844.565433.01
151154.30547.85434.00
152120.6521.177112.00
153115.618.97.01
154167.00555.196542.01
155142.87532.998840.00
156152.440.8827.00
15796.5213.26763.00
158160.051.225.01
159159.38549.044629.01
160149.8653.438845.00
161160.65554.090826.01
162160.65555.366645.01
163149.22542.240845.00
164125.09522.367811.00
165140.9740.936785.60
166154.9449.696726.01
167141.60544.338624.00
168160.0245.954557.01
169150.16541.957322.00
170155.57551.482724.00
171103.50512.75736.00
17294.61513.01244.00
173156.2144.111821.00
174153.03532.20579.00
175167.00556.755750.01
176149.8652.673440.00
177147.95536.485864.00
178159.38548.846232.01
179161.92556.954138.71
180155.57542.09926.00
181159.38550.178663.01
182146.68546.549962.00
183172.7261.801922.01
184166.3748.987941.01
185141.60531.524619.01
186142.87532.20517.00
187133.3523.756914.00
188127.63524.40899.01
189119.3821.51737.01
190151.76535.295174.00
191156.84545.642741.01
192148.5943.88533.00
193157.4845.557653.00
194149.8639.008918.00
195147.95541.163537.00
196102.23513.12586.00
197153.03545.245861.00
198160.65553.637344.01
199149.22552.304835.00
200114.318.34217.01
201100.96513.74954.01
202138.4339.09423.00
20391.4412.53054.01
204162.5645.699455.01
205149.22540.39853.00
206158.7551.482759.01
207149.8638.668757.00
208158.11539.235735.01
209156.2144.338629.00
210148.5939.519262.01
211143.5131.071118.00
212154.30546.776751.00
213131.44522.509514.00
214157.4840.624819.01
215157.4850.178642.01
216154.30541.276925.00
217107.9517.57676.01
218168.27554.641.01
219145.41544.990737.00
220147.95544.735516.00
221100.96514.40155.01
222113.0319.05099.01
223149.22535.805482.01
224154.9445.217528.01
225162.5648.109150.01
226156.84545.67143.00
227123.1920.80858.01
228161.01148.420931.01
229144.7841.191867.00
230143.5138.413639.00
231149.22542.127418.00
232110.4917.661711.00
233149.8638.243548.00
234165.73548.335930.01
235144.14538.923964.00
236157.4840.029572.01
237154.30550.20768.00
238163.8354.289344.01
239156.2145.643.00
240153.6740.766616.00
241134.6227.130513.00
242144.14539.434234.00
243114.320.496710.00
244162.5643.204662.01
245146.0531.864844.00
246120.6520.893611.01
247154.9445.444231.01
248144.7838.04529.00
249106.6815.98918.00
250146.68536.088962.00
251152.440.8867.00
252163.8347.910757.01
253165.73547.712232.01
254156.2146.379824.00
255152.441.163577.01
256140.33536.599262.00
257158.11543.091217.01
258163.19548.137567.01
259151.1336.712670.00
260171.1256.557337.01
261149.8638.697158.00
262163.8347.485435.01
263141.60536.202330.00
26493.9814.28815.00
265149.22541.276926.00
266105.4115.22375.00
267146.0544.763921.00
268161.2950.433841.01
269162.5655.281546.01
270145.41537.931649.00
271145.41535.493615.01
272170.81558.456728.01
273127.021.488912.00
274159.38544.423783.00
275159.444.454.01
276153.6744.565454.00
277160.0244.622168.01
278150.49540.483168.00
279149.22544.083556.00
280127.024.408915.00
281142.87534.416357.00
282142.11332.77222.00
283147.3235.947240.00
284162.5649.554919.01
285164.46553.183741.01
286160.0237.081175.91
287153.6740.511473.90
288167.00550.603949.01
289151.1343.970126.01
290147.95533.792617.00
291125.421.375513.00
292111.12516.66958.00
293153.03549.8988.01
294139.06533.594268.00
295152.443.856733.01
296154.9448.137526.00
297147.95542.75156.00
298143.5134.841516.01
299117.98324.097113.00
300144.14533.90634.00
30192.7112.07695.00
302147.95541.276917.00
303155.57539.717674.01
304150.49535.947269.00
305155.57550.915750.01
306154.30545.756144.00
307130.60725.259415.00
308101.615.33715.00
309157.4849.214718.00
310168.9158.825241.01
311150.49543.459827.00
312111.7617.83188.91
313160.0251.964638.01
314167.6450.688957.01
315144.14534.246264.50
316145.41539.377542.00
317160.0259.562324.01
318147.3240.31316.01
319164.46552.163171.01
320153.03539.972849.50
321149.22543.941733.01
322160.0254.601128.00
323149.22545.075747.00
32485.0911.45323.01
32584.45511.7651.01
32659.61385.89671.00
32792.7112.10523.01
328111.12518.31386.00
32990.80511.36815.00
330153.6741.333627.00
33199.69516.24435.00
33262.4846.803881.00
33381.91511.87842.01
33496.5214.96852.00
33580.019.865631.01
336150.49541.900655.00
337151.76542.52483.41
338140.6428.859812.01
33988.26512.78562.00
340158.11543.147963.01
341149.22540.823352.00
342151.76542.864449.01
343154.9446.209731.00
344123.82520.58179.00
345104.1415.87576.00
346161.2947.85435.01
347148.5942.524335.00
34897.15517.06647.00
34993.34513.18255.01
350160.65548.50624.01
351157.4845.869541.01
352167.00552.900232.01
353157.4847.570543.01
35491.4412.92746.00
35560.4525.66991.01
356137.1628.916515.01
357152.443.544863.00
358152.443.431421.00
35981.2811.50991.01
360109.2211.70832.00
36171.127.540971.01
36289.204812.70063.00
36367.317.200771.00
36485.0912.36041.01
36569.857.796111.00
366161.92553.21255.00
367152.444.678838.00
36888.912.55883.01
36990.1712.70063.01
37071.7557.370871.00
37183.829.213591.00
372159.38547.201928.01
373142.2428.63316.00
374142.2431.666436.00
375168.9156.443938.01
376123.1920.014712.01
37774.938.504851.01
37874.2958.30641.00
37990.80511.62333.00
380160.0255.791848.01
38167.9457.966211.00
382135.8927.215515.00
383158.11547.485445.01
38485.0910.80123.01
38593.34514.00473.00
386152.445.160838.00
387155.57545.529321.00
388154.30548.874550.00
389156.84546.578241.01
390120.01520.128113.00
391114.318.14378.01
39283.8210.91463.01
393156.2143.88530.00
394137.1627.158812.01
395114.319.05097.01
39693.9813.83464.00
397168.27556.04721.01
398147.95540.086238.00
399139.726.563515.01
400157.4850.802319.00
40176.29.213591.01
40266.047.569321.01
403160.746.331.01
404114.319.41948.00
405146.0537.903316.01
406161.2949.356521.01
40769.857.314170.00
408133.98528.151113.01
40967.9457.824460.01
410150.49544.111850.00
411163.19551.029139.01
412148.5940.766644.01
413148.5937.563136.00
414161.92551.596136.01
415153.6744.820618.00
41668.588.022910.00
417151.1343.403158.00
418163.8346.7258.01
419153.03539.547633.00
420151.76534.784821.50
421132.0822.79311.01
422156.2139.292426.01
423140.33537.449722.00
424158.7548.676128.01
425142.87535.60742.00
42684.4559.383682.01
427151.94343.714921.01
428161.2948.194219.01
429127.99129.85213.01
430160.98550.972448.01
431144.7843.998446.00
432132.0828.292811.01
433117.98320.35498.01
434160.0248.194225.01
435154.9439.17916.01
436160.98546.691651.01
437165.98956.415525.01
438157.98848.59128.01
439154.9448.222526.00
44097.993213.29595.01
44164.1356.662131.00
442160.65547.485454.01
443147.3235.550366.00
444146.736.620.00
445147.3248.959625.00
446172.99951.255938.01
447158.11546.521551.01
448147.3236.967748.00
449124.99325.117713.01
450106.04516.27266.01
451165.98948.647727.01
452149.8638.04522.00
45376.28.504851.00
454161.92547.28760.01
455140.00528.349515.00
45666.6758.136310.00
45762.8657.200770.01
458163.8355.394943.01
459147.95532.488512.01
460160.0254.204227.01
461154.9448.477630.01
462152.443.062929.00
46362.237.257470.00
464146.0534.189523.00
465151.99449.951830.00
466157.4841.305217.01
46755.884.847760.00
46860.966.236890.01
469151.76544.338641.00
470144.7833.452442.00
471118.1116.89637.00
47278.1058.221363.00
473160.65547.28743.01
474151.1346.124635.00
475121.9220.184810.00
47692.7112.75733.01
477153.6747.400475.51
478147.3240.851664.00
479139.750.348738.01
480157.4845.132424.20
48191.4411.62334.00
482154.9442.240826.01
483143.5141.645419.00
48483.1859.156892.01
485158.11545.217543.01
486147.3251.255938.00
487123.82521.205410.01
48888.911.59493.01
489160.0249.271423.01
490137.1627.952616.00
491165.151.199249.01
492154.9443.856741.00
493111.12517.69016.01
494153.6735.521923.00
495145.41534.246214.00
496141.60542.885443.00
497144.7832.545215.00
498163.8346.776721.01
499161.2941.872224.01
500154.938.220.01
501161.343.320.01
502170.1853.637334.01
503149.8642.977829.00
504123.82521.545611.01
50585.0911.42483.00
506160.65539.774365.01
507154.9443.346446.00
508106.04515.47888.00
509126.36521.914215.01
510166.3752.673443.01
511148.28538.441939.00
512124.4619.277712.00
51389.53511.1133.01
514101.613.49444.00
515151.76542.807743.00
516148.5935.890570.00
517153.6744.225226.00
51853.9754.252420.00
519146.68538.073448.00
52056.5155.159610.00
521100.96514.31655.01
522121.9223.21828.01
52381.584810.65943.00
524154.9444.111844.01
525156.2144.026833.00
526132.71524.975915.01
527125.09522.594612.00
528101.614.34485.00
529160.65547.882341.01
530146.0539.405837.40
531132.71524.777513.00
53287.6310.65946.00
533156.2141.050153.01
534152.440.823349.00
535162.5647.031827.00
536114.93517.527.01
53767.9457.229121.00
538142.87534.246231.00
53976.8358.022911.01
540145.41531.127817.01
541162.5652.163131.01
542156.2154.062521.00
54371.128.051260.01
544158.7552.531668.01

Use only adults and standardize

df2 = filter(row -> row[:age] >= 18, df);
df2[:weight] = convert(Vector{Float64}, df2[:weight]);
df2[:weight_s] = (df2[:weight] .- mean(df2[:weight])) / std(df2[:weight]);
df2[:weight_s2] = df2[:weight_s] .^ 2;
352-element Array{Float64,1}:
 0.19280615097192907
 1.7349763044783346 
 4.1325600023146265 
 1.5549757699350777 
 0.3308042660851645 
 7.7736359966100865 
 1.0919440933750901 
 2.6392838898982456 
 2.45691571770544   
 2.163583954566629  
 ⋮                  
 0.2005950450601446 
 0.748125332136797  
 0.37244351134337544
 0.416550377673148  
 0.0999553970190397 
 2.7690646673659187 
 1.2340428738476732 
 1.9741712709264059 
 1.3641165169515888 

Show the first six rows of the dataset.

first(df2, 6)

6 rows × 6 columns

heightweightagemaleweight_sweight_s2
Float64Float64Float64Int64Float64Float64
1151.76547.825663.010.4390970.192806
2139.736.485863.00-1.317181.73498
3136.52531.864865.00-2.032874.13256
4156.84553.041941.011.246991.55498
5145.41541.276951.00-0.5751560.330804
6163.8362.992635.012.788127.77364

Then define a structure to hold the data: observables, covariates, and the degrees of freedom for the prior.

Linear regression model $y ∼ Xβ + ϵ$, where $ϵ ∼ N(0, σ²)$ IID.

struct ConstraintHeightProblem{TY <: AbstractVector, TX <: AbstractMatrix}
    "Observations."
    y::TY
    "Covariates"
    X::TX
end;

Then make the type callable with the parameters as a single argument.

function (problem::ConstraintHeightProblem)(θ)
    @unpack y, X, = problem   # extract the data
    @unpack β, σ = θ            # works on the named tuple too
    ll = 0.0
    ll += logpdf(Normal(178, 100), X[1]) # a = X[1]
    ll += logpdf(Normal(0, 10), X[2]) # b1 = X[2]
    ll += logpdf(Normal(0, 10), X[3]) # b2 = X[3]
    ll += logpdf(TDist(1.0), σ)
    ll += loglikelihood(Normal(0, σ), y .- X*β)
    ll
end

Setup data and inits.

N = size(df2, 1)
X = hcat(ones(N), hcat(df2[:weight_s], df2[:weight_s2]));
y = convert(Vector{Float64}, df2[:height])
p = ConstraintHeightProblem(y, X);
p((β = [1.0, 2.0, 3.0], σ = 1.0))
-4.0013242576346947e6

Use a function to return the transformation (as it varies with the number of covariates).

problem_transformation(p::ConstraintHeightProblem) =
    as((β = as(Array, size(p.X, 2)), σ = asℝ₊))
problem_transformation (generic function with 1 method)

Wrap the problem with a transformation, then use Flux for the gradient.

P = TransformedLogDensity(problem_transformation(p), p)
∇P = LogDensityRejectErrors(ADgradient(:ForwardDiff, P));
LogDensityRejectErrors{InvalidLogDensityException,LogDensityProblems.ForwardDiffLogDensity{TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:β, :σ),Tuple{TransformVariables.ArrayTransform{TransformVariables.Identity,1},TransformVariables.ShiftedExp{true,Float64}}}},Main.ex-m4.5d.ConstraintHeightProblem{Array{Float64,1},Array{Float64,2}}},ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(LogDensityProblems, Symbol("##1#2")){TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:β, :σ),Tuple{TransformVariables.ArrayTransform{TransformVariables.Identity,1},TransformVariables.ShiftedExp{true,Float64}}}},Main.ex-m4.5d.ConstraintHeightProblem{Array{Float64,1},Array{Float64,2}}}},Float64},Float64,4,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(LogDensityProblems, Symbol("##1#2")){TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:β, :σ),Tuple{TransformVariables.ArrayTransform{TransformVariables.Identity,1},TransformVariables.ShiftedExp{true,Float64}}}},Main.ex-m4.5d.ConstraintHeightProblem{Array{Float64,1},Array{Float64,2}}}},Float64},Float64,4},1}}}}(ForwardDiff AD wrapper for TransformedLogDensity of dimension 4, w/ chunk size 4)

Draw samples.

chain, NUTS_tuned = NUTS_init_tune_mcmc(∇P, 1000);
(NUTS_Transition{Array{Float64,1},Float64}[NUTS_Transition{Array{Float64,1},Float64}([154.68144259166908, 6.1879685245233675, -0.0335400815421743, 1.5747045382544886], -1089.3699060031731, 3, DynamicHMC.DoubledTurn, 0.9956208592189933, 7), NUTS_Transition{Array{Float64,1},Float64}([155.06336625059575, 5.775236246745802, -0.34737977466365993, 1.6503933809162654], -1091.3701813334249, 3, DynamicHMC.DoubledTurn, 0.9116578504808491, 7), NUTS_Transition{Array{Float64,1},Float64}([154.98838747908349, 5.723563791147264, -0.2581881761383544, 1.6703005616874123], -1089.4414592986238, 2, DynamicHMC.AdjacentTurn, 0.9761295123241143, 7), NUTS_Transition{Array{Float64,1},Float64}([154.93329241870524, 5.7449871662394925, -0.4475666981488615, 1.7056799868587578], -1092.763196570722, 2, DynamicHMC.DoubledTurn, 0.842098026483578, 3), NUTS_Transition{Array{Float64,1},Float64}([154.6396883506302, 5.877063049463691, -0.20218484166364487, 1.696825291936204], -1092.458008487654, 2, DynamicHMC.DoubledTurn, 1.0, 3), NUTS_Transition{Array{Float64,1},Float64}([154.68698458652275, 5.820397962714695, -0.14203863661287164, 1.670289818170209], -1090.2776641427254, 3, DynamicHMC.DoubledTurn, 0.9598060436145998, 7), NUTS_Transition{Array{Float64,1},Float64}([154.54599069921792, 5.927105166725718, 0.07084436300542657, 1.6119598390156338], -1090.1222029166204, 1, DynamicHMC.AdjacentTurn, 0.8721741083525206, 3), NUTS_Transition{Array{Float64,1},Float64}([154.49080237294976, 6.024977120821876, 0.2507069066886138, 1.6552703534406723], -1090.2285415096237, 1, DynamicHMC.AdjacentTurn, 0.7930806699665314, 3), NUTS_Transition{Array{Float64,1},Float64}([154.58691853768957, 6.021386444496842, 0.2582109349202482, 1.6794485936658554], -1090.3725067097685, 2, DynamicHMC.DoubledTurn, 0.9130508431518797, 3), NUTS_Transition{Array{Float64,1},Float64}([154.80209435140958, 5.876279235940016, 0.17656167759237656, 1.600652497329514], -1090.568775831649, 2, DynamicHMC.AdjacentTurn, 0.99120793876878, 7)  …  NUTS_Transition{Array{Float64,1},Float64}([154.10451580574897, 5.930719893213091, 0.06996837892880195, 1.6585981256332705], -1093.2631000552976, 3, DynamicHMC.DoubledTurn, 0.798666202954589, 7), NUTS_Transition{Array{Float64,1},Float64}([154.19851587297907, 5.483320976521171, 0.10093012795253782, 1.6874211971123527], -1091.312750407877, 2, DynamicHMC.DoubledTurn, 0.8875220769555429, 3), NUTS_Transition{Array{Float64,1},Float64}([154.0916716222212, 5.401279683267812, 0.3587295050422174, 1.6842727823891492], -1092.7074067688022, 2, DynamicHMC.DoubledTurn, 0.91022937237642, 3), NUTS_Transition{Array{Float64,1},Float64}([155.51843012587662, 6.110425031342413, -0.46760489158069707, 1.6566851502073183], -1092.1864449251714, 3, DynamicHMC.DoubledTurn, 1.0, 7), NUTS_Transition{Array{Float64,1},Float64}([154.94430243281627, 6.186019942953062, -0.3223800435858167, 1.5902439678760907], -1092.2564857320162, 2, DynamicHMC.AdjacentTurn, 0.9902381179551946, 7), NUTS_Transition{Array{Float64,1},Float64}([154.17786873206566, 5.571160067758131, 0.3483212006691731, 1.625645830095357], -1091.6069020360947, 3, DynamicHMC.DoubledTurn, 0.9840680824130674, 7), NUTS_Transition{Array{Float64,1},Float64}([155.54115372552806, 6.487786270211303, -0.3377394907775441, 1.6679493754321295], -1094.6726163307173, 2, DynamicHMC.AdjacentTurn, 0.740801550868251, 7), NUTS_Transition{Array{Float64,1},Float64}([154.70965096034342, 6.34620215303118, -0.1760675766977283, 1.6848082635970425], -1094.8748243981952, 2, DynamicHMC.AdjacentTurn, 0.9826149831996952, 7), NUTS_Transition{Array{Float64,1},Float64}([154.60712042398876, 5.327718597513535, -0.0031178985921916647, 1.6117986067265087], -1093.9363211096552, 3, DynamicHMC.AdjacentTurn, 0.8532649285464331, 11), NUTS_Transition{Array{Float64,1},Float64}([154.45006897794553, 6.1877664061505575, 0.5333578863359139, 1.6387100224581679], -1094.407617590381, 3, DynamicHMC.DoubledTurn, 0.8047057764971309, 7)], NUTS sampler in 4 dimensions
  stepsize (ϵ) ≈ 0.648
  maximum depth = 10
  Gaussian kinetic energy, √diag(M⁻¹): [0.357463491409066, 0.28007702228690057, 0.22060922821385556, 0.047842895509854544]
)

We use the transformation to obtain the posterior from the chain.

posterior = TransformVariables.transform.(Ref(problem_transformation(p)), get_position.(chain));
posterior[1:5]
5-element Array{NamedTuple{(:β, :σ),Tuple{Array{Float64,1},Float64}},1}:
 (β = [154.68144259166908, 6.1879685245233675, -0.0335400815421743], σ = 4.829314529595091)
 (β = [155.06336625059575, 5.775236246745802, -0.34737977466365993], σ = 5.209028556614351)
 (β = [154.98838747908349, 5.723563791147264, -0.2581881761383544], σ = 5.313764671265668) 
 (β = [154.93329241870524, 5.7449871662394925, -0.4475666981488615], σ = 5.505127809329699)
 (β = [154.6396883506302, 5.877063049463691, -0.20218484166364487], σ = 5.456596762965159) 

Extract the parameter posterior means: β,

posterior_β = mean(first, posterior)
3-element Array{Float64,1}:
 154.61847426754244    
   5.8299075628152215  
  -0.016146952236143312

then σ:

posterior_σ = mean(last, posterior)
5.095700613912254

Effective sample sizes (of untransformed draws)

ess = mapslices(effective_sample_size,
                get_position_matrix(chain); dims = 1)
1×4 Array{Float64,2}:
 1000.0  1000.0  682.411  1000.0

NUTS-specific statistics

NUTS_statistics(chain)

cmdstan_result = "
Iterations = 1:1000
Thinning interval = 1
Chains = 1,2,3,4
Samples per chain = 1000

Empirical Posterior Estimates:
           Mean         SD       Naive SE       MCSE      ESS
    a 154.609019750 0.36158389 0.0057171433 0.0071845548 1000
   b1   5.838431778 0.27920926 0.0044146860 0.0048693502 1000
   b2  -0.009985954 0.22897191 0.0036203637 0.0047224478 1000
sigma   5.110136300 0.19096315 0.0030193925 0.0030728192 1000

Quantiles:
          2.5%        25.0%        50.0%       75.0%        97.5%
    a 153.92392500 154.3567500 154.60700000 154.8502500 155.32100000
   b1   5.27846200   5.6493250   5.83991000   6.0276275   6.39728200
   b2  -0.45954687  -0.1668285  -0.01382935   0.1423620   0.43600905
sigma   4.76114350   4.9816850   5.10326000   5.2300450   5.51500975
";
"\nIterations = 1:1000\nThinning interval = 1\nChains = 1,2,3,4\nSamples per chain = 1000\n\nEmpirical Posterior Estimates:\n           Mean         SD       Naive SE       MCSE      ESS\n    a 154.609019750 0.36158389 0.0057171433 0.0071845548 1000\n   b1   5.838431778 0.27920926 0.0044146860 0.0048693502 1000\n   b2  -0.009985954 0.22897191 0.0036203637 0.0047224478 1000\nsigma   5.110136300 0.19096315 0.0030193925 0.0030728192 1000\n\nQuantiles:\n          2.5%        25.0%        50.0%       75.0%        97.5%\n    a 153.92392500 154.3567500 154.60700000 154.8502500 155.32100000\n   b1   5.27846200   5.6493250   5.83991000   6.0276275   6.39728200\n   b2  -0.45954687  -0.1668285  -0.01382935   0.1423620   0.43600905\nsigma   4.76114350   4.9816850   5.10326000   5.2300450   5.51500975\n"

Extract the parameter posterior means: β,

[posterior_β, posterior_σ]
2-element Array{Any,1}:
  [154.61847426754244, 5.8299075628152215, -0.016146952236143312]
 5.095700613912254                                               

end of m4.5d.jl#- This page was generated using Literate.jl.