forked from urmilshroff/sampling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampling.py
More file actions
174 lines (116 loc) · 5.48 KB
/
Copy pathsampling.py
File metadata and controls
174 lines (116 loc) · 5.48 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#Testing of Hypothesis by Urmil Shroff
try:
import math
from scipy import stats
except ImportError:
print("\n***WARNING: THIS PROGRAM MAY NOT RUN CORRECTLY***\n\n1. Please make sure you have properly installed Python 3.6 from www.python.org\n2. After doing so, run 'python' in terminal\n3. Then type: 'pip install scipy'\n4. Now try again! If the problem still persists, close your computer and go back to sleep.")
def start():
num=int(input("Enter number of Samples:\n"))
if (num==1):
n=float(input("Enter size of the Sample:\n"))
if (n>=30):
large_sample(1,n,0) #single large sample
else:
small_sample(1,n,0) #single small sample
elif (num==2):
n1=float(input("Enter size of the first Sample:\n"))
n2=float(input("Enter size of the second Sample:\n"))
if (n1>=30) and (n2>=30):
large_sample(2,n1,n2) #double large sample
else:
small_sample(2,n1,n2) #double small sample
else:
print("Sorry, only one or two samples can be tested!")
def large_sample(num,n1,n2):
if(num==1): #single large sample
print("\nLarge Sample Test (one Sample)")
n=n1
x=float(input("Enter mean of the Sample:\n"))
u=float(input("Enter mean of the Population:\n"))
sd=float(input("Enter Standard Deviation:\n"))
z_a=los_calc_z(int(input("Enter LOS:\n")),int(input("Is the hypothesis one tailed or two tailed?\n")))
z=((x-u)/(sd/math.sqrt(n)))
print("\nZ =",z,"\nZa =",z_a)
if(z<0):
z=-z
if(z>z_a):
print("\nNull hypothesis rejected, alternate hypothesis accepted!\n")
elif(z<z_a):
print("\nNull hypothesis accepted, alternate hypothesis rejected!\n")
elif(num==2): #double large sample
print("\nLarge Sample Test (two Samples)")
yesno=input("Is Population SD the same for both populations? Y/N:\n")
if(yesno=="y") or (yesno=="Y"):
yesno=input("Is Population SD known? Y/N:\n")
if(yesno=="y") or (yesno=="Y"): #case 2
x1=float(input("Enter mean of the first Sample:\n"))
x2=float(input("Enter mean of the second Sample:\n"))
popsd=float(input("Enter common SD of the Populations:\n"))
z=case_z2(n1,n2,x1,x2,popsd)
else: #case 3
x1=float(input("Enter mean of the first Sample:\n"))
x2=float(input("Enter mean of the second Sample:\n"))
s1=float(input("Enter SD of the first Sample/Population:\n"))
s2=float(input("Enter SD of the second Sample/Population:\n"))
z=case_z3(n1,n2,x1,x2,s1,s2)
else: #case 1
x1=float(input("Enter mean of the first Sample:\n"))
x2=float(input("Enter mean of the second Sample:\n"))
s1=float(input("Enter SD of the first Sample/Population:\n"))
s2=float(input("Enter SD of the second Sample/Population:\n"))
z=case_z1(n1,n2,x1,x2,s1,s2)
z_a=los_calc_z(int(input("Enter LOS:\n")),int(input("Is the hypothesis one tailed or two tailed?\n")))
if(z<0):
z=-z
print("\nZ =",z,"\nZa =",z_a)
if(z>z_a):
print("\nNull hypothesis rejected, alternate hypothesis accepted!\n")
elif(z<z_a):
print("\nNull hypothesis accepted, alternate hypothesis rejected!\n")
else:
print("Error, only 1 or 2 Samples can be predicted!")
def small_sample(num,n1,n2):
if(num==1):
print("\nSmall Sample Test (one Sample)")
n=n1
x=float(input("Enter mean of the Sample:\n"))
u=float(input("Enter mean of the Population:\n"))
sd=float(input("Enter Standard Deviation:\n"))
t_a=los_calc_t(int(input("Enter LOS:\n")),n-1,num)
t=((x-u)/(sd/math.sqrt(n-1)))
if(t<0):
t=-t
print("\nT =",t,"\nTa =",t_a)
if(t>t_a):
print("\nNull hypothesis rejected, alternate hypothesis accepted!\n")
elif(t<t_a):
print("\nNull hypothesis accepted, alternate hypothesis rejected!\n")
elif(num==2): # TODO: fix this
print("\nSorry, small sample testing for two samples doesn't work yet! Submit a pull request if you can use SciPy to make it work.")
else:
print("Error, only 1 or 2 Samples can be predicted!")
def los_calc_z(los,tails): #LOS calculator for z-test (used for Large Samples)
if(los==5):
if(tails==2):
return 1.96 #two-tailed @ 5% LOS
else:
return 1.65 #one-tailed @ 5% LOS
elif(los==1):
if(tails==2):
return 2.58 #two-tailed @ 1% LOS
else:
return 2.33 #one-tailed @ 1% LOS
def los_calc_t(los,n,num): #LOS calculator for t-test (used for Small Samples)
if(num==1):
return stats.t.ppf((1-(los/100)),n)
elif(num==2): #TODO: fix this
print("WTF how did you call me?")
#small double population formula: t(n1+n2-2,t_a)
def case_z1(n1,n2,x1,x2,s1,s2): #population sds not known, so we take s1, s2
return ((x1-x2)/(math.sqrt(((s1**2)/n1)+((s2**2)/n2))))
def case_z2(n1,n2,x1,x2,popsd): #population sds are known and they are the same
return ((x1-x2)/(popsd*(math.sqrt((1/n1)+(1/n2)))))
def case_z3(n1,n2,x1,x2,s1,s2): #population sds not known but they are said to be the same
return ((x1-x2)/(math.sqrt(((s1**2)/n2)+((s2**2)/n1))))
print("\nTesting of Hypothesis by Urmil Shroff\n")
start()