Skip to content

Commit 375a78d

Browse files
committed
Rewrite of SDE tutorial
1 parent 84ae858 commit 375a78d

File tree

1 file changed

+64
-23
lines changed

1 file changed

+64
-23
lines changed
Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,79 @@
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.1 times) the magnitude of each of the states:
23+
24+
```math
25+
\begin{aligned}
26+
dx &= (\sigma (y-x))dt &+ 0.1xdB \\
27+
dy &= (x(\rho-z) - y)dt &+ 0.1ydB \\
28+
dz &= (xy - \beta z)dt &+ 0.1zdB \\
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+
In ModelingToolkit this can be done by `@brownian` variables.
935

1036
```@example SDE
1137
using ModelingToolkit, StochasticDiffEq
1238
using ModelingToolkit: t_nounits as t, D_nounits as D
39+
using Plots
1340
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]
41+
@parameters σ=10.0 ρ=2.33 β=26.0
42+
@variables x(t)=5.0 y(t)=5.0 z(t)=1.0
43+
@brownian B
44+
eqs = [D(x) ~ σ * (y - x) + 0.1B * x,
45+
D(y) ~ x * (ρ - z) - y + 0.1B * y,
46+
D(z) ~ x * y - β * z + 0.1B * z]
2147
2248
@mtkbuild de = System(eqs, t)
49+
```
50+
51+
Even though we did not explicitly use `SDESystem`, ModelingToolkit can still infer this from the equations.
2352

24-
u0map = [
25-
x => 1.0,
26-
y => 0.0,
27-
z => 0.0
28-
]
53+
```@example SDE
54+
typeof(de)
55+
```
2956

30-
parammap = [
31-
σ => 10.0,
32-
β => 26.0,
33-
ρ => 2.33
34-
]
57+
We continue by solving and plotting the SDE.
3558

36-
prob = SDEProblem(de, u0map, (0.0, 100.0), parammap)
59+
```@example SDE
60+
prob = SDEProblem(de, [], (0.0, 100.0), [])
3761
sol = solve(prob, LambaEulerHeun())
62+
plot(sol, idxs = [(1, 2, 3)])
63+
```
64+
65+
The noise present in all 3 equations is correlated, as can be seen on the below figure.
66+
If you want uncorrelated noise for each equation,
67+
multiple `@brownian` variables have to be declared.
68+
69+
```@example SDE
70+
@brownian Bx By Bz
71+
```
72+
73+
The figure also shows the multiplicative nature of the noise.
74+
Because states `x` and `y` generally take on larger values,
75+
the noise also takes on a more pronounced effect on these states compared to the state `z`.
76+
77+
```@example SDE
78+
plot(sol)
3879
```

0 commit comments

Comments
 (0)