Load the ilmtools package...


In [1]:
include("ilmtools.jl")


Out[1]:
create_loglikelihood (generic function with 1 method)

Generate a population from a bi-variate uniform distribution...


In [2]:
pop_db1 = create_pop_db_univariates(100, Uniform(0,10), Uniform(0,10))


Out[2]:
ind_idxy
117.3214724267728460.706273939122668
229.2314809240220659.846036948667118
331.38305274454913363.8892082096289737
445.9423871362179547.254722392731283
554.6808696484831728.33989052973654
663.699392395001024.598482547418767
770.8326711208547519.729997231932558
883.1323378339453389.136637241904534
994.1379035255337711.631541477712981
10106.7111715128477698.365907203913466
11111.34230590658846136.484382473805206
12121.86744949100560166.430064041751066
13134.0592206149236078.587607500935835
14149.721576636838417.493942118752061
15152.0745698441274634.7177304848878565
16161.5628404041462330.7574889241288107
17173.88810816179657740.36735118330215144
18182.86205438923719646.752863189195772
19195.096601376610458.657344078629945
20206.8107070128577578.711124368957407
21217.8420498086702296.91163257557416
22227.7424712570513026.3302570984383095
23231.34006183386722992.3208863054534645
24248.4360710153368155.691154670125229
25256.986162332352593.0670702324665355
26264.5242759556418795.133520268081531
27274.4757282891538871.9133377748825242
28283.49480898466455478.607843597666742
29291.95449211389561620.5594929107777058
30301.16738020955110777.442061912761442
&vellip&vellip&vellip&vellip

Generate the initial infection and propagate infection through population (SIR model) in continuous time with parameters $\alpha=2$ and $\beta=6$


In [3]:
evdb = infect_recover_loop(pop_db1, "discrete", "SIR", 2, 6)


Out[3]:
edb(100x3 DataFrame
| Row | ind_id | s | i    |
|-----|--------|---|------|
| 1   | 1      | 0 | 8.0  |
| 2   | 2      | 0 | 16.0 |
| 3   | 3      | 0 | 5.0  |
| 4   | 4      | 0 | 9.0  |
| 5   | 5      | 0 | 9.0  |
| 6   | 6      | 0 | 6.0  |
| 7   | 7      | 0 | 12.0 |
| 8   | 8      | 0 | 11.0 |
| 9   | 9      | 0 | 5.0  |
| 10  | 10     | 0 | 11.0 |
| 11  | 11     | 0 | 9.0  |
⋮
| 89  | 89     | 0 | 13.0 |
| 90  | 90     | 0 | 9.0  |
| 91  | 91     | 0 | 4.0  |
| 92  | 92     | 0 | 14.0 |
| 93  | 93     | 0 | 15.0 |
| 94  | 94     | 0 | 9.0  |
| 95  | 95     | 0 | 11.0 |
| 96  | 96     | 0 | 8.0  |
| 97  | 97     | 0 | 11.0 |
| 98  | 98     | 0 | 5.0  |
| 99  | 99     | 0 | 4.0  |
| 100 | 100    | 0 | 8.0  |,[1.0,2.0,2.0,3.0,3.0,3.0,4.0,4.0,4.0,4.0  …  13.0,13.0,13.0,14.0,14.0,14.0,14.0,15.0,15.0,16.0],"discrete")

Generate the efficient log likelihood function


In [4]:
ll = create_loglikelihood(pop_db1, evdb)


Out[4]:
dSI_ll (generic function with 1 method)

Time the log likelihood function (run twice)


In [5]:
ll((2, 6))
@time ll((2, 6))


elapsed time: 0.000992869 seconds (376984 bytes allocated)
Out[5]:
-73.74265521426908

Run MCMC


In [6]:
n = 20000
burnin = 10000
sim = Chains(n, 2, names = ["alpha", "beta"])
theta = AMMVariate([2.0, 6.0])
SigmaF = cholfact(eye(2))
@time for i in 1:n
    amm!(theta, SigmaF, ll, adapt = (i <= burnin))
    sim[i,:,1] = theta[1:2]
end
Mamba.describe(sim)


elapsed time: 54.885969224 seconds (14565503944 bytes allocated, 27.72% gc time)
Iterations = 1:20000
Thinning interval = 1
Chains = 1
Samples per chain = 20000

Empirical Posterior Estimates:
3x6 Array{Any,2}:
 ""        "Mean"   "SD"      "Naive SE"   "MCSE"         "ESS"
 "alpha"  1.81213  0.374386  0.00264731   0.00688161  7693.86  
 "beta"   6.43645  0.514608  0.00363883   0.0101574   7164.86  

Quantiles:
3x6 Array{Any,2}:
 ""        "2.5%"   "25.0%"   "50.0%"   "75.0%"   "97.5%"
 "alpha"  1.17396  1.54992   1.7803    2.03924   2.635   
 "beta"   5.49502  6.06808   6.4071    6.77212   7.52396 

Produce trace plots of $\alpha$ and $\beta$


In [7]:
plot(x=burnin:n, y=sim.value[burnin:n, 1, 1], Geom.line)


Out[7]:
x -5.0×10³ 0 5.0×10³ 1.0×10⁴ 1.5×10⁴ 2.0×10⁴ 2.5×10⁴ 3.0×10⁴ 3.5×10⁴ 0 5.00×10² 1.00×10³ 1.50×10³ 2.00×10³ 2.50×10³ 3.00×10³ 3.50×10³ 4.00×10³ 4.50×10³ 5.00×10³ 5.50×10³ 6.00×10³ 6.50×10³ 7.00×10³ 7.50×10³ 8.00×10³ 8.50×10³ 9.00×10³ 9.50×10³ 1.00×10⁴ 1.05×10⁴ 1.10×10⁴ 1.15×10⁴ 1.20×10⁴ 1.25×10⁴ 1.30×10⁴ 1.35×10⁴ 1.40×10⁴ 1.45×10⁴ 1.50×10⁴ 1.55×10⁴ 1.60×10⁴ 1.65×10⁴ 1.70×10⁴ 1.75×10⁴ 1.80×10⁴ 1.85×10⁴ 1.90×10⁴ 1.95×10⁴ 2.00×10⁴ 2.05×10⁴ 2.10×10⁴ 2.15×10⁴ 2.20×10⁴ 2.25×10⁴ 2.30×10⁴ 2.35×10⁴ 2.40×10⁴ 2.45×10⁴ 2.50×10⁴ 2.55×10⁴ 2.60×10⁴ 2.65×10⁴ 2.70×10⁴ 2.75×10⁴ 2.80×10⁴ 2.85×10⁴ 2.90×10⁴ 2.95×10⁴ 3.00×10⁴ 0 1×10⁴ 2×10⁴ 3×10⁴ 0 1.0×10³ 2.0×10³ 3.0×10³ 4.0×10³ 5.0×10³ 6.0×10³ 7.0×10³ 8.0×10³ 9.0×10³ 1.0×10⁴ 1.1×10⁴ 1.2×10⁴ 1.3×10⁴ 1.4×10⁴ 1.5×10⁴ 1.6×10⁴ 1.7×10⁴ 1.8×10⁴ 1.9×10⁴ 2.0×10⁴ 2.1×10⁴ 2.2×10⁴ 2.3×10⁴ 2.4×10⁴ 2.5×10⁴ 2.6×10⁴ 2.7×10⁴ 2.8×10⁴ 2.9×10⁴ 3.0×10⁴ -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 -5 0 5 10 -4.0 -3.5 -3.0 -2.5 -2.0 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 y

In [8]:
plot(x=burnin:n, y=sim.value[burnin:n, 2, 1], Geom.line)


Out[8]:
x -5.0×10³ 0 5.0×10³ 1.0×10⁴ 1.5×10⁴ 2.0×10⁴ 2.5×10⁴ 3.0×10⁴ 3.5×10⁴ 0 5.00×10² 1.00×10³ 1.50×10³ 2.00×10³ 2.50×10³ 3.00×10³ 3.50×10³ 4.00×10³ 4.50×10³ 5.00×10³ 5.50×10³ 6.00×10³ 6.50×10³ 7.00×10³ 7.50×10³ 8.00×10³ 8.50×10³ 9.00×10³ 9.50×10³ 1.00×10⁴ 1.05×10⁴ 1.10×10⁴ 1.15×10⁴ 1.20×10⁴ 1.25×10⁴ 1.30×10⁴ 1.35×10⁴ 1.40×10⁴ 1.45×10⁴ 1.50×10⁴ 1.55×10⁴ 1.60×10⁴ 1.65×10⁴ 1.70×10⁴ 1.75×10⁴ 1.80×10⁴ 1.85×10⁴ 1.90×10⁴ 1.95×10⁴ 2.00×10⁴ 2.05×10⁴ 2.10×10⁴ 2.15×10⁴ 2.20×10⁴ 2.25×10⁴ 2.30×10⁴ 2.35×10⁴ 2.40×10⁴ 2.45×10⁴ 2.50×10⁴ 2.55×10⁴ 2.60×10⁴ 2.65×10⁴ 2.70×10⁴ 2.75×10⁴ 2.80×10⁴ 2.85×10⁴ 2.90×10⁴ 2.95×10⁴ 3.00×10⁴ 0 1×10⁴ 2×10⁴ 3×10⁴ 0 1.0×10³ 2.0×10³ 3.0×10³ 4.0×10³ 5.0×10³ 6.0×10³ 7.0×10³ 8.0×10³ 9.0×10³ 1.0×10⁴ 1.1×10⁴ 1.2×10⁴ 1.3×10⁴ 1.4×10⁴ 1.5×10⁴ 1.6×10⁴ 1.7×10⁴ 1.8×10⁴ 1.9×10⁴ 2.0×10⁴ 2.1×10⁴ 2.2×10⁴ 2.3×10⁴ 2.4×10⁴ 2.5×10⁴ 2.6×10⁴ 2.7×10⁴ 2.8×10⁴ 2.9×10⁴ 3.0×10⁴ -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0 10.2 10.4 10.6 10.8 11.0 11.2 11.4 11.6 11.8 12.0 12.2 12.4 12.6 12.8 13.0 13.2 13.4 13.6 13.8 14.0 -5 0 5 10 15 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 y