子类化 scipy.stats.rv_continuous 以实现截断分布

问题描述 投票:0回答:1

我想从截断的互补误差函数创建一个随机变量。为此,我想对 scipy.stats.rv_continuous 进行子类化。我已经检查了子类化 scipy 的连续分布并使用 Python 3.8.5 编写了以下代码:

import numpy as np
from scipy import special

from scipy.stats import *
import scipy.stats

import scipy.integrate as integrate


class truncErfc_gen(rv_continuous):
    ''' Class for a truncated complementary error function
    a and b are bounds
    '''
    def _argcheck(self, a, b): return (a < b)

    def _get_support(self, a, b): return a, b

    def _pdf(self, x, a, b): return special.erfc(x) / integrate.quad(special.erfc(x), a, b)[0] # Normalize the function over its bounds
    
truncErfc = truncErfc_gen(name='truncErfc', momtype=1)

x = np.linspace(-1, 10, 1000)

y = truncErfc.pdf(x, a=0., b=10.)

但是,我收到错误消息:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

该错误来自用于标准化 pdf 的集成调用。 有什么建议吗?

python scipy statistics scipy.stats
1个回答
0
投票

方法

quad
接受 Callable,但您提供了一个计算数组。 只需提供该功能,它就会按预期工作:

def _pdf(self, x, a, b): return special.erfc(x) / integrate.quad(special.erfc, a, b)[0]
© www.soinside.com 2019 - 2024. All rights reserved.