Skip to content
Merged
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
8 changes: 4 additions & 4 deletions exercises/solved_notebooks/P5_mcmc/MCMC_2-basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ md"### 2: Unconditional expected value of Y"

# ╔═╡ 3decb2ec-210a-4b2f-842d-6fd40dd3f77b
@model function mole()
X ~ X_prior
X ~ Exponential(100)
Y ~ Uniform(0, X)
return Y
end
Expand Down Expand Up @@ -108,7 +108,7 @@ md"### 5: Conditional distribution of X (with more data)"

# ╔═╡ b2b16c34-e12a-4bea-8098-313d01913bbf
@model function mole2()
X ~ X_prior
X ~ Exponential(100)
Ys = zeros(4)
for i in 1:length(Ys)
Ys[i] ~ Uniform(0, X)
Expand Down Expand Up @@ -245,7 +245,7 @@ md"### 3: Conditional expected value"

# ╔═╡ 126d3954-c77d-4c98-abe0-fd87d14e6265
@model function lights()
μ ~ lights_prior
μ ~ LogNormal(log(40), 0.5)
lifespans = zeros(4)
for i in 1:length(lifespans)
lifespans[i] ~ Exponential(μ)
Expand Down Expand Up @@ -284,7 +284,7 @@ md"""

# ╔═╡ 8fc58fa4-b005-4f32-9eae-a8143582a1ae
@model function lights_censored(time_observed, n_working)
μ ~ lights_prior
μ ~ LogNormal(log(40), 0.5)

lifespans = zeros(2)
for i in 1:length(lifespans)
Expand Down
10 changes: 8 additions & 2 deletions exercises/solved_notebooks/P5_mcmc/MCMC_3-advanced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,18 @@ end

# ╔═╡ d9b50958-20ae-4085-80d6-19420c7d89df
@model function petrigrowth🌟(t_obs)
P0 ~ dropletdist🌟
P0 ~ MixtureModel(
[
truncated(Normal(10, sqrt(10)), lower = 0.0),
truncated(Normal(30, sqrt(30)), lower = 0.0)
],
[0.75, 0.25]
)
r ~ LogNormal(0.0, 0.3)
K ~ Normal(1e5, 1e4)

logfun = t -> logistic(t, P0, r, K)
Pt = logfun(t_obs)
Pt = max(logfun(t_obs), 0) # set to 0 if negative to prevent possible errors (not required)
P_obs ~ Poisson(Pt)

return logfun
Expand Down
67 changes: 30 additions & 37 deletions exercises/solved_notebooks/P6_calib/calib_fermenter_monod_sol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,34 +113,23 @@ Furthermore, suppose that at $t = 0\;h$ no substrate $S$ is present in the react
Calibrate the parameter values for $\mu_{max}$ and $K_s$ using the aforementioned measurement data for $S$ and $X$ in a timespan of `[0, 100]`$\mathrm{h}$. Take the values above as initial values for $\mu_{max}$ and $K_s$.
"""

# ╔═╡ 7a227eaf-18d0-44f4-ac4b-f529e81c7471
md"""
Create an `ODEProblem`. Use the aforementioned values as initial values for the problem.
"""

# ╔═╡ 6375478f-1af9-4fd2-b6f3-101a6f796f2d
# ╔═╡ 1689c839-9aa5-4bff-a26f-0c8fbf47aa32
# u0 = missing
u0 = [:S=>0.0, :X=>0.0005]

# ╔═╡ 38fe8304-af61-40a7-ac86-480dfb892185
# ╔═╡ 3fbc29b0-c299-4223-9922-d6fb9a4a6a90
# tspan = missing
tspan = (0.0, 100)

# ╔═╡ 87482f88-8413-4820-9613-7941f3d61bd7
# parms = missing
parms = [:μmax=>0.40, :Ks=>0.015, :Y=>0.67, :Q=>2, :V=>40, :Sin=>0.022]

# ╔═╡ 94f3bd7b-5c2c-4661-a0ab-2cdaf2cd6743
# oprob = missing
oprob = ODEProblem(fermenter_monod, u0, tspan, parms, combinatoric_ratelaws=false)

# ╔═╡ f6a8f134-6db0-4d74-8af5-82826347d8f0
md"""
Declare the Turing model. Use `InverseGamma()` for the standard deviations of the measurements and `LogNormal()` for `μmax` and `Ks`.
Declare the Turing model. Assume the following for the priors:
- The measurement error is an unknown positive value, but probably near $0$.
- The parameters `μmax` and `K` are both positive and expected to be in $[0.0, 1.0]$, presumably around $0.1$.
"""

# ╔═╡ 4c28a66a-ee2c-42a2-95c7-ea4ddb6a232d
# @model function fermenter_inference(t_meas, S, X)
# @model function fermenter_inference(t_meas)
# σ_S ~ missing
# σ_X ~ missing
# μmax ~ missing
Expand All @@ -151,34 +140,40 @@ Declare the Turing model. Use `InverseGamma()` for the standard deviations of th
# S ~ missing
# X ~ missing
# end
@model function fermenter_inference(t_meas, S, X)
σ_S ~ InverseGamma()
σ_X ~ InverseGamma()
μmax ~ LogNormal()
Ks ~ LogNormal()
parms = [:μmax=>μmax, :Ks=>Ks, :Y=>0.67, :Q=>2, :V=>40, :Sin=>0.022]
oprob = ODEProblem(fermenter_monod, u0, tspan, parms, combinatoric_ratelaws=false)
osol = solve(oprob, Tsit5(), saveat=t_meas)
S ~ MvNormal(osol[:S], σ_S^2 * I)
X ~ MvNormal(osol[:X], σ_X^2 * I)
@model function fermenter_inference(t_meas)
σ_S ~ Exponential()
σ_X ~ Exponential()
μmax ~ LogNormal(log(0.1))
Ks ~ LogNormal(log(0.1))
parms = [:μmax => μmax, :Ks => Ks, :Y => 0.67, :Q => 2, :V => 40, :Sin => 0.022]
oprob = ODEProblem(fermenter_monod, u0, tspan, parms)
osol = solve(oprob, AutoTsit5(Rosenbrock23()), saveat=t_meas)
S ~ MvNormal(osol[:S], σ_S)
X ~ MvNormal(osol[:X], σ_X)
end

# ╔═╡ c2120cce-5cae-42e5-b1c6-1d10b49d9ffc
md"""
!!! tip
This model can change between being non-stiff and stiff based on the sampled parameter values. You can use an auto-switching solver such as `AutoTsit5(Rosenbrock23())` here to make calibration more stable.
"""

# ╔═╡ 3136b15d-5078-4bcd-954b-e89bcb8aed1b
md"""
Provide the measurements to the Turing model.
"""

# ╔═╡ 6a508a62-61b9-4273-8e45-b26f594e8da9
# fermenter_inf = missing
fermenter_inf = fermenter_inference(t_meas, S_meas, X_meas)
fermenter_inf = fermenter_inference(t_meas) | (S = S_meas, X = X_meas)

# ╔═╡ 63420055-55f8-4def-8b0e-11ea61483010
md"""
Optimize the priors ($\sigma_S$, $\sigma_X$, $\mu_{max}$ and $K_s$). Do this with `MLE` method and Nelder-Mead. Store the optimization results in `results_mle`.
Optimize the likelihood of the parameters ($\sigma_S$, $\sigma_X$, $\mu_{max}$ and $K_s$) using the NelderMead optimizer. Store the optimization results in `results_mle`.
"""

# ╔═╡ d52c9da8-d8a4-4db0-ac6d-6d16ccf4775c
# results_map = missing
# results_mle = missing # Uncomment and complete the instruction
results_mle = optimize(fermenter_inf, MLE(), NelderMead())

# ╔═╡ e1b0ee01-f16c-40e9-a0f9-80072d690936
Expand Down Expand Up @@ -223,8 +218,8 @@ Create an ODEProblem and solve it. Use `Tsit5()` and `saveat=0.5`.
"""

# ╔═╡ 853c1a92-d50f-4b05-9ed3-d3ee1656665a
# oprob_opt = missing
oprob_opt = ODEProblem(fermenter_monod, u0, tspan, parms_opt, combinatoric_ratelaws=false)
# oprob_opt = missing # Uncomment and complete the instruction
oprob_opt = ODEProblem(fermenter_monod, u0, tspan, parms_opt)

# ╔═╡ f45e8124-e942-438e-99c5-3032ccc01454
# osol_opt = missing
Expand Down Expand Up @@ -266,13 +261,11 @@ end
# ╟─6c481447-28c6-4530-bf2c-64762121bc71
# ╠═918fd524-81fa-4aff-a403-37402e47235b
# ╟─ef977370-06ee-4a73-85e2-609a744167d3
# ╟─7a227eaf-18d0-44f4-ac4b-f529e81c7471
# ╠═6375478f-1af9-4fd2-b6f3-101a6f796f2d
# ╠═38fe8304-af61-40a7-ac86-480dfb892185
# ╠═87482f88-8413-4820-9613-7941f3d61bd7
# ╠═94f3bd7b-5c2c-4661-a0ab-2cdaf2cd6743
# ╠═1689c839-9aa5-4bff-a26f-0c8fbf47aa32
# ╠═3fbc29b0-c299-4223-9922-d6fb9a4a6a90
# ╟─f6a8f134-6db0-4d74-8af5-82826347d8f0
# ╠═4c28a66a-ee2c-42a2-95c7-ea4ddb6a232d
# ╟─c2120cce-5cae-42e5-b1c6-1d10b49d9ffc
# ╟─3136b15d-5078-4bcd-954b-e89bcb8aed1b
# ╠═6a508a62-61b9-4273-8e45-b26f594e8da9
# ╟─63420055-55f8-4def-8b0e-11ea61483010
Expand Down
Loading
Loading