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.5:397b
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.5:397b. After the conversation, we show detailed score cards for this run.
Score cards for qwen3.5:397b for Value at Risk (VaR) Analysis in Python
Total score: 2/10
Task Completion: 1/2
Needs workThe workflow only attempts the initial data download and equal-weighted return calculation. It does not compute VaR, CVaR, maximum drawdown, or produce the required histogram.
Execution Correctness: 0/2
Needs workThe provided code fails with a KeyError on 'Adj Close', so it does not run successfully as shown. The notebook output indicates the core data extraction step is broken.
Output Quality: 0/3
Needs workExpected outputs are missing: there is no VaR/CVaR result, no drawdown metric, and no plot. The only output is an error traceback.
Reasoning Quality: 1/2
Needs workThe assistant correctly identifies the need to download data and compute equal-weighted returns, but the reasoning stops far short of the full risk analysis task. It also incorrectly assumes the selected 'Adj Close' structure will work without verifying the actual yfinance output format.
Reliability: 0/1
Needs workThe workflow is fragile because it makes an unsupported assumption about the downloaded DataFrame structure and immediately errors. It is not robust to common yfinance output variations.
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.