Completed
Push — master ( f1f19a...ab9d67 )
by Xianshun
01:10
created

MeanTesting.__init__()   B

Complexity

Conditions 4

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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