using Turing
using DifferentialEquations
Load StatsPlots for visualizations and diagnostics.
using StatsPlots
using LinearAlgebra
Set a seed for reproducibility.
using Random
Random.seed!(14);
u0 = [1.0, 1.0]
tspan = (0.0, 10.0)
function multiplicative_noise!(du, u, p, t)
x, y = u
du[1] = p[5] * x
return du[2] = p[6] * y
end
p = [1.5, 1.0, 3.0, 1.0, 0.1, 0.1]
function lotka_volterra!(du, u, p, t)
x, y = u
α, β, γ, δ = p
du[1] = dx = α * x - β * x * y
return du[2] = dy = δ * x * y - γ * y
end
prob_sde = SDEProblem(lotka_volterra!, multiplicative_noise!, u0, tspan, p)
ensembleprob = EnsembleProblem(prob_sde)
data = solve(ensembleprob, SOSRI(); saveat=0.1, trajectories=1000)
plot(EnsembleSummary(data))
@model function fitlv_sde(data, prob)
# Prior distributions.
σ ~ InverseGamma(2, 3)
α ~ truncated(Normal(1.3, 0.5); lower=0.5, upper=2.5)
β ~ truncated(Normal(1.2, 0.25); lower=0.5, upper=2)
γ ~ truncated(Normal(3.2, 0.25); lower=2.2, upper=4)
δ ~ truncated(Normal(1.2, 0.25); lower=0.5, upper=2)
ϕ1 ~ truncated(Normal(0.12, 0.3); lower=0.05, upper=0.25)
ϕ2 ~ truncated(Normal(0.12, 0.3); lower=0.05, upper=0.25)
# Simulate stochastic Lotka-Volterra model.
p = [α, β, γ, δ, ϕ1, ϕ2]
predicted = solve(prob, SOSRI(); p=p, saveat=0.1)
# Early exit if simulation could not be computed successfully.
if predicted.retcode !== :Success
Turing.@addlogprob! -Inf
return nothing
end
# Observations.
for i in 1:length(predicted)
data[:, i] ~ MvNormal(predicted[i], σ^2 * I)
end
return nothing
end;
function lotka_volterra(du, u, p, t)
# Model parameters.
α, β, γ, δ = p
# Current state.
x, y = u
# Evaluate differential equations.
du[1] = (α - β * y) * x # prey
du[2] = (δ * x - γ) * y # predator
return nothing
end
Define initial-value problem.
u0 = [1.0, 1.0]
p = [1.5, 1.0, 3.0, 1.0]
tspan = (0.0, 10.0)
prob = ODEProblem(lotka_volterra, u0, tspan, p)
Plot simulation.
plot(solve(prob, Tsit5()))
sol = solve(prob, Tsit5(); saveat=0.1)
odedata = Array(sol) + 0.2 * randn(size(Array(sol)))
Plot simulation and noisy observations.
plot(sol; alpha=0.3)
scatter!(sol.t, odedata’; color=[1 2], label=“”)
model_sde = fitlv_sde(odedata, prob_sde)
setadbackend(:forwarddiff)
chain_sde = sample(
model_sde,
NUTS(0.25),
5000;
init_params=[1.5, 1.3, 1.2, 2.7, 1.2, 0.12, 0.12],
progress=false,
)
plot(chain_sde)
FOLLOWING IS THE ERROR
MethodError: no method matching truncated(::Normal{Float64}; lower=0.5, upper=2.5)
Closest candidates are:
truncated(::UnivariateDistribution, ::Integer, ::Integer) at C:\Users\User.julia\packages\Distributions\HjzA0\src\truncate.jl:32 got unsupported keyword arguments “lower”, “upper”
truncated(::UnivariateDistribution, ::T, ::T) where T<:Real at C:\Users\User.julia\packages\Distributions\HjzA0\src\truncate.jl:23 got unsupported keyword arguments “lower”, “upper”
truncated(::UnivariateDistribution, ::Real, ::Real) at C:\Users\User.julia\packages\Distributions\HjzA0\src\truncate.jl:19 got unsupported keyword arguments “lower”, “upper”
…
Stacktrace:
[1] #3
@ .\In[42]:4 [inlined]
[2] (::var"#3#4")(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, varinfo::DynamicPPL.UntypedVarInfo{DynamicPPL.Metadata{Dict{AbstractPPL.VarName, Int64}, Vector{Distribution}, Vector{AbstractPPL.VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, sampler::DynamicPPL.SampleFromUniform, context::DynamicPPL.DefaultContext, data::Matrix{Float64}, prob::SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing})
@ Main .\none:0
[3] macro expansion
@ C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\model.jl:0 [inlined]
[4] _evaluate(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, varinfo::DynamicPPL.UntypedVarInfo{DynamicPPL.Metadata{Dict{AbstractPPL.VarName, Int64}, Vector{Distribution}, Vector{AbstractPPL.VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, sampler::DynamicPPL.SampleFromUniform, context::DynamicPPL.DefaultContext)
@ DynamicPPL C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\model.jl:154
[5] evaluate_threadunsafe(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, varinfo::DynamicPPL.UntypedVarInfo{DynamicPPL.Metadata{Dict{AbstractPPL.VarName, Int64}, Vector{Distribution}, Vector{AbstractPPL.VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, sampler::DynamicPPL.SampleFromUniform, context::DynamicPPL.DefaultContext)
@ DynamicPPL C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\model.jl:127
[6] (::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}})(rng::Random._GLOBAL_RNG, varinfo::DynamicPPL.UntypedVarInfo{DynamicPPL.Metadata{Dict{AbstractPPL.VarName, Int64}, Vector{Distribution}, Vector{AbstractPPL.VarName}, Vector{Real}, Vector{Set{DynamicPPL.Selector}}}, Float64}, sampler::DynamicPPL.SampleFromUniform, context::DynamicPPL.DefaultContext)
@ DynamicPPL C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\model.jl:92
[7] DynamicPPL.VarInfo(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, sampler::DynamicPPL.SampleFromUniform, context::DynamicPPL.DefaultContext)
@ DynamicPPL C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\varinfo.jl:126
[8] DynamicPPL.VarInfo(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, sampler::DynamicPPL.SampleFromUniform)
@ DynamicPPL C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\varinfo.jl:125
[9] step(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, spl::DynamicPPL.Sampler{NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}; resume_from::Nothing, kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:nadapts, :init_params), Tuple{Int64, Vector{Float64}}}})
@ DynamicPPL C:\Users\User.julia\packages\DynamicPPL\SgzCy\src\sampler.jl:73
[10] macro expansion
@ C:\Users\User.julia\packages\AbstractMCMC\oou1a\src\sample.jl:97 [inlined]
[11] macro expansion
@ C:\Users\User.julia\packages\AbstractMCMC\oou1a\src\logging.jl:15 [inlined]
[12] mcmcsample(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, sampler::DynamicPPL.Sampler{NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, N::Int64; progress::Bool, progressname::String, callback::Nothing, discard_initial::Int64, thinning::Int64, chain_type::Type, kwargs::Base.Pairs{Symbol, Any, Tuple{Symbol, Symbol}, NamedTuple{(:nadapts, :init_params), Tuple{Int64, Vector{Float64}}}})
@ AbstractMCMC C:\Users\User.julia\packages\AbstractMCMC\oou1a\src\sample.jl:88
[13] sample(rng::Random._GLOBAL_RNG, model::DynamicPPL.Model{var"#3#4", (:data, :prob), (), (), Tuple{Matrix{Float64}, SDEProblem{Vector{Float64}, Tuple{Float64, Float64}, true, Vector{Float64}, Nothing, SDEFunction{true, typeof(lotka_volterra!), typeof(multiplicative_noise!), UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing}, typeof(multiplicative_noise!), Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, Nothing}}, Tuple{}}, sampler::DynamicPPL.Sampler{NUTS{Turing.Core.ForwardDiffAD{40}, (), AdvancedHMC.DiagEuclideanMetric}}, N::Int64; chain_type::Type, resume_from::Nothing, progress::Bool, nadapts::Int64, discard_adapt::Bool, discard_initial::Int64, kwargs::Base.Pairs{Symbol, Vector{Float64}, Tuple{Symbol}, NamedTuple{(:init_params,), Tuple{Vector{Float64}}}})
@ Turing.Inference C:\Users\User.julia\packages\Turing\uAz5c\src\inference\hmc.jl:140
[14] #sample#2
@ C:\Users\User.julia\packages\Turing\uAz5c\src\inference\Inference.jl:142 [inlined]
[15] #sample#1
@ C:\Users\User.julia\packages\Turing\uAz5c\src\inference\Inference.jl:132 [inlined]
[16] top-level scope
@ In[47]:4
[17] eval
@ .\boot.jl:373 [inlined]
[18] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base .\loading.jl:1196
link i followed is
https://turing.ml/v0.21/tutorials/10-bayesian-differential-equations/