-
Notifications
You must be signed in to change notification settings - Fork 0
/
stat_with_python.py
521 lines (343 loc) · 9.52 KB
/
stat_with_python.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 31 14:06:53 2020
@author: user
"""
# statistic with python
import numpy as np
import scipy.stats as spstats
import matplotlib.pyplot as plt
import seaborn as sns
# central tendency
# create a dataset of 100 numbers (10-200)
data = np.random.randint(10,200,100)
len(data)
# mean
np.mean(data)
#median
np.median(data)
#wighted mean
#problem
# create a matrix of size 5x5 containing data between 1-100 that indicates a score
# weights for the columns are:
'''
leadership quality
decision making,
technical skills
interpersonal skills
managerial skills
'''
# create scores for 5 employees
# calc the weighted mean of each employee using the given weights for the scores
# calc. the weightage average of each employee
data=np.random.randint(1,100,25).reshape(5,5)
data
# weights
weights =
weights=np.array([0.35,0.2,0.1,0.1,0.2])
# weightage average
np.sum(data*weights,axis=1)
# geometric mean
data=np.round(np.random.uniform(100,120,5),2)
data
np.array([])
############################################################################################
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 31 15:07:45 2020
@author: user
"""
# statistic with python
import numpy as np
import scipy.stats as spstats
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
# central tendency
# create a dataset of 100 numbers (10-200)
data = np.random.randint(10,200,100)
len(data)
# mean
np.mean(data)
#median
np.median(data)
#wighted mean
#problem
# create a matrix of size 5x5 containing data between 1-100 that indicates a score
# weights for the columns are:
'''
leadership quality
decision making,
technical skills
interpersonal skills
managerial skills
'''
# create scores for 5 employees
# calc the weighted mean of each employee using the given weights for the scores
# calc. the weightage average of each employee
data=np.random.randint(1,100,25).reshape(5,5)
data
# weights
weights =
weights=np.array([0.35,0.2,0.1,0.1,0.2])
# weightage average
np.sum(data*weights,axis=1)
# geometric mean
data=np.round(np.random.uniform(100,120,5),2)
data
ans=1
for x in data:
ans = ans*x
print(ans)
import sympy
sympy.integer_nthroot(int(ans),len(data))
# mode
# find the mode of given dataset
data=np.random.randint(1,6,50)
data
u_data = np.unique(data)
u_data
for i in u_data:
ctr = list(data).count(i)
print("number {}occur {} times".format(i,ctr))
### spread
data = np.random.randint(14,100,50)
print(data)
# range
np.max(data)-np.min(data)
# quartiles/deciles
np.percentile(data,25)
# get all the quartiles
# 25%--->value
# 50%--->value
# 75%--->value
perc = np.arange(25,76,25)
perc
np.percentile(data,perc)
# iqr
quartiles = np.percentile(data,perc)
quartiles[2] - quartiles[0]
# get all the deciles
perc = np.arange(10,101,10)
perc
deciles = np.percentile(data,perc)
# variance and standard deviation
np.var(data)
np.std(data)
# measure of association
# covariance and corelation (diff. bet.covariance,corelation)
X=np.round(np.random.uniform(10,70,6),1)
Y=np.round(np.random.uniform(1,5,6),1)
len(X)
len(Y)
# what is the relation between X and Y
# covariance --> measure of direction
np.cov(X,Y)
# correlation --> measure of strength
np.corrcoef(X,Y)
########################################################
# INFERENTIAL STATISTIC
# hypothesis testing
# given z-score, what is the area
spstats.norm.cdf(1.96)
# given the area what is the z-score
spstats.norm.ppf(0.95)
# 1-sample t-test (t-test done when sample size less than 30)
# problem statement: the average weight is 30kg
weights=np.round(np.random.uniform(16,60,11),2)
weights
# h0: mean is 30
# h1: mean is not 30
tail=2
tt= spstats.ttest_1samp(weights,30)
print(tt)
# Calculated
tcalc = tt[0]; tcalc
pval = tt[1]; pval
# Critical
alpha=0.05
dof = len(weights)-1
dof
# if its a 2-tail test, then split the alpha
if tail==2:
alpha/=2
alpha
# find the t-critical
tcritical = abs(spstats.t.ppf(alpha,dof))
tcritical
# interpretation
# 1) check tcalculated and tcritical
if tcalc > tcritical:
print("reject h0")
else:
print("fail to reject h0")
# OR
# ii) check pvalue and alpha
if pval < alpha:
print("reject h0")
else:
print("fail to reject h0")
########################################################
# Z-TEST (sample size is greter than 30)
# the average IQ of 100 people in the class is more than 95. test this sample at 95% CI
# claim: mean > 95
# opposite: mean<=95
# h0: mean = 95
# h1: mean > 95
tail = 1
iqscores = np.random.randint(90,180,100)
iqscores
z_test= spstats.ttest_1samp(iqscores,95)
print(z_test)
zcalc = z_test[0]; zcalc
pval = z_test[1]; pval
# if its a 2-tail test, then split the alpha
if tail==2:
alpha/=2
alpha
# find the z-critical
zcritical = spstats.norm.ppf(1-alpha)
zcritical
# interpretation
# 1) check zcalculated and zcritical
if zcalc > zcritical:
print("reject h0")
else:
print("fail to reject h0")
#################################################################3################
# assignment
# write python functions to do the 2-sample hypothesis testing for means and proportions
# use the same data
#2 2 sample test for variance are same
n1=249;n2=79;x1=20.14458;x2=30.48101;s1=6.41470;s2=6.10771
def variance(n1,n2,x1,x2,s1,s2,alpha=0.05,tail=2):
n = (n1-1)*np.square(s1) + (n2-1)*np.square(s2)
d = (n1+n2-2)
sp = np.sqrt(n/d)
tnumerator = x1-x2
tdenominator = sp*np.sqrt((1/n1)+(1/n2))
tstat = abs(tnumerator/tdenominator)
if tail ==2:
alpha/= 2
zcritical = spstats.norm.ppf(1-alpha)
if total > zcritical:
print("Reject H0")
else :
print("Fail to reject H0")
print("2sample mean")
return(tstat)
total = variance(249,79,20.14458,30.48101,6.41470,6.10771)
print("tstat={}".format(total))
i) assignment
#write function to do the 2sample hypothesis testing for means and proportion
#use the same data that was used in a class exercise.
'''2 sampe test for mean
proportion
'''
#2 sample test for mean variance are different
'''
H0:population mean are same
H1:population mean are not same'''
def sample(n1,n2,x1,x2,std1,std2):
n=x1-x2
d=np.square(std1)/75 + np.square(std2)/50
tt = n/np.sqrt(d)
tail=2
alpha = 0.05
if tail ==2:
alpha/= 2
zcritical = spstats.norm.ppf(1-alpha)
if total > zcritical:
print("Reject H0")
else :
print("Fail to reject H0")
print("2sample mean")
return(abs(tt))
total = sample(75,50,28,33,14.1,9.5)
print("tt={}".format(total))
#2 2 sample test for variance are same
#3Testing equality of 2 population proportions
'''
H0:population mean are same
H1:population mean are not same'''
n1=800;n2=1000;x1=200;x2=350
def proportion(n1,n2,x1,x2):
P1=x1/n1
P2=x2/n2
P = (x1+x2)/(n1+n2)
Q = 1-P
d=P*Q*(1/n1)+(1/n2)
tt = P1-P2 / np.sqrt(d)
tail=2
alpha = 0.05
if tail ==2:
alpha/= 2
zcritical = spstats.norm.ppf(1-alpha)
if total > zcritical:
print("Reject H0")
else :
print("Fail to reject H0")
print("2sample mean for proportion")
return(abs(tt))
total = proportion(800,1000,200,350)
print("tt={}".format(total))
# ANOVA testing
# create a dataset to store the petrol prices of 10 days of 5 indian cities
# claim: prices are similer
# H0: mu1=mu2=mu3=mu4=mu5
# H1: mu1<>mu2....
# create a list to store the petrol prices
delhi = np.round(np.random.uniform(69,99,10),1)
mumbai = np.round(np.random.uniform(70,96,10),1)
pune = np.round(np.random.uniform(68,92,10),1)
kolkata = np.round(np.random.uniform(67,88,10),1)
chennai = np.round(np.random.uniform(70,93,10),1)
# ANOVA test
ftest = spstats.f_oneway(delhi,mumbai,pune,kolkata,chennai)
fcalc = ftest[0]
pvalue = ftest[1]
print('fcalculated={},pvalue={}'.format(f_critical,pvalue))
pcritical= 0.05
# checking p-values
if pcalc < pcritical:
print("reject h0")
else:
print("fail to reject h0")
# find out the fcritical
dof_n = 4 # (number of group - 1)
dof_d = 45 # (from each sample, substract 1)
# alpha = 0.05
fcritical=spstats.f.ppf(1-0.05, dfn=dof_n,dfd=dof_d)
print(fcritical)
# checking the fscore
if fcalc > fcritical:
print("reject h0")
else:
print("fail to reject h0")
#######################################################################
# CHI-SQARE TEST
# maleria cases in 2 cities
city=['mumbai']*500 + ['delhi']*400
cases=['yes']*12 + ['no']*488 + ['yes']*7 + ['no']*393
data=pd.DataFrame({'city':city,'maleria':cases})
data
# cross tab of the data
ct = pd.crosstab(data.city,data.maleria)
# chisquare test
chisq = spstats.chi2_contingency(ct)
chisq
# OUPUT
# I) P-value
pcalc = chisq[0]
pcalc
pcrit = 0.05
# 2) chi square
chisq_calc = chisq[1]; chisq_calc
# 3) DOF
# 4) expected value matrix
if (pcalc < pcrit):
print('reject h0')
print('cases are dependent')
else:
print('failed to reject h0')
print('cases are independent')