Dan Chui
Happy Bytes
cybersecurity

From Excel to Python: Building a Value at Risk (VaR) Model for a Multi-Asset Portfolio

From Excel to Python: Building a Value at Risk (VaR) Model for a Multi-Asset Portfolio
5 min read
#cybersecurity

๐Ÿ’ก Why This Project?

As part of my transition into technology-focused roles, I wanted to revisit one of the core concepts from my background in financial risk: Value at Risk (VaR).

Rather than treating this as a purely academic exercise, I approached it with two goals:

  • Reinforce core financial risk concepts (VaR, covariance, portfolio risk)
  • Translate those concepts into a Python-based workflow to demonstrate automation and data analysis skills

This project became a useful bridge between:

  • Finance โ†’ Risk Modeling
  • Technology โ†’ Python, data processing, reproducibility

๐Ÿ“Š What is Value at Risk (VaR)?

Value at Risk (VaR) estimates the potential loss in value of a portfolio over a given time horizon at a specified confidence level.

For example:

A 10-day 95% VaR of $3,268 means that, under normal conditions, the portfolio is not expected to lose more than $3,268 over 10 days, 95% of the time.


๐Ÿงฉ Portfolio Setup

To keep things practical, I used a simple diversified portfolio:

AssetWeight
SPY40%
QQQ25%
AGG25%
GLD10%
  • Portfolio Value: $100,000
  • Data: ~1 year of daily returns

๐Ÿง  Why These Assets?

The portfolio was constructed using four widely traded ETFs to represent different asset classes:

AssetDescription
SPYU.S. broad equity market (S&P 500)
QQQU.S. growth / technology-focused equities
AGGU.S. investment-grade bonds
GLDGold (commodity / alternative asset)

These assets were selected to illustrate a simple diversified portfolio with exposure to:

  • Equities (SPY, QQQ) โ†’ higher growth, higher volatility
  • Fixed Income (AGG) โ†’ lower volatility, income component
  • Alternatives (GLD) โ†’ diversification and potential hedge during market stress

The combination helps demonstrate a key concept in portfolio risk:

Portfolio risk is not just driven by individual asset volatility, but by how assets move relative to each other (correlation).

By including assets with different risk profiles and correlations, the model highlights the impact of diversification on:

  • portfolio variance
  • covariance structure
  • and overall Value at Risk (VaR)

While simplified, this structure reflects a common multi-asset allocation framework used in portfolio and risk management.


โš™๏ธ Methodology

Two approaches were used:

1๏ธโƒฃ Historical VaR

  • Based on actual historical return distribution
  • No assumptions about normality
  • Computed using percentiles of portfolio returns

2๏ธโƒฃ Parametric VaR (Variance-Covariance)

  • Assumes returns follow a normal distribution
  • Uses:
    • Covariance matrix
    • Portfolio variance
    • Z-scores
  • Applies square-root-of-time scaling for multi-day VaR

๐Ÿงฎ Key Step: Portfolio Risk Aggregation

A core concept in this project is how portfolio risk is calculated.

Portfolio variance is computed as:

Portfolio Variance = wแต€ ฮฃ w

Where:

  • w = weights vector
  • ฮฃ = covariance matrix

This captures not just individual asset volatility, but also how assets move together.


๐Ÿ Why Python?

The Excel model was useful for validation, but Python adds:

  • Reproducibility
  • Automation
  • Scalability
  • Cleaner handling of datasets

Using Python libraries such as:

  • pandas โ†’ data handling
  • numpy โ†’ matrix operations
  • matplotlib โ†’ visualization

I was able to replicate the full VaR workflow programmatically.


๐Ÿ“ˆ Results

Key outputs from the analysis:

  • Portfolio Daily Volatility: ~0.63%
  • 1-Day Historical VaR (95%): ~$1,044
  • 10-Day Parametric VaR (95%): ~$3,268

Interpretation

Under normal market conditions, the portfolio is expected to lose no more than approximately $3,268 over a 10-day period with 95% confidence.


๐Ÿ“Š Visualization

The project also includes:

  • Distribution of portfolio returns (histogram)
Distribution of Portfolio Returns Histogram
  • Time series of portfolio returns
Time Series of Portfolio Returns

These help visualize:

  • volatility clustering
  • distribution shape
  • tail risk behavior

โš ๏ธ Limitations

Like all VaR models, there are important assumptions:

  • Parametric VaR assumes normally distributed returns
  • Historical VaR depends heavily on the chosen time window
  • Square-root-of-time scaling assumes independent returns
  • Extreme tail events are not fully captured

๐Ÿ” Connecting Finance and Technology

This project reflects a broader direction Iโ€™ve been working toward:

  • Applying analytical thinking from financial risk
  • Using technical tools (Python, data workflows)
  • Building repeatable, explainable models

The same mindset carries over into:

  • security monitoring
  • risk assessment
  • data-driven decision-making

๐Ÿ Final Thoughts

This wasnโ€™t about building the most complex VaR model.

It was about:

  • revisiting core concepts,
  • implementing them cleanly,
  • and demonstrating how traditional risk analysis can be translated into modern, technical workflows.

๐Ÿ”— Full report and supporting files available on GitHub


๐Ÿ“Œ Next Steps

Potential extensions include:

  • Monte Carlo simulation
  • stress testing scenarios
  • integrating real-time data
  • expanding into risk dashboards

โš ๏ธ Disclaimer:
This project is a simplified portfolio risk analysis created for educational and portfolio purposes. The data and assumptions used (including asset selection, return distributions, and time horizons) are illustrative and do not represent investment advice or real-world portfolio recommendations.


Thanks for reading! ๐Ÿ™

If you're interested in financial risk, data-driven analysis, or the intersection of finance and technology - feel free to connect with me on LinkedIn.

Iโ€™m particularly interested in roles and projects related to risk management, governance, and analytical problem-solving across finance and technology.

Feel free to reach out with any questions or thoughts.