1
""" Test functions for stats module
7
from scipy_test.testing import *
8
from scipy_test.testing import ScipyTestCase as TestCase
11
from scipy import stats
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)
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)
29
# generate test cases to test cdf and distribution consistency
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']
44
distfunc = eval('stats.'+dist)
45
nargs = distfunc.numargs
47
if dist == 'fatiguelife':
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':
57
vals[1] = vals[0] + 1.0
58
args = str(tuple(vals))
60
args = str(tuple(1.0+rand(nargs)))
62
class test_%s(TestCase):
65
D,pval = stats.kstest('%s','',args=%s,N=30)
67
D,pval = stats.kstest('%s','',args=%s,N=30)
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)
75
class test_randint(TestCase):
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'])
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))
95
x = scipy.r_[0:36:100j]
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)
101
class test_binom(TestCase):
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'])
112
class test_bernoulli(TestCase):
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'])
122
class test_nbinom(TestCase):
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'])
132
class test_geom(TestCase):
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'])
142
class test_hypergeom(TestCase):
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'])
153
class test_logser(TestCase):
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'])
163
class test_poisson(TestCase):
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'])
173
class test_zipf(TestCase):
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'])
183
class test_dlaplace(TestCase):
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'])
192
if __name__ == "__main__":
193
ScipyTest('stats.distributions').run()