Geometric Brownian Motion
- class quantfinlib.sim.GeometricBrownianMotion(drift=0.0, vol=0.1, cor=None)
Bases:
SimBase,SimNllMixinA class for simulating geometric Brownian motion paths with given drift and volatility.
Geometric Brownian motion is a continuous-time stochastic process used to model various random phenomena. In finance, it is the most widely used model for the random behavior of stock prices and other time series that have a asset prices that are strick positive.
Below an example of 10 geometric Brownian motion paths:
Examples
Generate 10 geometric Brownian motion paths. All paths start at 100, and have 252 steps with a business-day stepsize (dt=1/12). The drift is 0.05 and volatility is 0.30.
from quantfinlib.sim import GeometricBrownianMotion model = GeometricBrownianMotion(drift=0.05, vol=0.30) paths = model.path_sample( x0=100, label_start='2020-01-01', label_freq='B', num_steps=252, num_paths=10 ) print(paths)
S_0 S_1 ... S_8 S_9 2020-01-01 100.000000 100.000000 ... 100.000000 100.000000 2020-01-02 97.195051 99.167054 ... 97.411524 99.749313 2020-01-03 97.350814 100.576987 ... 100.766871 98.811000 2020-01-06 97.650057 101.494316 ... 99.753316 96.950812 2020-01-07 96.766620 100.320694 ... 100.654407 95.710336 ... ... ... ... ... ... 2020-12-14 76.730654 94.930205 ... 142.763991 68.443902 2020-12-15 77.066684 96.759006 ... 146.625840 66.686497 2020-12-16 79.427354 96.593931 ... 146.891945 66.912451 2020-12-17 81.757804 94.441878 ... 147.430721 66.956310 2020-12-18 81.836622 93.205992 ... 146.902597 68.004489 [253 rows x 10 columns]
(Source code, html)
Properties and Limitations
Drift (\(\mu\)) and volatility (\(\sigma\)) are considered constant over time.
Simulated values are always positive.
Geometric Brownian motion assumes a continuous path.
Use Cases in Finance
Geometric Brownian motion is widely used in financial modeling for modeling the random prices of stocks, futures, FX rates. Different asset type have different drift values. The next table show what drift value to use for what asset types. These drift values are based on well known no-arbitrage principles.
Assert Type
Drift
Stocks
Risk free interest rate (continuouly compounded). (Black & Scholes model)
Futures and Forwards
Zero. (Black 1976 model)
Currency exchange rates
Domestic - foreign interest rate (continuouly compounded). (Garman Kohlhagen model)
Details
The stochastic differential equation (SDE) for geometric Brownian motion is:
\[dX_t = \mu X_t dt + \sigma X_t dW_t\]where:
\(dX_t\) is the change in the process X at time t.
\(\mu\) is the drift coefficient (annualized drift rate).
\(\sigma\) is the volatility coefficient (annualized volatility rate).
\(dW_t\) is a Wiener process (standard Brownian motion).
For path simulations we use the exact solution of the discretize SDE:
\[X[t + dt] = X[t] \exp\left( (\mu - \frac{1}{2} \sigma^2 ) dt + \mathcal{N}(0,1) \sqrt{dt} \right)\]where:
\(\mu\) is the drift coefficient (annualized drift rate).
\(\sigma\) is the volatility coefficient (annualized volatility rate).
\(\mathcal{N}(0,1)\) is standard Normal distributed sample.
\(dt\) the time-step size.
Member functions
- __init__(drift=0.0, vol=0.1, cor=None)
Initializes the geometric BrownianMotion instance with specified drift and volatility.
- Parameters:
drift (float or array, optional) – The annualized drift rate (default is 0.0).
vol (float or array, optional) – The annualized volatility (default is 0.1).
cor (optional) – Correlation matrix for multivariate model (default is None, uncorrelated).
- aic(x: ndarray | DataFrame | Series, dt: float | None = None) float
Calculate the Akaike Information Criterion (AIC) for a given path.
- Parameters:
x (Union[np.ndarray, pd.DataFrame, pd.Series]) – The input data for AIC calculation.
dt (Optional[float]) – The time step between observations.
- Returns:
aic – The computed Akaike Information Criterion (AIC) value.
- Return type:
float
- bic(x: ndarray | DataFrame | Series, dt: float | None = None) float
Calculate the Bayesian Information Criterion (BIC) for a given path.
- Parameters:
x (Union[np.ndarray, pd.DataFrame, pd.Series]) – The input data for BIC calculation.
dt (Optional[float]) – The time step between observations.
- Returns:
bic – The computed Bayesian Information Criterion (BIC) value.
- Return type:
float
- fit(x: ndarray | DataFrame | Series, dt: float | None = None, **kwargs)
Calibrates the model to the given path(s).
- Parameters:
x (Union[np.ndarray, pd.DataFrame, pd.Series]) – The input data for calibration.
dt (Optional[float]) – The time step between observations.
**kwargs – Additional arguments for the fit process.
- Returns:
self – The fitted model instance.
- Return type:
Self
- nll(x: ndarray | DataFrame | Series, dt: float | None = None) float
Calculate the negative log-likelihood (lower is better) for a given path.
- Parameters:
x (Union[np.ndarray, pd.DataFrame, pd.Series]) – The input data for negative log-likelihood calculation.
dt (Optional[float]) – The time step between observations.
- Returns:
nll – The computed negative log-likelihood.
- Return type:
float
- path_sample(x0: float | ndarray | DataFrame | Series | str | None = None, dt: float | None = None, num_steps: int | None = 252, num_paths: int | None = 1, label_start=None, label_freq: str | None = None, columns: List[str] | None = None, random_state: int | None = None, include_x0: bool = True) ndarray | DataFrame | Series
Simulates random paths.
- Parameters:
x0 (Optional[Union[float, np.ndarray, pd.DataFrame, pd.Series, str]], optional) – The initial value(s) of the paths (default is None). The strings “first” and “last” will set x0 to the first or last value of the datasets used in a fit() call.
dt (Optional[float], optional) – The time step between observations (default is None). If dt is not specfied a values will be picked based on the bollowing fall-backs: * if label_freq is specfied dt will based on that * if the model is fitted with fit() the value of dt used during fitting is used * else dt will default to 1 / 252.
num_steps (Optional[int], optional) – The number of time steps to simulate (default is 252).
num_paths (Optional[int], optional) – The number of paths to simulate (default is 1).
label_start (optional, date-time like.) – The date-time start label for the simulated paths (default is None). When set, this function will return Pandas DataFrame with a DateTime index.
label_freq (Optional[str], optional) – The frequency for labeling the time steps (default is None).
columns (Optional[List[str]], optional) – A list of column names.
random_state (Optional[int], optional) – The seed for the random number generator (default is None).
include_x0 (bool, optional) – Whether to include the initial value in the simulated paths (default is True).
- Returns:
The simulated random paths.
- Return type:
Union[np.ndarray, pd.DataFrame, pd.Series]