m4.4m
#using Distributed
#@everywhere using MambaModels
using MambaModels
gr(size=(400,400))

# Data
line = Dict{Symbol, Any}()

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

df2 = filter(row -> row[:age] >= 18, df);
mean_weight = mean(df2[:weight])
df2[:weight_c] = convert(Vector{Float64}, df2[:weight]) .- mean_weight ;
line[:x] = convert(Array{Float64,1}, df2[:weight_c]);
line[:y] = convert(Array{Float64,1}, df2[:height]);
line[:xmat] = convert(Array{Float64,2}, [ones(length(line[:x])) line[:x]])
352×2 Array{Float64,2}:
 1.0    2.83512
 1.0   -8.50468
 1.0  -13.1256 
 1.0    8.05143
 1.0   -3.71361
 1.0   18.0021 
 1.0   -6.74701
 1.0   10.4895 
 1.0  -10.1206 
 1.0    9.49725
 ⋮             
 1.0    2.89182
 1.0   -5.58468
 1.0   -3.94041
 1.0   -4.16721
 1.0    2.04133
 1.0  -10.7443 
 1.0    7.17259
 1.0    9.07201
 1.0    7.54114

Model Specification

model = Model(
  y = Stochastic(1,
    (xmat, beta, s2) -> MvNormal(xmat * beta, sqrt(s2)),
    false
  ),
  beta = Stochastic(1, () -> MvNormal([178, 0], [sqrt(10000), sqrt(100)])),
  s2 = Stochastic(() -> Uniform(0, 50))
)
Object of type "Model"
-------------------------------------------------------------------------------
beta:
A monitored node of type "0-element ArrayStochastic{1}"
Float64[]
-------------------------------------------------------------------------------
y:
An unmonitored node of type "0-element ArrayStochastic{1}"
Float64[]
-------------------------------------------------------------------------------
s2:
A monitored node of type "ScalarStochastic"
NaN

Initial Values

inits = [
  Dict{Symbol, Any}(
    :y => line[:y],
    :beta => [rand(Normal(178, 100)), rand(Normal(0, 10))],
    :s2 => rand(Uniform(0, 50))
  )
  for i in 1:3
]
3-element Array{Dict{Symbol,Any},1}:
 Dict(:beta=>[200.815, 3.68352],:y=>[151.765, 139.7, 136.525, 156.845, 145.415, 163.83, 149.225, 168.91, 147.955, 165.1  …  156.21, 160.655, 146.05, 156.21, 152.4, 162.56, 142.875, 162.56, 156.21, 158.75],:s2=>30.1041)  
 Dict(:beta=>[203.827, -0.301006],:y=>[151.765, 139.7, 136.525, 156.845, 145.415, 163.83, 149.225, 168.91, 147.955, 165.1  …  156.21, 160.655, 146.05, 156.21, 152.4, 162.56, 142.875, 162.56, 156.21, 158.75],:s2=>49.33)  
 Dict(:beta=>[295.371, -0.447918],:y=>[151.765, 139.7, 136.525, 156.845, 145.415, 163.83, 149.225, 168.91, 147.955, 165.1  …  156.21, 160.655, 146.05, 156.21, 152.4, 162.56, 142.875, 162.56, 156.21, 158.75],:s2=>2.68406)

Tuning Parameters

scale1 = [0.5, 0.25]
summary1 = identity
eps1 = 0.5

scale2 = 0.5
summary2 = x -> [mean(x); sqrt(var(x))]
eps2 = 0.1
0.1

Define sampling scheme

scheme = [
  Mamba.NUTS([:beta]),
  Mamba.Slice([:s2], 10)
]

setsamplers!(model, scheme)
Object of type "Model"
-------------------------------------------------------------------------------
beta:
A monitored node of type "0-element ArrayStochastic{1}"
Float64[]
-------------------------------------------------------------------------------
y:
An unmonitored node of type "0-element ArrayStochastic{1}"
Float64[]
-------------------------------------------------------------------------------
s2:
A monitored node of type "ScalarStochastic"
NaN

MCMC Simulation

chn = mcmc(model, line, inits, 10000, burnin=1000, chains=3)
Object of type "ModelChains"

Iterations = 1001:10000
Thinning interval = 1
Chains = 1,2,3
Samples per chain = 9000

[25.4187 154.709 0.920336; 26.6117 154.77 0.98207; … ; 27.1471 154.283 0.867431; 25.4818 154.287 0.889286]

[25.374 154.785 0.893591; 25.7082 154.785 0.893591; … ; 26.8278 154.646 0.9003; 25.9587 154.446 0.900654]

[24.7338 154.574 0.900931; 25.2447 154.388 0.901082; … ; 25.9001 154.872 0.909671; 25.7867 154.872 0.909671]

Show draws summary

describe(chn)
Iterations = 1001:10000
Thinning interval = 1
Chains = 1,2,3
Samples per chain = 9000

Empirical Posterior Estimates:
            Mean          SD        Naive SE        MCSE        ESS
     s2  26.16130736 1.989228253 0.01210605763 0.01903204080 9000.0000
beta[1] 154.59957294 0.273740509 0.00166593169 0.00389110458 4949.1696
beta[2]   0.90468999 0.042338635 0.00025766473 0.00032517239 9000.0000

Quantiles:
            2.5%       25.0%        50.0%        75.0%        97.5%
     s2  22.5096692  24.7702763  26.07798872  27.44804314  30.29630489
beta[1] 154.0618462 154.4133301 154.60265035 154.78241808 155.13638960
beta[2]   0.8215179   0.8762166   0.90496442   0.93272244   0.98765192

Convert to MCMCChains.Chains object

chn2 = MCMCChains.Chains(chn.value, String.(chn.names))
Object of type Chains, with data of type 9000×3×3 Array{Float64,3}

Iterations        = 1:9000
Thinning interval = 1
Chains            = 1, 2, 3
Samples per chain = 9000
parameters        = beta[1], beta[2], s2

2-element Array{ChainDataFrame,1}

Summary Statistics
. Omitted printing of 1 columns
│ Row │ parameters │ mean    │ std       │ naive_se    │ mcse        │ ess     │
│     │ Symbol     │ Float64 │ Float64   │ Float64     │ Float64     │ Any     │
├─────┼────────────┼─────────┼───────────┼─────────────┼─────────────┼─────────┤
│ 1   │ beta[1]    │ 154.6   │ 0.273741  │ 0.00166593  │ 0.0038911   │ 5333.08 │
│ 2   │ beta[2]    │ 0.90469 │ 0.0423386 │ 0.000257665 │ 0.000325172 │ 17433.4 │
│ 3   │ s2         │ 26.1613 │ 1.98923   │ 0.0121061   │ 0.019032    │ 10857.3 │

Quantiles

│ Row │ parameters │ 2.5%     │ 25.0%    │ 50.0%    │ 75.0%    │ 97.5%    │
│     │ Symbol     │ Float64  │ Float64  │ Float64  │ Float64  │ Float64  │
├─────┼────────────┼──────────┼──────────┼──────────┼──────────┼──────────┤
│ 1   │ beta[1]    │ 154.062  │ 154.413  │ 154.603  │ 154.782  │ 155.136  │
│ 2   │ beta[2]    │ 0.821518 │ 0.876217 │ 0.904964 │ 0.932722 │ 0.987652 │
│ 3   │ s2         │ 22.5097  │ 24.7703  │ 26.078   │ 27.448   │ 30.2963  │

Describe the MCMCChains

MCMCChains.describe(chn2)
2-element Array{ChainDataFrame,1}

Summary Statistics
. Omitted printing of 1 columns
│ Row │ parameters │ mean    │ std       │ naive_se    │ mcse        │ ess     │
│     │ Symbol     │ Float64 │ Float64   │ Float64     │ Float64     │ Any     │
├─────┼────────────┼─────────┼───────────┼─────────────┼─────────────┼─────────┤
│ 1   │ beta[1]    │ 154.6   │ 0.273741  │ 0.00166593  │ 0.0038911   │ 5333.08 │
│ 2   │ beta[2]    │ 0.90469 │ 0.0423386 │ 0.000257665 │ 0.000325172 │ 17433.4 │
│ 3   │ s2         │ 26.1613 │ 1.98923   │ 0.0121061   │ 0.019032    │ 10857.3 │

Quantiles

│ Row │ parameters │ 2.5%     │ 25.0%    │ 50.0%    │ 75.0%    │ 97.5%    │
│     │ Symbol     │ Float64  │ Float64  │ Float64  │ Float64  │ Float64  │
├─────┼────────────┼──────────┼──────────┼──────────┼──────────┼──────────┤
│ 1   │ beta[1]    │ 154.062  │ 154.413  │ 154.603  │ 154.782  │ 155.136  │
│ 2   │ beta[2]    │ 0.821518 │ 0.876217 │ 0.904964 │ 0.932722 │ 0.987652 │
│ 3   │ s2         │ 22.5097  │ 24.7703  │ 26.078   │ 27.448   │ 30.2963  │

Plot chn2

MCMCChains.plot(chn2)
0 2000 4000 6000 8000 154.0 154.5 155.0 155.5 beta[1] Iteration Sample value 153.5 154.0 154.5 155.0 155.5 0.0 0.5 1.0 1.5 beta[1] Sample value Density 0 2000 4000 6000 8000 0.7 0.8 0.9 1.0 beta[2] Iteration Sample value 0.7 0.8 0.9 1.0 1.1 0 2 4 6 8 beta[2] Sample value Density 0 2000 4000 6000 8000 20 25 30 35 s2 Iteration Sample value 20 25 30 35 0.00 0.05 0.10 0.15 0.20 s2 Sample value Density

End of 04/m4.1m.jl

This page was generated using Literate.jl.