-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproportionTestTest.plg
177 lines (148 loc) · 8.03 KB
/
proportionTestTest.plg
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
% Copyright (c) 2010 Aubrey Barnard. This is free software. See
% LICENSE for details.
% Tests proportionTest.plg. This is a reimplementation of proportionTestTest.c.
% Imports
:- use_module('prologUnit.plg').
% Consult the module under test (because it's not a module)
:- reconsult('proportionTest.plg').
% Test data for binomial distribution PDF (successes, trials, probability, answer)
% Inputs generated with following python code:
% (1-5): nt = random.randint(1, 1000); ns = random.randint(0, nt); (ns, nt, random.random())
% (6-10): nt = random.randint(1, 100); ns = random.randint(0, nt); (ns, nt, random.random())
% (11-15): nt = random.randint(1, 10); ns = random.randint(0, nt); (ns, nt, random.random())
% Answers generated using R with options(digits=15): dbinom(successes, trials, prob)
testData_binomial(392, 620, 0.81119835850039301, 8.58994240095824e-26).
testData_binomial(172, 485, 0.56586248581055776, 3.89126507976308e-21).
testData_binomial(174, 629, 0.31834635978808645, 0.00267495784579504).
testData_binomial(162, 351, 0.63385150309939231, 1.96584040659893e-11).
testData_binomial(185, 196, 0.37044608582094984, 3.11204257196861e-65).
testData_binomial(53, 54, 0.14310979691793868, 8.2374976754006e-44).
testData_binomial(43, 77, 0.85737100412326572, 1.90514859509277e-10).
testData_binomial(11, 11, 0.41907865288461432, 7.00246475120943e-05).
testData_binomial(24, 57, 0.066572549173138817, 4.44729047409375e-14).
testData_binomial(2, 2, 0.49146400180019212, 0.241536865065459).
testData_binomial(4, 10, 0.64328116418045522, 0.074093291291514).
testData_binomial(0, 1, 0.33919458730124885, 0.660805412698751).
testData_binomial(3, 6, 0.42610226057295575, 0.292465775892408).
testData_binomial(2, 6, 0.64190743415040585, 0.101628915178332).
testData_binomial(1, 1, 0.73409989547983279, 0.734099895479833).
% Some basic cases
testData_binomial(0, 0, 0.8, 1.0).
testData_binomial(0, 1, 0.8, 0.2).
testData_binomial(1, 0, 0.8, 0.0).
testData_binomial(1, 1, 0.8, 0.8).
% Test the binomial function with the above data
testBinomial :-
testData_binomial(Successes, Trials, Probability, Answer),
binomial(Successes, Trials, Probability, Actual),
assertEqual(Answer, Actual),
fail.
testBinomial.
% Test data for binomial integral (start, end, trials, probability, answer)
% Inputs generated with the following Python:
% nt = random.randint(2, 1000); start = random.randint(0, nt - 1); (start, random.randint(start + 1, nt), nt, random.random())
% Again 3 groups for 1000, 100, 10
% Answer generated using R with options(digits=15): pbinom(end, trials, prob) - pbinom(start - 1, trials, prob)
testData_binomialIntegral(53, 416, 817, 0.50258268609630641, 0.65984740219146).
testData_binomialIntegral(293, 331, 619, 0.65541508377696323, 3.96175255072217e-10).
testData_binomialIntegral(271, 317, 343, 0.9408201930093304, 0.119114220132441).
testData_binomialIntegral(5, 46, 85, 0.15271239212919374, 0.997826988693654).
testData_binomialIntegral(32, 52, 57, 0.44934653940215386, 0.0588579463007108).
testData_binomialIntegral(28, 40, 97, 0.82166879021551376, 2.76347687604193e-19).
testData_binomialIntegral(7, 9, 9, 0.4115965021751895, 0.0296444371533974).
testData_binomialIntegral(3, 8, 9, 0.51216129787667342, 0.919130073559746).
testData_binomialIntegral(2, 5, 5, 0.86212696681350454, 0.998392571428867).
% Bounds cases
testData_binomialIntegral(0, 17, 17, 0.14406230391178321, 1.0).
testData_binomialIntegral(7, 7, 7, 0.12408185704756058, 4.52853775101822e-07).
testData_binomialIntegral(0, 0, 4, 0.12224604256553973, 0.593596299392311).
testData_binomialIntegral(9, 9, 10, 0.7670033381658905, 0.214054807293567).
% Test the binomial integral function with the above data
testBinomialIntegral :-
testData_binomialIntegral(Start, End, Trials, Probability, Answer),
%writeln(testData_binomialIntegral(Start, End, Trials, Probability, Answer)),
binomial_integral(Start, End, Trials, Probability, Actual),
assertEqual(Answer, Actual),
fail.
testBinomialIntegral.
% Test data for binomial two-tailed test (successes, trials, probability, answer)
% Answers generated with R: binom.test(successes, trials, p, alternative="two.sided")$p.value
testData_binomialTwoTailed(133, 393, 0.73549094368402157, 1.08719497880240e-60).
testData_binomialTwoTailed(541, 547, 0.072119113246073119, 0.0).
testData_binomialTwoTailed(272, 479, 0.99272523232985577, 2.37645111542111e-303).
testData_binomialTwoTailed(49, 65, 0.21873573703754501, 6.2068549823656e-20).
testData_binomialTwoTailed(8, 32, 0.93714825640860089, 9.23457859750034e-23).
testData_binomialTwoTailed(14, 34, 0.92874809760063326, 5.92640629517219e-15).
testData_binomialTwoTailed(4, 8, 0.6495356843124559, 0.463037655923138).
testData_binomialTwoTailed(3, 3, 0.19134931424597135, 0.00700617095935884).
testData_binomialTwoTailed(8, 9, 0.30584324339755709, 0.00050170465408135).
% Some basic cases
% FIXME commented out tests fail
testData_binomialTwoTailed(0, 1, 0.8, 0.2).
testData_binomialTwoTailed(1, 1, 0.8, 1.0).
%testData_binomialTwoTailed(0, 2, 0.3, 1.0).
%testData_binomialTwoTailed(1, 2, 0.3, 0.51).
testData_binomialTwoTailed(2, 2, 0.3, 0.09).
testData_binomialTwoTailed(0, 3, 0.4, 0.28).
testData_binomialTwoTailed(1, 3, 0.4, 1.0).
testData_binomialTwoTailed(2, 3, 0.4, 0.568).
testData_binomialTwoTailed(3, 3, 0.4, 0.064).
testData_binomialTwoTailed(0, 4, 0.5, 0.125).
testData_binomialTwoTailed(1, 4, 0.5, 0.625).
%testData_binomialTwoTailed(2, 4, 0.5, 1.0).
testData_binomialTwoTailed(3, 4, 0.5, 0.625).
testData_binomialTwoTailed(4, 4, 0.5, 0.125).
% Test the binomial two tailed function with the above data
testBinomialTwoTailed :-
testData_binomialTwoTailed(Successes, Trials, Probability, Answer),
%writeln(testData_binomialTwoTailed(Successes, Trials, Probability, Answer)),
binomial_test_twotailed(Successes, Trials, Probability, Actual),
assertEqual(Answer, Actual),
fail.
testBinomialTwoTailed.
% Test data for the proportions tests (portion1, portion2, binomial answer, chisquare answer)
testData_proportions(5347, 5970, 4.96569444330095e-09, 4.73328564072474e-09).
testData_proportions(7595, 6073, 9.00819116371014e-39, 9.59929941647448e-39).
testData_proportions(7708, 9930, 6.10564978996164e-63, 7.80457958627232e-63).
testData_proportions(8526, 6552, 2.96299054376206e-58, 3.76489477935706e-58).
testData_proportions(7208, 6305, 8.34142595979523e-15, 7.97052931445024e-15).
testData_proportions(598, 79, 1.48682339486739e-99, 1.59824761055216e-88).
testData_proportions(284, 446, 2.19369487864294e-09, 2.02375222637581e-09).
testData_proportions(762, 989, 6.36701897425784e-08, 5.80233104521966e-08).
testData_proportions(18, 942, 1.33504491190077e-251, 2.02776677867658e-195).
testData_proportions(531, 978, 6.05986681095489e-31, 1.21606957458038e-30).
testData_proportions(78, 97, 0.173443727973984, 0.150926950066716).
testData_proportions(67, 38, 0.00601608151524935, 0.00465319720901331).
testData_proportions(6, 63, 4.47352983649895e-13, 6.79060945202553e-12).
testData_proportions(82, 22, 2.56830196979963e-09, 4.01718815444534e-09).
testData_proportions(33, 21, 0.133674235364105, 0.102470434859750).
testData_proportions(4, 7, 0.548828125, 0.365712296281513).
testData_proportions(3, 5, 0.7265625, 0.479500122186953).
testData_proportions(7, 9, 0.803619384765625, 0.617075077451974).
testData_proportions(1, 3, 0.625, 0.317310507862914).
testData_proportions(4, 10, 0.1795654296875, 0.108809430040546).
% Test the binomial test with proportions and 0.5 probability
testBinomialTest :-
testData_proportions(Proportion1, Proportion2, Answer, _),
binomial_test(Proportion1, Proportion2, Actual),
assertEqual(Answer, Actual),
fail.
testBinomialTest.
% Test the chisquare test with proportions and 0.5 probability
testChisquareTest :-
testData_proportions(Proportion1, Proportion2, _, Answer),
chisquare_test(Proportion1, Proportion2, Actual),
assertEqual(Answer, Actual),
fail.
testChisquareTest.
% Run tests
main :-
Tests = [
testBinomial,
testBinomialIntegral,
testBinomialTwoTailed,
testBinomialTest,
testChisquareTest
],
runTests(Tests),
halt.