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
|
|
|
def __init__(self, sampling_distribution, significance_level=None): |
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
|
|
|
self.p_value_one_tail = 1 - pf |
24
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
25
|
|
|
else: |
26
|
|
|
td_df = sampling_distribution.point_estimate / sampling_distribution.standard_error |
27
|
|
|
self.test_statistic = td_df |
28
|
|
|
pf = t.cdf(td_df, sampling_distribution.df) |
29
|
|
|
self.p_value_one_tail = 1 - pf |
30
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
31
|
|
|
|
32
|
|
|
if significance_level is not None: |
33
|
|
|
self.reject_mean_same = (self.p_value_one_tail < significance_level, |
34
|
|
|
self.p_value_two_tail < significance_level) |
35
|
|
|
|
36
|
|
|
|
37
|
|
View Code Duplication |
class ProportionDiffTesting(object): |
|
|
|
|
38
|
|
|
sampling_distribution = None |
39
|
|
|
p_value_one_tail = None |
40
|
|
|
p_value_two_tail = None |
41
|
|
|
mean_null = None |
42
|
|
|
test_statistic = None |
43
|
|
|
significance_level = None |
44
|
|
|
reject_proportion_same = None |
45
|
|
|
|
46
|
|
|
def __init__(self, sampling_distribution, significance_level=None): |
47
|
|
|
self.sampling_distribution = sampling_distribution |
48
|
|
|
p_null = (sampling_distribution.grp1_point_estimate + sampling_distribution.grp2_point_estimate) / 2 |
49
|
|
|
self.p_null = p_null |
50
|
|
|
if significance_level is not None: |
51
|
|
|
self.significance_level = significance_level |
52
|
|
|
|
53
|
|
|
if self.sampling_distribution.distribution_family == DistributionFamily.normal: |
54
|
|
|
standard_error_null = math.sqrt(p_null * (1 - p_null) / sampling_distribution.sample_size) |
55
|
|
|
Z = sampling_distribution.point_estimate / standard_error_null |
56
|
|
|
self.test_statistic = Z |
57
|
|
|
pf = norm.cdf(Z) |
58
|
|
|
self.p_value_one_tail = 1 - pf |
59
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
60
|
|
|
else: |
61
|
|
|
standard_error_null = math.sqrt(p_null * (1 - p_null) / sampling_distribution.sample_size) |
62
|
|
|
td_df = sampling_distribution.point_estimate / standard_error_null |
63
|
|
|
self.test_statistic = td_df |
64
|
|
|
pf = t.cdf(td_df, sampling_distribution.df) |
65
|
|
|
self.p_value_one_tail = 1 - pf |
66
|
|
|
self.p_value_two_tail = self.p_value_one_tail * 2 |
67
|
|
|
|
68
|
|
|
if significance_level is not None: |
69
|
|
|
self.reject_proportion_same = (self.p_value_one_tail < significance_level, |
70
|
|
|
self.p_value_two_tail < significance_level) |