Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GlobalSensitivity = "af5da776-676b-467e-8baf-acd8249e4f0f"
Integrals = "de52edbc-65ea-441a-8357-d3a637375a31"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
ModelingToolkitBase = "7771a370-6774-4173-bd38-47e70ca0b839"
NLopt = "76087f3c-5699-56af-9a33-bf431cd00edd"
Optimization = "7f7a1694-90dd-40f0-9382-eb1efda571ba"
OptimizationBBO = "3e6eede4-6085-4f62-9a71-46d9bc1eb92b"
Expand All @@ -18,6 +19,7 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
SciMLExpectations = "afe9f18d-7609-4d0e-b7b7-af0cb72b8ea8"
SymbolicIndexingInterface = "2efcf032-c050-4f8e-a9bb-153293bab1f5"
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

Expand All @@ -29,6 +31,7 @@ GlobalSensitivity = "2"
Integrals = "5"
LinearAlgebra = "1"
ModelingToolkit = "11.26"
ModelingToolkitBase = "1"
NLopt = "1.1"
Optimization = "5.6"
OptimizationBBO = "0.4.11"
Expand All @@ -38,6 +41,7 @@ PrecompileTools = "1"
SafeTestsets = "0.1"
SciMLBase = "3.5"
SciMLExpectations = "2"
SymbolicIndexingInterface = "0.3.45"
SymbolicUtils = "4"
SciMLTesting = "2.13"
Test = "1"
Expand Down
2 changes: 2 additions & 0 deletions src/EasyModelAnalysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ using Plots
using LinearAlgebra: LinearAlgebra, I, norm
using DifferentialEquations: DifferentialEquations, remake, solve
using ModelingToolkit: ModelingToolkit, Num, Symbolics
using ModelingToolkitBase: ModelingToolkitBase, Initial, MTKParameters
using Distributions: Distributions, InverseGamma, MvNormal, product_distribution
using Plots: Plots, @layout, bar, plot, plot!, scatter!
using PrecompileTools: @compile_workload, @setup_workload
Expand All @@ -29,6 +30,7 @@ using SciMLExpectations: SciMLExpectations, ExpectationProblem, GenericDistribut
Koopman, SystemMap
using SciMLBase: SciMLBase, ContinuousCallback, EnsembleProblem, EnsembleSerial,
EnsembleSolution, EnsembleThreads, ODESolution, terminate!
using SymbolicIndexingInterface: is_parameter, remake_buffer
using SymbolicUtils: SymbolicUtils

include("basics.jl")
Expand Down
16 changes: 14 additions & 2 deletions src/datafit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,24 @@ function bayes_unpack_data(p)
return (pdist, pkeys)
end

function _bayesian_parameters(prob, keys, values)
# The symbolic remake updates Initial parameters along with their corresponding values.
if prob.p isa MTKParameters &&
all(
key -> key isa Num && is_parameter(prob, key) &&
!is_parameter(prob, Initial(key)), keys
)
return remake_buffer(prob, prob.p, keys, values)
end
return Pair.(keys, values)
end

Turing.@model function bayesianODE(prob, t, pdist, pkeys, data, noise_prior)
σ ~ noise_prior

pprior ~ product_distribution(pdist)

prob = remake(prob, tspan = (prob.tspan[1], t[end]), p = Pair.(pkeys, pprior))
prob = remake(prob; tspan = (prob.tspan[1], t[end]), p = _bayesian_parameters(prob, pkeys, pprior))
sol = solve(prob, saveat = t)
if !SciMLBase.successful_retcode(sol)
Turing.@addlogprob! -Inf
Expand All @@ -300,7 +312,7 @@ Turing.@model function bayesianODE(

pprior ~ product_distribution(pdist)

prob = remake(prob, tspan = (prob.tspan[1], lastt), p = Pair.(pkeys, pprior))
prob = remake(prob; tspan = (prob.tspan[1], lastt), p = _bayesian_parameters(prob, pkeys, pprior))
sol = solve(prob)
if !SciMLBase.successful_retcode(sol)
Turing.@addlogprob! -Inf
Expand Down
12 changes: 12 additions & 0 deletions test/Datafit/datafit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,15 @@ p_posterior = @time bayesian_datafit(
niter = 200
)
@test var.(getfield.(p_prior, :second)) >= var.(getfield.(p_posterior, :second))

@testset "Parameter-dependent initialization during Bayesian remakes" begin
@parameters offset = 2.0 slope = 1.0
@variables state(t) [guess = 1.0]
@named system = System(
[D(state) ~ offset + slope], t; initialization_eqs = [state ~ offset]
)
problem = ODEProblem(mtkcompile(system), Pair[], (0.0, 1.0))
p = EasyModelAnalysis._bayesian_parameters(problem, [offset, slope], [3.0, 4.0])
solution = solve(remake(problem; p), Tsit5(); saveat = [0.0, 1.0])
@test solution[state] ≈ [3.0, 10.0]
end
File renamed without changes.
4 changes: 2 additions & 2 deletions test/sensitivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ sensres = EasyModelAnalysis.get_sensitivity(prob, 100.0, y, pbounds)
@test_nowarn create_sensitivity_plot(sensres, pbounds)

@test length(sensres) == 5
@test collect(keys(sensres)) ==
[:ρ_first_order, :β_first_order, :ρ_total_order, :β_total_order, :ρ_β_second_order]
@test keys(sensres) ==
Set((:ρ_first_order, :β_first_order, :ρ_total_order, :β_total_order, :ρ_β_second_order))

sensres_max = get_sensitivity_of_maximum(prob, 100.0, y, pbounds, samples = 50)
@test length(sensres) == 5
10 changes: 10 additions & 0 deletions test/test_groups.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ arch = "x86"
versions = ["1"]
num_threads = 4

[Threshold]
versions = ["1"]
num_threads = 4

["Threshold 32-bit"]
group = "Threshold"
versions = ["1"]
os = ["ubuntu-latest"]
arch = "x86"

[QA]
versions = ["lts", "1"]
Loading