I created a quick monte-carlo simulation which seems to do what I want (simple version below). The code basically simulates a Poisson distribution, say this results in a simulation of 10
. it would then simulate 10
values from a lognormal distribution. It then applies two parameters to the results of that simulation, lim
and xs
, sums up the results and then stores it in a results matrix. The code then takes the mean of each simulation.
import numpy as np
import os
import scipy.stats as sp
sigma = 0.5
u = 10
mu = 100
no_sim = int(10e3)
no_col = 2
mat_res = np.zeros((no_sim,no_col)) #results matrix holder
lim = 40e3 # parameter 1 - needs to vary
xs = 2000 # parameter 2 - needs to vary
for i in range(1,no_sim):
no_clm = sp.poisson.rvs(mu=mu,size=1)
clm_sev = sp.lognorm.rvs(s=sigma,scale=np.exp(u),size=no_clm)
temp_mat = np.zeros((np.size(clm_sev),2)) # temp matrix for calculations
temp_mat[:,0] = clm_sev
temp_mat[:,1] = np.minimum(lim,np.maximum(0,temp_mat[:,0]-xs)) #want to expand this step for various values of lim and xs, e.g. 5 different options
mat_res[i,0] = np.sum(temp_mat[:,0])
mat_res[i,1] = np.sum(temp_mat[:,1])
#want these mean values for various different lim and xs values which are predefined
print("mean 1 is %s" % np.mean(mat_res[:,0]),
"mean 2 is %s" % np.mean(mat_res[:,1]))
I am trying to do two things:
Speed up the code. I will need to run over a million simulations in reality so want to do this in the best way possible - currently it takes a five minutes or so to run a million simulations
Expand the code in an efficient way so that I can vary the parameters
lim
andxs
and get a new result - i.e. right now I havelim = 40e3
andxs = 2000
but I would also want to run it with say lim = 50e3
and xs = 1000
and then return back the mean value in the print (along with the original mean for the original lim
and xs
parameters). One solution is to wrap the for-loop into a function with the parameters I require, however as I only use the lim
and xs
parameters in one line in the monte carlo simulation I don't think it's efficient to run the whole simulation again from scratch, but I can't think of any good way to build it into the for-loop as it stands.
lim
andxs
" are in. An ndarray? \$\endgroup\$