~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/stats/tests/test_distributions.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" Test functions for stats module
 
2
 
 
3
"""
 
4
 
 
5
 
 
6
import unittest,sys
 
7
from scipy_test.testing import *
 
8
from scipy_test.testing import ScipyTestCase as TestCase
 
9
set_package_path()
 
10
import scipy
 
11
from scipy import stats
 
12
del sys.path[0]
 
13
 
 
14
import types
 
15
 
 
16
def kolmogorov_test(diststr,args=(),N=20,significance=0.01):
 
17
    qtest = stats.ksoneisf(significance,N)
 
18
    cdf = eval('stats.'+diststr+'.cdf')
 
19
    dist = eval('stats.'+diststr)
 
20
    # Get random numbers
 
21
    kwds = {'size':N}
 
22
    vals = scipy.sort(dist.rvs(*args,**kwds))
 
23
    cdfvals = cdf(vals,*args)
 
24
    q = max(abs(cdfvals-arange(1.0,N+1)/N))
 
25
    assert (q < qtest), "Failed q=%f, bound=%f, alpha=%f" % (q, qtest, significance)
 
26
    return
 
27
 
 
28
 
 
29
# generate test cases to test cdf and distribution consistency
 
30
 
 
31
dists = ['uniform','norm','lognorm','expon','beta',
 
32
         'powerlaw','bradford','burr','fisk','cauchy','halfcauchy',
 
33
         'foldcauchy','gamma','gengamma','loggamma',
 
34
         'alpha','anglit','arcsine','betaprime','erlang',
 
35
         'dgamma','exponweib','exponpow','frechet_l','frechet_r',
 
36
         'gilbrat','f','ncf','chi2','chi','nakagami','genpareto',
 
37
         'genextreme','genhalflogistic','pareto','lomax','halfnorm',
 
38
         'halflogistic','fatiguelife','foldnorm','ncx2','t','nct',
 
39
         'weibull_min','weibull_max','dweibull','maxwell','rayleigh',
 
40
         'genlogistic', 'logistic','gumbel_l','gumbel_r','gompertz',
 
41
         'hypsecant', 'laplace', 'reciprocal','triang','tukeylambda']
 
42
 
 
43
for dist in dists:
 
44
    distfunc = eval('stats.'+dist)
 
45
    nargs = distfunc.numargs
 
46
    alpha = 0.01
 
47
    if dist == 'fatiguelife':
 
48
        alpha = 0.001        
 
49
    if dist == 'erlang':
 
50
        args = str((4,)+tuple(rand(2)))
 
51
    elif dist == 'frechet':
 
52
        args = str(tuple(2*rand(1))+(0,)+tuple(2*rand(2)))
 
53
    elif dist == 'triang':
 
54
        args = str(tuple(rand(nargs)))
 
55
    elif dist == 'reciprocal':
 
56
        vals = rand(nargs)
 
57
        vals[1] = vals[0] + 1.0
 
58
        args = str(tuple(vals))
 
59
    else:
 
60
        args = str(tuple(1.0+rand(nargs)))
 
61
    exstr = r"""
 
62
class test_%s(TestCase):
 
63
    def check_cdf(self):
 
64
        print "Testing %s"
 
65
        D,pval = stats.kstest('%s','',args=%s,N=30)
 
66
        if (pval < %f):
 
67
            D,pval = stats.kstest('%s','',args=%s,N=30)
 
68
            #if (pval < %f):
 
69
            #    D,pval = stats.kstest('%s','',args=%s,N=30)
 
70
        assert (pval > %f), "D = " + str(D) + "; pval = " + str(pval) + "; alpha = " + str(alpha) + "\nargs = " + str(%s)
 
71
""" % (dist,dist,dist,args,alpha,dist,args,alpha,dist,args,alpha,args)
 
72
    exec exstr
 
73
 
 
74
 
 
75
class test_randint(TestCase):
 
76
    def check_rvs(self):
 
77
        vals = stats.randint.rvs(5,30,size=100)
 
78
        assert(scipy.all(vals < 30) & scipy.all(vals >= 5))
 
79
        assert(len(vals) == 100)
 
80
        vals = stats.randint.rvs(5,30,size=(2,50))
 
81
        assert(scipy.shape(vals) == (2,50))
 
82
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
83
        val = stats.randint.rvs(15,46)
 
84
        assert((val >= 15) & (val < 46))
 
85
        assert(isinstance(val, scipy.ArrayType))
 
86
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
87
 
 
88
    def check_pdf(self):
 
89
        k = scipy.r_[0:36]
 
90
        out = scipy.where((k >= 5) & (k < 30), 1.0/(30-5), 0)
 
91
        vals = stats.randint.pmf(k,5,30)
 
92
        assert(scipy.all(vals == out))
 
93
 
 
94
    def check_cdf(self):
 
95
        x = scipy.r_[0:36:100j]
 
96
        k = scipy.floor(x)
 
97
        out = scipy.select([k>=30,k>=5],[1.0,(k-5.0+1)/(30-5.0)],0)
 
98
        vals = stats.randint.cdf(x,5,30)
 
99
        assert_array_almost_equal(vals, out, decimal=12)
 
100
 
 
101
class test_binom(TestCase):
 
102
    def check_rvs(self):
 
103
        vals = stats.binom.rvs(10, 0.75, size=(2, 50))
 
104
        assert(scipy.all(vals >= 0) & scipy.all(vals <= 10))
 
105
        assert(scipy.shape(vals) == (2, 50))
 
106
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
107
        val = stats.binom.rvs(10, 0.75)
 
108
        assert(isinstance(val, scipy.ArrayType))
 
109
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
110
        
 
111
        
 
112
class test_bernoulli(TestCase):
 
113
    def check_rvs(self):
 
114
        vals = stats.bernoulli.rvs(0.75, size=(2, 50))
 
115
        assert(scipy.all(vals >= 0) & scipy.all(vals <= 1))
 
116
        assert(scipy.shape(vals) == (2, 50))
 
117
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
118
        val = stats.bernoulli.rvs(0.75)
 
119
        assert(isinstance(val, scipy.ArrayType))
 
120
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
121
        
 
122
class test_nbinom(TestCase):
 
123
    def check_rvs(self):
 
124
        vals = stats.nbinom.rvs(10, 0.75, size=(2, 50))
 
125
        assert(scipy.all(vals >= 0))
 
126
        assert(scipy.shape(vals) == (2, 50))
 
127
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
128
        val = stats.nbinom.rvs(10, 0.75)
 
129
        assert(isinstance(val, scipy.ArrayType))
 
130
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
131
        
 
132
class test_geom(TestCase):
 
133
    def check_rvs(self):
 
134
        vals = stats.geom.rvs(0.75, size=(2, 50))
 
135
        assert(scipy.all(vals >= 0))
 
136
        assert(scipy.shape(vals) == (2, 50))
 
137
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
138
        val = stats.geom.rvs(0.75)
 
139
        assert(isinstance(val, scipy.ArrayType))
 
140
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
141
        
 
142
class test_hypergeom(TestCase):
 
143
    def check_rvs(self):
 
144
        vals = stats.hypergeom.rvs(20, 10, 3, size=(2, 50))
 
145
        assert(scipy.all(vals >= 0) &
 
146
               scipy.all(vals <= 3))
 
147
        assert(scipy.shape(vals) == (2, 50))
 
148
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
149
        val = stats.hypergeom.rvs(20, 3, 10)
 
150
        assert(isinstance(val, scipy.ArrayType))
 
151
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
152
        
 
153
class test_logser(TestCase):
 
154
    def check_rvs(self):
 
155
        vals = stats.logser.rvs(0.75, size=(2, 50))
 
156
        assert(scipy.all(vals >= 1))
 
157
        assert(scipy.shape(vals) == (2, 50))
 
158
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
159
        val = stats.logser.rvs(0.75)
 
160
        assert(isinstance(val, scipy.ArrayType))
 
161
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
162
        
 
163
class test_poisson(TestCase):
 
164
    def check_rvs(self):
 
165
        vals = stats.poisson.rvs(0.5, size=(2, 50))
 
166
        assert(scipy.all(vals >= 0))
 
167
        assert(scipy.shape(vals) == (2, 50))
 
168
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
169
        val = stats.poisson.rvs(0.5)
 
170
        assert(isinstance(val, scipy.ArrayType))
 
171
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
172
        
 
173
class test_zipf(TestCase):
 
174
    def check_rvs(self):
 
175
        vals = stats.zipf.rvs(1.5, size=(2, 50))
 
176
        assert(scipy.all(vals >= 1))
 
177
        assert(scipy.shape(vals) == (2, 50))
 
178
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
179
        val = stats.zipf.rvs(1.5)
 
180
        assert(isinstance(val, scipy.ArrayType))
 
181
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
182
        
 
183
class test_dlaplace(TestCase):
 
184
    def check_rvs(self):
 
185
        vals = stats.dlaplace.rvs(1.5 , size=(2, 50))
 
186
        assert(scipy.shape(vals) == (2, 50))        
 
187
        assert(vals.typecode() in scipy.typecodes['AllInteger'])
 
188
        val = stats.dlaplace.rvs(1.5)
 
189
        assert(isinstance(val, scipy.ArrayType))
 
190
        assert(val.typecode() in scipy.typecodes['AllInteger'])
 
191
        
 
192
if __name__ == "__main__":
 
193
    ScipyTest('stats.distributions').run()
 
194