蒙特卡洛
蒙特卡洛(Monte Carlo)又称统计模拟法、随机抽样技术,是一种随机模拟方法,以概率和统计理论方法使用随机数(或更常见的伪随机数)来解决很多计算问题的方法。将所求解的问题同一定的概率模型相联系,用电子计算机实现统计模圳成地样以获得问波的近似解。为象征性地表明这一方法的概率统计特征,故借用赌城蒙特卡罗命名。
随着二十世纪电子计算机的出现,蒙特卡洛法已经在诸多领域展现出了超强的能力, 主要出现在风险管理知识领域中的定量风险分析过程,是用于做项目定量风险分析的工具之一,同时蒙特卡洛模拟也可以用于估算进度或成本以及制定进度计划等,在机器学习和自然语言处理技术中,常常被用到的MCMC也是由此发展而来。
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
np.random.seed(1)
# 先画出三个要素工期的正态分布概率密度图
def normplot(mu,sigma,n,name,ax):
x = np.linspace(mu-3*sigma,mu+3*sigma,n)
y = stats.norm.pdf(x,mu,sigma)
#绘图需要用到latex
ax.plot(x, y,label=r'{}:$\mu={},\sigma={}$'.format(name,mu,sigma))
# plt.show()
# plt.savefig(name + '.png')
fig,ax = plt.subplots(1,2,figsize=(15,5))
normplot(14, 2, 1000,'desgin',ax[0])
# normplot(23, 3, 1000,'build',ax[0])
# normplot(22, 4, 1000,'tests',ax[0])
ax[0].legend(prop={'family':'SimHei','size':10})
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
x = pd.Series(np.random.normal(loc=14,scale=2,size=10000))
# x2 = pd.Series(np.random.normal(loc=23,scale=3,size=10000))
# x3 = pd.Series(np.random.normal(loc=22,scale=4,size=10000))
# x = x1+x2+x3
n,bins,paches = ax[1].hist(x,bins=50,range=(40,80),density=True)
mean,std = x.mean(),x.std()
y = stats.norm.pdf(bins,mean,std)
ax[1].plot(bins,y, label ='ret$\mu={:.2f},\sigma={:.2f},\sigma^2={:.2f}$'.format(mean, std,std**2))
ax[1].legend(prop={'family':'SimHei','size':10})
ax[1].set_ylim(0,0.09)
plt.show()
plt.savefig('mtkl.png')
参考
蒙特卡洛法 - coshaho - 博客园 (cnblogs.com)
蒙特卡洛模拟在实际的项目管理中的应用——Python_camerayuhangyuhang.的博客-CSDN博客_蒙特卡洛分析 项目管理