-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.py
More file actions
188 lines (154 loc) · 5.66 KB
/
Copy pathstatistics.py
File metadata and controls
188 lines (154 loc) · 5.66 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
@namespace("statistics")
from Promethium import List
from collections import Counter
# A small, opt-in subset of Python's statistics module, overloaded for
# `int`/`float` like `PromethiumBaseLibrary`'s own `sorted()`/`min()`/
# `max()` (see the note in `heapq.py`) — `median`/`variance`/`stdev` need
# ordering/arithmetic that unconstrained generic T doesn't have.
#
# `median` always returns `float`, unlike CPython's `statistics.median`
# (which returns the middle element's own type for odd-length input, and
# only promotes to `float` for the even-length average) — a Promethium
# function can't return one type or another depending on a runtime-only
# condition, so this always returns `float`, matching every case's numeric
# *value* even where CPython would give back an `int`.
#
# `sorted(...)`/`sqrt(...)` are called fully qualified (`Promethium.sorted`,
# `math.sqrt`) rather than bare, following the same pattern `heapq.py`
# established for calling a concrete (non-generic) function from another
# namespace — bare calls to those aren't reliably resolved via ambient
# `DefaultUses` opening the way `len(...)` is.
#
# `mode` is generic — it just needs `T` to support equality (via
# `collections.Counter`, not needing anything of its own beyond that), not
# ordering.
def mean(data: List[int]) -> float:
total: float = 0.0
index: int = 0
while index < len(data):
total += data.__getitem__(index)
index += 1
return total / len(data)
def mean(data: List[float]) -> float:
total: float = 0.0
index: int = 0
while index < len(data):
total += data.__getitem__(index)
index += 1
return total / len(data)
def median(data: List[int]) -> float:
ordered: List[int] = Promethium.sorted(data)
count: int = len(ordered)
middle: int = count / 2
if count % 2 == 1:
return ordered.__getitem__(middle)
lower: int = ordered.__getitem__(middle - 1)
upper: int = ordered.__getitem__(middle)
return (lower + upper) / 2.0
def median(data: List[float]) -> float:
ordered: List[float] = Promethium.sorted(data)
count: int = len(ordered)
middle: int = count / 2
if count % 2 == 1:
return ordered.__getitem__(middle)
lower: float = ordered.__getitem__(middle - 1)
upper: float = ordered.__getitem__(middle)
return (lower + upper) / 2.0
def median_low(data: List[int]) -> int:
ordered: List[int] = Promethium.sorted(data)
count: int = len(ordered)
middle: int = count / 2
if count % 2 == 1:
return ordered.__getitem__(middle)
return ordered.__getitem__(middle - 1)
def median_low(data: List[float]) -> float:
ordered: List[float] = Promethium.sorted(data)
count: int = len(ordered)
middle: int = count / 2
if count % 2 == 1:
return ordered.__getitem__(middle)
return ordered.__getitem__(middle - 1)
def median_high(data: List[int]) -> int:
ordered: List[int] = Promethium.sorted(data)
middle: int = len(ordered) / 2
return ordered.__getitem__(middle)
def median_high(data: List[float]) -> float:
ordered: List[float] = Promethium.sorted(data)
middle: int = len(ordered) / 2
return ordered.__getitem__(middle)
def harmonic_mean(data: List[int]) -> float:
total: float = 0.0
index: int = 0
while index < len(data):
total += 1.0 / data.__getitem__(index)
index += 1
return len(data) / total
def harmonic_mean(data: List[float]) -> float:
total: float = 0.0
index: int = 0
while index < len(data):
total += 1.0 / data.__getitem__(index)
index += 1
return len(data) / total
def geometric_mean(data: List[int]) -> float:
total: float = 0.0
index: int = 0
while index < len(data):
total += math.log(data.__getitem__(index))
index += 1
return math.exp(total / len(data))
def geometric_mean(data: List[float]) -> float:
total: float = 0.0
index: int = 0
while index < len(data):
total += math.log(data.__getitem__(index))
index += 1
return math.exp(total / len(data))
def mode[T](data: List[T]) -> T:
counts: Counter[T] = Counter[T](data)
top: List[tuple[T, int]] = counts.most_common(1)
return top.__getitem__(0)[0]
def pvariance(data: List[int]) -> float:
average: float = mean(data)
total: float = 0.0
index: int = 0
while index < len(data):
difference: float = data.__getitem__(index) - average
total += difference * difference
index += 1
return total / len(data)
def pvariance(data: List[float]) -> float:
average: float = mean(data)
total: float = 0.0
index: int = 0
while index < len(data):
difference: float = data.__getitem__(index) - average
total += difference * difference
index += 1
return total / len(data)
def variance(data: List[int]) -> float:
average: float = mean(data)
total: float = 0.0
index: int = 0
while index < len(data):
difference: float = data.__getitem__(index) - average
total += difference * difference
index += 1
return total / (len(data) - 1.0)
def variance(data: List[float]) -> float:
average: float = mean(data)
total: float = 0.0
index: int = 0
while index < len(data):
difference: float = data.__getitem__(index) - average
total += difference * difference
index += 1
return total / (len(data) - 1.0)
def pstdev(data: List[int]) -> float:
return math.sqrt(pvariance(data))
def pstdev(data: List[float]) -> float:
return math.sqrt(pvariance(data))
def stdev(data: List[int]) -> float:
return math.sqrt(variance(data))
def stdev(data: List[float]) -> float:
return math.sqrt(variance(data))