私は stats.exponweib.fit を使用してワイブル分布を当てはめようとしました - Scipy にはワイブルだけの当てはめがないため、指数ワイブルの当てはめを利用し、最初の形状パラメータを 1 に設定する必要があります。ただし、 stats.exponweib.fit 関数に、既知の形状パラメーターを持つワイブル分布からのデータが供給されると、フィットは異なる形状パラメーターのセットを返します。この動作を表示するコード例は次のとおりです。
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()
形状係数 k=5 およびラムダ=1 の既知のワイブル分布からの入力データと、異なる形状係数の exponweib.fit からの出力を示す次の曲線が得られます。
ワイブル データを入力し、exponweib.fit から出力します。
stackoverflow の最初の投稿 - うまくいけば、上記が質問を組み立てる正しい方法です。上記に関するアイデアや投稿に関する指針を歓迎します:)