Skip to content

Sowmyadoestech/var-model

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

📊 Quantitative Portfolio Risk Assessment — VaR Model

Python NumPy Pandas License Status

A quantitative risk model that calculates Value at Risk (VaR) for a multi-stock portfolio using Monte Carlo simulation — built from raw market data through to a production-grade risk report.


📌 Overview

This project simulates 10,000 market scenarios across a $1,000,000 hypothetical portfolio to answer one critical question:

"With 95% confidence, what is the maximum this portfolio can lose in a single day?"

That number is Value at Risk (VaR) — the core risk metric used by every major bank, hedge fund, and asset manager on Wall Street.


🗂️ Project Structure

var-model/
│
├── data/
│   └── prices.csv              # Historical closing prices (5 years, 5 stocks)
│
├── notebooks/
│   └── var_analysis.ipynb      # Full walkthrough with charts & commentary
│
├── src/
│   ├── data_pull.py            # Yahoo Finance data extraction
│   ├── returns.py              # Daily returns & volatility calculations
│   ├── covariance.py           # Covariance matrix construction
│   ├── monte_carlo.py          # 10,000-scenario Monte Carlo simulation
│   └── var_report.py           # VaR output & visualisations
│
├── requirements.txt
├── .gitignore
├── LICENSE
└── README.md

🔬 Methodology

Phase 1 — Portfolio Construction

A $1,000,000 portfolio is built across 5 stocks spanning uncorrelated sectors:

Ticker Company Sector Weight
AAPL Apple Technology 25%
XOM ExxonMobil Energy 20%
JPM JPMorgan Financials 20%
NVDA Nvidia Semiconductors 20%
JNJ J&J Healthcare 15%

Phase 2 — Data Extraction

5 years of daily closing prices pulled from Yahoo Finance via yfinance.

Phase 3 — Statistical Engine

# Daily log returns
returns = np.log(prices / prices.shift(1)).dropna()

# Volatility (annualised standard deviation)
volatility = returns.std() * np.sqrt(252)

# Covariance matrix — how stocks move relative to each other
cov_matrix = returns.cov() * 252

Phase 4 — Monte Carlo Simulation

Using Cholesky decomposition to preserve the real-world correlation structure between assets, the model generates 10,000 independent 1-day portfolio return scenarios.

# Cholesky decomposition preserves covariance structure
L = np.linalg.cholesky(cov_matrix)
simulated_returns = (L @ np.random.normal(size=(n_assets, n_simulations))).T

Phase 5 — VaR Output

95% 1-Day VaR:   -$XX,XXX  (portfolio will not lose more than this 95% of days)
99% 1-Day VaR:   -$XX,XXX  (tail risk — the worst 1% of scenarios)
Expected Shortfall (CVaR): -$XX,XXX  (average loss beyond VaR threshold)

🚀 Getting Started

Prerequisites

Python 3.10+

Installation

# 1. Clone the repo
git clone https://github.com/YOUR_USERNAME/var-model.git
cd var-model

# 2. Create a virtual environment
python -m venv venv
source venv/bin/activate        # Mac/Linux
venv\Scripts\activate           # Windows

# 3. Install dependencies
pip install -r requirements.txt

# 4. Run the model
python src/monte_carlo.py

Or open the notebook

jupyter notebook notebooks/var_analysis.ipynb

📈 Output

The model produces:

  • VaR figure at 95% and 99% confidence intervals
  • Return distribution histogram with VaR threshold marked
  • Correlation heatmap of the portfolio's asset relationships
  • Portfolio simulation fan chart across 10,000 scenarios

🧰 Tech Stack

Tool Purpose
Python Core language
NumPy Matrix algebra & simulation
Pandas Data wrangling
yfinance Market data extraction
Matplotlib Visualisation
SciPy Statistical functions
Jupyter Interactive analysis notebook

📄 License

MIT — see LICENSE

About

Quantitative portfolio risk assessment using Monte Carlo simulation to calculate Value at Risk (VaR) across a $1M multi-stock portfolio.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors