Professional Documents
Culture Documents
IE Prof Hernan Huwyler Monte Carlo Convolution Python 1712836424
IE Prof Hernan Huwyler Monte Carlo Convolution Python 1712836424
Script for convolving distributions and generating 100k Monte Carlo simulations
2 No installations required
4 Immediate charts
Examples
8%
of the
potential 12%
losses of the potential losses
(best cases) 80% (worst cases, pessimistic estimation)
of the potential
losses
(most common)
Setting the loss variables in Python
Example
Examples
Example
=
Loss exceedance curve
Mean: 5878.21
75.0th Percentile Loss: 7727.16
P(10): 2021.54
P(20): 3016.65
P(30): 3804.37
P(40): 4547.16
P(50): 5317.37
7727 P(60): 6145.36
5878 P(70): 7142.74
P(80): 8405.11
P(90): 10396.85
P(99): 16494.88
How do I read the results?
A contingency reserve of 7,727 USD
should be established to mitigate potential
financial losses arising from the identified
risks, with the objective of covering 75%
of the most common occurrences
… or ….
¨Step 4 Replace the line by the copied code and click Run
Use instructions
Step 5 Adjust the estimated number of risk events and financial losses for both the best-case
and worst-case scenarios for a given confidence level to better reflect your risk assessment
Use instructions
Step 6 Analyze the data and charts
Settings
Libraries
¨
• numpy: For numerical operations.
• matplotlib.pyplot: For plotting graphs
• scipy.stats: Specifically, importing lognorm, poisson, and norm for probability distributions
Data inputs
• Calculate lower and upper quantiles for the confidence level using the inverse cumulative
distribution function of the lognormal distribution
• Calculate the true mean and standard deviation of the log-transformed values for the
lognormal distribution
• Generate random values from a lognormal distribution to simulate impacts for losses
• Simulate Poisson-distributed random variables for the number of events
• Combine distributions using convolution for each scenario separately
• Calculate the total loss for each scenario
• Print the mean and given percentile of the simulated losses
• Calculate percentiles of the simulated losses and plot a loss exceedance curve
• Plot histograms for potential losses and potential incidents
Code
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import lognorm, poisson, norm
# Data input
Simulations = 1000000 # Number of scenarios to simulate
lower = 1000 # Lower value of the potential loss
upper = 2000 # Upper value of the potential loss
confidence_level = 0.8 # Confidence level in the estimated loss
Events = 4 # Number of expected annual events to materialize (Poisson distribution)
Reserve = 0.75 # This percentile is a measure of the risk exposure
np.random.seed(123) # Set a random seed for replicability