简体   繁体   English

安装定制 Scipy 分布

[英]Fitting a Custom Scipy Distribution

I have redefined the lognormal distribution using custom scipy class.我使用自定义 scipy class 重新定义了对数正态分布。 I have simulated this distribution and I am trying to recover the original parameters I have specified, however, the fit method is returning different parameters.我已经模拟了这个分布,我试图恢复我指定的原始参数,但是,fit 方法返回不同的参数。

import numpy as np
import pandas as pd
from scipy.stats import rv_continuous
from scipy.special import erf
from scipy.special import erfinv

class lognorm_v2(rv_continuous):

    def _pdf(self, x, mu, sigma):
        return 1 / (x * sigma * np.sqrt(2 * np.pi)) * np.exp(-0.5 * ((np.log(x) - mu)/sigma)**2)

    def _cdf(self, x, mu, sigma):
        return 0.5 + 0.5 * erf((np.log(x) - mu)/ (np.sqrt(2)*sigma))
    
    def _sf(self, x, mu, sigma):
        u = (x)**b/(1+x**b)
        return 1 - 0.5 + 0.5 * erf((np.log(x) - mu)/ (np.sqrt(2)*sigma))
    
    def _ppf(self,x, mu, sigma):
        return np.exp(sigma * erfinv(2*x - 1) - mu)
    
    def _argcheck(self, mu, sigma):
        s = sigma > 0
        return s

np.random.seed(seed=111)
logn = lognorm_v2(name='lognorm_v2',a=0,b=np.inf)
test = logn.rvs(mu=2,sigma=1,loc=0,scale=1,size=100000)

logn.fit(test)
logn.fit(test,floc=0,fscale=1)

When loc and scale are not fixed I obtain the parameters:当 loc 和 scale 不固定时,我获取参数:

(0.9216388162274325, 0.7061876689651909, -0.0003659266464081178, 0.05399544825451739) (0.9216388162274325, 0.7061876689651909, -0.0003659266464081178, 0.05399544825451739)

When they are fixed the result is:当它们被修复时,结果是:

(-2.0007136838780917, 0.7086144279779958, 0, 1) (-2.0007136838780917, 0.7086144279779958, 0, 1)

Why am I not able to extract the mu 2 and sigma 1 specified in the original simulation?为什么我无法提取原始模拟中指定的 mu 2 和 sigma 1? I understand I will not get the exact values, but they should be very close for 100K simulations.我知道我不会得到确切的值,但对于 100K 模拟,它们应该非常接近。 My numpy is version 1.19.2 and scipy is 1.5.2.我的 numpy 是 1.19.2 版本,scipy 是 1.5.2。 Thank you!谢谢!

I've corrected code with proper _ppf, and it seems to produce proper fits for mu and sigma我已经用正确的 _ppf 更正了代码,它似乎为 mu 和 sigma 产生了合适的拟合

Code, Python 3.9 Windows 10 x64代码,Python 3.9 Windows 10 x64

import numpy as np
from scipy.stats import rv_continuous
from scipy.special import erf
from scipy.special import erfinv

SQRT2 = np.float64(1.4142135623730951)

class lognorm_v2(rv_continuous):

    def _pdf(self, x, μ, σ):
        return 1 / (x * σ * SQRT2 * np.sqrt(np.pi)) * np.exp(-0.5 * ((np.log(x) - μ)/σ)**2)

    def _cdf(self, x, μ, σ):
        return 0.5 + 0.5 * erf((np.log(x) - μ)/ (SQRT2*σ))

    def _ppf(self, x, μ, σ):
        return np.exp(μ + σ * SQRT2 * erfinv(2.0*x - 1.0))

    def _argcheck(self, μ, σ):
        s = σ > 0.0
        return s

np.random.seed(seed=111)
logn = lognorm_v2(name='lognorm_v2', a=0.0, b=np.inf)
test = logn.rvs(μ=2.0,σ=1.0,loc=0.0,scale=1.0, size=100000)

logn.fit(test,floc=0,fscale=1)

prints out打印出来

(1.9990788106319746, 1.0021523463000124, 0, 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM