Monte Carlo Simulation in Python

点点圈 提交于 2019-12-12 10:17:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!