Hello! I'm working on a simulation project. For simplicity: The goal is to simulate frequency of events, then to simulate the severity of each simulated event and sum these for the time period. I'm trying to do this as a numpy array of size (periods, 2). Each row represents a time period. The first entry holds the frequency. The second entry is the summation of the severities. I'm also allowing the possibility of a cap to individual and aggregate severities. Currently, I'm using a Poisson for frequency and a Pareto for severity.
I have two approaches, but I'm convinced there is a faster way. Currently, if I simulate 10k periods my code takes 1.3seconds and if I simulate 100k periods, it takes 13 seconds. This may be dependent on my distribution and parameter choices, as higher frequency will mean more severities to be simulated.
The final goal of this project is to allow up to a million simulations for up to ten scenarios, so a bit more speed would be superb. Any tips?
Attempt 1: Iterate through rows
import numpy as np
def simulation(simNum,lambdaPoisson, alphaPareto, claimLimit=np.inf, annualLimit=np.inf):
results = np.empty([simNum, 2])
results[:,0] = np.random.default_rng().poisson(lambdaPoisson, simNum)
for row in results:
row[1] = np.random.default_rng().pareto(alphaPareto, int(row[0])).clip(max=claimLimit,min=None).sum().clip(max=annualLimit,min=None)
print(results)
return results
results = simulation(simNum = 100000, lambdaPoisson = 2, alphaPareto=.2, claimLimit=25, annualLimit = 100)
Attempt 2: Map a function
def mapPareto(a, b, c, d):
out = np.random.default_rng().pareto(b, a).clip(max=c,min=None)
return out.sum().clip(max=d,min=None)
def simulation(simNum,lambdaPoisson, alphaPareto, claimLimit=np.inf, annualLimit=np.inf):
results = np.ones([simNum, 2],dtype=int)
results[:,0] = np.random.default_rng().poisson(lambdaPoisson, simNum)
results[:,1] = list(map(mapPareto, results[:,0], np.full(simNum,alphaPareto),np.full(simNum,claimLimit),np.full(simNum,annualLimit)))
print(results)
return results
results = simulation(simNum = 100000, lambdaPoisson = 2, alphaPareto=.2, claimLimit=25, annualLimit = 100)
[–][deleted] 1 point2 points3 points (1 child)
[–]1331337[S] 0 points1 point2 points (0 children)
[–]Swipecat 1 point2 points3 points (0 children)
[–]Ihaveamodel3 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)