创建 scipy.stats 随机变量子类不会导致预期的对象类型

Sco*_*ott 5 python statistics class scipy

我正在尝试扩展 scipy.stats.rv_discrete 以为用户提供一些简单的分布。例如,在最简单的情况下,他们可能想要具有恒定输出的分布。这是我的代码:

from scipy.stats._distn_infrastructure import rv_sample

class const(rv_sample):  # a distribution with probability 1 for a single val
    def __init__(self, val, *args, **kwds):
        super(const, self).__init__(values=(val, 1), *args, **kwds)
Run Code Online (Sandbox Code Playgroud)

但是,这不会产生与内置随机变量分布相同类型的对象,这会扰乱我想要对分布执行的一些操作。将其与泊松分布进行比较:

from scipy.stats import poisson
import inspect

print('\nThese should both contain rv_discrete:')
print('1: ', inspect.getmro(poisson.__class__))
print('2: ', inspect.getmro(const.__class__))

print('\nThese should both be rv_frozen:')
print('1: ', inspect.getmro(poisson(5).__class__))
print('2: ', inspect.getmro(const(5).__class__))
Run Code Online (Sandbox Code Playgroud)

输出:

These should both contain rv_discrete:
1:  (<class 'scipy.stats._discrete_distns.poisson_gen'>, <class 'scipy.stats._distn_infrastructure.rv_discrete'>, <class 'scipy.stats._distn_infrastructure.rv_generic'>, <class 'object'>)
2:  (<class 'type'>, <class 'object'>)

These should both be rv_frozen:
1:  (<class 'scipy.stats._distn_infrastructure.rv_frozen'>, <class 'object'>)
2:  (<class '__main__.const'>, <class 'scipy.stats._distn_infrastructure.rv_sample'>, <class 'scipy.stats._distn_infrastructure.rv_discrete'>, <class 'scipy.stats._distn_infrastructure.rv_generic'>, <class 'object'>)
Run Code Online (Sandbox Code Playgroud)

关于我在这里做错的任何提示?我在子类化方面相对缺乏经验,所以它可能很简单。谢谢!

Sco*_*ott 0

目前无法解决此问题,但它是 scipy 发行版大修的一部分,如下所示: https: //github.com/scipy/scipy/issues/15928