我试图使用下面的代码来计算Weibull分布,但是我收到了溢出exp的运行时警告:鉴于我的数据是6个成员的列表,但包含大数字,看来计算需要很多情况下,我的情节不像我在链接中看到的那样。 我使用了合适的并得到了错误:
要解包的值太多(预期为2)
import scipy.stats as s
import numpy as np
import matplotlib.pyplot as plt
def weib(x,n,a):
return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)
data = [ 79000 , 85900 , 95000, 101000, 155250 , 280000]
(loc, scale) = s.exponweib.fit_loc_scale(data, 1, 1)
print loc, scale
x = np.linspace(data[0], data[-1], 1000)
plt.plot(x, weib(x, loc, scale))
plt.hist(data, data[-1], normed=True)
plt.show()
方法2: 我尝试了以下代码:使用此源(code 2)
data = [ 79000 , 85900 , 95000, 101000, 155250 , 280000]
xdata = np.asarray(data)
a = 1. # shape
s1 = np.random.weibull(a, 1000)
x = xdata
def weib(x,n,a):
return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)
count, bins, ignored = plt.hist(np.random.weibull(5.,1000))
x = xdata
scale = count.max()/weib(x, 1., 5.).max()
plt.plot(x, weib(x, 1., 5.)*scale)
plt.show()
我得到了这样的结果:
我不知道为什么不将曲线和波段一起获得,而且我不确定s1是否也应该是随机参数还是应该分别选择
更新1:
在寻找另一种方法时,我使用了以下代码,但是这里的问题是我将x和y值获取为NAN
# 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 = len(data)
for i in range(1,n) :
p=data[i]
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)
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()