~ubuntu-branches/ubuntu/saucy/python-scipy/saucy

« back to all changes in this revision

Viewing changes to Lib/sandbox/montecarlo/tests/test_dictsampler.py

  • Committer: Bazaar Package Importer
  • Author(s): Ondrej Certik
  • Date: 2008-06-16 22:58:01 UTC
  • mfrom: (2.1.24 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080616225801-irdhrpcwiocfbcmt
Tags: 0.6.0-12
* The description updated to match the current SciPy (Closes: #489149).
* Standards-Version bumped to 3.8.0 (no action needed)
* Build-Depends: netcdf-dev changed to libnetcdf-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
""" Test functions for the 'dictsampler' derived class.
4
 
 
5
 
Author: Ed Schofield, 2003-2006
6
 
Copyright: Ed Schofield, 2003-2006
7
 
 
8
 
"""
9
 
 
10
 
from numpy import arange, add, array, dot, zeros, identity
11
 
 
12
 
import sys
13
 
from numpy.testing import *
14
 
set_package_path()
15
 
from numpy import *
16
 
#from scipy.montecarlo import *
17
 
from scipy.sandbox.montecarlo import *
18
 
from scipy import stats
19
 
restore_path()
20
 
 
21
 
import unittest
22
 
 
23
 
 
24
 
class test_dictsampler(ScipyTestCase):
25
 
    def check_simple(self):
26
 
        """
27
 
        # Sample from this discrete distribution:
28
 
        #    x       'a'       'b'       'c'
29
 
        #    p(x)    10/180    150/180   20/180
30
 
        """
31
 
        table = {}
32
 
        table['a'] = 10
33
 
        table['b'] = 150
34
 
        table['c'] = 20
35
 
        sampler = dictsampler(table)
36
 
        n = 1000
37
 
        #import pdb
38
 
        #pdb.set_trace()
39
 
        s = sampler.sample(n)
40
 
        assert sum(s[i]=='b' for i in range(n),axis=0)*1./n > 0.75
41
 
 
42
 
        #lam = 10.0
43
 
        #n = 35
44
 
        #numsamples = 10000
45
 
        #a = array([stats.poisson.pmf(i, lam) for i in range(n)])
46
 
        #sampler = intsampler(a)
47
 
        #x = sampler.sample(numsamples)
48
 
        #m = x.mean()
49
 
        #s = x.std()
50
 
        ## Use a normal approximation for confidence intervals for the mean
51
 
        #z = 2.5758   # = norminv(0.995), for a 1% confidence interval
52
 
        #assert abs(m - lam) < z * lam/sqrt(numsamples)
53
 
 
54
 
    def check_sanity(self):
55
 
        # Sample from this pmf:
56
 
        #      x        0       1       2       3       4
57
 
        #      p(x)     0.5     0.1     0.15    0       0.25
58
 
 
59
 
        # The true mean and variance are:
60
 
        truemean = 1.4
61
 
        truevar = 2.74
62
 
 
63
 
        numsamples = 10000
64
 
        a = array([0.5, 0.1, 0.15, 0., 0.25])
65
 
        sampler = intsampler(a)
66
 
        x = sampler.sample(numsamples)
67
 
        m = x.mean()
68
 
        v = x.var()
69
 
        assert x.max() == 4
70
 
        assert x.min() == 0
71
 
        assert sum(x==3,axis=0) == 0
72
 
        assert 0.08 < average(x==1,axis=0) < 0.12
73
 
        # Use a normal approx for confidence intervals for the mean
74
 
        z = 2.5758   # = norminv(0.995), for a 1% confidence interval
75
 
        assert abs(m - truemean) < z * sqrt(truevar/numsamples)
76
 
 
77
 
 
78
 
if __name__ == "__main__":
79
 
    ScipyTest().run()