# Find project root and include ensure_packages.jl
project_root = let
current = pwd()
while !isfile(joinpath(current, "Project.toml")) && !isfile(joinpath(current, "_quarto.yml"))
parent = dirname(current)
parent == current && break
current = parent
end
current
end
include(joinpath(project_root, "scripts", "ensure_packages.jl"))
@auto_using StochasticDiffEq OrdinaryDiffEq Random Statistics CairoMakie
Random.seed!(42)
# Example: Treatment policy in stochastic disease model
β = 0.2 # Disease progression rate
α = 0.3 # Treatment effectiveness
σ = 0.05 # Process noise
# Policy 1: No treatment
function f_no_treatment!(du, u, p, t)
"""Disease SDE drift: no treatment policy."""
X = u[1]
du[1] = β * X
end
function g_no_treatment!(du, u, p, t)
"""Disease SDE diffusion: additive noise."""
du[1] = σ
end
# Policy 2: Treat if X > 0.5 (threshold policy)
function f_threshold!(du, u, p, t)
"""Disease SDE drift: threshold policy (treat if X > 0.5)."""
X = u[1]
A = X > 0.5 ? 1.0 : 0.0
du[1] = β * X - α * A * X
end
function g_threshold!(du, u, p, t)
"""Disease SDE diffusion: additive noise."""
du[1] = σ
end
# Policy 3: Always treat
function f_always_treat!(du, u, p, t)
"""Disease SDE drift: always treat policy."""
X = u[1]
du[1] = β * X - α * X
end
function g_always_treat!(du, u, p, t)
"""Disease SDE diffusion: additive noise."""
du[1] = σ
end
u0 = [0.1]
tspan = (0.0, 20.0)
# Monte Carlo evaluation
n_sims = 200
outcomes1 = Float64[]
outcomes2 = Float64[]
outcomes3 = Float64[]
for i in 1:n_sims
prob1 = SDEProblem(f_no_treatment!, g_no_treatment!, u0, tspan)
sol1 = solve(prob1, EM(), dt=0.1)
push!(outcomes1, sol1.u[end][1])
prob2 = SDEProblem(f_threshold!, g_threshold!, u0, tspan)
sol2 = solve(prob2, EM(), dt=0.1)
push!(outcomes2, sol2.u[end][1])
prob3 = SDEProblem(f_always_treat!, g_always_treat!, u0, tspan)
sol3 = solve(prob3, EM(), dt=0.1)
push!(outcomes3, sol3.u[end][1])
end
# Compare expected outcomes with uncertainty
println("Expected final severity (mean ± std):")
println(" Policy 1 (no treatment): ", round(mean(outcomes1), digits=3), " ± ", round(std(outcomes1), digits=3))
println(" Policy 2 (threshold): ", round(mean(outcomes2), digits=3), " ± ", round(std(outcomes2), digits=3))
println(" Policy 3 (always treat): ", round(mean(outcomes3), digits=3), " ± ", round(std(outcomes3), digits=3))