DEV Community

Deepak Goyal
Deepak Goyal

Posted on AI-assisted

Calculating Value at Risk (VaR) in Python: Historical, Parametric & Monte Carlo

Value at Risk (VaR) is the number every bank risk team reports every day: "With 99% confidence, we won't lose more than X tomorrow." It's also one of the most tested topics in the FRM exam.

In this post we'll compute VaR three ways in Python, add Expected Shortfall, and see why the method you choose changes the answer.

What VaR actually says

A 1-day 99% VaR of ₹40,000 on a ₹10 lakh portfolio means: on 99 out of 100 normal trading days, the loss should be smaller than ₹40,000. It says nothing about how bad the other 1 day can be. That gap is why regulators moved towards Expected Shortfall.

Setup

We'll use simulated fat-tailed returns so the code runs anywhere. Swap in your own return series (from a CSV or a market data API) later.

import numpy as np
from scipy.stats import norm

rng = np.random.default_rng(42)
returns = rng.standard_t(df=4, size=1000) * 0.01  # fat-tailed, ~1% daily moves

portfolio_value = 1_000_000  # INR 10 lakh
confidence = 0.99
Enter fullscreen mode Exit fullscreen mode

A Student-t distribution with 4 degrees of freedom has fatter tails than a normal distribution, which is much closer to how real equity returns behave.

Method 1: Historical VaR

No distribution assumption. Sort past returns and read off the 1st percentile.

def historical_var(returns, confidence):
    return -np.percentile(returns, (1 - confidence) * 100)
Enter fullscreen mode Exit fullscreen mode

Method 2: Parametric (variance-covariance) VaR

Assume returns are normal, then VaR is just mean and standard deviation.

def parametric_var(returns, confidence):
    mu, sigma = returns.mean(), returns.std(ddof=1)
    return -(mu + norm.ppf(1 - confidence) * sigma)
Enter fullscreen mode Exit fullscreen mode

Method 3: Monte Carlo VaR

Simulate many possible returns from a model, then take the percentile. Here the model is a fitted normal; in practice you'd plug in a richer model (GARCH, copulas, full repricing of options).

def monte_carlo_var(returns, confidence, n_sims=100_000, seed=0):
    mu, sigma = returns.mean(), returns.std(ddof=1)
    sims = np.random.default_rng(seed).normal(mu, sigma, n_sims)
    return -np.percentile(sims, (1 - confidence) * 100)
Enter fullscreen mode Exit fullscreen mode

Bonus: Expected Shortfall

Expected Shortfall (ES) is the average loss on the days that are worse than VaR, so it measures the tail that VaR ignores.

def expected_shortfall(returns, confidence):
    var = historical_var(returns, confidence)
    return -returns[returns <= -var].mean()
Enter fullscreen mode Exit fullscreen mode

Results

for name, fn in [("Historical", historical_var),
                 ("Parametric", parametric_var),
                 ("Monte Carlo", monte_carlo_var)]:
    v = fn(returns, confidence)
    print(f"{name:<12} 1-day 99% VaR: {v:.2%}  (INR {v * portfolio_value:,.0f})")

es = expected_shortfall(returns, confidence)
print(f"{'Hist. ES':<12} 1-day 99% ES : {es:.2%}  (INR {es * portfolio_value:,.0f})")
Enter fullscreen mode Exit fullscreen mode

Output:

Historical   1-day 99% VaR: 4.03%  (INR 40,264)
Parametric   1-day 99% VaR: 3.45%  (INR 34,546)
Monte Carlo  1-day 99% VaR: 3.48%  (INR 34,810)
Hist. ES     1-day 99% ES : 5.57%  (INR 55,719)
Enter fullscreen mode Exit fullscreen mode

What this tells you

  1. The normal assumption underestimates risk. Parametric and normal Monte Carlo VaR come in about 14% lower than historical VaR, because the data has fat tails and the normal distribution doesn't.
  2. ES is much larger than VaR. On the worst 1% of days, the average loss is about ₹55,700, not ₹40,000. That's why Basel's market risk rules (FRTB) replaced 99% VaR with 97.5% Expected Shortfall.
  3. Your model is a choice. Monte Carlo is only as good as the distribution you simulate from.

Scaling to 10 days

Regulatory VaR is often quoted over 10 days. The quick approximation is the square-root-of-time rule:

v10 = historical_var(returns, confidence) * np.sqrt(10)
Enter fullscreen mode Exit fullscreen mode

It assumes returns are independent and identically distributed, which real markets violate (volatility clusters), so treat it as a rough estimate.

Going further

  • Replace the simulated data with real NIFTY 50 or portfolio returns.
  • Backtest your VaR: count how many days the actual loss exceeded VaR (Kupiec test).
  • Try a GARCH(1,1) volatility model so VaR reacts to changing market conditions.

If you're preparing for the FRM exam, VaR, Expected Shortfall and backtesting are core Part I and Part II topics. I teach them at RBei Classes, and you can find more FRM and CFA resources at rbeiset.com.

Deepak Goyal, CFA, FRM — Founder, RBei Classes, Noida

Top comments (0)