我试图将截断的正态分布与特定的a和b参数叠加在由完全相同的分布生成的样本直方图上。
我如何适应truncnorm(a,b)的pdf?

import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import matplotlib.mlab as mlab
from IPython.display import Math, Latex
# for displaying images
from IPython.core.display import Image
# import seaborn
import seaborn as sns
# settings for seaborn plotting style
sns.set()
# settings for seaborn plot sizes
sns.set(rc={'figure.figsize':(5,5)})
tempdist=[]
samples=100
for k in range(1,samples):
#Probability
#Storage temp as truncated normal
#temperature as normal mean 55 with 5F variation
storagetempfarenht = 57 #55
storagetempkelvin = (storagetempfarenht + 459.67) * (5.0/9.0)
highesttemp=storagetempfarenht + 5
lowesttemp= storagetempfarenht -5
sigma = ((highesttemp + 459.67) * (5.0/9.0)) - storagetempkelvin
mu, sigma = storagetempkelvin, sigma
lower, upper = mu-2*sigma , mu+2*sigma
a=(lower - mu) / sigma
b=(upper - mu) / sigma
temp =stats.truncnorm.rvs(a, b, loc=mu, scale=sigma, size=1)
mean, var, skew, kurt = stats.truncnorm.stats(a, b, moments='mvsk')
tempdist.append(temp)
#Theses are the randomly generated values
tempdist=np.array(tempdist)
x = range(250,350)
ax = sns.distplot(tempdist,
bins=500,
kde=True,
color='r',
fit=stats.truncnorm,
hist_kws={"linewidth": 15,'alpha':1})
ax.set(xlabel='Trunc Normal Distribution', ylabel='Frequency')发布于 2020-04-14 22:26:34
对于truncnorm,sns.distplot的fit参数不能很好地工作,至少在数据有限的情况下不能。truncnorm.fit需要一些猜测,而distplot不知道如何提供这些猜测。
This post解释了如何手动调整truncnorm。下面的代码只绘制带有初始参数的truncnorm.pdf。要获得合适的参数,您可以使用链接帖子中的代码。
以下是一些备注:
kde=True图时,这并没有什么帮助;默认情况下,这是一个高斯核的总和;distplot生成的数组越多,"linewidth": 15就越遵循数据的细节(而不是它一般的hist_kws中创建围绕15个像素宽的直方图条的直线)。这比条形图本身宽得多,导致了一个看起来很奇怪的情节。最好将行宽设置为1左右。for k in range(1,samples)运行for k in range(1,samples)-1次。这与Python从0开始的数组索引相关,而不是从1开始。import numpy as np
import matplotlib.pyplot as plt
import scipy.stats as stats
import matplotlib.mlab as mlab
import seaborn as sns
sns.set()
sns.set(rc={'figure.figsize': (5, 5)})
samples = 100
storagetempfarenht = 57 # 55
storagetempkelvin = (storagetempfarenht + 459.67) * (5.0 / 9.0)
highesttemp = storagetempfarenht + 5
lowesttemp = storagetempfarenht - 5
sigma = ((highesttemp + 459.67) * (5.0 / 9.0)) - storagetempkelvin
mu, sigma = storagetempkelvin, sigma
lower, upper = mu - 2 * sigma, mu + 2 * sigma
a = (lower - mu) / sigma
b = (upper - mu) / sigma
temp = stats.truncnorm.rvs(a, b, loc=mu, scale=sigma, size=1)
# mean, var, skew, kurt = stats.truncnorm.stats(a, b, moments='mvsk')
# Theses are the randomly generated values
tempdist = stats.truncnorm.rvs(a, b, loc=mu, scale=sigma, size=samples)
ax = sns.distplot(tempdist,
# bins=10, # 10 bins is the default
kde=True,
color='r',
# fit=stats.truncnorm, # doesn't work for truncnorm
hist_kws={"linewidth": 1, 'alpha': 1, 'label': 'Histogram'},
kde_kws={"linewidth": 2, 'alpha': 1, 'color': 'dodgerblue', 'label': 'Estimated kde'})
ax.set(xlabel='Trunc Normal Distribution', ylabel='Frequency')
x_left, x_right = ax.get_xlim()
x = np.linspace(x_left, x_right, 500)
y = stats.truncnorm.pdf(x, a, b, loc=mu, scale=sigma)
ax.plot(x, y, color='limegreen', label='Given truncnormal')
# for xi in (lower, upper): # optionally draw vertical lines at upper and lower
# ax.axvline(xi, linestyle=':', color='limegreen')
plt.legend()
plt.tight_layout()
plt.show()

https://stackoverflow.com/questions/61201303
复制相似问题