I’ll create an example using a stochastic process - let’s simulate and analyze a Random Walk with drift and an Ornstein-Uhlenbeck process, which is often used in financial modeling.
1 | import numpy as np |
Let me explain this example which demonstrates two different types of stochastic processes:
Random Walk with Drift:
- A discrete-time process where each step includes:
- A constant drift term (trend)
- A random component (noise)
- Properties:
- Non-stationary process
- Variance increases with time
- Used in financial modeling for asset prices
- A discrete-time process where each step includes:
Ornstein-Uhlenbeck Process:
- A continuous-time mean-reverting process
- Properties:
- Mean-reverting (tends to return to a long-term average)
- Stationary process
- Often used to model interest rates or volatility
Key Components of the Code:
simulate_random_walk
:- Adds drift to random normal steps
- Uses cumulative sum to create the path
- Multiple paths show variation in possible outcomes
simulate_ornstein_uhlenbeck
:- Implements the OU stochastic differential equation
- Mean reversion rate ($theta$) controls speed of return to mean
- $Sigma$ controls volatility of the process
Visualization Explanation:
Random Walk Statistics: Final values: [109.66602791 135.41811862 102.91710728 90.64039111 75.3631803 ] Mean final value: 102.801 Std of final values: 20.059 Ornstein-Uhlenbeck Statistics: Final values: [ 0.57734044 0.33628409 0.89794221 0.3854573 -0.2574374 ] Mean final value: 0.388 Std of final values: 0.378
Top Graph (Random Walk):
- Shows $5$ sample paths with positive drift
- Black dashed line shows theoretical drift
- Paths tend to move upward due to positive drift
- Increasing spread over time shows growing uncertainty
Bottom Graph (Ornstein-Uhlenbeck):
- Shows $5$ sample paths
- Black dashed line shows long-term mean ($0$)
- Paths constantly revert toward the mean
- Consistent spread shows stationary behavior
Key Differences Between the Processes:
The Random Walk:
- Tends to drift away from starting point
- Variance increases with time
- No mean reversion
The Ornstein-Uhlenbeck Process:
- Stays around the mean
- Has constant variance over time
- Shows strong mean reversion
This example demonstrates important concepts in stochastic processes:
- Mean reversion vs random drift
- Stationary vs non-stationary processes
- The role of drift and volatility
- Different types of randomness in financial modeling