diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2009-03-14 20:18:53 +0000 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2009-03-14 20:18:53 +0000 |
commit | a1e2f9b5ab6d240655b040c3d3ae0188f1132cb1 (patch) | |
tree | 0e40817790765bfde18437c4054d0f6a9d43ae12 | |
parent | 016afc58740cf8a766595b68191b7eb9f7435776 (diff) | |
download | numpy-a1e2f9b5ab6d240655b040c3d3ae0188f1132cb1.tar.gz |
Fix tickets #921 and #923. Add regression tests.
-rw-r--r-- | numpy/random/mtrand/distributions.c | 4 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 42 |
2 files changed, 34 insertions, 12 deletions
diff --git a/numpy/random/mtrand/distributions.c b/numpy/random/mtrand/distributions.c index 08a0d49f1..d138be948 100644 --- a/numpy/random/mtrand/distributions.c +++ b/numpy/random/mtrand/distributions.c @@ -814,7 +814,7 @@ long rk_hypergeometric_hrua(rk_state *state, long good, long bad, long sample) if (good > bad) Z = m - Z; /* another fix from rv.py to allow sample to exceed popsize/2 */ - if (m < sample) Z = bad - Z; + if (m < sample) Z = good - Z; return Z; } @@ -876,7 +876,7 @@ long rk_logseries(rk_state *state, double p) return result; } } - if (V <= q) { + if (V >= q) { return 1; } return 2; diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 9d3de27e2..b316d7b79 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -2,6 +2,38 @@ from numpy.testing import * from numpy import random import numpy as np +class TestRegression(TestCase): + + def test_VonMises_range(self): + """Make sure generated random variables are in [-pi, pi]. + + Regression test for ticket #986. + """ + for mu in np.linspace(-7., 7., 5): + r = random.mtrand.vonmises(mu,1,50) + assert np.all(r > -np.pi) and np.all(r <= np.pi) + + def test_hypergeometric_range(self) : + """Test for ticket #921""" + assert_(np.all(np.random.hypergeometric(3, 18, 11, size=10) < 4)) + assert_(np.all(np.random.hypergeometric(18, 3, 11, size=10) > 0)) + + def test_logseries_convergence(self) : + """Test for ticket #923""" + N = 100000 + rvsn = np.random.logseries(0.8, size=N) + # these two frequency counts should be close to theoretical + # numbers with this large sample + # theoretical large N result is 0.49706795 + freq = np.sum(rvsn == 1) / float(N) + msg = "Obsevered frequency was %f, should be > 0.45" % freq + assert_(freq > 0.45, msg) + # theoretical large N result is 0.19882718 + freq = np.sum(rvsn == 2) / float(N) + msg = "Obsevered frequency was %f, should be < 0.23" % freq + assert_(freq < 0.23, msg) + + class TestMultinomial(TestCase): def test_basic(self): random.multinomial(100, [0.2, 0.8]) @@ -15,16 +47,6 @@ class TestMultinomial(TestCase): assert np.all(-5 <= x) assert np.all(x < -1) -class TestVonMises(TestCase): - def test_output_domain(self): - """Make sure generated random variables are in [-pi, pi]. - - Regression test for ticket #986. - """ - for mu in np.linspace(-7., 7., 5): - r = random.mtrand.vonmises(mu,1,50) - assert np.all(r>-np.pi) and np.all(r<=np.pi) - class TestSetState(TestCase): |