Skip to content

pieropls/VaR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

18 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Value at Risk (VaR) in Python

Overview

This repository presents three methods, mathematically detailed and implemented in Python, for estimating Value at Risk (VaR), a key metric in financial risk management.

VaR/
β”‚
β”œβ”€β”€ 1.VaR.Montecarlo.ipynb          # Monte Carlo Simulation approach
β”œβ”€β”€ 2.VaR.Parametric.ipynb          # Parametric (Variance-Covariance) approach
β”œβ”€β”€ 3.VaR.Historical.ipynb          # Historical Simulation approach

What is VaR?

Value at Risk (VaR) measures the maximum expected loss of a portfolio over a given time horizon at a specified confidence level. Formally, for confidence level $1 - \alpha$, it is defined as:

$$ VaR_{1-\alpha}(X) := \inf_{t \in \mathbb{R}} \left( t : \mathbb{P}(X \le t) \ge 1 - \alpha \right) $$

where $X$ is the portfolio return distribution, $\alpha$ the significance level (e.g., $\alpha = 0.05$ for 95% confidence), and $t$ the loss threshold exceeded with probability $\alpha$.

In practice, if VaR(95%) = $10,000, there is a 95% chance that losses will not exceed $10,000 and a 5% chance they will.

Methods Comparison

Method Assumptions Formula & Key Points
Monte Carlo Simulation - In this implementation: returns are simulated from a normal distribution.
- In general: any return distribution can be modeled.
- Model parameters (mean $\mu$, volatility $\sigma$) are constant.
- Sufficient number of simulations to approximate distribution.
Simulate $N$ portfolio return paths.
Deterministic part: $P_0 \mu T$
Stochastic part: $P_0 \sigma Z \sqrt{T}$

$$\Delta P = P_0(\mu T + \sigma Z \sqrt{T})$$
$$VaR = -\text{Percentile}(\Delta P, (1-\alpha) \times 100)$$
Parametric (Variance-Covariance) - Portfolio returns are normally distributed.
- Constant mean and variance over time.
- Portfolio is linear (no significant derivatives).
Analytical solution using $\sigma_p$ and $Z_\alpha$.
Time adjustment via $\sqrt{T/252}$.

$$VaR = P_0 \sigma_p Z_\alpha \sqrt{\frac{T}{252}}$$
Historical Simulation - Historical returns are representative of future risks.
- No specific distributional assumptions.
- Past extreme events must be reflected in historical data.
Empirical quantile approach.
VaR based on historical percentiles.

$$VaR = -\text{Percentile}(X, 100 \times (1-\alpha)) \times P_0$$

Acknowledgments

This project was inspired by the tutorials of Ryan O'Connell, CFA, FRM. You can check out his videos here:

About

Practical Value at Risk in Python - Monte Carlo, Parametric, and Historical approaches with full code, formulas, and plots.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors