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 workflow does
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 example is for
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.
Expected analysis outcomes
These are the results the AI workflow is expected to generate.
- 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
Tools and libraries used
Main Python packages and tooling used to run this AI data analysis task.
- yfinance
- pandas
- numpy
- matplotlib
- scipy
Prompt sequence
This is the exact list of prompts used in this workflow. The same prompt sequence is sent to each model so outputs and scores can be compared fairly.
- 1download 3 years of SPY, QQQ, GLD prices and compute daily portfolio returns assuming equal weights with yfinance
- 2calculate 95% and 99% Value at Risk using historical simulation
- 3calculate Conditional VaR (Expected Shortfall) at 95%
- 4plot the return distribution with VaR and CVaR thresholds marked
Model Comparison
We compared several LLM models on the same analysis task. The table below shows their scores side by side. You can also open each model run to check the full conversation and notebook results.
| Model Source | Score | Task | Execution | Output | Reasoning | Reliability | Run |
|---|---|---|---|---|---|---|---|
| glm-5.1 | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open glm-5.1 conversation |
| gpt-oss:120b | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open gpt-oss:120b conversation |
| qwen3-coder-next | 10/10 | 2/2 | 2/2 | 3/3 | 2/2 | 1/1 | Open qwen3-coder-next conversation |
| gemma4:31b | 9/10 | 2/2 | 2/2 | 2/3 | 2/2 | 1/1 | Open gemma4:31b conversation |
| gpt-5.4 | 9/10 | 2/2 | 2/2 | 2/3 | 2/2 | 1/1 | Open gpt-5.4 conversation |
| qwen3.5:397b | 2/10 | 1/2 | 0/2 | 0/3 | 1/2 | 0/1 | Open qwen3.5:397b conversation |
How scoring works
Each run is graded on five dimensions:
- Task Completion (0-2): how fully the workflow answered the requested analysis task and completed required steps.
- Execution Correctness (0-2): whether generated code is correct, consistent, and likely runnable without major fixes.
- Output Quality (0-3): how well tables, charts, and results match the expected analytical outcomes.
- Reasoning Quality (0-2): clarity and correctness of interpretation, explanation, and conclusions.
- Reliability (0-1): robustness of the run, including low hallucination risk and stable behavior.
The final score is the sum of these dimensions, so total score = 0-10. Higher values indicate more complete, correct, and reliable notebook results for this workflow.
AI Analysis with gemma4:31b
This run uses AI with a step-by-step list of prompts to perform the analysis task Value at Risk (VaR) Analysis in Python. Below, you can review the full conversation generated with gemma4:31b. After the conversation, we show detailed score cards for this run.
Score cards for gemma4:31b for Value at Risk (VaR) Analysis in Python
Total score: 9/10
Task Completion: 2/2
ExcellentAll major requested steps were completed: data download, equal-weighted portfolio returns, VaR at 95% and 99%, CVaR at 95%, and a histogram with thresholds marked.
Execution Correctness: 2/2
ExcellentThe provided code is syntactically coherent and likely runnable. It uses yfinance, computes returns, quantiles, CVaR, and plots the distribution without obvious major errors.
Output Quality: 2/3
GoodThe outputs include the portfolio return series, VaR values, CVaR, and a plot. However, the VaR values are slightly different from the approximate expected outcomes, so this is mostly correct rather than fully matching.
Reasoning Quality: 2/2
ExcellentThe reasoning correctly explains historical simulation VaR/CVaR and the use of quantiles and tail averages. It is clear and consistent with the workflow.
Reliability: 1/1
ExcellentThe workflow is reasonably robust and internally consistent, with no major hallucinations or unsafe steps.
Try MLJAR Studio
Run the same type of AI-powered data analysis on your own datasets with conversational notebooks in MLJAR Studio.
Explore More AI Analysis Examples
Discover additional workflows across categories. Each example includes prompts, conversation outputs, and model-level scoring so you can compare approaches and results.