Scipy:对数正态拟合 [英] Scipy: lognormal fitting
问题描述
已经有很多关于使用 Scipy 处理 lognorm
分发的帖子,但我仍然没有掌握它.
There have been quite a few posts on handling the lognorm
distribution with Scipy but i still dont get the hang of it.
2参数对数正态通常由参数\mu
和\sigma
描述,分别对应Scipys的loc=0
和\sigma=shape
, \mu=np.log(scale)
.
The 2 parameter lognormal is usually described by the parameters \mu
and \sigma
which corresponds to Scipys loc=0
and \sigma=shape
, \mu=np.log(scale)
.
在scipy,对数正态分布 - 参数,我们可以阅读如何生成lognorm(\mu,\sigma)
使用随机分布指数的样本.现在让我们试试别的:
At scipy, lognormal distribution - parameters, we can read how to generate a lognorm(\mu,\sigma)
sample using the exponential of a random distribution. Now lets try something else:
直接创建对数范数有什么问题:
Whats the problem in creating a lognorm directly:
# lognorm(mu=10,sigma=3)
# so shape=3, loc=0, scale=np.exp(10) ?
x=np.linspace(0.01,20,200)
sample_dist = sp.stats.lognorm.pdf(x, 3, loc=0, scale=np.exp(10))
shape, loc, scale = sp.stats.lognorm.fit(sample_dist, floc=0)
print shape, loc, scale
print np.log(scale), shape # mu and sigma
# last line: -7.63285693379 0.140259699945 # not 10 and 3
B)
我使用拟合的返回值来创建拟合分布.但我显然又做错了:
B)
I use the return values of a fit to create a fitted distribution. But again im doing something wrong apparently:
samp=sp.stats.lognorm(0.5,loc=0,scale=1).rvs(size=2000) # sample
param=sp.stats.lognorm.fit(samp) # fit the sample data
print param # does not coincide with shape, loc, scale above!
x=np.linspace(0,4,100)
pdf_fitted = sp.stats.lognorm.pdf(x, param[0], loc=param[1], scale=param[2]) # fitted distribution
pdf = sp.stats.lognorm.pdf(x, 0.5, loc=0, scale=1) # original distribution
plt.plot(x,pdf_fitted,'r-',x,pdf,'g-')
plt.hist(samp,bins=30,normed=True,alpha=.3)
推荐答案
我做了同样的观察:所有参数的自由拟合大部分时间都失败.您可以通过提供更好的初始猜测来提供帮助,无需修正参数.
I made the same observations: a free fit of all parameters fails most of the time. You can help by providing a better initial guess, fixing the parameter is not necessary.
samp = stats.lognorm(0.5,loc=0,scale=1).rvs(size=2000)
# this is where the fit gets it initial guess from
print stats.lognorm._fitstart(samp)
(1.0, 0.66628696413404565, 0.28031095750445462)
print stats.lognorm.fit(samp)
# note that the fit failed completely as the parameters did not change at all
(1.0, 0.66628696413404565, 0.28031095750445462)
# fit again with a better initial guess for loc
print stats.lognorm.fit(samp, loc=0)
(0.50146296628099118, 0.0011019321419653122, 0.99361128537912125)
您也可以自己编写函数来计算初始猜测,例如:
You can also make up your own function to calculate the initial guess, e.g.:
def your_func(sample):
# do some magic here
return guess
stats.lognorm._fitstart = your_func
这篇关于Scipy:对数正态拟合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!