python - 值错误: Domain error in arguments scipy rv_continuous

标签 python numpy scipy

我试图使用 scipy.stats.rv_continuous 对给定概率密度函数(pdf)的随机变量进行采样:

class Distribution(stats.rv_continuous):
    def _pdf(self,x, _a, _c):
        return first_hitting_time(x, _a, _c)

函数first_hitting_time在哪里

#pdf of first hitting time of W_t + c*t on a. 
def first_hitting_time(_t, _a, _c=0.0):
    return _a/_t*np.exp(-0.5/_t*(_a-_c*_t)**2)/np.sqrt(2.0*np.pi*_t)

然后我继续

myrv= Distribution(name='hittingtime', a=0.002,b=30.0)
data3= myrv.rvs(size=10000, _a=1.0, _c=0.0)

口译员开始提示-

Traceback (most recent call last):

  File "<ipython-input-246-71f67047462b>", line 1, in <module>
    data3= myrv.rvs(size=10000, _a=1.0, _c=0.0)

  File "C:\Users\ME\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 856, in rvs
    raise ValueError("Domain error in arguments.")

ValueError: Domain error in arguments.

看来,如果我将 _c 设置为大于 0.0 的某个数字,它就可以正常工作,但对于小于 0 的 _c 则不行。

我对此有点困惑。任何帮助,将不胜感激。

最佳答案

来自the documentation :

Subclassing

New random variables can be defined by subclassing the rv_continuous class and re-defining at least the _pdf or the _cdf method (normalized to location 0 and scale 1).

If positive argument checking is not correct for your RV then you will also need to re-define the _argcheck method.

从你的函数中我不清楚 _a_c 代表什么,但看起来你想让它们为负数。

查看默认实现in the source of _distn_infrastructure

关于python - 值错误: Domain error in arguments scipy rv_continuous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39420430/

相关文章:

python - 如何在 fiddle 图中将多个 pandas 数据框绘制为单独的类别?

python - numpy reshape 与负形状值混淆

python - python中是否有用于均方根误差(RMSE)的库函数?

python - 从 python 中关闭文件

python - matplotlib 使用散点图反向绘制图表

python - 如何编码匹配 '/search?query=a'

python - 使用 Scipy 与 ROOT 等拟合(高斯)

python - 如何在python中替换字符串中的连续空格

python dict时间之谜

python - 使用 Python lfilter 过滤信号