-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMoCT.py
241 lines (192 loc) · 7.86 KB
/
MoCT.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# -*- coding: utf-8 -*-
#!/usr/bin/python
# Author: Niam Moltta
# UY - 2017
# MIT License
# Measures of Central Tendency with Python
import math
import numpy as np
import pandas as pd
from scipy import stats
from scipy.stats import norm
import scipy.stats as st
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import re
import seaborn
import scipy.stats
print(' ')
print(' ')
print(' Welcome to MoCT.py')
print(' --by Niam Moltta--')
print (' ~~/\//V\ ')
print(' ')
print(' ')
print("Application: MEASURES OF CENTRAL TENDENCY.\n\nINSTRUCTIONS:\n\n- Make sure that the .csv file is in the same folder of this script.\n- To start, enter the name of the file without 'quotes' and ending with .csv\n Example: scores.csv\n- Enter 'ya' to analyze more columns or quit.\n- Returns measures of central tendency:\n N, mean, standard deviation, variance, standard error, etc...\n- Returns Normal Distribution graph.\n- Select sample, select point estimate for sampling distribution.\n- Returns z-score and p-value from z-table.\n- Returns Sampling Distribution graph.\n- Returns One tailed T-test: default alpha = 0.05\n- Returns acceptance/rejection of the null hypothesis.\n")
fhand = raw_input('Enter file name: ')
filecsv = str(fhand)
if filecsv == '':
print(' ')
print ('Ciao, human!')
print(' ')
exit()
data = pd.read_csv(filecsv)
print ' '
frame = pd.DataFrame(data)
coolist = frame.columns
columns = np.asarray(coolist)
while True:
print ' '
print 'Columns in', re.findall('(.+?).csv', filecsv), 'are:\n'
print columns
print ' '
hand = raw_input('Enter column header:\n\n')
column = str(hand)
if (column == 'ya') | (column == ''):
print ' '
print 'Hasta la vista, human.'
print ' '
exit()
else:
numbers = data[column]
data[column].fillna(0,inplace=True) # Missing values to zeros.
A = list()
for number in numbers :
value = float(number)
A.append(value)
sigma = sum(A) #sumation
n = len(A) #total of elements
mean = sigma / n
Dev = list()
AbsDev = list()
SqDev = list()
for number in A :
val = number - mean
Dev.append(val) #Deviation from the mean
for element in Dev :
val = abs(element)
AbsDev.append(val) # Absolute Deviation
for element in AbsDev :
val = (element**2)
SqDev.append(val) #Square Deviations
SS = sum(SqDev) #Sum of Squares
Var = SS / n #Variance
StdDev = math.sqrt(Var) #Standard Deviation
StdE = StdDev / math.sqrt(n) #Standard Error
print ('---------------------------------------------')
print ('MEASURES OF CENTRAL TENDENCY for:'), column
print (' ')
print ('N ='), n
print ('Mean ='), mean
print ('Sum of Squares ='), SS
print ('Variance ='), Var
print ('Standard Deviation ='), StdDev
print ('Standard Error ='), StdE
print ('---------------------------------------------')
print(' ')
Array = np.asarray(A)
lista = Array.sort()
legend = str(column) + ' distribution'
Mean = np.mean(Array)
Variance = np.var(Array)
Sigma = np.sqrt(Variance)
plt.figure(1)
plt.hist(Array, bins=20, normed=True)
plt.xlim((min(Array), max(Array)))
x = np.linspace(min(Array), max(Array), n)
fig = plt.plot(x, mlab.normpdf(x,Mean,Sigma))
plt.title(legend)
plt.xlabel("Value")
plt.ylabel("Frequency")
plt.show(fig)
print ('To continue, you must save the figure and close it. \nYou can also zoom in it or move the graph to see it better, \nuse the buttons.\n')
print ' '
while True:
sample = raw_input('Enter n value for sample distribution: ')
fh = str(sample)
if fh[0] == '#':
continue
if (fh == 'ya') | (fh == ''):
print ' '
break
newn = float(fh)
standarderror = StdDev / math.sqrt(newn)
print('----------------------------------------------------------------')
print 'The Standard Error for', newn,'is: ', standarderror
print('----------------------------------------------------------------')
print ' '
anyvalue = raw_input('Enter point estimate: ')
if anyvalue[0] == '#':
continue
if anyvalue == '':
print ' '
break
newmean = float(anyvalue)
rest = newmean - mean
zscore = rest / standarderror
print ' '
print '| The z-score for', anyvalue, 'is:', zscore, '|'
pvalue = st.norm.cdf(zscore)
print('---------------------------------------------')
print '| Z table value =', pvalue, '|'
print('---------------------------------------------')
prob = 1 - pvalue
print 'The probability of getting at least', anyvalue,'is:'
print ' '
print 'p =', prob
print ' '
print '-------------------------------------------------------------'
Devs = (1.96 * standarderror)
print 'Approximately 95% of the sample means fall within', Devs, '\nof', mean, '(Population mean)'
Dev1 = mean - Devs
Dev2 = mean + Devs
print ' '
print 'The 95% confidence interval is:', Dev1,'<', newmean, '<', Dev2
print ' '
Devss = (2.33 * standarderror)
''' Some notes for T-test:
Ho = (null hypothesis):
Mean = Intervention Mean:
- The sample mean falls somewhere out the critical region.
Ha = (alternative hypothesis):
Mean != Intervention Mean (two-tailed),
Mean < Intervention Mean (one-tailed - right side),
Mean > Intervention Mean (one-tailed - left side):
- The sample mean falls somewhere in the critical region.'''
Dev3 = mean - Devss
Dev4 = mean + Devss
print 'The 98% confidence interval is:', Dev3,'<', newmean, '<', Dev4
print ' '
print 'The margin of error is:', Devs, 'for 95% \nand:', Devss,'for 98%'
print '--------------------------------------------------------------'
print ' '
if (zscore < 1.96)&(zscore > -1.96):
print 'Alpha = 0.05\nHo = Accepted' # Alpha is 0.05 because is used more often
print ' '
print '--------------------------------------------------------------'
else:
print 'Alpha = 0.05\nHo = Rejected'
print ' '
print '--------------------------------------------------------------'
pdf2 = stats.norm.pdf(Array, mean, standarderror)
altn = int(newn)
legen = ("n = "+str(altn))
fig2 = plt.plot(Array, pdf2, label=legen)
plt.title("Sampling distribution\nalpha = 0.05")
plt.xlabel("Value")
plt.ylabel("Frequency")
altm = str(mean)
legenda = ("Mean =\n "+altm)
plt.axvline(x= mean, color='r', linestyle='dashed', label=legenda)
plt.axvline(x= Dev1, color ='g', linestyle='dashed', label=Dev1)
plt.axvline(x= Dev2, color = 'g', linestyle='dashed', label=Dev2)
zscorev = mean+(zscore*standarderror)
zscor = ("z-score =\n"+str(zscore))
plt.axvline(x= zscorev, color = 'purple', label=zscor)
print ('To continue, you must save the figure and close it, or just close it. \nYou can also zoom in it or move the graph to see it better, \nuse the buttons.\n')
plt.legend()
plt.show(fig2)
continue
print(' ')
print 'Ciao, human!'
print(' ')