05
Euler–Maruyama method from scratch
Euler–Maruyama is the simplest explicit method for approximating an SDE on a time grid. It adds one Brownian diffusion contribution to the familiar Euler drift update.
From the SDE to a computable update
On the grid \(t_n=n\Delta t\), Euler–Maruyama replaces the infinitesimal expression by
Where the update comes from
Over one interval from \(t_n\) to \(t_{n+1}\), the SDE can be written in integral form:
Euler–Maruyama holds both coefficients at their known left-endpoint values during this short interval:
Adding these approximations to \(X_n\) gives the Euler–Maruyama update. This is a numerical approximation, not an algebraic cancellation of \(dt\) or \(dW\).
Four operations in every step
- Evaluate drift at the current state.
- Evaluate diffusion at the current state.
- Generate one new Brownian increment.
- Add drift change and diffusion change to the current state.
Both coefficients use \(X_n\), not the already updated value \(X_{n+1}\). This makes the method explicit.
Scenario: fluctuating logistic population
Let \(X(t)\) be a population growing toward carrying capacity \(K\), while environmental variation changes growth continuously:
The drift is logistic growth. The diffusion scale \(\eta X\) makes environmental fluctuation proportional to current population size.
Calculate the first update by hand
For the values used in the program, \(X_0=40\), \(r=0.8\), \(K=500\), \(\eta=0.12\) and \(\Delta t=0.02\):
With seed 31, the first generated value is approximately \(Z_0=-0.3953\), so
The population increases because the positive drift contribution is larger than the negative diffusion contribution in this particular step.
Interactive Python laboratory
The code implements Euler–Maruyama line by line and compares it with deterministic Euler using the same drift and initial value.
Output
Run the code to see the result.
Read one stored row
| Column | Meaning |
|---|---|
X_n | Current population before the update. |
dW_n | New Brownian increment for this interval. |
drift change | Systematic contribution \(f(X_n)\Delta t\). |
diffusion change | Random contribution \(g(X_n)\Delta W_n\). |
X_next | Sum of the current state and both contributions. |
Euler versus Euler–Maruyama
| Euler | Euler–Maruyama |
|---|---|
| \(x_{n+1}=x_n+f(x_n)\Delta t\) | Adds \(g(x_n)\Delta W_n\) as well. |
| Same inputs give the same path. | Different Brownian increments give different paths. |
| Approximates an ODE. | Approximates an SDE. |
Numerical cautions
A smaller \(\Delta t\) usually represents the SDE more accurately but needs more steps. Euler–Maruyama may generate biologically impossible negative values for some models. Do not silently replace them without explaining the chosen boundary treatment; a later lesson examines this issue directly.
What this lesson adds
You can now translate an SDE into an explicit Euler–Maruyama loop, separate drift and diffusion changes, store diagnostic rows, and compare a stochastic path with deterministic Euler.