forked from TeddovanMierle/ML-Methods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT-tests.py
58 lines (48 loc) · 1.86 KB
/
T-tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pandas as pd
import scipy.stats as stats
import numpy as np
class t_test():
"""
A class containing methods that perform various t-tests
Parameters
----------
data1 : (array) array of data of interest
data2 : (array) [optional] array of data of interest, only need to pass it for two sample test
"""
def __init__(self, data1, data2=None) -> None:
self.data1 = data1
self.data2 = data2
def one_sample_t_test(self, population_mean, side):
"""
Perform one sample t-test with a side and population mean
Parameters
----------
population_mean : (float) population mean to be tested
side : (str) only allows 'two-sided', 'less', 'greater', side of the test to perform
Returns
-------
t-statistic (float)
"""
if side not in ['two-sided', 'less', 'greater']:
raise Exception("Only accept 'two-sided', 'less', or 'greater' for parameter 'side'")
return stats.ttest_1samp(self.data1, population_mean, alternative=side)
def two_sample_t_test(self, side):
"""
Perform two sample t-test between data1 and data2
Parameters
----------
side : (str) only allows 'two-sided', 'less', 'greater', side of the test to perform
Returns
-------
t-statistic (float)
"""
if side not in ['two-sided', 'less', 'greater']:
raise Exception("Only accept 'two-sided', 'less', or 'greater' as a parameter")
return stats.ttest_ind(self.data1, self.data2, alternative=side)
def paired_sample_t_test(self):
"""Perform paired sample t-test between data1 and data2
Returns
-------
t-statistic (float)
"""
return stats.ttest_rel(self.data1, self.data2)