问题
I was trying to sample random variables subject to a given probability density function (pdf) with scipy.stats.rv_continuous:
class Distribution(stats.rv_continuous):
def _pdf(self,x, _a, _c):
return first_hitting_time(x, _a, _c)
where the function first_hitting_time is
#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)
then I continue with
myrv= Distribution(name='hittingtime', a=0.002,b=30.0)
data3= myrv.rvs(size=10000, _a=1.0, _c=0.0)
and interpreter starts to complain-
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.
it seems if I set _c
to be some number larger than 0.0, it works fine, but not for _c
less than 0.
I am a bit confused about this. Any help would be appreciated.
回答1:
From 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.
It's not clear to me from your function what _a
and _c
represent, but it looks like you want to allow them to be negative.
See the default implementation in the source of _distn_infrastructure
来源:https://stackoverflow.com/questions/39420430/valueerror-domain-error-in-arguments-scipy-rv-continuous