def normal(x, mu, sigma): p = 1 / math.sqrt(2 * math.pi * sigma**2) return p * np.exp(-0.5 / sigma**2 * (x - mu)**2)
时间: 2023-11-26 09:03:48 浏览: 199
这是一个用Python定义正态分布函数的例子。该函数接受三个参数:x,mu和sigma,分别代表随机变量x的取值、均值和标准差。函数返回x在正态分布下的概率密度值。具体来说,函数首先计算出正态分布的概率密度函数,然后将x带入该函数中计算出概率密度值并返回。其中,math和numpy是Python中的数学库,分别提供了数学函数和数组操作函数。
相关问题
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()
python如何编辑这个公式1/(√2π xσ) e^(-(lnx-μ)^2/(2σ^2 ))
您可以使用 Python 计算公式 1/(√2π xσ) e^(-(lnx-μ)^2/(2σ^2 )) 的值,代码如下:
```
import math
def normal_pdf(x, mu, sigma):
return 1 / (math.sqrt(2 * math.pi) * sigma) * math.exp(-(math.log(x) - mu) ** 2 / (2 * sigma ** 2))
```
在上面的代码中,我们定义了一个名为 "normal_pdf" 的函数,该函数接收三个参数:x、mu 和 sigma。然后使用这些参数计算公式的值并返回结果。
阅读全文