Hello everyone,
I am trying to estimate parameters of a SDE system using Turing.jl package, but I keep running into this issue and I cannot figure out how to solve it.
This is my code:
using Turing
using Plots
using StatsPlots
using RecursiveArrayTools
#Prepare the data
t_end = 100.0
n_points = length(observed_data)
dt = t_end / (n_points - 1)
t_eval = 0:dt:t_end
#Set up the SDE problem with initial epsilon values
initial_ε = [0.1, 0.5, 0.4, 0.7, 0.2]
noise_dim = 2*(K+L)
noise_prototype = zeros(noise_dim, noise_dim)
prob = SDEProblem(drift!, diffusion!, u0, (0,100), initial_ε,
noise_rate_prototype=noise_prototype)
@model function fit_sde(data, prob)
#Priors for epsilon parameters
alpha ~ Uniform(0.0, 1.0)
beta ~ filldist(Uniform(0.0, 1.0), K) # Assuming K epsilon values
#Construct p
p = vcat(alpha, beta)
#Sample from the prior
_prob = remake(prob, p=p)
#Solve the SDE ensemble
predicted = solve(_prob, ISSEulerHeun(), tstops=t; p=p, saveat=0.1)
#Likelihood (assuming Gaussian noise on observations)
for j in eachindex(t)
data[:, j] ~ MvNormal(predicted[j], Diagonal(fill(0.01^2, 12)))
end
end
#Create the model instance
model = fit_sde(observed_data, prob)
#Sample from the posterior distribution (using NUTS sampler)
chain = sample(model, NUTS(0.65), 1000)
and this is the error I keep getting:
MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5})
Closest candidates are:
(::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat
@ Base rounding.jl:207
(::Type{T})(::T) where T<:Number
@ Core boot.jl:792
Float64(::IrrationalConstants.Sqrt2π)
@ IrrationalConstants ~/.julia/packages/IrrationalConstants/vp5v4/src/macro.jl:112
...
Stacktrace:
[1] convert(::Type{Float64}, x::ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5})
@ Base ./number.jl:7
[2] setindex!
@ ./array.jl:1024 [inlined]
[3] macro expansion
@ ./multidimensional.jl:960 [inlined]
[4] macro expansion
@ ./cartesian.jl:64 [inlined]
[5] _unsafe_setindex!(::IndexLinear, ::Matrix{Float64}, ::Diagonal{ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5}}}, ::UnitRange{Int64}, ::UnitRange{Int64})
@ Base ./multidimensional.jl:955
[6] _setindex!
@ ./multidimensional.jl:944 [inlined]
[7] setindex!(::Matrix{Float64}, ::Diagonal{ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5}}}, ::UnitRange{Int64}, ::UnitRange{Int64})
@ Base ./abstractarray.jl:1396
[8] diffusion!(dW::Matrix{Float64}, u::Vector{ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5}}, p::Vector{ForwardDiff.Dual{ForwardDiff.Tag{DynamicPPL.DynamicPPLTag, Float64}, Float64, 5}}, t::Float64)
@ Main ./In[195]:485
Does anyone know how to fix this? I would really appreciate it.
Thank you!