Skip to main content

generate_ou_process

Function generate_ou_process 

Source
pub fn generate_ou_process(
    x0: Positive,
    mu: Positive,
    theta: Positive,
    volatility: Positive,
    dt: Positive,
    steps: usize,
) -> Result<Vec<Positive>, SimulationError>
Expand description

Generates a mean-reverting Ornstein-Uhlenbeck process time series

This function simulates a discrete-time Ornstein-Uhlenbeck stochastic process, which is commonly used in financial mathematics to model mean-reverting processes such as interest rates, volatility, or commodity prices. The process follows the stochastic differential equation:

dX_t = θ(μ - X_t)dt + σdW_t

Where:

  • θ (theta) is the speed of reversion to the mean
  • μ (mu) is the long-term mean level
  • σ (sigma) is the volatility or intensity of random fluctuations
  • W_t is a Wiener process (standard Brownian motion)

§Arguments

  • x0 - Initial value of the process
  • mu - Long-term mean the process reverts to
  • theta - Speed of mean reversion (higher values cause faster reversion)
  • sigma - Volatility parameter controlling the intensity of random fluctuations
  • dt - Time step size for the simulation
  • steps - Number of time steps to simulate

§Returns

A vector containing the simulated values of the Ornstein-Uhlenbeck process at each time step

§Randomness

The normal increments come from the thread RNG, so two calls differ. A reproducible OU path is a crate::simulation::WalkType::MeanReverting walk with crate::simulation::WalkParams::seed set.

§Errors

Returns SimulationError::PositiveError when sqrt(dt) or the mean reversion term theta * (mu - x) * dt leaves the representable Positive range, and SimulationError::Decimal when the diffusion term or the accumulated level leaves the representable Decimal range. Returns SimulationError::InvalidParameters when a path of steps points cannot be allocated.

There is no limit to return in their place: the process level is the state being simulated, so a step that cannot be represented ends the path rather than continuing from a stand-in value.

§Examples

use rust_decimal_macros::dec;
use optionstratlib_core::model::Positive;
use optionstratlib_core::pos_or_panic;
use optionstratlib_simulation::simulation::ou::generate_ou_process;

// Simulate an OU process with initial value 1.0, mean 1.5,
// reversion speed 0.1, volatility 0.2, time step 0.01, for 1000 steps
let process = generate_ou_process(
    Positive::ONE,       // initial value
    pos_or_panic!(1.5),       // long-term mean
    pos_or_panic!(0.1),       // speed of reversion
    pos_or_panic!(0.2),       // volatility
    pos_or_panic!(0.01),      // time step
    1000             // number of steps
)?;