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
|
|
View Code Duplication |
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
|
|
|
if Z < 0: |
28
|
|
|
pf -= 0.5 |
29
|
|
|
self.p_value_one_tail = 1 - pf |
30
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
31
|
|
|
else: |
32
|
|
|
standard_error_null = sampling_distribution.standard_error |
33
|
|
|
td_df = (sampling_distribution.point_estimate - mean_null) / standard_error_null |
34
|
|
|
self.test_statistic = td_df |
35
|
|
|
pf = t.cdf(td_df, sampling_distribution.df) |
36
|
|
|
if td_df < 0: |
37
|
|
|
pf -= 0.5 |
38
|
|
|
self.p_value_one_tail = 1 - pf |
39
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
40
|
|
|
|
41
|
|
|
if significance_level is not None: |
42
|
|
|
self.reject_mean_null = (self.p_value_one_tail < significance_level, |
43
|
|
|
self.p_value_two_tail < significance_level) |
44
|
|
|
|
45
|
|
|
def will_reject(self, significance_level): |
46
|
|
|
|
47
|
|
|
return self.p_value_one_tail < significance_level, self.p_value_two_tail < significance_level |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
class ProportionTesting(object): |
51
|
|
|
sampling_distribution = None |
52
|
|
|
p_value_one_tail = None |
53
|
|
|
p_value_two_tail = None |
54
|
|
|
mean_null = None |
55
|
|
|
test_statistic = None |
56
|
|
|
significance_level = None |
57
|
|
|
reject_mean_null = None |
58
|
|
|
|
59
|
|
View Code Duplication |
def __init__(self, sampling_distribution, p_null, significance_level=None): |
|
|
|
|
60
|
|
|
self.sampling_distribution = sampling_distribution |
61
|
|
|
self.p_null = p_null |
62
|
|
|
if significance_level is not None: |
63
|
|
|
self.significance_level = significance_level |
64
|
|
|
|
65
|
|
|
if self.sampling_distribution.distribution_family == DistributionFamily.normal: |
66
|
|
|
standard_error_null = math.sqrt(p_null * (1 - p_null) / sampling_distribution.sample_size) |
67
|
|
|
Z = (sampling_distribution.point_estimate - p_null) / standard_error_null |
68
|
|
|
self.test_statistic = Z |
69
|
|
|
pf = norm.cdf(Z) |
70
|
|
|
if Z < 0: |
71
|
|
|
pf -= 0.5 |
72
|
|
|
self.p_value_one_tail = 1 - pf |
73
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
74
|
|
|
else: |
75
|
|
|
standard_error_null = math.sqrt(p_null * (1 - p_null) / sampling_distribution.sample_size) |
76
|
|
|
td_df = (sampling_distribution.point_estimate - p_null) / standard_error_null |
77
|
|
|
self.test_statistic = td_df |
78
|
|
|
pf = t.cdf(td_df, sampling_distribution.df) |
79
|
|
|
if td_df < 0: |
80
|
|
|
pf -= 0.5 |
81
|
|
|
self.p_value_one_tail = 1 - pf |
82
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
83
|
|
|
|
84
|
|
|
if significance_level is not None: |
85
|
|
|
self.reject_mean_null = (self.p_value_one_tail < significance_level, |
86
|
|
|
self.p_value_two_tail < significance_level) |
87
|
|
|
|
88
|
|
|
|