m4.1d

Heights problem

We estimate simple linear regression model with a half-T prior.

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);

352 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
19161.2948.987939.01
20156.2142.722729.00
21146.435.493656.01
22148.5937.903345.00
23147.3235.465219.00
24147.95540.31329.01
25161.92555.111430.01
26146.0537.506424.00
27146.0538.498635.00
28152.70546.606633.00
29142.87538.838827.00
30142.87535.578632.00
31147.95547.400436.00
32160.65547.882324.01
33151.76549.413230.01
34162.86549.384824.01
35171.4556.557352.01
36147.3239.122342.00
37147.95549.895119.00
38154.30541.248555.01
39143.5138.555343.00
40146.742.420.01
41157.4844.650518.01
42165.73558.598442.01
43152.446.7244.00
44141.60544.225260.00
45158.850.920.00
46155.57554.317637.00
47164.46545.897850.01
48151.76548.024150.00
49161.2952.219831.01
50154.30547.627225.00
51145.41545.642723.00
52145.41542.410952.00
53152.436.485879.31
54163.8355.933635.01
55144.14537.194527.00
56153.6748.307538.01
57142.87537.336339.00
58167.00547.173630.01
59158.4247.28724.00
60165.73557.549551.01
61149.8637.931646.00
62154.9447.201922.00
63160.9643.204629.01
64161.92550.263738.01
65147.95539.377530.00
66159.38550.68945.01
67148.5939.434247.00
68136.52536.287479.00
69158.11546.266445.01
70144.7842.269154.00
71156.84547.627231.01
72179.0755.706823.01
73170.1848.562741.01
74146.0542.807723.00
75147.3235.068336.00
76162.5656.755730.00
77152.451.255934.00
78160.0247.230344.01
79149.8640.936743.00
80142.87532.715373.30
81167.00557.067538.01
82159.38542.977843.01
83154.9439.944433.00
84162.5645.954535.01
85152.441.106829.00
86170.1847.598858.01
87146.0537.506453.00
88159.38545.01951.01
89151.1342.269148.00
90160.65554.856329.01
91169.54553.523941.01
92158.7552.191481.751
93149.8642.410935.00
94153.03549.583346.00
95161.92541.730529.01
96162.5656.018642.01
97149.22542.155727.00
98163.19553.098622.01
99161.92550.235343.01
100145.41542.524353.00
101163.19549.101343.01
102151.1338.498641.00
103150.49549.810150.00
104170.81559.760733.01
105157.4847.93962.01
106152.439.292449.00
107147.3236.882722.00
108145.41542.127429.00
109157.4844.565433.01
110154.30547.85434.00
111167.00555.196542.01
112142.87532.998840.00
113152.440.8827.00
114160.051.225.01
115159.38549.044629.01
116149.8653.438845.00
117160.65554.090826.01
118160.65555.366645.01
119149.22542.240845.00
120140.9740.936785.60
121154.9449.696726.01
122141.60544.338624.00
123160.0245.954557.01
124150.16541.957322.00
125155.57551.482724.00
126156.2144.111821.00
127153.03532.20579.00
128167.00556.755750.01
129149.8652.673440.00
130147.95536.485864.00
131159.38548.846232.01
132161.92556.954138.71
133155.57542.09926.00
134159.38550.178663.01
135146.68546.549962.00
136172.7261.801922.01
137166.3748.987941.01
138141.60531.524619.01
139151.76535.295174.00
140156.84545.642741.01
141148.5943.88533.00
142157.4845.557653.00
143149.8639.008918.00
144147.95541.163537.00
145153.03545.245861.00
146160.65553.637344.01
147149.22552.304835.00
148138.4339.09423.00
149162.5645.699455.01
150149.22540.39853.00
151158.7551.482759.01
152149.8638.668757.00
153158.11539.235735.01
154156.2144.338629.00
155148.5939.519262.01
156143.5131.071118.00
157154.30546.776751.00
158157.4840.624819.01
159157.4850.178642.01
160154.30541.276925.00
161168.27554.641.01
162145.41544.990737.00
163149.22535.805482.01
164154.9445.217528.01
165162.5648.109150.01
166156.84545.67143.00
167161.01148.420931.01
168144.7841.191867.00
169143.5138.413639.00
170149.22542.127418.00
171149.8638.243548.00
172165.73548.335930.01
173144.14538.923964.00
174157.4840.029572.01
175154.30550.20768.00
176163.8354.289344.01
177156.2145.643.00
178144.14539.434234.00
179162.5643.204662.01
180146.0531.864844.00
181154.9445.444231.01
182144.7838.04529.00
183146.68536.088962.00
184152.440.8867.00
185163.8347.910757.01
186165.73547.712232.01
187156.2146.379824.00
188152.441.163577.01
189140.33536.599262.00
190163.19548.137567.01
191151.1336.712670.00
192171.1256.557337.01
193149.8638.697158.00
194163.8347.485435.01
195141.60536.202330.00
196149.22541.276926.00
197146.0544.763921.00
198161.2950.433841.01
199162.5655.281546.01
200145.41537.931649.00
201170.81558.456728.01
202159.38544.423783.00
203159.444.454.01
204153.6744.565454.00
205160.0244.622168.01
206150.49540.483168.00
207149.22544.083556.00
208142.87534.416357.00
209142.11332.77222.00
210147.3235.947240.00
211162.5649.554919.01
212164.46553.183741.01
213160.0237.081175.91
214153.6740.511473.90
215167.00550.603949.01
216151.1343.970126.01
217153.03549.8988.01
218139.06533.594268.00
219152.443.856733.01
220154.9448.137526.00
221147.95542.75156.00
222144.14533.90634.00
223155.57539.717674.01
224150.49535.947269.00
225155.57550.915750.01
226154.30545.756144.00
227157.4849.214718.00
228168.9158.825241.01
229150.49543.459827.00
230160.0251.964638.01
231167.6450.688957.01
232144.14534.246264.50
233145.41539.377542.00
234160.0259.562324.01
235164.46552.163171.01
236153.03539.972849.50
237149.22543.941733.01
238160.0254.601128.00
239149.22545.075747.00
240153.6741.333627.00
241150.49541.900655.00
242151.76542.52483.41
243158.11543.147963.01
244149.22540.823352.00
245151.76542.864449.01
246154.9446.209731.00
247161.2947.85435.01
248148.5942.524335.00
249160.65548.50624.01
250157.4845.869541.01
251167.00552.900232.01
252157.4847.570543.01
253152.443.544863.00
254152.443.431421.00
255161.92553.21255.00
256152.444.678838.00
257159.38547.201928.01
258142.2431.666436.00
259168.9156.443938.01
260160.0255.791848.01
261158.11547.485445.01
262152.445.160838.00
263155.57545.529321.00
264154.30548.874550.00
265156.84546.578241.01
266156.2143.88530.00
267168.27556.04721.01
268147.95540.086238.00
269157.4850.802319.00
270160.746.331.01
271161.2949.356521.01
272150.49544.111850.00
273163.19551.029139.01
274148.5940.766644.01
275148.5937.563136.00
276161.92551.596136.01
277153.6744.820618.00
278151.1343.403158.00
279163.8346.7258.01
280153.03539.547633.00
281151.76534.784821.50
282156.2139.292426.01
283140.33537.449722.00
284158.7548.676128.01
285142.87535.60742.00
286151.94343.714921.01
287161.2948.194219.01
288160.98550.972448.01
289144.7843.998446.00
290160.0248.194225.01
291160.98546.691651.01
292165.98956.415525.01
293157.98848.59128.01
294154.9448.222526.00
295160.65547.485454.01
296147.3235.550366.00
297146.736.620.00
298147.3248.959625.00
299172.99951.255938.01
300158.11546.521551.01
301147.3236.967748.00
302165.98948.647727.01
303149.8638.04522.00
304161.92547.28760.01
305163.8355.394943.01
306160.0254.204227.01
307154.9448.477630.01
308152.443.062929.00
309146.0534.189523.00
310151.99449.951830.00
311151.76544.338641.00
312144.7833.452442.00
313160.65547.28743.01
314151.1346.124635.00
315153.6747.400475.51
316147.3240.851664.00
317139.750.348738.01
318157.4845.132424.20
319154.9442.240826.01
320143.5141.645419.00
321158.11545.217543.01
322147.3251.255938.00
323160.0249.271423.01
324165.151.199249.01
325154.9443.856741.00
326153.6735.521923.00
327141.60542.885443.00
328163.8346.776721.01
329161.2941.872224.01
330154.938.220.01
331161.343.320.01
332170.1853.637334.01
333149.8642.977829.00
334160.65539.774365.01
335154.9443.346446.00
336166.3752.673443.01
337148.28538.441939.00
338151.76542.807743.00
339148.5935.890570.00
340153.6744.225226.00
341146.68538.073448.00
342154.9444.111844.01
343156.2144.026833.00
344160.65547.882341.01
345146.0539.405837.40
346156.2141.050153.01
347152.440.823349.00
348162.5647.031827.00
349142.87534.246231.00
350162.5652.163131.01
351156.2154.062521.00
352158.7552.531668.01

Show the first six rows of the dataset.

first(df2, 6)

6 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

Half-T for σ, see below.

struct HeightsProblem{TY <: AbstractVector, Tν <: Real}
    "Observations."
    y::TY
    "Degrees of freedom for prior on sigma."
    ν::Tν
end;

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

function (problem::HeightsProblem)(θ)
    @unpack y, ν = problem   # extract the data
    @unpack μ, σ = θ
    loglikelihood(Normal(μ, σ), y) + logpdf(TDist(ν), σ)
end;

Setup problem with data and inits.

obs = convert(Vector{Float64}, df2[:height]);
p = HeightsProblem(obs, 1.0);
p((μ = 178, σ = 5.0,))
-5170.976519811121

Write a function to return properly dimensioned transformation.

problem_transformation(p::HeightsProblem) =
    as((σ = asℝ₊, μ  = as(Real, 100, 250)), )
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.ShiftedExp{true,Float64},TransformVariables.ScaledShiftedLogistic{Int64}}}},Main.ex-m4.1d.HeightsProblem{Array{Float64,1},Float64}},ForwardDiff.GradientConfig{ForwardDiff.Tag{getfield(LogDensityProblems, Symbol("##1#2")){TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:σ, :μ),Tuple{TransformVariables.ShiftedExp{true,Float64},TransformVariables.ScaledShiftedLogistic{Int64}}}},Main.ex-m4.1d.HeightsProblem{Array{Float64,1},Float64}}},Float64},Float64,2,Array{ForwardDiff.Dual{ForwardDiff.Tag{getfield(LogDensityProblems, Symbol("##1#2")){TransformedLogDensity{TransformVariables.TransformTuple{NamedTuple{(:σ, :μ),Tuple{TransformVariables.ShiftedExp{true,Float64},TransformVariables.ScaledShiftedLogistic{Int64}}}},Main.ex-m4.1d.HeightsProblem{Array{Float64,1},Float64}}},Float64},Float64,2},1}}}}(ForwardDiff AD wrapper for TransformedLogDensity of dimension 2, w/ chunk size 2)

Tune and sample.

chain, NUTS_tuned = NUTS_init_tune_mcmc(∇P, 1000);
(NUTS_Transition{Array{Float64,1},Float64}[NUTS_Transition{Array{Float64,1},Float64}([2.0286197159657395, -0.5598009580347787], -1219.3532902020388, 2, DynamicHMC.AdjacentTurn, 0.999355966571135, 7), NUTS_Transition{Array{Float64,1},Float64}([2.1035408803379507, -0.5449044291088926], -1220.8308867900455, 2, DynamicHMC.DoubledTurn, 0.8087931551449169, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0047960713908113, -0.5834140026520368], -1224.7478851842423, 2, DynamicHMC.DoubledTurn, 0.6690022450147763, 3), NUTS_Transition{Array{Float64,1},Float64}([2.021966871204559, -0.5755147679551557], -1221.7039764132219, 1, DynamicHMC.DoubledTurn, 1.0, 1), NUTS_Transition{Array{Float64,1},Float64}([2.0681216020953186, -0.5411973943155898], -1220.3905216457963, 2, DynamicHMC.AdjacentTurn, 1.0, 7), NUTS_Transition{Array{Float64,1},Float64}([2.023825968604533, -0.5627733294098167], -1221.5327717007956, 1, DynamicHMC.AdjacentTurn, 0.8975386735329957, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0791311884297796, -0.5535537435369932], -1222.500391146448, 2, DynamicHMC.DoubledTurn, 0.6580120040050063, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0084870670329393, -0.5497362702975317], -1220.7896701433892, 2, DynamicHMC.DoubledTurn, 0.8947582098692664, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0103869662508096, -0.5723458578756284], -1221.5534073332535, 2, DynamicHMC.DoubledTurn, 0.8495108377157523, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0053232666801177, -0.5680308759022042], -1220.3760563574738, 2, DynamicHMC.DoubledTurn, 1.0, 3)  …  NUTS_Transition{Array{Float64,1},Float64}([2.085191497359603, -0.5652933004750212], -1223.3218878558084, 2, DynamicHMC.DoubledTurn, 0.5404595971858003, 3), NUTS_Transition{Array{Float64,1},Float64}([2.007096840858527, -0.549225247385076], -1220.0030560118958, 2, DynamicHMC.DoubledTurn, 0.9949901859986762, 3), NUTS_Transition{Array{Float64,1},Float64}([1.9746361949615314, -0.5604937238569611], -1221.360203163533, 2, DynamicHMC.DoubledTurn, 0.9234860301091139, 3), NUTS_Transition{Array{Float64,1},Float64}([2.048424483073958, -0.5476122787239495], -1221.3937069240772, 2, DynamicHMC.DoubledTurn, 0.954937282955858, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0479100635805856, -0.5648214083080484], -1220.398579456042, 2, DynamicHMC.DoubledTurn, 0.8972472907730523, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0387225892863428, -0.5560451762358397], -1219.7789856286995, 2, DynamicHMC.DoubledTurn, 0.936408840825818, 3), NUTS_Transition{Array{Float64,1},Float64}([1.9513439265413752, -0.5573030903257915], -1222.8752213941646, 1, DynamicHMC.AdjacentTurn, 0.4780338477943813, 3), NUTS_Transition{Array{Float64,1},Float64}([2.0726071337206804, -0.5477283134366904], -1224.3383152754377, 1, DynamicHMC.AdjacentTurn, 0.746636698100699, 3), NUTS_Transition{Array{Float64,1},Float64}([2.044106939886548, -0.5407054018673291], -1220.9618601719837, 2, DynamicHMC.DoubledTurn, 0.9022056832395459, 3), NUTS_Transition{Array{Float64,1},Float64}([2.060939497796793, -0.5795610748375615], -1222.5706438155003, 2, DynamicHMC.DoubledTurn, 0.7191095819843493, 3)], NUTS sampler in 2 dimensions
  stepsize (ϵ) ≈ 0.967
  maximum depth = 10
  Gaussian kinetic energy, √diag(M⁻¹): [0.03805148661809207, 0.011042191267764418]
)

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

posterior = TransformVariables.transform.(Ref(problem_transformation(p)), get_position.(chain));
1000-element Array{NamedTuple{(:σ, :μ),Tuple{Float64,Float64}},1}:
 (σ = 7.6035840067253115, μ = 154.539027315885) 
 (σ = 8.195136596563385, μ = 155.05711396182065)
 (σ = 7.424579658161287, μ = 153.7221115915155) 
 (σ = 7.553166438784449, μ = 153.99479457711467)
 (σ = 7.9099511188486655, μ = 155.18636227271)  
 (σ = 7.567221571754781, μ = 154.43590119412184)
 (σ = 7.997517559236255, μ = 154.75604605444917)
 (σ = 7.45203438579522, μ = 154.88883976902596) 
 (σ = 7.466205958113087, μ = 154.1043560171591) 
 (σ = 7.428494893538293, μ = 154.25369709983994)
 ⋮                                              
 (σ = 7.441681570584242, μ = 154.906626428534)  
 (σ = 7.203998326135102, μ = 154.5149843864077) 
 (σ = 7.75567228252298, μ = 154.9627833689666)  
 (σ = 7.751683639522457, μ = 154.3648921716421) 
 (σ = 7.680791404306823, μ = 154.66945337686164)
 (σ = 7.038139970616106, μ = 154.62575528815503)
 (σ = 7.9455111479426, μ = 154.95874270842498)  
 (σ = 7.722259016011593, μ = 155.2035254611476) 
 (σ = 7.853344545400182, μ = 153.8550394887367) 

Extract the parameter posterior means: β,

posterior_μ = mean(last, posterior)
154.59032105775808

then σ:

posterior_σ = mean(first, posterior)
7.748504398651419

Effective sample sizes (of untransformed draws)

ess = mapslices(effective_sample_size,
                get_position_matrix(chain); dims = 1)
1×2 Array{Float64,2}:
 831.564  981.145

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
sigma   7.7641872 0.29928194 0.004732063 0.0055677898 1000
   mu 154.6055177 0.41989355 0.006639100 0.0085038356 1000

Quantiles:
         2.5%      25.0%       50.0%      75.0%       97.5%
sigma   7.21853   7.5560625   7.751355   7.9566775   8.410391
   mu 153.77992 154.3157500 154.602000 154.8820000 155.431000
";
"\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\nsigma   7.7641872 0.29928194 0.004732063 0.0055677898 1000\n   mu 154.6055177 0.41989355 0.006639100 0.0085038356 1000\n\nQuantiles:\n         2.5%      25.0%       50.0%      75.0%       97.5%\nsigma   7.21853   7.5560625   7.751355   7.9566775   8.410391\n   mu 153.77992 154.3157500 154.602000 154.8820000 155.431000\n"

Extract the parameter posterior means: β,

[posterior_μ, posterior_σ]
2-element Array{Float64,1}:
 154.59032105775808 
   7.748504398651419

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