拟合自定义分布 scipy.stats 会导致溢出

问题描述

我正在尝试将广义误差分布拟合到我拥有的一些数据中。分布的形式为

enter image description here

我尝试了以下实现

void l(int a[45])

然而这给了

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.01,1)

我如何正确实施此分发?我试图适应真实数据(不是问题中生成的玩具普通数据)

解决方法

正如评论中发布的那样,要使其正常工作,我需要覆盖默认的 _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,kappa):
        
        s = sigma > 0
        k = kappa < 1
        
        return s and k