| 
1 | 1 | # Modeling with Stochasticity  | 
2 | 2 | 
 
  | 
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  | 
5 | 6 | [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.  | 
9 | 35 | 
 
  | 
10 | 36 | ```@example SDE  | 
11 | 37 | using ModelingToolkit, StochasticDiffEq  | 
12 | 38 | using ModelingToolkit: t_nounits as t, D_nounits as D  | 
 | 39 | +using Plots  | 
13 | 40 | 
  | 
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]  | 
21 | 47 | 
  | 
22 | 48 | @mtkbuild de = System(eqs, t)  | 
 | 49 | +```  | 
 | 50 | + | 
 | 51 | +Even though we did not explicitly use `SDESystem`, ModelingToolkit can still infer this from the equations.  | 
23 | 52 | 
 
  | 
24 |  | -u0map = [  | 
25 |  | -    x => 1.0,  | 
26 |  | -    y => 0.0,  | 
27 |  | -    z => 0.0  | 
28 |  | -]  | 
 | 53 | +```@example SDE  | 
 | 54 | +typeof(de)  | 
 | 55 | +```  | 
29 | 56 | 
 
  | 
30 |  | -parammap = [  | 
31 |  | -    σ => 10.0,  | 
32 |  | -    β => 26.0,  | 
33 |  | -    ρ => 2.33  | 
34 |  | -]  | 
 | 57 | +We continue by solving and plotting the SDE.  | 
35 | 58 | 
 
  | 
36 |  | -prob = SDEProblem(de, u0map, (0.0, 100.0), parammap)  | 
 | 59 | +```@example SDE  | 
 | 60 | +prob = SDEProblem(de, [], (0.0, 100.0), [])  | 
37 | 61 | 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)  | 
38 | 79 | ```  | 
0 commit comments