Skip to content

Commit aaf6893

Browse files
Merge pull request #3084 from ArnoStrouwen/docs2
Rewrite of SDE tutorial
2 parents 84ae858 + 87cbf0e commit aaf6893

File tree

1 file changed

+80
-24
lines changed

1 file changed

+80
-24
lines changed
+80-24
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,94 @@
11
# Modeling with Stochasticity
22

3-
All models with `ODESystem` are deterministic. `SDESystem` adds another element
4-
to the model: randomness. This is a
3+
All previous differential equations tutorials deal with deterministic `ODESystem`s.
4+
In this tutorial, we add randomness.
5+
In particular, we show how to represent a
56
[stochastic differential equation](https://en.wikipedia.org/wiki/Stochastic_differential_equation)
6-
which has a deterministic (drift) component and a stochastic (diffusion)
7-
component. Let's take the Lorenz equation from the first tutorial and extend
8-
it to have multiplicative noise by creating `@brownian` variables in the equations.
7+
as a `SDESystem`.
8+
9+
!!! note
10+
11+
The high level `@mtkmodel` macro used in the
12+
[getting started tutorial](@ref getting_started)
13+
is not yet compatible with `SDESystem`.
14+
We thus have to use a lower level interface to define stochastic differential equations.
15+
For an introduction to this interface, read the
16+
[programmatically generating ODESystems tutorial](@ref programmatically).
17+
18+
Let's take the Lorenz equation and add noise to each of the states.
19+
To show the flexibility of ModelingToolkit,
20+
we do not use homogeneous noise, with constant variance,
21+
but instead use heterogeneous noise,
22+
where the magnitude of the noise scales with (0.3 times) the magnitude of each of the states:
23+
24+
```math
25+
\begin{aligned}
26+
\frac{dx}{dt} &= (\sigma (y-x)) &+ 0.1x\frac{dB}{dt} \\
27+
\frac{dy}{dt} &= (x(\rho-z) - y) &+ 0.1y\frac{dB}{dt} \\
28+
\frac{dz}{dt} &= (xy - \beta z) &+ 0.1z\frac{dB}{dt} \\
29+
\end{aligned}
30+
```
31+
32+
Where $B$, is standard Brownian motion, also called the
33+
[Wiener process](https://en.wikipedia.org/wiki/Wiener_process).
34+
We use notation similar to the
35+
[Langevin equation](https://en.wikipedia.org/wiki/Stochastic_differential_equation#Use_in_physics),
36+
often used in physics.
37+
By "multiplying" the equations by $dt$, the notation used in
38+
[probability theory](https://en.wikipedia.org/wiki/Stochastic_differential_equation#Use_in_probability_and_mathematical_finance)
39+
can be recovered.
40+
41+
We use this Langevin-like notation because it allows us to extend MTK modeling capacity from ODEs to SDEs,
42+
using only a single new concept, `@brownian` variables, which represent $\frac{dB}{dt}$ in the above equation.
943

1044
```@example SDE
1145
using ModelingToolkit, StochasticDiffEq
1246
using ModelingToolkit: t_nounits as t, D_nounits as D
47+
using Plots
1348
14-
# Define some variables
15-
@parameters σ ρ β
16-
@variables x(t) y(t) z(t)
17-
@brownian a
18-
eqs = [D(x) ~ σ * (y - x) + 0.1a * x,
19-
D(y) ~ x * (ρ - z) - y + 0.1a * y,
20-
D(z) ~ x * y - β * z + 0.1a * z]
49+
@parameters σ=10.0 ρ=2.33 β=26.0
50+
@variables x(t)=5.0 y(t)=5.0 z(t)=1.0
51+
@brownian B
52+
eqs = [D(x) ~ σ * (y - x) + 0.3x * B,
53+
D(y) ~ x * (ρ - z) - y + 0.3y * B,
54+
D(z) ~ x * y - β * z + 0.3z * B]
2155
2256
@mtkbuild de = System(eqs, t)
57+
```
58+
59+
Even though we did not explicitly use `SDESystem`, ModelingToolkit can still infer this from the equations.
60+
61+
```@example SDE
62+
typeof(de)
63+
```
64+
65+
We continue by solving and plotting the SDE.
66+
67+
```@example SDE
68+
prob = SDEProblem(de, [], (0.0, 100.0), [])
69+
sol = solve(prob, SRIW1())
70+
plot(sol, idxs = [(1, 2, 3)])
71+
```
2372

24-
u0map = [
25-
x => 1.0,
26-
y => 0.0,
27-
z => 0.0
28-
]
73+
The noise present in all 3 equations is correlated, as can be seen on the below figure.
74+
The figure also shows the multiplicative nature of the noise.
75+
Because states `x` and `y` generally take on larger values,
76+
the noise also takes on a more pronounced effect on these states compared to the state `z`.
2977

30-
parammap = [
31-
σ => 10.0,
32-
β => 26.0,
33-
ρ => 2.33
34-
]
78+
```@example SDE
79+
plot(sol)
80+
```
81+
82+
If you want uncorrelated noise for each equation,
83+
multiple `@brownian` variables have to be declared.
3584

36-
prob = SDEProblem(de, u0map, (0.0, 100.0), parammap)
37-
sol = solve(prob, LambaEulerHeun())
85+
```@example SDE
86+
@brownian Bx By Bz
87+
eqs = [D(x) ~ σ * (y - x) + 0.3x * Bx,
88+
D(y) ~ x * (ρ - z) - y + 0.3y * By,
89+
D(z) ~ x * y - β * z + 0.3z * Bz]
90+
@mtkbuild de = System(eqs, t)
91+
prob = SDEProblem(de, [], (0.0, 100.0), [])
92+
sol = solve(prob, SRIW1())
93+
plot(sol)
3894
```

0 commit comments

Comments
 (0)