问题
I just wrote a simple code for a monte carlo simulation:
def loss(r, loc, arg, scale, lam):
X = []
for x in range(27000):
if(r < poisson.cdf(x, lam)):
out = 0
else:
out = lognorm.rvs(s=arg,loc=loc, scale=scale)
X.append(out)
return np.sum(X)
losses = []
for _ in range(2000):
r = np.random.random()
losses.append(loss(r, loc, arg, scale, lam))
E = np.sum(losses)/len(losses)
print(E)
plt.hist(losses, bins='auto')
But now, the sum is only consisting of lognormal distributed random variables - Is there a possibility to combine, lets say 2 monte carlo simulations (one lognormal and one gamma) and plot this in one histogram?
Many thanks in advance and kind regards
回答1:
check for the threading module that allows you to run functions simultaneously as thread: https://www.tutorialspoint.com/python/python_multithreading.htm
to show multiple plots you can use the subplot function of matplotlib: https://matplotlib.org/gallery/subplots_axes_and_figures/subplot.html
来源:https://stackoverflow.com/questions/54049965/monte-carlo-simulation-in-python