Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More minor updates #710

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions docs/src/tutorials/sde_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,49 +136,48 @@ timepoint_meancor(sol, 0.2, 0.7) # Gives both means and then the correlation coe

## Example 2: Systems of SDEs with Diagonal Noise

More generally, an SDE
In general, a system of SDEs

```math
du = f(u,p,t)dt + g(u,p,t)dW
du = f(u,p,t)dt + g(u,p,t)dW,
```

generalizes to systems of equations is done in the same way as ODEs. Here, `g`
is now a matrix of values. One common case, and the default for DifferentialEquations.jl,
is diagonal noise where `g` is a diagonal matrix. This means that every function in
the system gets a different random number. Instead of handling matrices in this case,
we simply define both `f` and `g` as in-place functions. Thus, `f(du,u,p,t)` gives a
vector of `du` which is the deterministic change, and `g(du2,u,p,t)` gives a vector
`du2` for which `du2.*W` is the stochastic portion of the equation.
where `g` is now a matrix of values, is numerically integrated in the
same way as ODEs. A common scenario is when we have diagnol noise, which
is the default for DifferentialEquations.jl. Physically this means that
every variable in the system gets a different random kick. Consequently, `g` is a
diagonal matrix and we can handle this in a simple manner by defining
the deterministic part `f(du,u,p,t)` and the stochastic part
`g(du2,u,p,t)` as in-place functions.

For example, the Lorenz equation with additive noise has the same deterministic
portion as the Lorenz equations, but adds an additive noise, which is simply
`3*N(0,dt)` where `N` is the normal distribution `dt` is the time step, to each
step of the equation. This is done via:
Consider for example a stochastic variant of the Lorenz equations, where we introduce a
simple additive noise to each of `x,y,z`, which is simply `3*N(0,dt)`. Here `N` is the normal
distribution and `dt` is the time step. This is done as follows:

```@example sde2
using DifferentialEquations
using Plots
function lorenz(du, u, p, t)
du[1] = 10.0(u[2] - u[1])
function lorenz!(du, u, p, t)
du[1] = 10.0 * (u[2] - u[1])
du[2] = u[1] * (28.0 - u[3]) - u[2]
du[3] = u[1] * u[2] - (8 / 3) * u[3]
end

function σ_lorenz(du, u, p, t)
function σ_lorenz!(du, u, p, t)
du[1] = 3.0
du[2] = 3.0
du[3] = 3.0
end

prob_sde_lorenz = SDEProblem(lorenz, σ_lorenz, [1.0, 0.0, 0.0], (0.0, 10.0))
prob_sde_lorenz = SDEProblem(lorenz!, σ_lorenz!, [1.0, 0.0, 0.0], (0.0, 10.0))
sol = solve(prob_sde_lorenz)
plot(sol, idxs = (1, 2, 3))
```

Note that it's okay for the noise function to mix terms. For example

```@example sde2
function σ_lorenz(du, u, p, t)
function σ_lorenz!(du, u, p, t)
du[1] = sin(u[3]) * 3.0
du[2] = u[2] * u[1] * 3.0
du[3] = 3.0
Expand Down
Loading