Completed
Push — master ( fe8c00...4ceb7d )
by Xianshun
01:07
created

ProportionDiffTestingUnitTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 95.24 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 60
loc 63
rs 10
c 1
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B test_student() 30 30 5
B test_normal() 30 30 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
import unittest
2
from random import random
3
4
from numpy.random.mtrand import normal
5
6
from pysie.dsl.two_groups import MeanDiffTesting, ProportionDiffTesting
7
from pysie.stats.distributions import MeanDiffSamplingDistribution, DistributionFamily, \
8
    ProportionDiffSamplingDistribution
9
from pysie.stats.samples import Sample, SampleDistribution
10
11
12
class MeanDiffTestingUnitTest(unittest.TestCase):
13
14 View Code Duplication
    def test_normal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
15
        grp1_mu = 0.0
16
        grp1_sigma = 1.0
17
        grp1_sample_size = 31
18
        grp1_sample = Sample()
19
20
        grp2_mu = 0.09
21
        grp2_sigma = 2.0
22
        grp2_sample_size = 36
23
        grp2_sample = Sample()
24
25
        for i in range(grp1_sample_size):
26
            grp1_sample.add_numeric(normal(grp1_mu, grp1_sigma))
27
28
        for i in range(grp2_sample_size):
29
            grp2_sample.add_numeric(normal(grp2_mu, grp2_sigma))
30
31
        sampling_distribution = MeanDiffSamplingDistribution(grp1_sample_distribution=SampleDistribution(grp1_sample),
32
                                                             grp2_sample_distribution=SampleDistribution(grp2_sample))
33
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.normal)
34
        testing = MeanDiffTesting(sampling_distribution=sampling_distribution)
35
        print('one tail p-value: ' + str(testing.p_value_one_tail))
36
        print('two tail p-value: ' + str(testing.p_value_two_tail))
37
        reject_one_tail, reject_two_tail = testing.will_reject(0.01)
38
        print('will reject mean_1 == mean_2 (one-tail) ? ' + str(reject_one_tail))
39
        print('will reject mean_1 == mean_2 (two-tail) ? ' + str(reject_two_tail))
40
        self.assertFalse(reject_one_tail)
41
        self.assertFalse(reject_two_tail)
42
43 View Code Duplication
    def test_student(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
44
        grp1_mu = 0.0
45
        grp1_sigma = 1.0
46
        grp1_sample_size = 29
47
        grp1_sample = Sample()
48
49
        grp2_mu = 0.09
50
        grp2_sigma = 2.0
51
        grp2_sample_size = 28
52
        grp2_sample = Sample()
53
54
        for i in range(grp1_sample_size):
55
            grp1_sample.add_numeric(normal(grp1_mu, grp1_sigma))
56
57
        for i in range(grp2_sample_size):
58
            grp2_sample.add_numeric(normal(grp2_mu, grp2_sigma))
59
60
        sampling_distribution = MeanDiffSamplingDistribution(grp1_sample_distribution=SampleDistribution(grp1_sample),
61
                                                             grp2_sample_distribution=SampleDistribution(grp2_sample))
62
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.student_t)
63
        testing = MeanDiffTesting(sampling_distribution=sampling_distribution)
64
        print('one tail p-value: ' + str(testing.p_value_one_tail))
65
        print('two tail p-value: ' + str(testing.p_value_two_tail))
66
        reject_one_tail, reject_two_tail = testing.will_reject(0.01)
67
        print('will reject mean_1 == mean_2 (one-tail) ? ' + str(reject_one_tail))
68
        print('will reject mean_1 == mean_2 (two-tail) ? ' + str(reject_two_tail))
69
        self.assertFalse(reject_one_tail)
70
        self.assertFalse(reject_two_tail)
71
72
73
class ProportionDiffTestingUnitTest(unittest.TestCase):
74
75 View Code Duplication
    def test_normal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
76
        grp1_sample = Sample()
77
        grp2_sample = Sample()
78
79
        for i in range(100):
80
            if random() <= 0.6:
81
                grp1_sample.add_category("OK")
82
            else:
83
                grp1_sample.add_category("CANCEL")
84
85
        for i in range(100):
86
            if random() <= 0.61:
87
                grp2_sample.add_category("OK")
88
            else:
89
                grp2_sample.add_category("CANCEL")
90
91
        sampling_distribution = ProportionDiffSamplingDistribution(grp1_sample_distribution=SampleDistribution(
92
            grp1_sample, categorical_value="OK"),
93
            grp2_sample_distribution=SampleDistribution(
94
                grp2_sample, categorical_value="OK"))
95
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.normal)
96
97
        testing = ProportionDiffTesting(sampling_distribution=sampling_distribution)
98
        print('one tail p-value: ' + str(testing.p_value_one_tail))
99
        print('two tail p-value: ' + str(testing.p_value_two_tail))
100
        reject_one_tail, reject_two_tail = testing.will_reject(0.01)
101
        print('will reject p_1 == p_2 (one-tail) ? ' + str(reject_one_tail))
102
        print('will reject p_1 == p_2 (two-tail) ? ' + str(reject_two_tail))
103
        self.assertFalse(reject_one_tail)
104
        self.assertFalse(reject_two_tail)
105
106 View Code Duplication
    def test_student(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
107
        grp1_sample = Sample()
108
        grp2_sample = Sample()
109
110
        for i in range(20):
111
            if random() <= 0.6:
112
                grp1_sample.add_category("OK")
113
            else:
114
                grp1_sample.add_category("CANCEL")
115
116
        for i in range(20):
117
            if random() <= 0.61:
118
                grp2_sample.add_category("OK")
119
            else:
120
                grp2_sample.add_category("CANCEL")
121
122
        sampling_distribution = ProportionDiffSamplingDistribution(grp1_sample_distribution=SampleDistribution(
123
            grp1_sample, categorical_value="OK"),
124
            grp2_sample_distribution=SampleDistribution(
125
                grp2_sample, categorical_value="OK"))
126
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.simulation)
127
128
        testing = ProportionDiffTesting(sampling_distribution=sampling_distribution)
129
        print('one tail p-value: ' + str(testing.p_value_one_tail))
130
        print('two tail p-value: ' + str(testing.p_value_two_tail))
131
        reject_one_tail, reject_two_tail = testing.will_reject(0.01)
132
        print('will reject p_1 == p_2 (one-tail) ? ' + str(reject_one_tail))
133
        print('will reject p_1 == p_2 (two-tail) ? ' + str(reject_two_tail))
134
        self.assertFalse(reject_one_tail)
135
        self.assertFalse(reject_two_tail)
136
137
if __name__ == '__main__':
138
    unittest.main()