mu = [14, 23, 22] sigma = [2, 3, 4] tips = ['design', 'build', 'test'] figureIndex = 0 fig = plt.figure(figureIndex, figsize=(10, 8)) color = ['r', 'g', 'b'] ax = fig.add_subplot(111) for i in range(3): x = np.linspace(mu[i] - 3*sigma[i], mu[i] + 3*sigma[i], 100) y_sig = np.exp(-(x - mu[i])**2/(2*sigma[i]**2))/(math.sqrt(2*math.pi)) ax.plot(x, y_sig, color[i], label=tips[i]) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days') ax.set_ylabel('probability') plt.grid(True) plt.show() size = 100000 samples = [np.random.normal(mu[i], sigma[i], size) for i in range(3)] data = np.zeros(len(samples[1])) for i in range(len(samples[1])): for j in range(3): data[i] += samples[j][i] data[i] = int(data[i]) a, b = count(data) pdf = [x/size for x in b] cdf = np.zeros(len(a)) for i in range(len(a)): if i > 0: cdf[i] += cdf[i - 1] cdf = cdf/size figureIndex += 1 fig = plt.figure(figureIndex, figsize=(10, 8)) ax = fig.add_subplot(211) ax.bar(a, height=pdf, color='blue', edgecolor='white', label='MC PDF') ax.plot(a, pdf) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days for project') ax.set_ylabel('probability') ax.set_title('Monte Carlo Simulation') ax = fig.add_subplot(212) ax.plot(a, cdf) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days for project') ax.set_ylabel('probability') ax.grid(True) plt.show()
时间: 2023-06-25 22:04:14 浏览: 106
这段代码实现了蒙特卡罗模拟的概率分布和累积分布函数的绘制。具体来说,代码首先定义了三个平均值 mu 和标准差 sigma,以及对应的项目阶段 tips。然后通过 np.linspace 函数生成一组数据 x,计算出正态分布的概率密度函数 y_sig,并将其绘制在图像中。接着,代码通过 np.random.normal 生成一些随机样本,将三个样本的数据相加得到一个新的数据集 data,并对 data 进行统计,得到概率密度函数和累积分布函数,并将其绘制在图像中。最终通过 plt.show() 函数将图像显示出来。
相关问题
mu = [14, 23, 22] sigma = [2, 3, 4] tips = ['design', 'build', 'test'] figureIndex = 0 fig = plt.figure(figureIndex, figsize=(10, 8)) color = ['r', 'g', 'b'] ax = fig.add_subplot(111) for i in range(3): x = np.linspace(mu[i] - 3*sigma[i], mu[i] + 3*sigma[i], 100) y_sig = np.exp(-(x - mu[i])**2/(2*sigma[i]**2))/(math.sqrt(2*math.pi)) plt.plot = (x, y_sig, color[i]) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days') ax.set_ylabel('probability') plt.grid(True) plt.show()
这是一段Python代码,用于绘制三个正态分布的概率密度函数曲线,并将它们绘制在同一个图上。其中mu和sigma分别表示三个正态分布的均值和标准差,tips是每个分布对应的标签,figureIndex是图像的编号,fig是绘图对象,ax是坐标轴对象。代码中使用了numpy和matplotlib库。具体解释如下:
```python
# 导入必要的库
import numpy as np
import matplotlib.pyplot as plt
import math
# 定义三个正态分布的参数
mu = [14, 23, 22] # 均值
sigma = [2, 3, 4] # 标准差
tips = ['design', 'build', 'test'] # 标签
# 创建绘图对象
figureIndex = 0
fig = plt.figure(figureIndex, figsize=(10, 8))
color = ['r', 'g', 'b'] # 颜色
# 绘制三个正态分布的概率密度函数曲线
ax = fig.add_subplot(111)
for i in range(3):
x = np.linspace(mu[i] - 3*sigma[i], mu[i] + 3*sigma[i], 100) # 生成x轴数据
y_sig = np.exp(-(x - mu[i])**2/(2*sigma[i]**2))/(math.sqrt(2*math.pi)*sigma[i]) # 生成y轴数据
ax.plot(x, y_sig, color[i], label=tips[i]) # 绘制曲线并打上标签
# 设置图例、坐标轴标签和网格
ax.legend(loc='best', frameon=False)
ax.set_xlabel('# of days')
ax.set_ylabel('probability')
plt.grid(True)
# 显示图像
plt.show()
```
运行代码后,会生成一张包含三个正态分布曲线的图像。其中,x轴表示随机变量的取值,y轴表示对应取值的概率密度。每个曲线的颜色不同,标签也不同。这个示例可以帮助我们更好地理解正态分布及其参数对随机变量分布的影响。
import numpy as np import matplotlib.pyplot as plt import math def count(lis): lis = np.array(lis) key = np.unique(lis) x = [] y = [] for k in key: mask = (lis == k) list_new = lis[mask] v = list_new.size x.append(k) y.append(v) return x, y mu = [14, 23, 22] sigma = [2, 3, 4] tips = ['design', 'build', 'test'] figureIndex = 0 fig = plt.figure(figureIndex, figsize=(10, 8)) color = ['r', 'g', 'b'] ax = fig.add_subplot(111) for i in range(3): x = np.linspace(mu[i] - 3*sigma[i], mu[i] + 3*sigma[i], 100) y_sig = np.exp(-(x - mu[i])**2/(2*sigma[i]**2))/(math.sqrt(2*math.pi)) ax.plot = (x, y_sig, color[i] + '-') ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days') ax.set_ylabel('probability') plt.show() plt.grid(True) size = 100000 samples = [np.random.normal(mu[i], sigma[i], size) for i in range(3)] data = np.zeros(len(samples[1])) for i in range(len(samples[1])): for j in range(3): data[i] += samples[j][i] data[i] = int(data[i]) a, b = count(data) pdf = [x/size for x in b] cdf = np.zeros(len(a)) for i in range(len(a)): if i > 0: cdf[i] += cdf[i - 1] cdf = cdf/size figureIndex += 1 fig = plt.figure(figureIndex, figsize=(10, 8)) ax = fig.add_subplot(211) ax.bar(a, height=pdf, color='blue', edgecolor='white', label='MC PDF') ax.plot(a, pdf) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days for project') ax.set_ylabel('probability') ax.set_title('Monte Carlo Simulation') ax = fig.add_subplot(212) ax.plot(a, cdf) ax.legend(loc='best', frameon=False) ax.set_xlabel('# of days for project') ax.set_ylabel('probability') ax.grid(True) plt.show()修改一下代码
import numpy as np
import matplotlib.pyplot as plt
import math
def count(lis):
lis = np.array(lis)
key = np.unique(lis)
x = []
y = []
for k in key:
mask = (lis == k)
list_new = lis[mask]
v = list_new.size
x.append(k)
y.append(v)
return x, y
mu = [14, 23, 22]
sigma = [2, 3, 4]
tips = ['design', 'build', 'test']
figureIndex = 0
fig = plt.figure(figureIndex, figsize=(10, 8))
color = ['r', 'g', 'b']
ax = fig.add_subplot(111)
for i in range(3):
x = np.linspace(mu[i] - 3*sigma[i], mu[i] + 3*sigma[i], 100)
y_sig = np.exp(-(x - mu[i])**2/(2*sigma[i]**2))/(math.sqrt(2*math.pi))
ax.plot(x, y_sig, color[i] + '-', label=tips[i])
ax.legend(loc='best', frameon=False)
ax.set_xlabel('# of days')
ax.set_ylabel('probability')
plt.grid(True)
plt.show()
size = 100000
samples = [np.random.normal(mu[i], sigma[i], size) for i in range(3)]
data = np.zeros(len(samples[1]))
for i in range(len(samples[1])):
for j in range(3):
data[i] += samples[j][i]
data[i] = int(data[i])
a, b = count(data)
pdf = [x/size for x in b]
cdf = np.zeros(len(a))
for i in range(len(a)):
if i > 0:
cdf[i] += cdf[i - 1]
cdf[i] = pdf[i] + cdf[i]
figureIndex += 1
fig = plt.figure(figureIndex, figsize=(10, 8))
ax = fig.add_subplot(211)
ax.bar(a, height=pdf, color='blue', edgecolor='white', label='MC PDF')
ax.plot(a, pdf)
ax.legend(loc='best', frameon=False)
ax.set_xlabel('# of days for project')
ax.set_ylabel('probability')
ax.set_title('Monte Carlo Simulation')
ax = fig.add_subplot(212)
ax.plot(a, cdf)
ax.legend(loc='best', frameon=False)
ax.set_xlabel('# of days for project')
ax.set_ylabel('probability')
ax.grid(True)
plt.show()
阅读全文