我一直在尝试使用 stats.exponweib.fit 拟合 Weibull 分布 - Scipy 中不适合 Weibull,因此,需要利用指数 Weibull 拟合并将第一个形状参数设置为 1。但是,当 stats.exponweib.fit 函数从具有已知形状参数的威 bool 分布中输入数据时 - 拟合返回一组不同的形状参数。显示此行为的一些示例代码是:
from numpy import random, exp, log
import matplotlib.pyplot as plt
from scipy import stats
import csv
# Expoential Weibull PDF
def expweibPDF(x, k, lam, alpha):
return (alpha * (k/lam) *
((x/lam)**(k-1)) *
((1 - exp(-(x/lam)**k))**(alpha-1)) *
exp(-(x/lam)**k))
# Expoential Weibull CDF
def exp_cdf(x, k, lam, alpha):
return (1 - exp(-(x / lam)**k))**alpha
# Expoential Weibull Inverse CDF
def exp_inv_cdf(p, k, lam, alpha):
return lam * ( - log( (1 - p)**(1/alpha) ))**(1/k)
# parameters for the fit - alpha = 1.0 reduces to normal Webull
# the shape parameters k = 5.0 and lam = 1.0 are demonstrated on Wikipedia:
# https://en.wikipedia.org/wiki/Weibull_distribution
alpha = 1.0
k0 = 5.0
lam0 = 1.0
x = []
y = []
# create a Weibull distribution
random.seed(123)
n = 1000
for i in range(1,n) :
p = random.random()
x0 = exp_inv_cdf(p,k0,lam0,alpha)
x += [ x0 ]
y += [ expweibPDF(x0,k0,lam0,alpha) ]
# now fit the Weibull using python library
# setting f0=1 should set alpha = 1.0
# so, shape parameters should be the k0 = 5.0 and lam = 1.0
(exp1, k1, loc1, lam1) = stats.exponweib.fit(y,floc=0, f0=1)
print (exp1, k1, loc1, lam1)
这里的输出是:
(1, 2.8146777019890856, 0, 1.4974049126907345)
我会期望:
(1, 5.0, 0, 1.0)
当我们绘制曲线时:
# plotting the two curves
fig, ax = plt.subplots(2, 1)
ax[0].plot(x,y, 'ro', lw=2)
ax[1].plot(x,stats.exponweib.pdf(x,exp1,k1,loc1,lam1), 'ro', lw=2)
plt.show()
我们得到以下曲线,显示了来自已知威 bool 分布的输入数据,形状因子 k=5 和 lambda=1,以及 exponweib.fit 输出的不同形状因子。
Input Weibull data and output from exponweib.fit
stackoverflow 上的第一篇文章 - 所以,希望以上是提出问题的正确方法。欢迎对上述内容的任何想法以及发布时的任何指示:)
最佳答案
在我的笔记本中,我尝试了 OpenTURNS 的 WeibullMaxFactory在您的 x 上拟合威 bool 分布
import openturns as ot
from openturns.viewer import View
sample = ot.Sample(x, 1) # formats your x into a 'Sample' of dimension = 1
distribution = ot.WeibullMaxFactory().build(sample) # fits a Weibull to your data
graph = distribution.drawPDF() # build the PDF
graph.setLegends(['Weibull'])
View(graph)
获取威 bool 参数:
print(distribution)
>>> WeibullMax(beta = 0.618739, alpha = 2.85518, gamma = 1.48269)
关于python - 使用 stats.exponweib.fit 在 python 中拟合威 bool 分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579505/