-
Notifications
You must be signed in to change notification settings - Fork 0
/
statsWorkspace.py
241 lines (206 loc) · 6.04 KB
/
statsWorkspace.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
the_list = []
class XY:
def __init__(self, x, y):
self.x = x
self.y = y
def __str__(self):
return f"({self.x},{self.y})"
def insertion_sort(alist):
"""Returns sorted list"""
for j in range(1, len(alist)):
key = alist[i]
i = j-1
while(alist[i] > key and i > 0):
alist[i+1] = alist[i]
i = i - 1
alist[i+1] = key
return alist
#clears the grid and then prints it
def cleardata():
"""Clears data"""
global the_list
the_list = []
print(the_list)
print("list cleared")
def input_data():
"""Inputs the data"""
global the_list
print("Enter data points separated by tabs (type 'x' to finish):")
while True:
user_input = input("Enter data points (or 'x' to finish): ")
if user_input.lower() == 'x':
break
try:
data_points = [float(point) for point in user_input.split('\t')]
the_list.extend(data_points)
except ValueError:
print("Invalid input. Please enter valid numbers separated by tabs.")
def input_xy():
"""Inputs data in XY pairs"""
global the_list
print("Enter data in xy pairs separated by a comma. tabs to separate data(type 'x' to finish):")
while True:
u_in = input("Enter data:")
if u_in.lower() == 'x':
break
try:
for s in u_in.split('\t'):
try:
x,y = s.split(',')
xyP = XY(x=x,y=y)
the_list.append(xyP)
except SyntaxError:
("please enter x,y pair")
except ValueError:
print("Invalid Input")
def print_list():
"""Prints the list"""
print("[")
for n in the_list:
if n == len(the_list)-1:
print(n)
break
print(f"{n}, ")
print("]")
def s_mean():
"""Prints sample mean"""
global the_list
tot = 0.0
n = len(the_list)
for x in the_list:
tot += x
ret = tot/float(n)
print(f"The sample mean is: {ret} ")
return ret
def s_median():
"""Prints sample median"""
global the_list
sorted_list = sorted(the_list)
#print(sorted_list)
n = len(sorted_list)
if n%2 == 0:
middle1 = sorted_list[n // 2 - 1]
middle2 = sorted_list[n // 2]
median = (middle1 + middle2) / 2
else:
# If the length is odd, the median is the middle element
median = sorted_list[n // 2]
print(f"The median is: ",end='')
return median
#NOT WORKING
def trimmed_mean(trim):
"""Returns trimmed mean
:param trim: arg1,
:type trim: int
:return:trimmed mean
"""
global the_list
#check if list empty
if not the_list:
print("list empty")
return None
n = len(the_list)
#check if trim is within a valid range
if trim <= 0 or trim >= 100:
print("Error: trim should be a percentage between 0 and 100 exclusive.")
return None
how_many_to_trim = n * (float(trim) / 100)
if how_many_to_trim < 0.0:
trimup = int(-(-how_many_to_trim // 1))
trimdown = int(how_many_to_trim)
if trimdown < 0:
print("Error: trim value results in a negative k.")
return None
if n <= 2 * trimup:
print("Error: Not enough elements after trimming.")
return None
t_list_u = the_list[trimup:n-trimup]
t_list_d = the_list[trimdown:n-trimdown]
mean1 = sum(t_list_u)/float(n-2 * trimup)
mean2 = sum(t_list_d)/float(n-2 * trimdown)
ret = (mean1+mean2) / 2
else:
how_many_to_trim = int(how_many_to_trim)
# Check if how_many_to_trim is less than 0
if how_many_to_trim < 0:
print("Error: trim value results in a negative k.")
return None
# Check if there are enough elements after trimming
if n <= 2 * how_many_to_trim:
print("Error: Not enough elements after trimming.")
return None
t_list = the_list[how_many_to_trim:n-how_many_to_trim]
# find mean of trimmed list
tot = sum(t_list)
ret = tot / float(n - 2 * how_many_to_trim)
print(f"A {trim}% trimmed mean is: {ret}")
return ret
def skew():
"""Prints skew"""
global the_list
mean = s_mean()
median = s_median()
if mean > median:
print("negative skew")
elif median < mean:
print("positive skew")
else:
print("no skew")
def s_sum():
"""Prints the sample sum"""
tot = 0
for x in the_list:
tot += x
return x
def s_range():
"""Prints the sample range"""
sorted_list = sorted(the_list)
hi = sorted_list[-1]
lo = sorted_list[0]
print(f"The highest value is: {hi}")
print(f"The lowest value is: {lo}")
print(f"Range is: {(hi-lo)}")
return (hi-lo)
def s_variance():
"""Prints sample variance"""
global the_list
if not the_list:
print("Error: the_list is empty.")
return None
n = len(the_list)
# Calculate the mean
mean = sum(the_list) / n
# Calculate the sum of squared differences from the mean
sum_squared_diff = sum((x - mean) ** 2 for x in the_list)
# Divide by (n-1) for sample variance
variance = sum_squared_diff / (n - 1)
return variance
def s_std():
"""Prints the sample standard deviation"""
v = s_variance()
return v**0.5
def sum_sqr():
"""Prints the sum of squares"""
global the_list
sum_squares = sum(entry ** 2 for entry in the_list)
return sum_squares
def riemann_sum():
"""Prints the """
def s_stats():
"""Prints all sample stats functions. Specifically:
s_mean()
s_median()
s_sum()
s_range()
s_variance()
s_std()
"""
print("Printing sample stats")
print("-------------------------------------")
s_mean()
s_median()
s_sum()
s_range()
s_variance()
s_std()
print("-------------------------------------")