Brownian Motion & Stochastic Differential Equations
Brownian Motion & Stochastic Differential Equations
Section titled “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
Section titled “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
Section titled “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
Section titled “Example”- Stock price: dS_t = μ S_t dt + σ S_t dW_t (geometric Brownian motion).
2. Brownian Motion: Wiener Process
Section titled “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
Section titled “Derivation”Limit of random walk: S_n = sum Z_i / sqrt(n), Z_i ~ N(0,1), converges to W_t.
ML Insight
Section titled “ML Insight”- Brownian motion as noise in diffusion models for generation.
3. Stochastic Differential Equations (SDEs)
Section titled “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?
Section titled “Why Ito?”Ordinary chain rule misses quadratic variation of W_t: dW_t² = dt.
ML Application
Section titled “ML Application”- Langevin dynamics: dθ_t = -∇L dt + sqrt(2) dW_t for sampling.
4. Solving SDEs: Analytical and Numerical
Section titled “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
Section titled “ML Connection”- Simulate SDEs for diffusion model training.
5. Applications in Machine Learning
Section titled “5. Applications in Machine Learning”- Diffusion Models: Reverse SDE to denoise from Brownian motion.
- Stochastic Optimization: SGD as Euler discretization of SDE.
- Reinforcement Learning: SDEs model continuous MDPs.
- Bayesian Inference: Langevin MCMC for posterior sampling.
Challenges
Section titled “Challenges”- High-Dim: Numerical stability in SDE solving.
- Nonlinear SDEs: No closed forms.
6. Numerical Simulations of SDEs
Section titled “6. Numerical Simulations of SDEs”Implement Euler-Maruyama, simulate Brownian motion.
::: code-group
import numpy as npimport matplotlib.pyplot as plt
# Brownian motion simulationdef 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.01t, W = brownian_motion(T, dt)plt.plot(t, W)plt.title("Brownian Motion")plt.show()
# Euler-Maruyama for SDE dX = -X dt + dWdef 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.0t, 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 denoisingnoise = np.random.randn(100)steps = 100x = 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()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
Section titled “8. Symbolic Derivations with SymPy”Derive Ito’s lemma.
::: code-group
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)fn main() { println!("Ito's lemma: df = (∂f/∂t + μ ∂f/∂x + (1/2) σ² ∂²f/∂x²) dt + σ ∂f/∂x dW");}:::
9. Challenges in ML Applications
Section titled “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
Section titled “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
Section titled “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
Section titled “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.