【发布时间】:2021-09-20 22:00:13
【问题描述】:
我正在尝试通过实现scipy.stats.rv_continuous 来编写具有更易于使用的参数(真实范围、mu 和 sig)的scipy.stats.truncnorm 版本。我提供了_argcheck、_get_support、_pdf 和_rvs 的代码,但出现错误
_parse_args() missing 4 required positional arguments: 'a', 'b', 'mu', and 'sig'
我怀疑它与shapes 或实现_parse_args 有关,但不知道如何解决它(我见过How do you use scipy.stats.rv_continuous?)。
我正在使用 scipy v 1.5.2 和 Python 3.8.5。
代码:
from scipy.stats import *
import scipy.stats
class truncgauss_gen(rv_continuous):
''' a and b are bounds of true support
mu is mean
sig is std dev
'''
def _argcheck(self, a, b, sig): return (a < b) and (sig > 0)
def _get_support(self, a, b): return a, b
def _pdf(self, x, a, b, mu, sig): return scipy.stats.truncnorm.pdf(x, (a - mu) / sig, (b - mu) / sig, ac=mu, scale=sig)
def _rvs(self, a, b, mu, sig, size): return scipy.stats.truncnorm.rvs((a - mu) / sig, (b - mu) / sig, ac=mu, scale=sig, size=size)
truncgauss = truncgauss_gen(name='truncgauss', momtype=1)
if __name__ == '__main__':
print(scipy.__version__)
tg = truncgauss()
dat = tg.rvs(a=-5.1, b=10.07, mu=2.3, n=10)
print(dat)
追溯:
1.5.2
Traceback (most recent call last):
File "testDistr.py", line 41, in <module>
tg = truncgauss()
File ".../opt/anaconda3/lib/python3.8/site-packages/scipy/stats/_distn_infrastructure.py", line 780, in __call__
return self.freeze(*args, **kwds)
File ".../opt/anaconda3/lib/python3.8/site-packages/scipy/stats/_distn_infrastructure.py", line 777, in freeze
return rv_frozen(self, *args, **kwds)
File ".../opt/anaconda3/lib/python3.8/site-packages/scipy/stats/_distn_infrastructure.py", line 424, in __init__
shapes, _, _ = self.dist._parse_args(*args, **kwds)
TypeError: _parse_args() missing 4 required positional arguments: 'a', 'b', 'mu', and 'sig'
【问题讨论】:
标签: python scipy.stats