Monte Carlo API Reference¶
financelib.quant.monte_carlo.geometric_brownian_motion(S0: float, mu: float, sigma: float, T: float, n_steps: int = 252, n_simulations: int = 10000, seed: Optional[int] = None) -> np.ndarray
¶
Simulate price paths using Geometric Brownian Motion.
dS = mu * S * dt + sigma * S * dW
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
S0
|
float
|
Initial price. |
required |
mu
|
float
|
Expected return (annualized drift). |
required |
sigma
|
float
|
Volatility (annualized). |
required |
T
|
float
|
Time horizon in years. |
required |
n_steps
|
int
|
Number of time steps. |
252
|
n_simulations
|
int
|
Number of simulation paths. |
10000
|
seed
|
Optional[int]
|
Random seed for reproducibility. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape (n_steps + 1, n_simulations) with simulated prices. |
Example
paths = geometric_brownian_motion(S0=100, mu=0.05, sigma=0.2, T=1) paths.shape (253, 10000)
Source code in financelib/quant/monte_carlo.py
financelib.quant.monte_carlo.monte_carlo_var(returns: pd.Series, confidence: float = 0.95, n_simulations: int = 10000, horizon: int = 1, seed: Optional[int] = None) -> float
¶
Calculate Value at Risk using Monte Carlo simulation.
Simulates future returns based on historical distribution parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
Series
|
Historical returns series. |
required |
confidence
|
float
|
Confidence level (e.g., 0.95). |
0.95
|
n_simulations
|
int
|
Number of simulations. |
10000
|
horizon
|
int
|
Holding period in days. |
1
|
seed
|
Optional[int]
|
Random seed. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
VaR as a negative decimal (potential loss). |
Source code in financelib/quant/monte_carlo.py
financelib.quant.monte_carlo.portfolio_monte_carlo(returns: pd.DataFrame, weights: np.ndarray, initial_value: float = 1000000, n_simulations: int = 5000, horizon: int = 252, seed: Optional[int] = None) -> Tuple[np.ndarray, dict]
¶
Simulate portfolio value paths and compute risk statistics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
returns
|
DataFrame
|
DataFrame of asset returns (columns = assets). |
required |
weights
|
ndarray
|
Portfolio weights array. |
required |
initial_value
|
float
|
Initial portfolio value. |
1000000
|
n_simulations
|
int
|
Number of simulation paths. |
5000
|
horizon
|
int
|
Simulation horizon in trading days. |
252
|
seed
|
Optional[int]
|
Random seed. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Tuple of (simulated_values array, statistics dict). |
dict
|
statistics contains: mean_final, median_final, var_95, cvar_95, |
Tuple[ndarray, dict]
|
prob_loss, expected_return, worst_case, best_case. |