Finance
Value at Risk (VaR) Analysis in Python
Compute Value at Risk (VaR), Conditional VaR (CVaR), and maximum drawdown for a stock portfolio using historical simulation.
What
This AI Data Analyst workflow downloads 3 years of SPY, QQQ, and GLD price data, computes daily returns, and builds an equal-weight portfolio return series. It estimates 95% and 99% Value at Risk (VaR) using historical simulation and computes 95% Conditional VaR (CVaR/Expected Shortfall). It visualizes the return distribution with VaR and CVaR thresholds and reports maximum drawdown for the portfolio.
Who
This is for analysts and students who need a reproducible Python notebook for portfolio risk metrics from market data. It helps validate downside risk estimates and communicate them with standard plots and summary statistics.
Tools
- yfinance
- pandas
- numpy
- matplotlib
- scipy
Outcomes
- Daily equal-weight portfolio return series
- Historical-simulation VaR estimates at 95% and 99%
- CVaR (Expected Shortfall) at 95%
- Maximum drawdown statistic
- Histogram of returns with VaR/CVaR threshold lines
Quality Score
9/10
Last scored: Apr 7, 2026
Task Completion: 2/2
ExcellentAll requested steps are present: data download for SPY/QQQ/GLD, equal-weight portfolio returns, historical VaR (95/99), 95% CVaR, and a histogram/KDE plot with VaR/CVaR lines.
Execution Correctness: 2/2
ExcellentProvided Python code is coherent and likely runnable end-to-end (yfinance download, return computation, quantiles, tail mean, seaborn/matplotlib plot) with no evident syntax or logic-breaking errors.
Output Quality: 2/3
GoodOutputs include the portfolio return series, VaR values, CVaR numeric output, and the required plot. However, the VaR 95% (~1.29%) does not match the expected approximate ~-1.5% (though VaR 99% ~2.25% is close to ~-2.3%), so it only mostly matches expectations.
Reasoning Quality: 2/2
ExcellentExplanations correctly describe historical simulation VaR, the sign convention (loss as positive vs cutoff return as negative), and CVaR as the mean of the worst 5% tail.
Reliability: 1/1
ExcellentWorkflow is reasonably robust (dropna, clear variable definitions, standard methods) and avoids hallucinated libraries/functions; minor fragility remains around data availability/network dependence via yfinance.