给定 pdf 的 scipy 中的自定义分布 [英] Custom distribution in scipy with pdf given

查看:57
本文介绍了给定 pdf 的 scipy 中的自定义分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用通过 scipy.stats 给出的 pdf 定义自定义分发

I try to define a custom distribution with pdf given via scipy.stats

import numpy as np
from scipy.stats import rv_continuous

class CustomDistribution(rv_continuous):
    def __init__(self, pdf=None):
        super(CustomDistribution, self).__init__()
        self.custom_pdf = pdf
        print "Initialized!"

    def _pdf(self, x, *args):
        if self.custom_pdf is None:
            # print 'PDF is not overridden'
            return super(CustomDistribution, self)._pdf(x, *args)
        else:
            # print 'PDF is overridden'
            return self.custom_pdf(x)

def g(x, mu):
    if x < 0:
        return 0
    else:
        return mu * np.exp(- mu * x)

my_exp_dist = CustomDistribution(pdf=lambda x: g(x, .5))
print my_exp_dist.mean()

如您所见,我尝试定义参数 mu=0.5 的指数分布,但输出如下.

As you see I try to define exponential distribution wuth parameter mu=0.5, but the output is as follows.

已初始化!

D:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py:357:

D:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py:357:

IntegrationWarning:算法不收敛.舍入误差在外推表中检测到.假设无法达到要求的容差,并且返回的结果(如果 full_output = 1)是可以获得的最好的.
warnings.warn(msg, IntegrationWarning)

IntegrationWarning: The algorithm does not converge. Roundoff error is detected in the extrapolation table. It is assumed that the requested tolerance cannot be achieved, and that the returned result (if full_output = 1) is the best which can be obtained.
warnings.warn(msg, IntegrationWarning)

D:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py:357:

D:\Anaconda2\lib\site-packages\scipy\integrate\quadpack.py:357:

IntegrationWarning: 最大细分数 (50)实现了.

IntegrationWarning: The maximum number of subdivisions (50) has been achieved.

2.0576933609

2.0576933609

如果增加限制没有改善,建议分析被积函数以确定困难.如果可以确定局部难度的位置(奇点,不连续性)人们可能会从拆分间隔并在子范围上调用积分器.也许一个应使用专用积分器.警告.警告(味精,集成警告)

If increasing the limit yields no improvement it is advised to analyze the integrand in order to determine the difficulties. If the position of a local difficulty can be determined (singularity, discontinuity) one will probably gain from splitting up the interval and calling the integrator on the subranges. Perhaps a special-purpose integrator should be used. warnings.warn(msg, IntegrationWarning)

我应该怎么做才能改善这种情况?

What should I do to improve this?

注意:计算精度问题在 这个 GitHub issue 中讨论.

推荐答案

这似乎可以满足您的需求.每次创建实例时,必须为类的实例赋予 lambda 参数的值.rv_continuous 足够聪明,可以推断出您没有提供的项目,但您当然可以提供我在此处提供的更多定义.

This seems to do what you want. An instance of the class must be given a value for the lambda parameter each time the instance is created. rv_continuous is clever enough to infer items that you do not supply but you can, of course, offer more definitions that I have here.

from scipy.stats import rv_continuous
import numpy

class Neg_exp(rv_continuous): 
    "negative exponential"
    def _pdf(self, x, lambda):
        self.lambda=lambda
        return lambda*numpy.exp(-lambda*x)
    def _cdf(self, x, lambda):
        return 1-numpy.exp(-lambda*x)
    def _stats(self,lambda):
        return [1/self.lambda,0,0,0]

neg_exp=Neg_exp(name="negative exponential",a=0)

print (neg_exp.pdf(0,.5))
print (neg_exp.pdf(5,.5))

print (neg_exp.stats(0.5))

print (neg_exp.rvs(0.5))

这篇关于给定 pdf 的 scipy 中的自定义分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆