This project demonstrates how to calculate and backtest Value at Risk (VaR) and Conditional Value at Risk (CVaR) for a diversified portfolio of stocks using Python. I have used daily stock data from Yahoo Finance (yfinance) over a 3-year period and analyzed tail risk through quantitative and visual techniques.
Objective:
Estimate portfolio and individual asset risk using Value at Risk (VaR) and Conditional VaR (CVaR), and perform backtesting to verify model accuracy.
Techniques Used:
- VaR and CVaR calculation (historical method)
- Backtesting (Kupiec Test)
- Outlier and breach analysis
- Risk visualization (exception timelines)
- βοΈ Data Collection
- Downloaded 3 years of daily price data for a diversified portfolio
-
π Calculated Daily Returns
-
π Compute VaR
- Calculate 99% daily VaR (1% left tail quantile)
- π¨ Identify VaR Breaches (Exceptions)
- Days where the actual return < VaR threshold
- π Backtesting VaR β Kupiec Test
- To test if the number of breaches is consistent with the 1% expected violation rate
- π Outlier / RNIV Factors
- Returns lower than VaR thresholds are potential outliers or Risk Not in VaR (RNIV) events
- π Visualization β VaR Breach Timeline
- Example: AAPL 99% VaR and breach days
- π Compute CVaR
- Captures the average loss beyond the VaR threshold
Figure: AAPL 99% VaR Breaches Timeline (2020β2023)
The chart above illustrates the 99% Value at Risk (VaR) breaches for Apple Inc. (AAPL) from 2020β2023. Each red dot represents a day where the actual return fell below the predicted VaR threshold - meaning losses were larger than expected by the 99% confidence model.
π Key Observations
- Clustered Breaches during Market Stress:
- Most VaR breaches occurred during early 2020, aligning with the COVID-19 market crash, when volatility spiked across global equities.
- A few additional breaches appear in mid-2022, coinciding with market uncertainty and tightening monetary policy.
- Model Performance:
- The number of breaches is roughly consistent with the 1% expected violation rate, indicating that the VaR model is reasonably calibrated.
- Occasional exceedances are normal - if breaches were far more frequent, the VaR model would underestimate risk.
- Tail Behavior:
- The breaches (red points) are significantly below the VaR threshold line, highlighting that extreme losses can exceed the modeled boundary - a reminder that VaR does not capture the full magnitude of tail risk.
- This emphasizes the need to also measure CVaR, which reflects expected losses beyond VaR.
π Risk Takeaway
- VaR provides a probabilistic boundary for potential losses, but CVaR captures the severity of losses when those rare events occur.
- During crisis periods, both measures should be reassessed as historical volatility assumptions may break down.
- The Kupiec backtest p-value (typically > 0.05 in this case) supports that the VaR model was statistically consistent over the test period.
π Summary Statistics
| Metric | Description | Value |
|---|---|---|
| Total Observations | Number of daily return data points analyzed | 1,005 |
| Confidence Level | VaR calculated at 99% confidence (1% left tail) | 99% |
| Expected Breaches | Expected number of observations below VaR (1% of total) | 10.05 |
| Actual Breaches per Asset | Number of observed VaR exceedances for each ticker | 11 |
| Observed Breach Rate | (11 / 1,005) Γ 100 = 1.09% | 1.09% |
| Assets Analyzed | Portfolio constituents | AAPL, AMZN, JPM, TSLA, XOM |
| Data Period | Historical window used for analysis | Jan 2020 β Dec 2023 |
| Data Frequency | Return calculation interval | Daily |
| Backtesting Result | Observed breaches β Expected β Model well-calibrated | β Pass |