拟合自定义分布 scipy.stats 会溢出
Fitting custom distribution scipy.stats gives overflow
我正在尝试将广义误差分布拟合到我拥有的一些数据中。分布形式为
我尝试了以下实现
import numpy as np
import scipy.stats as st
from scipy.special import gamma
class ged(st.rv_continuous):
def _pdf(self, x, mu, sigma, kappa):
term1 = gamma(3*kappa)/gamma(kappa)
exponent = (term1 * ((x - mu)/sigma)**2)**(1/(2*kappa))
term2 = np.exp(-exponent)
term3 = 2*sigma*gamma(kappa+1)
fx = term1**0.5 * term2/term3
return fx
ged_inst = ged(name='ged')
data = np.random.normal(size=1000)
ged_inst.fit(data, 0, 0.01, 1)
然而这给出了
OverflowError: (34, 'Numerical result out of range')
如何正确实施此分布?我试图适应真实数据(不是问题中生成的玩具正常数据)
如评论中所述,要使此功能正常工作,我需要覆盖默认的 _argcheck
函数。以下作品:
class ged(st.rv_continuous):
def _pdf(self, x, mu, sigma, kappa):
term1 = gamma(3*kappa)/gamma(kappa)
exponent = (term1 * ((x - mu)/sigma)**2)**(1/(2*kappa))
term2 = np.exp(-exponent)
term3 = 2*sigma*gamma(kappa+1)
fx = term1**0.5 * term2/term3
return fx
def _argcheck(self, mu, sigma, kappa):
s = sigma > 0
k = kappa < 1
return s and k
我正在尝试将广义误差分布拟合到我拥有的一些数据中。分布形式为
我尝试了以下实现
import numpy as np
import scipy.stats as st
from scipy.special import gamma
class ged(st.rv_continuous):
def _pdf(self, x, mu, sigma, kappa):
term1 = gamma(3*kappa)/gamma(kappa)
exponent = (term1 * ((x - mu)/sigma)**2)**(1/(2*kappa))
term2 = np.exp(-exponent)
term3 = 2*sigma*gamma(kappa+1)
fx = term1**0.5 * term2/term3
return fx
ged_inst = ged(name='ged')
data = np.random.normal(size=1000)
ged_inst.fit(data, 0, 0.01, 1)
然而这给出了
OverflowError: (34, 'Numerical result out of range')
如何正确实施此分布?我试图适应真实数据(不是问题中生成的玩具正常数据)
如评论中所述,要使此功能正常工作,我需要覆盖默认的 _argcheck
函数。以下作品:
class ged(st.rv_continuous):
def _pdf(self, x, mu, sigma, kappa):
term1 = gamma(3*kappa)/gamma(kappa)
exponent = (term1 * ((x - mu)/sigma)**2)**(1/(2*kappa))
term2 = np.exp(-exponent)
term3 = 2*sigma*gamma(kappa+1)
fx = term1**0.5 * term2/term3
return fx
def _argcheck(self, mu, sigma, kappa):
s = sigma > 0
k = kappa < 1
return s and k