quaintitative

I write about my quantitative explorations in visualisation, data science, machine and deep learning here, as well as other random musings.

For more about me and my other interests, visit playgrd or socials below


Categories
Subscribe

Monte Carlo Simulation of Value at Risk in Python

If you recall the basics of the notebook where we provided an introduction on market risk measures and VAR, you will recall that parametric VAR simply assumes a distribution and uses the first two moments (mean and standard deviation) to compute the VAR; whereas for historical VAR, you use the actual historical data and use the specific datapoint (or interpolated values between 2 datapoints) for the confidence level.

VAR can also be computed via simulation. Which is a good way to provide a quick introduction to Monte Carlo simulation.

Simulated VAR at its core is quite simple. You basically take the moments (say mean and standard deviation if you assume a normal distribution), generate a simulated set of data with Monte Carlo simulation, and then get the required percentile. What this means is that we could also assume a non-normal distribution, say a t-distribution, and use that for simulation and to compute VAR.

First, let’s get market prices of AAPL from Quandl again, and compute the returns.

end = datetime.datetime.now()
start = end - datetime.timedelta(365)
AAPL = quandl.get('EOD/AAPL', start_date=start, end_date=end)

rets_1 = (AAPL['Close']/AAPL['Close'].shift(1))-1

We shall compute the mean and standard deviation of the AAPL returns first as we will use this later to perform Monte Carlo simulation.

mean = np.mean(rets_1)
std = np.std(rets_1)
Z_99 = stats.norm.ppf(1-0.99)
price = AAPL.iloc[-1]['Close']
print(mean, std, Z_99, price)

Out:
0.0016208298475378427 0.013753943856014762 -2.32634787404 220.79

Now, let’s compute the parametric and historical VAR numbers so we have a basis for comparison.

ParamVAR = price*Z_99*std
HistVAR = price*np.percentile(rets_1.dropna(), 1)

print('Parametric VAR is {0:.3f} and Historical VAR is {1:.3f}'
        .format(ParamVAR, HistVAR))

Out:
Parametric VAR is -7.064 and Historical VAR is -6.166

For Monte Carlo simulation, we simply apply a simulation using the assumptions of normality, and the mean and std computed above.

np.random.seed(42)
n_sims = 1000000
sim_returns = np.random.normal(mean, std, n_sims)
SimVAR = price*np.percentile(sim_returns, 1)
print('Simulated VAR is ', SimVAR)

Out:
Simulated VAR is  -6.7185294884

And that’s it! The full code can be found at the notebook here.


Articles

Comparing Prompts for Different Large Language Models (Other than ChatGPT)
AI and UIs
Listing NFTs
Extracting and Processing Wikidata datasets
Extracting and Processing Google Trends data
Extracting and Processing Reddit datasets from PushShift
Extracting and Processing GDELT GKG datasets from BigQuery
Some notes relating to Machine Learning
Some notes relating to Python
Using CCapture.js library with p5.js and three.js
Introduction to PoseNet with three.js
Topic Modelling
Three.js Series - Manipulating vertices in three.js
Three.js Series - Music and three.js
Three.js Series - Simple primer on three.js
HTML Scraping 101
(Almost) The Simplest Server Ever
Tweening in p5.js
Logistic Regression Classification in plain ole Javascript
Introduction to Machine Learning Right Inside the Browser
Nature and Math - Particle Swarm Optimisation
Growing a network garden in D3
Data Analytics with Blender
The Nature of Code Ported to Three.js
Primer on Generative Art in Blender
How normal are you? Checking distributional assumptions.
Monte Carlo Simulation of Value at Risk in Python
Measuring Expected Shortfall in Python
Style Transfer X Generative Art
Measuring Market Risk in Python
Simple charts | crossfilter.js and dc.js
d3.js vs. p5.js for visualisation
Portfolio Optimisation with Tensorflow and D3 Dashboard
Setting Up a Data Lab Environment - Part 6
Setting Up a Data Lab Environment - Part 5
Setting Up a Data Lab Environment - Part 4
Setting Up a Data Lab Environment - Part 3
Setting Up a Data Lab Environment - Part 2
Setting Up a Data Lab Environment - Part 1
Generating a Strange Attractor in three.js
(Almost) All the Most Common Machine Learning Algorithms in Javascript
3 Days of Hand Coding Visualisations - Day 3
3 Days of Hand Coding Visualisations - Day 2
3 Days of Hand Coding Visualisations - Day 1
3 Days of Hand Coding Visualisations - Introduction