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
119.4677091897788769.549770698293207
223.67472191011289257.209528937919522
330.83154740969177089.623270101299983
444.3673775635114546.786115040344695
554.5521255756736497.463773708972887
667.1271120499510253.588989446624209
777.8815813198630637.120939338384657
887.1827904327610638.140282873336812
991.7604894084671943.950723955388342
10105.2924741589231730.4738983583632095
11115.1719413822579253.244323557927702
12123.05683093498660746.272177042587808
13137.0133215627982735.310445131681789
14141.6351359832684324.07545352679035
15151.5112973767564029.347927468767077
16162.05215979796952834.064515559623684
17178.998119090304844.80569851356851
18188.9676082502634640.22772152148523128
19193.03436643926767959.082287994918595
20201.0619362802971315.540994220585944
21215.8283817604979391.4075768552927048
22226.2294842613101999.191133454871284
23236.9676967124649838.16582783290955
24249.8901467165185427.337257742284249
25259.1649891305059251.6326231404762925
26265.464961462352382.2768032662019433
27271.05798962621690646.474824765924869
28284.3769808849166482.8516448250674764
29293.88125993243472634.739342412261225
30305.4406030550411313.855145371422426
&vellip&vellip&vellip&vellip

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


In [3]:
evdb = infect_recover_loop(pop_db1, "continuous", "SI", 2, 6)


Out[3]:
edb(100x3 DataFrame
| Row | ind_id | s | i       |
|-----|--------|---|---------|
| 1   | 1      | 0 | 1.63338 |
| 2   | 2      | 0 | 1.55827 |
| 3   | 3      | 0 | 3.15572 |
| 4   | 4      | 0 | 1.50927 |
| 5   | 5      | 0 | 1.53509 |
| 6   | 6      | 0 | 1.27439 |
| 7   | 7      | 0 | 1.61051 |
| 8   | 8      | 0 | 1.55885 |
| 9   | 9      | 0 | 1.60665 |
| 10  | 10     | 0 | 1.26589 |
| 11  | 11     | 0 | 1.0245  |
⋮
| 89  | 89     | 0 | 1.6076  |
| 90  | 90     | 0 | 1.61267 |
| 91  | 91     | 0 | 1.6076  |
| 92  | 92     | 0 | 3.44986 |
| 93  | 93     | 0 | 1.0     |
| 94  | 94     | 0 | 1.56291 |
| 95  | 95     | 0 | 1.27827 |
| 96  | 96     | 0 | 1.61076 |
| 97  | 97     | 0 | 1.61248 |
| 98  | 98     | 0 | 1.70283 |
| 99  | 99     | 0 | 1.6371  |
| 100 | 100    | 0 | 1.26071 |,[1.0,1.00001,1.003,1.00492,1.00892,1.0245,1.06725,1.07877,1.08093,1.16184  …  2.09353,2.114,2.11895,2.18509,2.26525,2.75425,3.11534,3.15572,3.44986,5.27512],"continuous")

Create loglikelihood function


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


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

Time the loglikelihood function


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


elapsed time: 0.00219451 seconds (1815784 bytes allocated)
Out[5]:
337.01549435030125

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: 135.180494512 seconds (72151537116 bytes allocated, 53.34% 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"  2.24519  0.270152  0.00191026   0.0052094  7333.9   
 "beta"   5.87499  0.140049  0.000990296  0.0026809  7387.8   

Quantiles:
3x6 Array{Any,2}:
 ""        "2.5%"   "25.0%"   "50.0%"   "75.0%"   "97.5%"
 "alpha"  1.75872  2.05405   2.23255   2.42002   2.79984 
 "beta"   5.59461  5.78013   5.87742   5.97154   6.14232 

Produce trace plots for $\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⁴ 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 3.50 3.55 3.60 3.65 3.70 3.75 3.80 3.85 3.90 3.95 4.00 4.05 4.10 4.15 4.20 4.25 4.30 4.35 4.40 4.45 4.50 4.55 4.60 4.65 4.70 4.75 4.80 4.85 4.90 4.95 5.00 5.05 5.10 5.15 5.20 5.25 5.30 5.35 5.40 5.45 5.50 5.55 5.60 5.65 5.70 5.75 5.80 5.85 5.90 5.95 6.00 6.05 6.10 6.15 6.20 6.25 6.30 6.35 6.40 6.45 6.50 6.55 6.60 6.65 6.70 6.75 6.80 6.85 6.90 6.95 7.00 7.05 7.10 7.15 7.20 7.25 7.30 7.35 7.40 7.45 7.50 7.55 7.60 7.65 7.70 7.75 7.80 7.85 7.90 7.95 8.00 0 2 4 6 8 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 7.9 8.0 y