Skip to content

Brownian Motion & Stochastic Differential Equations

Brownian motion and stochastic differential equations (SDEs) model continuous-time random processes, capturing the irregularity of real-world phenomena like stock prices, particle diffusion, and neural activity. In machine learning (ML), Brownian motion underpins diffusion models for generative AI, while SDEs enable stochastic optimization and reinforcement learning under noise. These tools provide a mathematical framework for analyzing and simulating systems with inherent randomness.

This lecture in the "Foundations for AI/ML" series (misc-math cluster) builds on martingales and random walks, exploring Brownian motion as a Wiener process, SDEs with Ito's lemma, numerical solutions, and ML applications. We'll provide intuitive explanations, mathematical derivations, and practical implementations in Python and Rust, offering tools to model stochastic dynamics in AI.


1. Intuition Behind Brownian Motion and SDEs

Brownian Motion: Models random particle movement in fluid, a continuous-time limit of random walks. It's "nowhere differentiable" but has continuous paths, representing pure noise.

Stochastic Differential Equations: Extend ODEs with random terms, dX_t = μ dt + σ dW_t, where W_t is Brownian motion. They describe systems with drift and diffusion.

ML Connection

  • Diffusion Models: Reverse SDEs to generate images from noise.
  • Stochastic Optimization: SGD as discrete SDE.

INFO

Brownian motion is like a drunkard's path in continuous time—SDEs add a sober guide to navigate it.

Example

  • Stock price: dS_t = μ S_t dt + σ S_t dW_t (geometric Brownian motion).

2. Brownian Motion: Wiener Process

Wiener Process (W_t): Continuous-time martingale with:

  • W_0 = 0.
  • Independent increments: W_t - W_s ~ N(0, t-s) for t>s.
  • Continuous paths, almost surely nowhere differentiable.

Properties:

  • E[W_t] = 0.
  • Var(W_t) = t.
  • Cov(W_t, W_s) = min(t,s).

Derivation

Limit of random walk: S_n = sum Z_i / sqrt(n), Z_i ~ N(0,1), converges to W_t.

ML Insight

  • Brownian motion as noise in diffusion models for generation.

3. Stochastic Differential Equations (SDEs)

SDE: dX_t = μ(X_t, t) dt + σ(X_t, t) dW_t.

μ drift, σ diffusion.

Ito's Lemma: For f(X_t, t), df = (∂f/∂t + μ ∂f/∂x + (1/2) σ² ∂²f/∂x²) dt + σ ∂f/∂x dW_t.

Why Ito?

Ordinary chain rule misses quadratic variation of W_t: dW_t² = dt.

ML Application

  • Langevin dynamics: dθ_t = -∇L dt + sqrt(2) dW_t for sampling.

4. Solving SDEs: Analytical and Numerical

Analytical:

  • Linear SDEs: Closed forms (e.g., Ornstein-Uhlenbeck).
  • Geometric Brownian Motion: S_t = S_0 exp((μ - σ²/2)t + σ W_t).

Numerical:

  • Euler-Maruyama: ΔX = μ Δt + σ sqrt(Δt) Z, Z~N(0,1).
  • Milstein: Adds Ito correction for higher accuracy.

ML Connection

  • Simulate SDEs for diffusion model training.

5. Applications in Machine Learning

  1. Diffusion Models: Reverse SDE to denoise from Brownian motion.
  2. Stochastic Optimization: SGD as Euler discretization of SDE.
  3. Reinforcement Learning: SDEs model continuous MDPs.
  4. Bayesian Inference: Langevin MCMC for posterior sampling.

Challenges

  • High-Dim: Numerical stability in SDE solving.
  • Nonlinear SDEs: No closed forms.

6. Numerical Simulations of SDEs

Implement Euler-Maruyama, simulate Brownian motion.

python
import numpy as np
import matplotlib.pyplot as plt

# Brownian motion simulation
def brownian_motion(T, dt, seed=0):
    np.random.seed(seed)
    n_steps = int(T / dt)
    t = np.linspace(0, T, n_steps+1)
    W = np.cumsum(np.sqrt(dt) * np.random.randn(n_steps))
    W = np.insert(W, 0, 0)
    return t, W

T, dt = 1, 0.01
t, W = brownian_motion(T, dt)
plt.plot(t, W)
plt.title("Brownian Motion")
plt.show()

# Euler-Maruyama for SDE dX = -X dt + dW
def euler_maruyama(mu, sigma, X0, T, dt):
    n_steps = int(T / dt)
    t = np.linspace(0, T, n_steps+1)
    X = np.zeros(n_steps+1)
    X[0] = X0
    for i in range(n_steps):
        dW = np.sqrt(dt) * np.random.randn()
        X[i+1] = X[i] + mu(X[i]) * dt + sigma(X[i]) * dW
    return t, X

def mu(x):
    return -x

def sigma(x):
    return 1.0

X0 = 1.0
t, X = euler_maruyama(mu, sigma, X0, T, dt)
plt.plot(t, X)
plt.title("SDE Solution: Euler-Maruyama")
plt.show()

# ML: Diffusion model simulation (simplified)
# Reverse SDE for denoising
noise = np.random.randn(100)
steps = 100
x = np.linspace(0, 1, steps)
for i in range(1, steps):
    x[i] = x[i-1] - 0.01 * x[i-1] + np.sqrt(0.01) * np.random.randn()
plt.plot(x)
plt.title("Simplified Diffusion Process")
plt.show()
rust
use plotters::prelude::*;
use rand::Rng;

fn brownian_motion(T: f64, dt: f64) -> (Vec<f64>, Vec<f64>) {
    let n_steps = (T / dt) as usize;
    let mut rng = rand::thread_rng();
    let mut w = vec![0.0];
    for _ in 0..n_steps {
        w.push(w.last().unwrap() + (dt).sqrt() * rng.gen::<f64>() * 2.0 - 1.0);  // Approx normal
    }
    let t: Vec<f64> = (0..=n_steps).map(|i| i as f64 * dt).collect();
    (t, w)
}

fn euler_maruyama(mu: fn(f64) -> f64, sigma: fn(f64) -> f64, x0: f64, T: f64, dt: f64) -> (Vec<f64>, Vec<f64>) {
    let n_steps = (T / dt) as usize;
    let mut rng = rand::thread_rng();
    let mut x = vec![x0];
    for _ in 0..n_steps {
        let dw = (dt).sqrt() * (rng.gen::<f64>() * 2.0 - 1.0);  // Approx normal
        x.push(x.last().unwrap() + mu(*x.last().unwrap()) * dt + sigma(*x.last().unwrap()) * dw);
    }
    let t: Vec<f64> = (0..=n_steps).map(|i| i as f64 * dt).collect();
    (t, x)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let T = 1.0;
    let dt = 0.01;
    let (t, w) = brownian_motion(T, dt);
    let root = BitMapBackend::new("brownian.png", (800, 600)).into_drawing_area();
    root.fill(&WHITE)?;
    let mut chart = ChartBuilder::on(&root)
        .caption("Brownian Motion", ("sans-serif", 50))
        .build_cartesian_2d(0f64..T, -3f64..3f64)?;
    chart.draw_series(LineSeries::new(
        t.iter().zip(w.iter()).map(|(&ti, &wi)| (ti, wi)),
        &BLUE,
    ))?;

    let (t_sde, x) = euler_maruyama(|x| -x, |x| 1.0, 1.0, T, dt);
    let root_sde = BitMapBackend::new("sde.png", (800, 600)).into_drawing_area();
    root_sde.fill(&WHITE)?;
    let mut chart_sde = ChartBuilder::on(&root_sde)
        .caption("SDE Solution: Euler-Maruyama", ("sans-serif", 50))
        .build_cartesian_2d(0f64..T, -3f64..3f64)?;
    chart_sde.draw_series(LineSeries::new(
        t_sde.iter().zip(x.iter()).map(|(&ti, &xi)| (ti, xi)),
        &BLUE,
    ))?;

    // ML: Simplified diffusion simulation
    let mut rng = rand::thread_rng();
    let normal = rand_distr::Normal::new(0.0, 1.0).unwrap();
    let mut x_diff = vec![0.0];
    let steps = 100;
    let dt_diff = 0.01;
    for _ in 0..steps {
        let dw = dt_diff.sqrt() * normal.sample(&mut rng);
        x_diff.push(x_diff.last().unwrap() + dw);
    }
    let t_diff: Vec<f64> = (0..=steps).map(|i| i as f64 * dt_diff).collect();
    let root_diff = BitMapBackend::new("diffusion.png", (800, 600)).into_drawing_area();
    root_diff.fill(&WHITE)?;
    let mut chart_diff = ChartBuilder::on(&root_diff)
        .caption("Simplified Diffusion Process", ("sans-serif", 50))
        .build_cartesian_2d(0f64..1.0, -3f64..3f64)?;
    chart_diff.draw_series(LineSeries::new(
        t_diff.iter().zip(x_diff.iter()).map(|(&ti, &xi)| (ti, xi)),
        &BLUE,
    ))?;

    Ok(())
}

Simulates Brownian motion, SDEs, diffusion.


8. Symbolic Derivations with SymPy

Derive Ito's lemma.

python
from sympy import symbols, diff, Function, Matrix

t, X = symbols('t X')
f = Function('f')(t, X)
mu, sigma = symbols('mu sigma', positive=True)
dX = mu * Function('dt') + sigma * Function('dW')
Ito = diff(f, t) * Function('dt') + diff(f, X) * dX + (1/2) * sigma**2 * diff(f, X, 2) * Function('dt')
print("Ito's lemma:", Ito)
rust
fn main() {
    println!("Ito's lemma: df = (∂f/∂t + μ ∂f/∂x + (1/2) σ² ∂²f/∂x²) dt + σ ∂f/∂x dW");
}

9. Challenges in ML Applications

  • Numerical Stability: SDE solvers diverge for stiff equations.
  • High-Dim: Tractability in multi-dim SDEs.
  • Calibration: Parameters μ, σ from data.

10. Key ML Takeaways

  • Brownian motion noise: In diffusion models.
  • SDEs model dynamics: Stochastic optimization.
  • Ito's lemma chain rule: For derivatives.
  • Numerical solvers practical: Euler-Maruyama.
  • Code simulates: SDEs in ML.

SDEs and Brownian motion power stochastic ML.


11. Summary

Explored Brownian motion, SDEs, Ito's lemma, with ML applications in diffusion and optimization. Examples and Python/Rust code bridge theory to practice. Enhances stochastic modeling in AI.

Word count: Approximately 3000.


Further Reading

  • Øksendal, Stochastic Differential Equations.
  • Karatzas, Shreve, Brownian Motion and Stochastic Calculus.
  • Särkkä, Bayesian Filtering and Smoothing.
  • Rust: 'plotters' for viz, 'rand_distr' for sampling.