[Scipy-svn] r6834 - in trunk/scipy/stats: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Tue Oct 12 22:29:33 EDT 2010
Author: warren.weckesser
Date: 2010-10-12 21:29:33 -0500 (Tue, 12 Oct 2010)
New Revision: 6834
Modified:
trunk/scipy/stats/distributions.py
trunk/scipy/stats/tests/test_distributions.py
Log:
BUG: stats: In a couple of the frozen distribution methods, giving a keyword argument modified the saved kwds dict. Also the frozen moment() method called the distribution's moment() method with keywords, but it doesn't take any. (Ticket #1293)
Modified: trunk/scipy/stats/distributions.py
===================================================================
--- trunk/scipy/stats/distributions.py 2010-10-12 20:02:12 UTC (rev 6833)
+++ trunk/scipy/stats/distributions.py 2010-10-13 02:29:33 UTC (rev 6834)
@@ -324,43 +324,59 @@
# Frozen RV class
class rv_frozen(object):
+
def __init__(self, dist, *args, **kwds):
self.args = args
self.kwds = kwds
self.dist = dist
- def pdf(self,x): #raises AttributeError in frozen discrete distribution
- return self.dist.pdf(x,*self.args,**self.kwds)
- def cdf(self,x):
- return self.dist.cdf(x,*self.args,**self.kwds)
- def ppf(self,q):
- return self.dist.ppf(q,*self.args,**self.kwds)
- def isf(self,q):
- return self.dist.isf(q,*self.args,**self.kwds)
+
+ def pdf(self, x): #raises AttributeError in frozen discrete distribution
+ return self.dist.pdf(x, *self.args, **self.kwds)
+
+ def cdf(self, x):
+ return self.dist.cdf(x, *self.args, **self.kwds)
+
+ def ppf(self, q):
+ return self.dist.ppf(q, *self.args, **self.kwds)
+
+ def isf(self, q):
+ return self.dist.isf(q, *self.args, **self.kwds)
+
def rvs(self, size=None):
- kwds = self.kwds
+ kwds = self.kwds.copy()
kwds.update({'size':size})
- return self.dist.rvs(*self.args,**kwds)
- def sf(self,x):
- return self.dist.sf(x,*self.args,**self.kwds)
- def stats(self,moments='mv'):
- kwds = self.kwds
+ return self.dist.rvs(*self.args, **kwds)
+
+ def sf(self, x):
+ return self.dist.sf(x, *self.args, **self.kwds)
+
+ def stats(self, moments='mv'):
+ kwds = self.kwds.copy()
kwds.update({'moments':moments})
- return self.dist.stats(*self.args,**kwds)
+ return self.dist.stats(*self.args, **kwds)
+
def median(self):
return self.dist.median(*self.args, **self.kwds)
+
def mean(self):
- return self.dist.mean(*self.args,**self.kwds)
+ return self.dist.mean(*self.args, **self.kwds)
+
def var(self):
return self.dist.var(*self.args, **self.kwds)
+
def std(self):
return self.dist.std(*self.args, **self.kwds)
- def moment(self,n):
- return self.dist.moment(n,*self.args,**self.kwds)
+
+ def moment(self, n):
+ return self.dist.moment(n, *self.args)
+
def entropy(self):
- return self.dist.entropy(*self.args,**self.kwds)
+ return self.dist.entropy(*self.args, **self.kwds)
+
def pmf(self,k):
- return self.dist.pmf(k,*self.args,**self.kwds)
- def interval(self,alpha):
+ return self.dist.pmf(k, *self.args, **self.kwds)
+
+ def interval(self, alpha):
return self.dist.interval(alpha, *self.args, **self.kwds)
Modified: trunk/scipy/stats/tests/test_distributions.py
===================================================================
--- trunk/scipy/stats/tests/test_distributions.py 2010-10-12 20:02:12 UTC (rev 6833)
+++ trunk/scipy/stats/tests/test_distributions.py 2010-10-13 02:29:33 UTC (rev 6834)
@@ -436,5 +436,125 @@
assert_(len(vals5) == 2+len(args))
assert_(vals5[2] == args[2])
+class TestFrozen(TestCase):
+ """Test that a frozen distribution gives the same results as the original object.
+
+ Only tested for the normal distribution (with loc and scale specified) and for the
+ gamma distribution (with a shape parameter specified).
+ """
+ def test_norm(self):
+ dist = stats.norm
+ frozen = stats.norm(loc=10.0, scale=3.0)
+
+ result_f = frozen.pdf(20.0)
+ result = dist.pdf(20.0, loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.cdf(20.0)
+ result = dist.cdf(20.0, loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.ppf(0.25)
+ result = dist.ppf(0.25, loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.isf(0.25)
+ result = dist.isf(0.25, loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.sf(10.0)
+ result = dist.sf(10.0, loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.median()
+ result = dist.median(loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.mean()
+ result = dist.mean(loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.var()
+ result = dist.var(loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.std()
+ result = dist.std(loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.entropy()
+ result = dist.entropy(loc=10.0, scale=3.0)
+ assert_equal(result_f, result)
+
+ result_f = frozen.moment(2)
+ result = dist.moment(2)
+ assert_equal(result_f, result)
+
+ def test_gamma(self):
+ a = 2.0
+ dist = stats.gamma
+ frozen = stats.gamma(a)
+
+ result_f = frozen.pdf(20.0)
+ result = dist.pdf(20.0, a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.cdf(20.0)
+ result = dist.cdf(20.0, a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.ppf(0.25)
+ result = dist.ppf(0.25, a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.isf(0.25)
+ result = dist.isf(0.25, a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.sf(10.0)
+ result = dist.sf(10.0, a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.median()
+ result = dist.median(a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.mean()
+ result = dist.mean(a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.var()
+ result = dist.var(a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.std()
+ result = dist.std(a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.entropy()
+ result = dist.entropy(a)
+ assert_equal(result_f, result)
+
+ result_f = frozen.moment(2)
+ result = dist.moment(2, a)
+ assert_equal(result_f, result)
+
+ def test_regression_02(self):
+ """Regression test for ticket #1293."""
+ # Create a frozen distribution.
+ frozen = stats.lognorm(1)
+ # Call one of its methods that does not take any keyword arguments.
+ m1 = frozen.moment(2)
+ # Now call a method that takes a keyword argument.
+ s = frozen.stats(moments='mvsk')
+ # Call moment(2) again.
+ # After calling stats(), the following was raising an exception.
+ # So this test passes if the following does not raise an exception.
+ m2 = frozen.moment(2)
+ # The following should also be true, of course. But it is not
+ # the focus of this test.
+ assert_equal(m1, m2)
+
+
if __name__ == "__main__":
run_module_suite()
More information about the Scipy-svn
mailing list