forked from hwyler/HernanHuwylerRiskManagement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonInputVariables
More file actions
145 lines (114 loc) · 6.98 KB
/
PythonInputVariables
File metadata and controls
145 lines (114 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"""
Risk Assessment with User Prompts
Objective:
This repository contains a Python code for assessing risks using Monte Carlo simulations. The code allows users to input the expected
range of losses, the confidence level in setting the range of losses, the expected range of the number of incidents per year, and the
confidence level in setting the range of probabilities.
How to Use the Risk Assessment Python Code
This Python code is designed to assess risks using Monte Carlo simulations.
It allows users to input the expected range of losses and probabilities in the best and worst scenarios,
along with a confidence level. The code then generates random samples based on the provided inputs and
calculates the expected exposures.
It can be run on this web compiler https://www.codabrainy.com/en/python-compiler/
Inputs
- Expected Maximum and Minimum Values of a Loss
These inputs represent the range of losses that you expect in the worst and best scenarios,
respectively. For example, if you estimate that the loss per event could range from 90 to 100 USD,
you would enter 100 as the maximum value and 90 as the minimum value.
- Confidence Level in Setting the Range of Losses:
This input represents your confidence in the estimated range of losses. It is a value between 0 and 1 (as percentage).
For instance, if you set the confidence level to 0.9 (90%), it means that you expect 10% of the events to
cause losses lower than the minimum value (e.g., 90 USD) or higher than the maximum value (e.g., 100 USD).
- Expected Maximum and Minimum Number of Expected Incidents per Year:
These inputs represent the range of the number of incidents you expect to occur in a year,
in the worst and best scenarios, respectively. The incidents are whole numbers or counting numbers (1, 2, 3).
- Confidence Level in Setting the Range of Probabilities:
This input represents your confidence in the estimated range of the number of incidents.
It is a value between 0 and 1.
Log-normal Distribution
The code assumes that the distribution of losses follows a log-normal distribution.
This distribution is commonly used to model operational incidents because it allows for a
long tail of events causing higher losses. In other words, there are more scenarios
that can cause losses exceeding the maximum value compared to scenarios causing losses below the minimum value.
Convolution
The code performs a convolution between the losses and probabilities to calculate the expected exposures.
Convolution is a mathematical operation that combines two functions (in this case, the log-normal distribution
of losses and the Poisson distribution of probabilities) to produce a third function
(the distribution of expected exposures).
In the context of risk assessment, convolution helps to determine the distribution of total losses by considering both the severity of individual losses and the frequency of incidents. By convolving the loss distribution with the probability distribution, the code generates a distribution of expected exposures that takes into account the variability in both losses and probabilities.
Use case
Example to illustrate the inputs and interpretation of the results:
Expected Maximum Value of a Loss: 100 USD
Expected Minimum Value of a Loss: 90 USD
Confidence Level in Setting the Range of Losses: 0.9 (90%)
Expected Maximum Number of Expected Incidents per Year: 10
Expected Minimum Number of Expected Incidents per Year: 5
Confidence Level in Setting the Range of Probabilities: 0.8 (80%)
In this example, you estimate that the loss per event could range from 90 to 100 USD,
with a 90% confidence level. This means that you expect 10% of the events to cause losses lower
than 90 USD or higher than 100 USD. Additionally, you estimate that the number of incidents per
year could range from 5 to 10, with an 80% confidence level.
The code will generate random samples based on these inputs and calculate the expected exposures.
The resulting histogram and loss exceedance curve will provide insights into the distribution of
expected exposures and the probability of exceeding different exposure levels.
Libraries:
- numpy for mathematical operations and array manipulation
- matplotlib.pyplot for data visualization
- scipy.stats.lognorm for generating random numbers from a log-normal distribution
- scipy.stats.poisson for generating random numbers from a Poisson distribution
Developed by:
Prof. Hernan Huwyler MBA CPA
Academic Director, IE Business School, IE Law School, IE Executive Education
Keywords:
Risk, Compliance, Quantitative Risks, Modelling, Covolution, Monte Carlo Simulations
Log-normal Distribution, Poisson Distribution, Expected Exposures, Loss Exceedance Curve
License
This code is licensed under the MIT License. See the LICENSE file for more information.
Created by: Prof. Hernan Huwyler
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import lognorm, poisson
# User inputs
max_loss = float(input("Enter the expected maximum value of a loss: "))
min_loss = float(input("Enter the expected minimum value of a loss: "))
loss_confidence = float(input("Enter the confidence level in setting the range of losses (0-1): "))
max_incidents = int(input("Enter the expected maximum number of expected incidents per year: "))
min_incidents = int(input("Enter the expected minimum number of expected incidents per year: "))
prob_confidence = float(input("Enter the confidence level in setting the range of probabilities (0-1): "))
# Calculate parameters for log-normal distribution of impacts
loss_mean = np.log(min_loss) + (np.log(max_loss) - np.log(min_loss)) * loss_confidence
loss_std = (np.log(max_loss) - np.log(min_loss)) * (1 - loss_confidence) / 2
# Calculate parameters for Poisson distribution of probabilities
prob_mean = min_incidents + (max_incidents - min_incidents) * prob_confidence
# Perform Monte Carlo simulations
num_simulations = 100000
losses = lognorm.rvs(loss_std, scale=np.exp(loss_mean), size=num_simulations)
probabilities = poisson.rvs(prob_mean, size=num_simulations)
exposures = losses * probabilities
# Calculate statistics of expected exposures
percentiles = np.arange(0.1, 1, 0.1)
exposure_stats = np.percentile(exposures, percentiles * 100)
# Display statistics
print("Expected Exposures:")
for p, e in zip(percentiles, exposure_stats):
print(f"P({int(p*100)}): {e:.2f}")
# Display histogram
plt.figure(figsize=(8, 6))
plt.hist(exposures, bins=50, density=True, alpha=0.7)
plt.xlabel("Exposure")
plt.ylabel("Probability Density")
plt.title("Histogram of Expected Exposures")
plt.grid(True)
plt.show()
input("Press any key to continue...")
# Display loss exceedance curve
sorted_exposures = np.sort(exposures)
exceedance_probabilities = 1 - np.arange(1, len(exposures) + 1) / len(exposures)
plt.figure(figsize=(8, 6))
plt.plot(sorted_exposures, exceedance_probabilities)
plt.xlabel("Exposure")
plt.ylabel("Exceedance Probability")
plt.title("Loss Exceedance Curve")
plt.grid(True)
plt.show()