【发布时间】:2021-07-20 02:09:09
【问题描述】:
我需要几个带有间隙的连续分布来拟合一些数据,并为此目的对scipy.stats.rv_continuous 进行子类化。下面是一个带有间隙的均匀分布的示例。在s0 和l 之间以及h 和s1 之间的分布是平坦的。
from scipy.stats import *
class gapF_gen(rv_continuous):
''' Class for a flat distribution with a gap in it
s0, s1: bounds of support
l, h: gap
s0 < l < h < s1
'''
def _argcheck(self, s0, s1, l, h): return (s0 < l < h < s1)
def _get_support(self, s0, s1, l, h): return s0, s1
def _pdf(self, x, s0, s1, l, h):
if (s0 <= x <= l) or (h <= x <= s1): return 1 / (s1 - h + l - s0)
else: return 0
gapF = gapF_gen(name='gapF')
bf = gapF(s0=-2.6, s1=4.77, l=-1.3, h=3.5)
print(bf.pdf(-2.8)) # OK
print(bf.pdf([-23.8, 3.8, 2.6, 6.9, 77.9])) # Not OK
我定义了_pdf 来检查值是否为零。这在将标量值传递给自动生成的pdf 时有效,但是当列表传递给pdf 时,由于范围检查,事情不起作用:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
另一方面,如果我重命名我的函数以覆盖```pdf`,那么对于标量我会得到错误:
TypeError: _parse_args() got an unexpected keyword argument 's0'
有什么建议可以解决这个问题吗?
【问题讨论】:
标签: python inheritance scipy