[SciPy-Dev] distributions.py expect
nicky van foreest
vanforeest@gmail....
Fri Sep 14 15:21:46 CDT 2012
Hi,
I am trying to implement the expect method in rv_frozen. To understand
the normal working of the expect method I tried the following:
from scipy.stats import geom, norm, gamma
print norm.expect(loc = 3,scale =5)
print gamma.expect(None, 4.5)
print gamma.expect(lambda x: x, 4.5)
print geom.expect(lambda x: x, 1./3)
This is the result:
3.0
4.5
4.5
Traceback (most recent call last):
File "expecttest.py", line 6, in <module>
print geom.expect(lambda x: x, 1./3)
File "/home/nicky/prog/scipy/scipy/stats/distributions.py", line
6375, in expect
self._argcheck(*args) # (re)generate scalar self.a and self.b
TypeError: _argcheck() argument after * must be a sequence, not float
So the first examples work, but the rv_discrete example doesn't. One
thing is that the _argcheck is not called in rv_continuous while it is
in rv_discrete. Removing this line results in another error:
3.0
4.5
4.5
Traceback (most recent call last):
File "expecttest.py", line 6, in <module>
print geom.expect(lambda x: x, 1./3)
File "/home/nicky/prog/scipy/scipy/stats/distributions.py", line
6394, in expect
low, upp = self._ppf(0.001, *args), self._ppf(0.999, *args)
TypeError: _ppf() argument after * must be a sequence, not float
What would be actually the right way to call the expect method for
rv_discrete? Or perhaps the other way around, should this method be
changed so that my example with geom works?
----
I extended rv_frozen with this method, and this appears to work well
for continuous rvs, but also fails for discrete rvs.
def expect(self, *args, **kwds):
args += self.args
kwds.update(self.kwds)
return self.dist.expect(*args, **kwds)
Nicky
More information about the SciPy-Dev
mailing list