Bayesian Inference of ODE
For this tutorial, we will show how to do Bayesian inference to infer the parameters of the Lotka-Volterra equations using each of the three backends:
- Turing.jl
- Stan.jl
- DynamicHMC.jl
Setup
First, let's set up our ODE and the data. For the data, we will simply solve the ODE and take that solution at some known parameters as the dataset. This looks like the following:
using DiffEqBayes, ParameterizedFunctions, OrdinaryDiffEq, RecursiveArrayTools,
Distributions
f1 = @ode_def LotkaVolterra begin
dx = a * x - x * y
dy = -3 * y + x * y
end a
p = [1.5]
u0 = [1.0, 1.0]
tspan = (0.0, 10.0)
prob1 = ODEProblem(f1, u0, tspan, p)
σ = 0.01 # noise, fixed for now
t = collect(1.0:10.0) # observation times
sol = solve(prob1, Tsit5())
priors = [Normal(1.5, 1)]
randomized = VectorOfArray([(sol(t[i]) + σ * randn(2)) for i in 1:length(t)])
data = convert(Array, randomized)2×10 Matrix{Float64}:
2.76288 6.78088 0.965908 1.87682 … 4.34643 3.23957 1.03179
0.263743 2.0135 1.90044 0.324051 0.304739 4.55058 0.908419Inference Methods
Stan
using StanSample #required for using the Stan backend
bayesian_result_stan = stan_inference(prob1, :rk45, t, data, priors)1000×3 DataFrame
Row │ sigma1.1 sigma1.2 theta_1
│ Float64 Float64 Float64
──────┼─────────────────────────────
1 │ 0.215596 0.172286 1.50549
2 │ 0.263497 0.139185 1.50033
3 │ 0.258454 0.129351 1.49995
4 │ 0.152569 0.312668 1.49635
5 │ 0.150607 0.306456 1.50188
6 │ 0.134115 0.265014 1.50047
7 │ 0.209355 0.23045 1.50084
8 │ 0.284987 0.234286 1.50134
⋮ │ ⋮ ⋮ ⋮
994 │ 0.222384 0.162073 1.50023
995 │ 0.290342 0.26463 1.4978
996 │ 0.255993 0.268509 1.49685
997 │ 0.255881 0.268591 1.5089
998 │ 0.227153 0.222606 1.49588
999 │ 0.242699 0.202593 1.49828
1000 │ 0.23052 0.158867 1.49858
985 rows omittedTuring
bayesian_result_turing = turing_inference(prob1, Tsit5(), t, data, priors)╭─FlexiChain (1000 iterations, 1 chain) ───────────────────────────────────────╮
│ ↓ iter = 501:1500 │
│ → chain = 1:1 │
│ │
│ Parameters (2) ── AbstractPPL.VarName │
│ Float64 theta[1] │
│ Vector{Float64} σ │
│ │
│ Extras (14) │
│ Int64 n_steps, tree_depth │
│ Bool is_accept, numerical_error │
│ Float64 acceptance_rate, log_density, hamiltonian_energy, │
│ hamiltonian_energy_error, max_hamiltonian_energy_error, step_size, │
│ nom_step_size, logprior, loglikelihood, logjoint │
╰──────────────────────────────────────────────────────────────────────────────╯DynamicHMC
We can use DynamicHMC.jl as the backend for sampling with the dynamic_inference function. It is similarly used as follows:
bayesian_result_hmc = dynamichmc_inference(prob1, Tsit5(), t, data, priors)(posterior = [(parameters = [1.5006629639336493], σ = [0.01028985527010753, 0.005692783159511336]), (parameters = [1.5002263684252957], σ = [0.008395953933357545, 0.006714710979854991]), (parameters = [1.500581970042866], σ = [0.009258679347634167, 0.005465637573944086]), (parameters = [1.5004193572142974], σ = [0.009887818717408481, 0.006115211952894113]), (parameters = [1.5003293102638509], σ = [0.012475081566724222, 0.00779707102954994]), (parameters = [1.5002529256181827], σ = [0.008801557997057344, 0.005449337186036284]), (parameters = [1.5006243007641855], σ = [0.011279166859456685, 0.00845002306540129]), (parameters = [1.5000810650788934], σ = [0.008617432867088366, 0.005741572536951074]), (parameters = [1.5001698647956678], σ = [0.009130675339099784, 0.005182855134324397]), (parameters = [1.500332876622432], σ = [0.00765724067802313, 0.013860089940859473]) … (parameters = [1.500290207718875], σ = [0.007716268577460984, 0.008605173552075325]), (parameters = [1.5005115358469174], σ = [0.011649672740460266, 0.00549254848508021]), (parameters = [1.5002319786699125], σ = [0.01290587333674099, 0.006780717076000522]), (parameters = [1.500432828845276], σ = [0.007180956153973529, 0.009376199603777051]), (parameters = [1.5003351168755599], σ = [0.011315734746655601, 0.004809082269379787]), (parameters = [1.5004644908869689], σ = [0.007798317771944735, 0.012549557902582383]), (parameters = [1.5003822483559695], σ = [0.011329057300933289, 0.008006544725122987]), (parameters = [1.5003798245360325], σ = [0.008745599945830376, 0.004766497969578905]), (parameters = [1.5003762020525053], σ = [0.007399513598434443, 0.004188410854956746]), (parameters = [1.500354737681082], σ = [0.007087420285708463, 0.006403404470666321])], posterior_matrix = [0.40590698642132717 0.40561600900558165 … 0.4057158780310957 0.4057015719357777; -4.576596794336402 -4.780005363775636 … -4.906341010873612 -4.949433857157871; -5.168556018705513 -5.00345449090791 … -5.475433887882865 -5.050925481509369], tree_statistics = DynamicHMC.TreeStatisticsNUTS[DynamicHMC.TreeStatisticsNUTS(48.20097776162304, 3, turning at positions -6:1, 0.855953529076647, 7, DynamicHMC.Directions(0x54b847c1)), DynamicHMC.TreeStatisticsNUTS(51.919329377316316, 2, turning at positions -3:0, 0.9276343593177563, 3, DynamicHMC.Directions(0x25c553a8)), DynamicHMC.TreeStatisticsNUTS(52.05668690394026, 2, turning at positions 0:3, 0.8918542418964582, 3, DynamicHMC.Directions(0xedfdab83)), DynamicHMC.TreeStatisticsNUTS(52.959369495428184, 2, turning at positions -2:1, 0.9795113805525154, 3, DynamicHMC.Directions(0xf9c8e9a1)), DynamicHMC.TreeStatisticsNUTS(53.40248157316245, 2, turning at positions 0:3, 0.9404835253527053, 3, DynamicHMC.Directions(0xb13ba833)), DynamicHMC.TreeStatisticsNUTS(53.15597359606748, 3, turning at positions -7:0, 0.9960603423093684, 7, DynamicHMC.Directions(0x1158e0c8)), DynamicHMC.TreeStatisticsNUTS(52.37510970200329, 3, turning at positions -2:5, 0.8930489278465311, 7, DynamicHMC.Directions(0xe7fccbfd)), DynamicHMC.TreeStatisticsNUTS(51.174713789509596, 3, turning at positions -1:6, 0.9814506815055575, 7, DynamicHMC.Directions(0x729a8bc6)), DynamicHMC.TreeStatisticsNUTS(52.65138783693629, 2, turning at positions -1:2, 0.9999999999999999, 3, DynamicHMC.Directions(0x9eb2879a)), DynamicHMC.TreeStatisticsNUTS(48.03247039170763, 2, turning at positions -3:-6, 0.5129471936399754, 7, DynamicHMC.Directions(0x68132bf1)) … DynamicHMC.TreeStatisticsNUTS(50.38679369527446, 3, turning at positions -2:5, 0.7721665720967389, 7, DynamicHMC.Directions(0x5748462d)), DynamicHMC.TreeStatisticsNUTS(52.633983138819154, 3, turning at positions -2:5, 0.9803164315421827, 7, DynamicHMC.Directions(0x6d798435)), DynamicHMC.TreeStatisticsNUTS(52.466926156957975, 2, turning at positions -1:-4, 0.9789649700767037, 7, DynamicHMC.Directions(0x7eee37b3)), DynamicHMC.TreeStatisticsNUTS(51.90220967808124, 3, turning at positions -6:1, 0.9403060912944248, 7, DynamicHMC.Directions(0x1f3ea161)), DynamicHMC.TreeStatisticsNUTS(51.449236083416736, 3, turning at positions -2:5, 0.9743546484479916, 7, DynamicHMC.Directions(0x26542a85)), DynamicHMC.TreeStatisticsNUTS(50.422349353509, 3, turning at positions 0:7, 0.7833399444419313, 7, DynamicHMC.Directions(0xcfff24cf)), DynamicHMC.TreeStatisticsNUTS(50.352240065362025, 3, turning at positions 0:7, 0.9626867757822614, 7, DynamicHMC.Directions(0xea0ad76f)), DynamicHMC.TreeStatisticsNUTS(52.931466216410136, 2, turning at positions -1:-4, 0.9727627216824889, 7, DynamicHMC.Directions(0x10aa46cb)), DynamicHMC.TreeStatisticsNUTS(51.23618974028188, 3, turning at positions -3:4, 0.8820058247226664, 7, DynamicHMC.Directions(0x41332e2c)), DynamicHMC.TreeStatisticsNUTS(51.27558583468641, 2, turning at positions 0:3, 0.9999999999999999, 3, DynamicHMC.Directions(0xf44c7a6b))], logdensities = [52.81297468395271, 54.14706095326153, 53.333954784855756, 54.41339461249248, 53.60267860131762, 54.02948077957046, 53.081799784063655, 52.975145767069804, 53.42555102058104, 50.969997088622655 … 53.57636356175445, 53.39359297631469, 53.58080364255879, 53.037976786303375, 53.155150682200755, 51.71097652509887, 53.88668836181589, 53.395846215618356, 51.61221864375219, 53.8833164202551], κ = Gaussian kinetic energy (Diagonal), √diag(M⁻¹): [0.00013873986744069385, 0.25012910738602745, 0.2797871661870127], ϵ = 0.5803034410301919)More Information
For a better idea of the summary statistics and plotting, you can take a look at the benchmarks.