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 qwen3-coder-next
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 qwen3-coder-next. After the conversation, we show detailed score cards for this run.
Score cards for qwen3-coder-next for Value at Risk (VaR) Analysis in Python
Total score: 10/10
Task Completion: 2/2
ExcellentThe workflow completed all major steps: downloaded SPY/QQQ/GLD data, computed equal-weighted portfolio returns, calculated 95% and 99% VaR, computed 95% CVaR, and produced a histogram with thresholds marked.
Execution Correctness: 2/2
ExcellentThe code shown is coherent and likely runnable. The notebook outputs confirm successful execution, including the return series, VaR/CVaR values, and plot generation.
Output Quality: 3/3
ExcellentOutputs match the expected outcomes semantically: a portfolio return series is shown, VaR/CVaR are printed, and the distribution plot includes VaR/CVaR threshold lines and shaded tail.
Reasoning Quality: 2/2
ExcellentThe reasoning correctly explains historical simulation, percentile-based VaR, and CVaR as the mean of the worst 5% of returns. It also correctly interprets the MultiIndex structure from yfinance.
Reliability: 1/1
ExcellentThe workflow is reasonably robust and consistent, with outputs supporting the calculations. Minor fragility exists in relying on notebook state across cells, but no hallucinated logic is present.
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.