Completed
Push — master ( b7c711...fe8c00 )
by Xianshun
01:10
created

MeanDiffTesting   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 75.76 %

Importance

Changes 0
Metric Value
dl 25
loc 33
rs 10
c 0
b 0
f 0
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B __init__() 25 25 6

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
from pysie.stats.distributions import DistributionFamily
2
from scipy.stats import norm, t
3
import math
4
5
6
class MeanDiffTesting(object):
7
    sampling_distribution = None
8
    p_value_one_tail = None
9
    p_value_two_tail = None
10
    test_statistic = None
11
    significance_level = None
12
    reject_mean_same = None
13
14 View Code Duplication
    def __init__(self, sampling_distribution, significance_level=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
15
        self.sampling_distribution = sampling_distribution
16
        if significance_level is not None:
17
            self.significance_level = significance_level
18
19
        if self.sampling_distribution.distribution_family == DistributionFamily.normal:
20
            Z = sampling_distribution.point_estimate / sampling_distribution.standard_error
21
            self.test_statistic = Z
22
            pf = norm.cdf(Z)
23
            if Z < 0:
24
                pf = 1 - pf
25
            self.p_value_one_tail = 1 - pf
26
            self.p_value_two_tail = self.p_value_one_tail * 2
27
        else:
28
            td_df = sampling_distribution.point_estimate / sampling_distribution.standard_error
29
            self.test_statistic = td_df
30
            pf = t.cdf(td_df, sampling_distribution.df)
31
            if td_df < 0:
32
                pf = 1 - pf
33
            self.p_value_one_tail = 1 - pf
34
            self.p_value_two_tail = self.p_value_one_tail * 2
35
36
        if significance_level is not None:
37
            self.reject_mean_same = (self.p_value_one_tail < significance_level,
38
                                     self.p_value_two_tail < significance_level)
39
40
41
class ProportionDiffTesting(object):
42
    sampling_distribution = None
43
    p_value_one_tail = None
44
    p_value_two_tail = None
45
    mean_null = None
46
    test_statistic = None
47
    significance_level = None
48
    reject_proportion_same = None
49
50 View Code Duplication
    def __init__(self, sampling_distribution, significance_level=None):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
51
        self.sampling_distribution = sampling_distribution
52
        p_null = (sampling_distribution.grp1_point_estimate + sampling_distribution.grp2_point_estimate) / 2
53
        self.p_null = p_null
54
        if significance_level is not None:
55
            self.significance_level = significance_level
56
57
        if self.sampling_distribution.distribution_family == DistributionFamily.normal:
58
            standard_error_null = math.sqrt(p_null * (1 - p_null) / sampling_distribution.sample_size)
59
            Z = sampling_distribution.point_estimate / standard_error_null
60
            self.test_statistic = Z
61
            pf = norm.cdf(Z)
62
            if Z < 0:
63
                pf = 1 - pf
64
            self.p_value_one_tail = 1 - pf
65
            self.p_value_two_tail = self.p_value_one_tail * 2
66
        else:
67
            standard_error_null = math.sqrt(p_null * (1 - p_null) / sampling_distribution.sample_size)
68
            td_df = sampling_distribution.point_estimate / standard_error_null
69
            self.test_statistic = td_df
70
            pf = t.cdf(td_df, sampling_distribution.df)
71
            if td_df < 0:
72
                pf = 1 - pf
73
            self.p_value_one_tail = 1 - pf
74
            self.p_value_two_tail = self.p_value_one_tail * 2
75
76
        if significance_level is not None:
77
            self.reject_proportion_same = (self.p_value_one_tail < significance_level,
78
                                           self.p_value_two_tail < significance_level)