Skip to content

Commit

Permalink
Deterministic tests; also shared.
Browse files Browse the repository at this point in the history
  • Loading branch information
aarchiba committed Oct 29, 2009
1 parent 72649e7 commit 225678c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 49 deletions.
36 changes: 16 additions & 20 deletions test_htest.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
import numpy as np
import scipy.stats

from test_kuiper import seed, double_check, check_uniform, check_fpp
import htest
import zm2

def test_null():
for N in 100, 1000, 10000:
yield check_null, N
def F(x):
return htest.h_test(x)[2]

def check_null(N):
H, M, fpp = htest.h_test(np.random.random(N))
assert fpp>0.01
def test_uniform():
for N in 20, 100, 1000:
yield check_uniform, F, N

@seed()
@double_check
def test_non_null():
H, M, fpp = htest.h_test(np.random.random(1000)/2)
assert fpp<0.01


def test_fpp():
for N in 100, 200:
yield check_fpp, N, 1000, 0.05

def check_fpp(N,M,thresh):
fp = 0
for i in range(M):
H, m, fpp = htest.h_test(np.random.random(N))
if fpp<thresh:
fp += 1
print thresh, float(fp)/M
assert scipy.stats.binom(M,thresh).sf(fp-1)>0.005
assert scipy.stats.binom(M,thresh).cdf(fp-1)>0.005
for N in 10, 50, 100:
yield check_fpp, F, N, 1000, 0.05

@seed()
@double_check
def test_mean_stddev():
Hs = []
for i in range(1000):
H, M, fpp = htest.h_test(np.random.random(1000))
H, M, fpp = htest.h_test(np.random.random(100))
Hs.append(H)
print np.mean(Hs), np.std(Hs)
assert np.abs(np.mean(Hs)-2.51)<0.1
assert np.abs(np.std(Hs)-2.51)<0.3

@seed()
def test_compare_zm2():
data = np.random.random(1000)
H, M, fpp = htest.h_test(data)
Z, zm2fpp = zm2.Zm2(data, M)
Z, zm2fpp = zm2.zm2(data, M)
assert np.abs(Z - (H+4*M-4))<1e-8
assert fpp>zm2fpp

42 changes: 32 additions & 10 deletions test_kuiper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@

import kuiper

def seed(n=0):
"""Seed the random number generator before running a test."""

def wrap(f):
def wrapped(*args,**kwargs):
np.random.seed(n)
return f(*args,**kwargs)
wrapped.__name__ = f.__name__
wrapped.__dict__ = f.__dict__
wrapped.__doc__ = f.__doc__
wrapped.__module__ = f.__module__
return wrapped
return wrap


# FIXME
#@make_decorator doesn't work!
def double_check(f):
Expand All @@ -29,35 +44,41 @@ def double(*args,**kwargs):

def test_uniform():
for N in [10,100,1000,10000]:
yield check_uniform, N
yield check_uniform, lambda x: kuiper.kuiper(x)[1], N

@seed()
@double_check
def check_uniform(N):
assert kuiper.kuiper(np.random.random(N))[1]>0.01
def check_uniform(f,N):
assert f(np.random.random(N))>0.01

def test_fpp():
def F(x):
return kuiper.kuiper(x)[1]
for N in [10,100,1000]:
yield check_fpp, N, 100, 0.05
yield check_fpp, N, 100, 0.25
yield check_fpp, F, N, 100, 0.05
yield check_fpp, F, N, 100, 0.25
#Seems to fail for N==5
#yield check_fpp, 5, 1000, 0.05

@seed()
@double_check
def check_fpp(N,M,fpp):
def check_fpp(F,N,M,fpp):
fps = 0
for i in range(M):
D, f = kuiper.kuiper(np.random.random(N))
f = F(np.random.random(N))
if f<fpp:
fps += 1
assert scipy.stats.binom(M,fpp).sf(fps-1)>0.005
assert scipy.stats.binom(M,fpp).cdf(fps-1)>0.005

@seed()
@double_check
def test_detect_nonuniform():
D, f = kuiper.kuiper(np.random.random(500)*0.5)
assert f<0.01


@seed()
@double_check
def test_weighted():
a = (np.random.random(100) * 3.4 + 0.8)%1
Expand All @@ -67,6 +88,7 @@ def test_weighted():
assert kuiper.kuiper(a,cdf)[1]>0.01


# Out of sheer laziness I'm not going to generify these tests.
def test_kuiper_two():
for (N,M) in [(100,100),
(20,100),
Expand All @@ -78,22 +100,22 @@ def test_kuiper_two():
yield check_kuiper_two_nonuniform, N, M
yield check_fpp_kuiper_two, N, M, 100, 0.05

@seed()
@double_check
def check_kuiper_two_uniform(N,M):
assert kuiper.kuiper_two(np.random.random(N),np.random.random(M))[1]>0.01

@seed()
@double_check
def check_kuiper_two_nonuniform(N,M):
assert kuiper.kuiper_two(np.random.random(N)**2,np.random.random(M)**2)[1]>0.01

@seed()
@double_check
def test_detect_kuiper_two_different():
D, f = kuiper.kuiper_two(np.random.random(500)*0.5,np.random.random(500))
assert f<0.01

def test_kuiper_two_fpp():
pass

@double_check
def check_fpp_kuiper_two(N,M,R,fpp):
fps = 0
Expand Down
27 changes: 9 additions & 18 deletions test_zm2.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import numpy as np
import scipy.stats

from test_kuiper import seed, double_check, check_uniform, check_fpp
import zm2

def test_null():
for N in 100, 1000, 10000:
yield check_null, N

def check_null(N):
Z, fpp = zm2.Zm2(np.random.random(N),5)
assert fpp>0.01
for m in [1,2,5]:
yield check_uniform, lambda x: zm2.zm2(x,m)[1], N

@seed()
@double_check
def test_non_null():
Z, fpp = zm2.Zm2(np.random.random(1000)/2,5)
Z, fpp = zm2.zm2(np.random.random(1000)/2,5)
assert fpp<0.01

def test_fpp():
for N in 100, 200:
yield check_fpp, N, 1000, 0.05

def check_fpp(N,M,thresh):
fp = 0
for i in range(M):
Z, fpp = zm2.Zm2(np.random.random(N),5)
if fpp<thresh:
fp += 1
print thresh, float(fp)/M
assert scipy.stats.binom(M,thresh).sf(fp-1)>0.005
assert scipy.stats.binom(M,thresh).cdf(fp-1)>0.005
for N in 10, 100, 500:
for m in [1,2,5]:
yield check_fpp, lambda x: zm2.zm2(x,m)[1], N, 1000, 0.05

2 changes: 1 addition & 1 deletion zm2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
import scipy.stats

def Zm2(events, M):
def zm2(events, M):
"""The Z_m^2 test for periodicity.
The Z_m^2 test uses a sequence of events to construct an estimate of
Expand Down

0 comments on commit 225678c

Please sign in to comment.