I would like to create a random variable from a truncated complementary error function. For this I want to subclass scipy.stats.rv_continuous. I have checked Subclassing scipy’s continuous distributions and written the following code using 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.)
However, I am getting the error message :
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
The error comes form the integration call used to normalize the pdf.
Any suggestions?