-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStatsMath.ts
More file actions
183 lines (159 loc) · 4.33 KB
/
StatsMath.ts
File metadata and controls
183 lines (159 loc) · 4.33 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
import { QuantData, Report } from "../Types";
export const SpeakerAutoPoints = 5;
export const SpeakerTeleopPoints = 2;
export const AmpAutoPoints = 2;
export const AmpTeleopPoints = 1;
export const TrapPoints = 5;
export const ArtifactPoints = 3;
export const MotifArtifactPoints = 5;
export const OverflowArtifactPoints = 1;
export const DepotArtifactPoints = 1;
type Selector<T extends QuantData> = ((r: T) => number) | (keyof T & string);
function getSelection<T extends QuantData>(
selector: Selector<T>,
report: Record<string, any>,
) {
return typeof selector === "string"
? report.data[selector]
: selector(report.data);
}
/**
* Rounds to two decimal places
*
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function Round(n: number): number {
return Math.round(n * 100) / 100;
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function StandardDeviation(numbers: number[]) {
const mean = numbers.reduce((a, b) => a + b, 0) / numbers.length;
const variance =
numbers.reduce((a, b) => a + (b - mean) ** 2, 0) / numbers.length;
return Math.sqrt(variance);
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function NumericalTotal<T extends QuantData>(
selector: Selector<T>,
reports: Report<T>[],
) {
let sum = 0;
reports?.forEach((report) => (sum += getSelection(selector, report)));
return Round(sum);
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function MostCommonValue<T extends QuantData>(
selector: Selector<T>,
objects: Record<string, any>[],
) {
// Get a list of all values of the specified field
let values: string[] = [];
objects?.forEach((report) => {
const val = getSelection(selector, report);
values.push((val as any)?.toString?.() ?? JSON.stringify(val));
});
// Count the occurrences of each value
const occurences: { [key: string]: number } = {};
values.forEach((num) =>
occurences[num] ? (occurences[num] += 1) : (occurences[num] = 1),
);
// Return the most common value
const sortedValues = Object.keys(occurences).sort(
(a, b) => occurences[b] - occurences[a],
);
const mode = sortedValues[0];
return mode === "undefined" ? "Unknown" : mode;
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function BooleanAverage<T extends QuantData>(
selector: Selector<T>,
reports: Report<T>[],
) {
const trues = reports?.filter(
(report) => getSelection(selector, report) === true,
).length;
return trues / Math.max(reports?.length, 1) > 0.5;
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function NumericalAverage<T extends QuantData>(
selector: Selector<T>,
reports: Report<T>[],
) {
return Round(NumericalTotal(selector, reports) / reports?.length);
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function ComparativePercent<T extends QuantData>(
selector1: Selector<T>,
selector2: Selector<T>,
reports: Report<T>[],
) {
const a = NumericalTotal(selector1, reports);
const b = NumericalTotal(selector2, reports);
if (a === 0 && b === 0) {
return "0%";
}
return Round((a / (b + a)) * 100) + "%";
}
/**
* @tested_by tests/lib/client/StatsMath.test.ts
*/
export function ComparativePercentMulti<T extends QuantData>(
selectors: Selector<T>[],
reports: Report<T>[],
) {
const results: string[] = [];
const totals = selectors.map((selectors) =>
NumericalTotal(selectors, reports),
);
let sum = 0;
for (let i = 0; i < totals.length; i++) {
sum += totals[i];
}
for (let i = 0; i < totals.length; i++) {
results.push(Round((totals[i] / sum) * 100) + "%");
}
if (sum === 0) {
return 0;
}
return results;
}
//Takes a list of Quantitative reports and a stat and returns the minimum value recorded for said stat
export function GetMinimum(
quantitativeReports: Report<QuantData>[],
stat: string,
) {
if (!quantitativeReports) return 0;
let minimum = quantitativeReports[0].data[stat];
for (let repo of quantitativeReports) {
if (repo.data[stat] < minimum) {
minimum = repo.data[stat];
}
}
return minimum;
}
//Takes a list of Quantitative reports and a stat and returns the maximum value recorded for said stat
export function GetMaximum(
quantitativeReports: Report<QuantData>[],
stat: string,
) {
if (!quantitativeReports) return 0;
let maximum = 0;
for (let repo of quantitativeReports) {
if (repo.data[stat] > maximum) {
maximum = repo.data[stat];
}
}
return maximum;
}