Python中的蒙特卡洛模拟 [英] Monte Carlo Simulation in Python

查看:360
本文介绍了Python中的蒙特卡洛模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为蒙特卡洛模拟编写了一个简单的代码:

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

推荐答案

检查线程模块,该模块允许您同时作为线程运行函数: https://www.tutorialspoint.com/python/python_multithreading.htm

check for the threading module that allows you to run functions simultaneously as thread: https://www.tutorialspoint.com/python/python_multithreading.htm

要显示多个图,可以使用matplotlib的子图功能: https://matplotlib.org/gallery/subplots_axes_and_figures/subplot.html

to show multiple plots you can use the subplot function of matplotlib: https://matplotlib.org/gallery/subplots_axes_and_figures/subplot.html

这篇关于Python中的蒙特卡洛模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆