-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStats.py
More file actions
99 lines (87 loc) · 2.47 KB
/
Stats.py
File metadata and controls
99 lines (87 loc) · 2.47 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
import math
import matplotlib.pyplot as plt
class stats:
def __init__(self,x,px):
self.x = x
self.px = px
def mean(self):
sum = 0
for i,j in zip(self.x,self.px):
v1 = i*j
sum += v1
return round(sum,3)
def geometric_mean(self):
try:
sum=0
for i,j in zip(self.x,self.px):
v1 = math.log(i)*j
sum = v1
return sum
except ValueError:
print("Domain Error of log, Remove 0 or negative values")
def harmonic_mean(self):
if 0 in self.x:
print("Cannot divide by zero")
return
sum=0
for i,j in zip(self.x,self.px):
v1 = (1/i)*j
sum = v1
return 1/sum
def median(self):
sum= 0
index = 0
for j in self.px:
sum += j
if sum == 0.5:
ans = self.x[index]
return ans
elif sum>0.5:
ans = (self.x[index] + self.x[index - 1])/2
return ans
else:
pass
index += 1
def QD(self):
try:
sum= 0
index = 0
for j in self.px:
sum += j
if sum == 0.25:
q1 = self.x[index]
elif sum < 0.25:
q1 = (self.x[index] + self.x[index + 1])/2
if sum == 0.75:
q3 = self.x[index]
break
elif sum > 0.75:
q3 = (self.x[index] + self.x[index - 1])/2
break
else:
pass
index += 1
quartile_d = (q3 - q1)/2
return {'QuartileD':quartile_d,'Q3':q3,'Q1':q1}
except Exception:
print("Invalid Inputs")
def MD(self):
sum = 0
for i,j in zip(self.x,self.px):
sum += abs(i - self.mean())*j
return round(sum,3)
def m_(self,p):
sum = 0
if p == 1:
return self.mean()
for i,j in zip(self.x,self.px):
sum += (i**p)*j
return sum
def SD(self):
Var = self.m_(2) - self.mean()**2
return {'Variance':round(Var,3),'SD':round(math.sqrt(Var),3)}
def plot(self,xlabel,ylabel):
plt.plot(self.x,self.px)
plt.xlabel(f'{xlabel}')
plt.ylabel(f'{ylabel}')
plt.show()