1
|
|
|
import unittest |
2
|
|
|
from random import random |
3
|
|
|
|
4
|
|
|
from numpy.random.mtrand import normal |
5
|
|
|
|
6
|
|
|
from pysie.dsl.one_group import MeanTesting, ProportionTesting |
7
|
|
|
from pysie.stats.distributions import MeanSamplingDistribution, ProportionSamplingDistribution |
8
|
|
|
from pysie.stats.samples import Sample, SampleDistribution |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class MeanTestingUnitTest(unittest.TestCase): |
12
|
|
|
def test_mean_normal(self): |
13
|
|
|
mu = 0.0 |
14
|
|
|
sigma = 1.0 |
15
|
|
|
sample_size = 31 |
16
|
|
|
sample = Sample() |
17
|
|
|
|
18
|
|
|
for i in range(sample_size): |
19
|
|
|
sample.add_numeric(normal(mu, sigma)) |
20
|
|
|
|
21
|
|
|
sampling_distribution = MeanSamplingDistribution(sample_distribution=SampleDistribution(sample)) |
22
|
|
|
testing = MeanTesting(sampling_distribution=sampling_distribution, mean_null=0.0) |
23
|
|
|
|
24
|
|
|
print('one tail p-value: ' + str(testing.p_value_one_tail)) |
25
|
|
|
print('two tail p-value: ' + str(testing.p_value_two_tail)) |
26
|
|
|
reject_one_tail, reject_two_tail = testing.will_reject(0.01) |
27
|
|
|
print('will reject mean = 0 (one-tail) ? ' + str(reject_one_tail)) |
28
|
|
|
print('will reject mean = 0 (two-tail) ? ' + str(reject_two_tail)) |
29
|
|
|
self.assertFalse(reject_one_tail) |
30
|
|
|
self.assertFalse(reject_two_tail) |
31
|
|
|
|
32
|
|
|
def test_mean_student(self): |
33
|
|
|
mu = 0.0 |
34
|
|
|
sigma = 1.0 |
35
|
|
|
sample_size = 29 |
36
|
|
|
sample = Sample() |
37
|
|
|
|
38
|
|
|
for i in range(sample_size): |
39
|
|
|
sample.add_numeric(normal(mu, sigma)) |
40
|
|
|
|
41
|
|
|
sampling_distribution = MeanSamplingDistribution(sample_distribution=SampleDistribution(sample)) |
42
|
|
|
testing = MeanTesting(sampling_distribution=sampling_distribution, mean_null=0.0) |
43
|
|
|
|
44
|
|
|
print('one tail p-value: ' + str(testing.p_value_one_tail)) |
45
|
|
|
print('two tail p-value: ' + str(testing.p_value_two_tail)) |
46
|
|
|
reject_one_tail, reject_two_tail = testing.will_reject(0.01) |
47
|
|
|
print('will reject mean = 0 (one-tail) ? ' + str(reject_one_tail)) |
48
|
|
|
print('will reject mean = 0 (two-tail) ? ' + str(reject_two_tail)) |
49
|
|
|
self.assertFalse(reject_one_tail) |
50
|
|
|
self.assertFalse(reject_two_tail) |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
class ProportionTestingUnitTest(unittest.TestCase): |
54
|
|
View Code Duplication |
def test_proportion_normal(self): |
|
|
|
|
55
|
|
|
sample = Sample() |
56
|
|
|
|
57
|
|
|
for i in range(100): |
58
|
|
|
if random() <= 0.6: |
59
|
|
|
sample.add_category("OK") |
60
|
|
|
else: |
61
|
|
|
sample.add_category("CANCEL") |
62
|
|
|
|
63
|
|
|
sampling_distribution = ProportionSamplingDistribution( |
64
|
|
|
sample_distribution=SampleDistribution(sample, categorical_value="OK")) |
65
|
|
|
|
66
|
|
|
testing = ProportionTesting(sampling_distribution=sampling_distribution, p_null=0.6) |
67
|
|
|
|
68
|
|
|
print('one tail p-value: ' + str(testing.p_value_one_tail)) |
69
|
|
|
print('two tail p-value: ' + str(testing.p_value_two_tail)) |
70
|
|
|
reject_one_tail, reject_two_tail = testing.will_reject(0.01) |
71
|
|
|
print('will reject p = 0.6 (one-tail) ? ' + str(reject_one_tail)) |
72
|
|
|
print('will reject p = 0.6 (two-tail) ? ' + str(reject_two_tail)) |
73
|
|
|
self.assertFalse(reject_one_tail) |
74
|
|
|
self.assertFalse(reject_two_tail) |
75
|
|
|
|
76
|
|
View Code Duplication |
def test_proportion_simulation(self): |
|
|
|
|
77
|
|
|
sample = Sample() |
78
|
|
|
|
79
|
|
|
for i in range(10): |
80
|
|
|
if random() <= 0.6: |
81
|
|
|
sample.add_category("OK") |
82
|
|
|
else: |
83
|
|
|
sample.add_category("CANCEL") |
84
|
|
|
|
85
|
|
|
sampling_distribution = ProportionSamplingDistribution( |
86
|
|
|
sample_distribution=SampleDistribution(sample, categorical_value="OK")) |
87
|
|
|
|
88
|
|
|
testing = ProportionTesting(sampling_distribution=sampling_distribution, p_null=0.6) |
89
|
|
|
|
90
|
|
|
print('one tail p-value: ' + str(testing.p_value_one_tail)) |
91
|
|
|
print('two tail p-value: ' + str(testing.p_value_two_tail)) |
92
|
|
|
reject_one_tail, reject_two_tail = testing.will_reject(0.01) |
93
|
|
|
print('will reject p = 0.6 (one-tail) ? ' + str(reject_one_tail)) |
94
|
|
|
print('will reject p = 0.6 (two-tail) ? ' + str(reject_two_tail)) |
95
|
|
|
self.assertFalse(reject_one_tail) |
96
|
|
|
self.assertFalse(reject_two_tail) |
97
|
|
|
|
98
|
|
|
if __name__ == '__main__': |
99
|
|
|
unittest.main() |
100
|
|
|
|