VAR with Monte Carlo

tomtom

7 Nov '26

Project codes are available at Github

I have been exploring the field of financial risk management, out of curiosity on how markets assess risk. My readings led me to the often-mentioned risk measure called Value at Risk (VAR) and Monte Carlo simulation. In this article, I explore what are VAR and Monte Carlo, and will demonstrate how to calculate VAR using Monte Carlo simulation using simple Python code. The code is available on the Github repository link above.

Value at Risk (VaR)

Risk managers are concerned with with the possibility of adverse events. Although volatility of an asset can be measured by its standard deviation, risk managers and senior management are concerned about concerned about the tails of the probability distribution. Value at Risk (VaR) is an important measure that focuses on adverse events and their probabilities. It is a function of two parameters: the time horizon and the confidence level. It is defined as the maximum loss that a portfolio of financial assets can incur over a defined period for a given confidence interval. For example, if a portfolio has a 1-day 5% VaR of $1 million, it means that there is a 5% chance that the portfolio will lose more than $1 million in a single day. Another way to interpret this is that there is a 95% chance that the portfolio will not lose more than $1 million in a single day. VaR can be calculated using different methods, including historical simulation, variance-covariance (delta-normal) method, and Monte Carlo simulation. In this blog post, I will focus on the Monte Carlo simulation method.

Monte Carlo Simulation

The Monte Carlo is a simple approach to approximate the expected value of a random variable using numberical methods. It draws from an assumed data generating process and then applies a function(s) to generate a realisation. This process is repeated many times to generate a distribution of possible outcomes. Repeating the simulation improves the accuracy of the approximation.

Calculating VaR using Monte Carlo Simulation

Below is an attempt to calculate VaR using Monte Carlo simulation. It is a simple simulation in which the values like the ticker, confidence level, and time horizon are specified in the code. In this simulation, the results will be interpreted as there is a 5% chance that the portfolio will lose more than the VAR in a 30-day period or there is a 95% chance that the portfolio will not lose more than the VAR in a 30-day period.

Parameters used in the simulation: Ticker: MU; Confidence Level: 95%; Time Horizon: 30 days; Downloaded Data: 31/8/2024 - 31/8/2026

hsbc
Downloading data from Yahoo Finance

The average daily return and volatility are calculated from the downloaded data using Python's pandas library. The results are used as the inputs to generate future prices.

hsbc
Calculating daily returns and statistics

hsbc
Results in terminal

This code sets up and runs a Monte Carlo simulation of future asset prices using Geometric Brownian Motion (GBM) in Python with NumPy. It simulates 1000 possible future price paths across a 30-row grid using Geometric Brownian Motion. First, it defines the number of simulations, time horizon, and starting price from the latest closing data. Next, it generates a grid of standard normal random shocks and creates a price table with row zero set to that starting price. Then, looping step by step through time, it combines drift, volatility adjustment, and each step's random shock to turn previous prices into next-step prices across all simulations.

hsbc
Monte Carlo calculations

The VAR is calculated by taking the simulated price paths and determining the value at risk at the specified confidence level. The final row across all 1000 simulated columns in price_paths are selected. The 5th percentile cutoff of final_prices, which is the price level below which the worst 5% of simulated outcomes fall is assigned to worst_case_price.This corresponds to a 95% confidence level on the downside tail. Finally, the 95% VAR is computed by subtracting the worst_case_price from the last_price

hsbc
Calculating VAR from simulated price paths

The results are then plotted using Python's matplotlib library. Combining it with the calculations obtained, the interpretation is that there is a 5% chance that the portfolio will lose more than the $223.44 in a 30-day period or there is a 95% chance that the portfolio will not lose more than the $223.44 in a 30-day period.

hsbc
Simulation results plotted using matplotlib

hsbc
Results in terminal

This project is a simple demonstration of how to use the Monte Carlo method to calculate Value at Risk (VaR) for a single stock. It uses an assumption that the data is generated using Geometric Brownian Motion, which may not always be accurate in real-world scenarios. E.g. Real equity returns often display fat tails (leptokurtosis). A more robust and complex implementation would allow user to key in the parameters for the simulation. Perhaps that will be another project in the pipeline. ☺