Brownian Motion

class quantfinlib.sim.BrownianMotion(drift=0.0, vol=0.1, cor=None)

Bases: SimBase, SimNllMixin

A class for simulating Brownian motion paths with given drift and volatility.

Brownian motion is a continuous-time stochastic process used to model various random phenomena. In finance, it is commonly used to model the random behavior of asset prices.

Below an example of 10 Brownian motion paths:

Examples

Generate 3 Brownian motion paths. All paths start at 1, and have 6 steps with a stepsize of dt=1/4. The drift is -2 and volatility is 0.7.

from quantfinlib.sim import BrownianMotion

bm = BrownianMotion(drift=-2, vol=0.7)
paths = bm.path_sample(x0=1, dt=1/4, num_steps=6, num_paths=3)

print(paths)
[[ 1.          1.          1.        ]
 [ 0.66083277  0.5718456   0.21628761]
 [-0.18222194  0.26008155 -0.16687054]
 [-0.38730218 -0.54692363 -0.62936262]
 [-0.7378438  -1.20838479 -0.89878857]
 [-1.36310195 -1.68269894 -0.81262148]
 [-1.92449556 -1.85304315 -0.9741197 ]]

Below is a plot of 10 Brownian motion paths of length 252.

import plotly.express as px
from quantfinlib.sim import BrownianMotion

model = BrownianMotion(drift=-2, vol=0.7)
paths = model.path_sample(x0=1.5, dt=1/252, num_steps=252, num_paths=10)

fig = px.line(paths)
fig.show()

(Source code, html)

Properties and Limitations

  • Drift (\(\mu\)) and volatility (\(\sigma\)) are considered constant over time.

  • Simulated values can be both positive and negative, which might not be realistic for certain assets (e.g., stock prices, which cannot be negative).

  • Brownian motion assumes a continuous path, which is a good approximation for high-frequency trading but may not capture large jumps or discontinuities in asset prices.

Use Cases in Finance

Brownian motion is widely used in financial modeling for:

  • Modeling the returns of assets: Brownian motion is often used to simulate the returns of financial assets, assuming that returns follow a random walk.

  • Spread modeling: It can be used to model the spread between different assets, capturing the random fluctuations around a mean value.

  • Risk management: Brownian motion can be used to assess the risk and uncertainty in asset price movements over short time intervals.

Details

The stochastic differential equation (SDE) for Brownian motion is:

\[dX_t = \mu dt + \sigma 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] + \mu * dt + \mathcal{N}(0,1) * \sqrt{dt}\]

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 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]