Skip to content

Commit 7c4baff

Browse files
authored
remove SDE part (transfered to TuringLang#445)
1 parent 1b00352 commit 7c4baff

File tree

1 file changed

+0
-80
lines changed
  • tutorials/10-bayesian-differential-equations

1 file changed

+0
-80
lines changed

tutorials/10-bayesian-differential-equations/index.qmd

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -364,83 +364,3 @@ sample(model_sensealg, NUTS(;adtype=AutoZygote()), 1000; progress=false)
364364
```
365365

366366
For more examples of adjoint usage on large parameter models, consult the [DiffEqFlux documentation](https://diffeqflux.sciml.ai/dev/).
367-
368-
## Inference of a Stochastic Differential Equation
369-
370-
A [Stochastic Differential Equation (SDE)](https://diffeq.sciml.ai/stable/tutorials/sde_example/) is a differential equation that has a stochastic (noise) term in the expression of the derivatives.
371-
Here we fit a stochastic version of the Lokta-Volterra system.
372-
373-
We use a quasi-likelihood approach in which all trajectories of a solution are compared instead of a reduction such as mean, this increases the robustness of fitting and makes the likelihood more identifiable.
374-
We use SOSRI to solve the equation.
375-
376-
```{julia}
377-
u0 = [1.0, 1.0]
378-
tspan = (0.0, 10.0)
379-
function multiplicative_noise!(du, u, p, t)
380-
x, y = u
381-
du[1] = p[5] * x
382-
return du[2] = p[6] * y
383-
end
384-
p = [1.5, 1.0, 3.0, 1.0, 0.1, 0.1]
385-
386-
function lotka_volterra!(du, u, p, t)
387-
x, y = u
388-
α, β, γ, δ = p
389-
du[1] = dx = α * x - β * x * y
390-
return du[2] = dy = δ * x * y - γ * y
391-
end
392-
393-
prob_sde = SDEProblem(lotka_volterra!, multiplicative_noise!, u0, tspan, p)
394-
395-
ensembleprob = EnsembleProblem(prob_sde)
396-
data = solve(ensembleprob, SOSRI(); saveat=0.1, trajectories=1000)
397-
plot(EnsembleSummary(data))
398-
```
399-
400-
```{julia}
401-
@model function fitlv_sde(data, prob)
402-
# Prior distributions.
403-
σ ~ InverseGamma(2, 3)
404-
α ~ truncated(Normal(1.3, 0.5); lower=0.5, upper=2.5)
405-
β ~ truncated(Normal(1.2, 0.25); lower=0.5, upper=2)
406-
γ ~ truncated(Normal(3.2, 0.25); lower=2.2, upper=4)
407-
δ ~ truncated(Normal(1.2, 0.25); lower=0.5, upper=2)
408-
ϕ1 ~ truncated(Normal(0.12, 0.3); lower=0.05, upper=0.25)
409-
ϕ2 ~ truncated(Normal(0.12, 0.3); lower=0.05, upper=0.25)
410-
411-
# Simulate stochastic Lotka-Volterra model.
412-
p = [α, β, γ, δ, ϕ1, ϕ2]
413-
predicted = solve(prob, SOSRI(); p=p, saveat=0.1)
414-
415-
# Early exit if simulation could not be computed successfully.
416-
if predicted.retcode !== :Success
417-
Turing.@addlogprob! -Inf
418-
return nothing
419-
end
420-
421-
# Observations.
422-
for i in 1:length(predicted)
423-
data[:, i] ~ MvNormal(predicted[i], σ^2 * I)
424-
end
425-
426-
return nothing
427-
end;
428-
```
429-
430-
The probabilistic nature of the SDE solution makes the likelihood function noisy which poses a challenge for NUTS since the gradient is changing with every calculation.
431-
Therefore we use NUTS with a low target acceptance rate of `0.25` and specify a set of initial parameters.
432-
SGHMC might be a more suitable algorithm to be used here.
433-
434-
```{julia}
435-
model_sde = fitlv_sde(odedata, prob_sde)
436-
437-
setadbackend(:forwarddiff)
438-
chain_sde = sample(
439-
model_sde,
440-
NUTS(0.25),
441-
5000;
442-
initial_params=[1.5, 1.3, 1.2, 2.7, 1.2, 0.12, 0.12],
443-
progress=false,
444-
)
445-
plot(chain_sde)
446-
```

0 commit comments

Comments
 (0)