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 gpt-oss:120b
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 gpt-oss:120b. After the conversation, we show detailed score cards for this run.
Score cards for gpt-oss:120b for Value at Risk (VaR) Analysis in Python
Total score: 10/10
Task Completion: 2/2
ExcellentThe notebook completed the requested workflow: downloaded SPY/QQQ/GLD data, computed equal-weight portfolio returns, calculated VaR/CVaR, and produced a histogram with threshold lines.
Execution Correctness: 2/2
ExcellentThe code shown is syntactically coherent and the outputs indicate it ran successfully, including the plotted figure and numeric risk metrics.
Output Quality: 3/3
ExcellentOutputs match the expected outcomes semantically: portfolio return series, VaR values, CVaR value, and a histogram with marked thresholds. The VaR magnitudes are reasonable and the plot is present.
Reasoning Quality: 2/2
ExcellentThe workflow correctly uses historical simulation and equal-weight aggregation, and the explanations for VaR and CVaR are appropriate and consistent with the calculations.
Reliability: 1/1
ExcellentThe approach is standard and consistent, with no obvious hallucinations or unstable logic in the provided notebook content.
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.