3
""" Test functions for the 'dictsampler' derived class.
5
Author: Ed Schofield, 2003-2006
6
Copyright: Ed Schofield, 2003-2006
10
from numpy import arange, add, array, dot, zeros, identity
13
from numpy.testing import *
16
#from scipy.montecarlo import *
17
from scipy.sandbox.montecarlo import *
18
from scipy import stats
24
class test_dictsampler(ScipyTestCase):
25
def check_simple(self):
27
# Sample from this discrete distribution:
29
# p(x) 10/180 150/180 20/180
35
sampler = dictsampler(table)
40
assert sum(s[i]=='b' for i in range(n),axis=0)*1./n > 0.75
45
#a = array([stats.poisson.pmf(i, lam) for i in range(n)])
46
#sampler = intsampler(a)
47
#x = sampler.sample(numsamples)
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)
54
def check_sanity(self):
55
# Sample from this pmf:
57
# p(x) 0.5 0.1 0.15 0 0.25
59
# The true mean and variance are:
64
a = array([0.5, 0.1, 0.15, 0., 0.25])
65
sampler = intsampler(a)
66
x = sampler.sample(numsamples)
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)
78
if __name__ == "__main__":