Skip to content

halim-quantlab/VaR-MonteCarlo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📊 Monte Carlo Simulation for Value at Risk (VaR) with Jump Diffusion Model

This project simulates future stock price paths using a Monte Carlo method with a Geometric Brownian Motion + Jump Diffusion model. It calculates Value at Risk (VaR) and Conditional VaR (CVaR), identifies historical jumps in log returns, and outputs detailed simulation data and risk metrics in both graphical and CSV format.


🔧 Features

  • ✅ Fetches historical stock data using yfinance
  • ✅ Computes log-adjusted returns and statistical parameters
  • ✅ Simulates multiple price paths using:
    • Geometric Brownian Motion
    • Poisson-driven Jump Diffusion
  • ✅ Calculates:
    • Value at Risk (VaR)
    • Conditional VaR (CVaR / Expected Shortfall)
  • ✅ Detects price jumps based on standard deviation cutoff
  • ✅ Visualizes:
    • Simulated price paths
    • Histogram of final simulated prices with VaR overlay
  • ✅ Saves:
    • Price path simulations
    • Jump event logs
    • Summary statistics

📁 Project Structure

image


🧪 How It Works

  1. Fetch Stock Data
    User provides a stock ticker, date range, and simulation preferences. image

  2. Jump Detection
    Users can choose to auto-detect jumps based on statistical deviations from a specified sigma cutoff, or manually input their own values for average jump frequency (λ), mean jump size (μj), and jump volatility (σj). Example for auto-detect output:

image

Example for user input and no average jump:

image

  1. Monte Carlo Simulation
    Thousands of future paths are simulated incorporating both drift, volatility, and jump events. image

  2. Risk Calculation
    From simulated ending prices:

    • VaR is estimated at a user-defined confidence level
    • CVaR is the average of losses beyond the VaR threshold image image
  3. Data Export and Visualization
    image

Results are plotted and optionally saved to CSV files for further analysis.


📌 Example Output

Exports

  • data/{Ticker}_price_paths.csv — Simulated prices for all trials
  • data/{Ticker}_jumps.csv — Dates where return jumps exceeded threshold
  • data/{Ticker}_summary.csv — Summary stats (VaR, CVaR, λ, etc.)
  • data/{Ticker}_Simulated_Price_Paths.png — 📈 Line chart of simulated price paths
  • data/{Ticker}_Distribution_of_Final_Simulated_Prices.png — 📊 Histogram showing final price distribution with VaR cutoff

✏️ User Inputs

  • Ticker: Stock symbol (e.g., AAPL, MSFT)
  • Date Range: Start and end date for historical data. If end date is NA, will take today's date
  • Simulation Days: Number of future trading days to simulate (e.g., 1, 5, 30)
  • Trials: Number of Monte Carlo trials (e.g., 1000, 10,000)
  • Confidence Level: For VaR and CVaR (e.g., 0.90, 0.95)

📦 Requirements

pip install yfinance numpy pandas matplotlib seaborn scipy tabulate

About

Monte Carlo Value at Risk Simulator with jump diffusion model

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors