在采样强制微分方程时,朱莉娅/图林的typeserror

发布于 2025-02-08 06:44:47 字数 2075 浏览 2 评论 0原文

我是朱莉娅(Julia)和图灵(Turing)的新手,并且正在尝试将强制的0-D框颂歌适合数据,但是进行采样时会遇到类型错误。

遵循此页面(在外部强迫< /a>),我添加了强迫作为ODE参数的插值手柄,而ODE求解器则没有问题。

当我将强制颂歌与图灵结合在一起时,跟随 https://turing.ml /dev/tutorials/10-bayesian-differential-equiations/,我在HMC的梯度计算期间会遇到类型错误,引用“ TypeError:typeEserror:typeassert,预期的float64,获得了type fortherdiff的价值。 ,3}”。

以下是我的代码,应该重现我遇到的错误。

感谢任何帮助,谢谢。

以下是我的代码:

    using Interpolations, DifferentialEquations, Plots, Turing, LinearAlgebra

    # Define ODE
    function f(du,u,p,t)
          α, β, F = p
          du[1] = α * u[1] + β * F(t) # Interpolations.jl interpolates via ()
    end

    # Define initial value problem
    begin
        # Define forcing 
        time_forcing = -1.:9.
        data_forcing = [10,0,0,0,0,0,0,0,0, 0, 0]
        F = Interpolations.scale(interpolate(data_forcing, BSpline(Linear())), time_forcing)
        α = -0.5
        β = 1
        p_lin = (α, β, F) 

        # Define u0 and tspan
        u0 = [0.]
        tspan = (-1.,9.) # Note, that we would need to extrapolate beyond 
        ode_lin = ODEProblem(f,u0,tspan,p_lin)
    end

    # Generate data
    sol  = solve(ode_lin, Tsit5(); saveat=1)
    data = Array(sol) + 0.2 * randn(size(Array(sol)))

    @model function fit_simple_box(data, F, ode_lin)
        # Prior distributions.
        σ ~ InverseGamma(2, 3)
        α ~ Normal(0, 3)
        β ~ Normal(0, 3)

        # Simulate Lotka-Volterra model. 
        p = (α, β, F)
        predicted = solve(ode_lin, Tsit5(); p=p, saveat=1)

        # Observations.
        for i in 1:length(predicted)
            data[i] ~ Normal(predicted[i][1], σ^2)
        end

        return nothing
    end

    model = fit_simple_box(data, F, ode_lin)
    chain = sample(model, NUTS(0.65), MCMCSerial(), 1000, 2)

I am new to Julia and Turing and am trying to fit a forced 0-D box ODE to data, but I get type error when doing sampling.

Following this page (solve system of ODEs with read in external forcing), I added an interpolation handle of the forcing as a parameter of the ODE, and the ODE solver runs without problems.

When I combine the forced ODE with Turing, following https://turing.ml/dev/tutorials/10-bayesian-differential-equations/, I get a type error during HMC's gradient calculation, quoting ”TypeError: in typeassert, expected Float64, got a value of type ForwardDiff.Dual{Nothing, Float64, 3}”.

Below are my codes that should reproduce the error I got.

I appreciate any help, Thanks.

Below are my codes:

    using Interpolations, DifferentialEquations, Plots, Turing, LinearAlgebra

    # Define ODE
    function f(du,u,p,t)
          α, β, F = p
          du[1] = α * u[1] + β * F(t) # Interpolations.jl interpolates via ()
    end

    # Define initial value problem
    begin
        # Define forcing 
        time_forcing = -1.:9.
        data_forcing = [10,0,0,0,0,0,0,0,0, 0, 0]
        F = Interpolations.scale(interpolate(data_forcing, BSpline(Linear())), time_forcing)
        α = -0.5
        β = 1
        p_lin = (α, β, F) 

        # Define u0 and tspan
        u0 = [0.]
        tspan = (-1.,9.) # Note, that we would need to extrapolate beyond 
        ode_lin = ODEProblem(f,u0,tspan,p_lin)
    end

    # Generate data
    sol  = solve(ode_lin, Tsit5(); saveat=1)
    data = Array(sol) + 0.2 * randn(size(Array(sol)))

    @model function fit_simple_box(data, F, ode_lin)
        # Prior distributions.
        σ ~ InverseGamma(2, 3)
        α ~ Normal(0, 3)
        β ~ Normal(0, 3)

        # Simulate Lotka-Volterra model. 
        p = (α, β, F)
        predicted = solve(ode_lin, Tsit5(); p=p, saveat=1)

        # Observations.
        for i in 1:length(predicted)
            data[i] ~ Normal(predicted[i][1], σ^2)
        end

        return nothing
    end

    model = fit_simple_box(data, F, ode_lin)
    chain = sample(model, NUTS(0.65), MCMCSerial(), 1000, 2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀中猫帐中妖 2025-02-15 06:44:47

感谢Chris Rackauckas,
更改

predicted = solve(ode_lin, Tsit5(); p=p, saveat=1)

predicted = solve(ode_lin, Tsit5(); p=p, saveat=1, u0 = typeof(α).(ode_lin.u0))

将U0推广到正确的类型并解决问题。
参考: https://discourse.julialang.org/t/typeerror-in-in-julia-turing-when-smpling-for-a-forced-differential-equation/82937/2

Thanks to Chris Rackauckas,
changing

predicted = solve(ode_lin, Tsit5(); p=p, saveat=1)

to

predicted = solve(ode_lin, Tsit5(); p=p, saveat=1, u0 = typeof(α).(ode_lin.u0))

promotes u0 to the correct type and solves the problem.
Ref: https://discourse.julialang.org/t/typeerror-in-julia-turing-when-sampling-for-a-forced-differential-equation/82937/2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文