forked from INET-Complexity/isle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributionaggregate.py
More file actions
330 lines (280 loc) · 11.7 KB
/
Copy pathdistributionaggregate.py
File metadata and controls
330 lines (280 loc) · 11.7 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import pickle as pkl
from typing import Tuple
import scipy.stats
import scipy.interpolate
import scipy.signal
import numpy as np
from genericclasses import RiskProperties
with open("./pdf_data.pkl", "rb") as rfile:
pdfs: dict = pkl.load(rfile)
def find_nearest(array, value):
"""Given an array and a value, returns the element of the array nearest to the value"""
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return array[idx]
def find_nearest_index(array, value):
"""Given an array and a value, returns the index of the element of the array nearest to the value"""
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return idx
class AggregateDistribtion(scipy.stats.rv_continuous):
"""
Overall damage distribution for n risks in the same category. Distribution is normalised to lie between 0 and 1,
so needs to be mulitplied by total_value
"""
def __init__(self, n):
super().__init__(a=0, b=1)
self.n = n
try:
self._pdf_data = pdfs[n]
self._pdf_x = np.array(self._pdf_data[0]) / self.n
self._pdf_y = np.array(self._pdf_data[1]) * self.n
except KeyError:
n_fake = find_nearest(pdfs.keys(), self.n)
print(
f"Warning: tried to use non-existant pre-computed distribution for n = {self.n}"
)
print(f"Using scaled version of closest available value, {n_fake}")
self._pdf_data = pdfs[n_fake]
self._pdf_x = np.array(self._pdf_data[0]) / n_fake
self._pdf_y = np.array(self._pdf_data[1]) * n_fake
# The stored data usually isn't perfectly normalised due to numerical inaccuracy / floating point errors
self._pdf_y = self._pdf_y / np.sum(self._pdf_y) * len(self._pdf_x)
self._cdf_y = np.cumsum(self._pdf_y) / len(self._pdf_x)
self._pdf = scipy.interpolate.interp1d(
self._pdf_x,
self._pdf_y,
kind="cubic",
bounds_error=False,
fill_value=0,
assume_sorted=True,
)
self._cdf = scipy.interpolate.interp1d(
self._pdf_x,
self._cdf_y,
kind="linear",
bounds_error=False,
fill_value=(0, 1),
assume_sorted=True,
)
def _rvs(self, *args):
raise Exception("Shouldn't generate rvs from this, there are better ways...")
def contract_risk(
number_risks: int,
value_per_risk: int,
deductible: int,
limit: int,
runtime: int,
max_claims: int,
var_tail_size: float = 0.005,
cat_frequency: float = 3 / 100,
max_res: int = 2000,
approx: bool = True,
) -> Tuple[float, float, int, int]:
"""
Calculates various risk statistics for a contract.
Uses the pre-calculated density functions for aggregate claims, so should be accurate even for only a few risks.
If the total value (number_risks * value_per_risk) is very high (>10,000) then the calculations will be less
accurate (not by much), as the calculations will be scaled for performance reasons. This is controled by max_res
MU = monetary units
Args:
number_risks: The number of risk being (re)insured
value_per_risk: The value of each risk (all risks have the same value) in MU
deductible: The deductible of the contract under consideration in MU
limit: The limit of the contract under consideration in MU
runtime: The total runtime of the contract
max_claims: The maximum number of claims that can be made. If 0, will be assumed equal to runtime.
var_tail_size: The tail size to use when calculating the VaR (default 0.005)
cat_frequency: The probability of a catastrophe in each time unit (default 3/100)
max_res: The maximum number of value increments used in the calculations. Should be at least 1000
Returns:
expected_total_claim: The expectation of the total claims under this contract
std: The standard deviation of the total claims
var: The VaR in MU at the tail size given
exposure: limit - deductible
"""
# We assume throughout that claims are only whole-number monetary units, and are the ceiling of the actual damage.
total_value = number_risks * value_per_risk
# We change value_per_risk to get the resolution of the calculations to a suitable scale
if total_value > max_res:
factor = total_value / max_res
total_value = int(round(total_value / factor))
deductible = int(round(deductible / factor))
limit = int(round(limit / factor))
else:
factor = 0
claim_amounts = np.arange(total_value + 1)
damage_probabilities = AggregateDistribtion(number_risks).pdf(
claim_amounts / total_value
)
# Change from a density to a pmf
damage_probabilities = damage_probabilities / np.sum(damage_probabilities)
claim_probabilities = np.zeros_like(damage_probabilities)
claim_probabilities[0 : limit - deductible] = damage_probabilities[deductible:limit]
claim_probabilities[0] += np.sum(damage_probabilities[0:deductible])
claim_probabilities[limit - deductible] += np.sum(damage_probabilities[limit:])
# plt.plot(claim_amounts, damage_probabilities)
# plt.plot(claim_amounts, claim_probabilities)
# plt.show()
if not (0 < max_claims <= runtime):
max_claims = runtime
per_event_exposure = min(limit, total_value) - deductible
exposure = per_event_exposure * max_claims
total_claim_values = np.arange(exposure + 1)
total_claim_probabilities = np.zeros(total_claim_values.shape)
for number_claims in range(max_claims + 1):
p = scipy.stats.binom.pmf(n=runtime, k=number_claims, p=cat_frequency)
if approx and p < 0.01:
# Skip if probability of this number of events is close to trivial
continue
if number_claims == max_claims:
# Add on all the events where there are more cats than the max number of claims
p += scipy.stats.binom.sf(n=runtime, k=number_claims, p=cat_frequency)
if number_claims == 0:
# [1, 0, 0, ...]
dist_this_number = np.array([1.0] + [0] * exposure)
else:
dist_this_number = claim_probabilities.copy()
if number_claims > 1:
dist_this_number = fftconvolve_n(claim_probabilities, number_claims)
if len(dist_this_number) > len(total_claim_probabilities):
total_claim_probabilities += (
p * dist_this_number[: len(total_claim_probabilities)]
)
else:
total_claim_probabilities[: len(dist_this_number)] += p * dist_this_number
# Try to minimise the effect of numerical error and approximation by normalising
total_claim_probabilities = total_claim_probabilities / np.sum(
total_claim_probabilities
)
total_claim_cumulative_probabilities = np.cumsum(total_claim_probabilities)
if total_claim_cumulative_probabilities[-1] >= 1 - var_tail_size:
var = total_claim_values[
np.searchsorted(
total_claim_cumulative_probabilities, 1 - var_tail_size, side="left"
)
]
else:
raise RuntimeError
expected_total_claim = np.dot(total_claim_values, total_claim_probabilities)
expected_square_total_claim = np.dot(
total_claim_values ** 2, total_claim_probabilities
)
variance = expected_square_total_claim - expected_total_claim ** 2
std = np.sqrt(variance)
if not max(round(expected_total_claim), var) <= exposure:
raise RuntimeError(
"var or expected claim greater than exposure, something's wrong"
)
if factor:
expected_total_claim *= factor
var = round(var * factor)
exposure = round(exposure * factor)
std = round(std * factor)
return float(expected_total_claim), float(std), int(var), int(exposure)
def get_contract_risk(
risk: RiskProperties, params: dict, max_claims=0
) -> Tuple[float, float, int, int]:
tail_size = params["value_at_risk_tail_probability"]
cat_sep = params["event_time_mean_separation"]
for prop in [
"number_risks",
"value",
"deductible_fraction",
"limit_fraction",
"runtime",
]:
if risk.__dict__[prop] is None:
raise ValueError(f"Risk parameter {prop} no present but required")
if not risk.deductible_fraction < risk.limit_fraction <= 1:
raise ValueError("Invalid deductible/limit passed")
return contract_risk(
number_risks=risk.number_risks,
value_per_risk=int(round(risk.value / risk.number_risks)),
deductible=int(round(risk.deductible_fraction * risk.value)),
limit=int(round(risk.limit_fraction * risk.value)),
runtime=risk.runtime,
max_claims=max_claims,
var_tail_size=tail_size,
cat_frequency=1 / cat_sep,
)
def fftconvolve_n(inp, n, axes=None):
"""
Uses fft to convolve an array with itself n times.
This function is a modification of scipy.signal.fftconvolve.
Takes an input of a single numpy array-like and convolves it with itself n times using a fast fourier transform.
Args:
inp:
n:
axes:
Returns:
"""
from scipy.fftpack.helper import _init_nd_shape_and_axes_sorted
import scipy.fftpack as fftpack
inp = np.asarray(inp)
if inp.ndim == 0: # scalar input
return inp ** n
elif inp.size == 0:
return np.array([])
_, axes = _init_nd_shape_and_axes_sorted(inp, shape=None, axes=axes)
if axes is not None and not axes.size:
raise ValueError("when provided, axes cannot be empty")
shape = np.array(inp.shape)
shape[axes] = shape[axes] * n - (n - 1) # This is vital!
# Speed up FFT by padding to optimal size for FFTPACK
fshape = [fftpack.helper.next_fast_len(d) for d in shape[axes]]
fslice = tuple([slice(sz) for sz in shape])
sp = np.fft.rfftn(inp, fshape, axes=axes)
ret = np.fft.irfftn(sp ** n, fshape, axes=axes)[fslice].copy()
return ret
def main():
import matplotlib.pyplot as plt
for _ in range(100):
risks = []
for res in np.logspace(start=1, stop=5, num=50):
risks.append(
contract_risk(
number_risks=200,
value_per_risk=10000,
deductible=100 * 10000,
limit=200 * 10000,
runtime=12,
max_claims=1,
max_res=int(res),
)
)
# risks = np.asarray(risks)
# plt.plot(np.logspace(start=1, stop=5, num=50), risks, "rx")
# for statistic in risks[-1]:
# plt.axhline(y=statistic, lw=1)
# plt.xscale('log')
# plt.legend()
# plt.show()
# import distributiontruncated
#
# est_dist = distributiontruncated.TruncatedDistWrapper(
# lower_bound=0.25,
# upper_bound=1.0,
# dist=scipy.stats.pareto(b=2, loc=0, scale=0.25),
# )
# # lb = 1
# # ub = 100
# # ns = range(lb, ub)
# # vars = []
# # for n in ns:
# # vars.append(AggregateDistribtion(n=n).ppf(1 - 0.025))
# # plt.plot(range(lb, ub), vars, label="VaR for each n")
# # plt.plot(range(lb, ub), [est_dist.ppf(1 - 0.025) for _ in range(lb, ub)],
# label="Estimated VaR using truncated Pareto")
# ns = [1, 2, 3, 4, 5, 10, 20, 50, 100]
# x = np.linspace(0, 1, num=100)
#
# for n in ns:
# dist = AggregateDistribtion(n=n)
# plt.plot(x, dist.cdf(x), label=f"n = {n}")
#
# plt.plot(x, est_dist.cdf(x), label="Truncated Pareto")
# plt.legend()
# plt.show()
if __name__ == "__main__":
main()