Completed
Push — master ( e2ceb6...712fe6 )
by Xianshun
01:17
created

ProportionSamplingDistributionUnitTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 56.6 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 30
loc 53
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A test_confidence_interval_with_sample_normal() 15 15 3
A test_confidence_interval_with_sample_stats_normal() 0 9 1
A test_confidence_interval_with_sample_simulation() 15 15 3
A test_confidence_interval_with_sample_stats_simulation() 0 9 1

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
3
from numpy.random import normal, random
4
5
from pysie.stats.distributions import MeanSamplingDistribution, DistributionFamily, ProportionSamplingDistribution
6
from pysie.stats.samples import Sample, SampleDistribution
7
8
9
class MeanSamplingDistributionUnitTest(unittest.TestCase):
10
11
    def test_confidence_interval_with_sample_stats_normal(self):
12
        sample_mean = 0
13
        sample_sd = 1
14
        sample_size = 31
15
        sampling_distribution = MeanSamplingDistribution(sample_mean=sample_mean, sample_sd=sample_sd, sample_size=sample_size)
16
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.normal)
17
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
18
              + ', standard_error=' + str(sampling_distribution.standard_error) + ')')
19
        print('confidence interval for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
20 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
    def test_confidence_interval_with_sample_normal(self):
22
        mu = 0.0
23
        sigma = 1.0
24
        sample_size = 31
25
        sample = Sample()
26
27
        for i in range(sample_size):
28
            sample.add_numeric(normal(mu, sigma))
29
30
        sampling_distribution = MeanSamplingDistribution(sample_distribution=SampleDistribution(sample))
31
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.normal)
32
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
33
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
34
        print('confidence interval for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
35
36
    def test_confidence_interval_with_sample_stats_student(self):
37
        sample_mean = 0
38
        sample_sd = 1
39
        sample_size = 29
40
        sampling_distribution = MeanSamplingDistribution(sample_mean=sample_mean, sample_sd=sample_sd, sample_size=sample_size)
41
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.student_t)
42
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
43 View Code Duplication
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
44
        print('confidence interval for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
45
46
    def test_confidence_interval_with_sample_student(self):
47
        mu = 0.0
48
        sigma = 1.0
49
        sample_size = 29
50
        sample = Sample()
51
52
        for i in range(sample_size):
53
            sample.add_numeric(normal(mu, sigma))
54
55
        sampling_distribution = MeanSamplingDistribution(sample_distribution=SampleDistribution(sample))
56
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.student_t)
57
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
58
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
59
        print('confidence interval for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
60
61
62
class ProportionSamplingDistributionUnitTest(unittest.TestCase):
63
64
    def test_confidence_interval_with_sample_stats_normal(self):
65
        sample_proportion = 0.6
66
        sample_size = 31
67
        sampling_distribution = ProportionSamplingDistribution(sample_proportion=sample_proportion,
68
                                                               sample_size=sample_size)
69
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.normal)
70
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
71
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
72
        print('confidence level for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
73
74 View Code Duplication
    def test_confidence_interval_with_sample_normal(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
75
        sample = Sample()
76
77
        for i in range(100):
78
            if random() <= 0.6:
79
                sample.add_category("OK")
80
            else:
81
                sample.add_category("CANCEL")
82
83
        sampling_distribution = ProportionSamplingDistribution(sample_distribution=SampleDistribution(sample,
84
                                                                                                      categorical_value="OK"))
85
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.normal)
86
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
87
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
88
        print('confidence level for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
89
90
    def test_confidence_interval_with_sample_stats_simulation(self):
91
        sample_proportion = 0.6
92
        sample_size = 10
93
        sampling_distribution = ProportionSamplingDistribution(sample_proportion=sample_proportion,
94
                                                               sample_size=sample_size)
95
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.simulation)
96
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
97
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
98
        print('confidence level for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
99
100 View Code Duplication
    def test_confidence_interval_with_sample_simulation(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
101
        sample = Sample()
102
103
        for i in range(10):
104
            if random() <= 0.6:
105
                sample.add_category("OK")
106
            else:
107
                sample.add_category("CANCEL")
108
109
        sampling_distribution = ProportionSamplingDistribution(sample_distribution=SampleDistribution(sample,
110
                                                                                                      categorical_value="OK"))
111
        self.assertEqual(sampling_distribution.distribution_family, DistributionFamily.simulation)
112
        print('sampling distribution: (point_estimate = ' + str(sampling_distribution.point_estimate)
113
              + ', standard_error = ' + str(sampling_distribution.standard_error) + ')')
114
        print('confidence level for 95% confidence level: ' + str(sampling_distribution.confidence_interval(0.95)))
115
116
117
if __name__ == '__main__':
118
    unittest.main()
119