semantic-typescript 0.3.0 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/collectable.d.ts +19 -24
- package/dist/collectable.js +329 -177
- package/dist/collector.d.ts +93 -16
- package/dist/collector.js +297 -107
- package/dist/factory.d.ts +6 -0
- package/dist/factory.js +224 -148
- package/dist/guard.d.ts +3 -1
- package/dist/guard.js +14 -2
- package/dist/hook.d.ts +14 -2
- package/dist/hook.js +85 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/semantic.d.ts +2 -6
- package/dist/semantic.js +335 -185
- package/dist/statistics.d.ts +13 -17
- package/dist/statistics.js +204 -522
- package/dist/utility.d.ts +4 -0
- package/dist/utility.js +2 -1
- package/dist/window.d.ts +12 -0
- package/dist/window.js +60 -0
- package/package.json +1 -1
- package/readme.md +273 -47
package/dist/statistics.js
CHANGED
|
@@ -1,329 +1,162 @@
|
|
|
1
1
|
import { OrderedCollectable } from "./collectable";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { Collector, useBigIntAverage, useBigIntMedian, useBigIntMode, useBigIntSummate, useBigIntVariance, useFrequency, useNumericAverage, useNumericMedian, useNumericMode, useNumericStandardDeviation, useNumericSummate, useNumericVariance, useToAsyncGeneratorFunction, useToGeneratorFunction } from "./collector";
|
|
3
|
+
import { isFunction } from "./guard";
|
|
4
|
+
import { useCompare, useToBigInt, useToNumber } from "./hook";
|
|
5
5
|
import { StatisticsSymbol, NumericStatisticsSymbol, BigIntStatisticsSymbol } from "./symbol";
|
|
6
|
-
import { invalidate } from "./utility";
|
|
7
6
|
export class Statistics extends OrderedCollectable {
|
|
8
7
|
Statistics = StatisticsSymbol;
|
|
9
8
|
constructor(parameter, comparator) {
|
|
10
|
-
|
|
11
|
-
if (isFunction(comparator)) {
|
|
12
|
-
super(parameter, comparator);
|
|
13
|
-
}
|
|
14
|
-
else {
|
|
15
|
-
super(parameter);
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
else if (isFunction(parameter)) {
|
|
19
|
-
if (isFunction(comparator)) {
|
|
20
|
-
super(parameter, comparator);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
super(parameter);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
9
|
+
super(parameter, comparator || useCompare);
|
|
26
10
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (invalidate(argument1)) {
|
|
32
|
-
if (!this.isEmpty()) {
|
|
33
|
-
return this.collect(() => {
|
|
34
|
-
return Optional.ofNullable();
|
|
35
|
-
}, (result, element) => {
|
|
36
|
-
if (result.isEmpty()) {
|
|
37
|
-
return Optional.of(element);
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
let current = result.get();
|
|
41
|
-
if (useCompare(current, element) > 0) {
|
|
42
|
-
return Optional.of(element);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
return result;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}, (result) => result);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
return Optional.ofNullable();
|
|
52
|
-
}
|
|
11
|
+
*[Symbol.iterator]() {
|
|
12
|
+
try {
|
|
13
|
+
let collector = useToGeneratorFunction();
|
|
14
|
+
yield* collector.collect(this.source());
|
|
53
15
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
if (!this.isEmpty()) {
|
|
57
|
-
return this.collect(() => {
|
|
58
|
-
return Optional.ofNullable();
|
|
59
|
-
}, (result, element) => {
|
|
60
|
-
if (result.isEmpty()) {
|
|
61
|
-
return Optional.of(element);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
let current = result.get();
|
|
65
|
-
if (comparator(current, element) > 0) {
|
|
66
|
-
return Optional.of(element);
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}, (result) => result);
|
|
73
|
-
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
throw new Error("Uncaught error on Generator.");
|
|
74
18
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return this.collect(() => {
|
|
81
|
-
return Optional.ofNullable();
|
|
82
|
-
}, (result, element) => {
|
|
83
|
-
if (result.isEmpty()) {
|
|
84
|
-
return Optional.of(element);
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
let current = result.get();
|
|
88
|
-
if (useCompare(current, element) < 0) {
|
|
89
|
-
return Optional.of(element);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
return result;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}, (result) => result);
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
return Optional.ofNullable();
|
|
99
|
-
}
|
|
19
|
+
}
|
|
20
|
+
async *[Symbol.asyncIterator]() {
|
|
21
|
+
try {
|
|
22
|
+
let collector = useToAsyncGeneratorFunction();
|
|
23
|
+
yield* collector.collect(this.source());
|
|
100
24
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (!this.isEmpty()) {
|
|
104
|
-
return this.collect(() => {
|
|
105
|
-
return Optional.ofNullable();
|
|
106
|
-
}, (result, element) => {
|
|
107
|
-
if (result.isEmpty()) {
|
|
108
|
-
return Optional.of(element);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
let current = result.get();
|
|
112
|
-
if (comparator(current, element) < 0) {
|
|
113
|
-
return Optional.of(element);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
return result;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}, (result) => result);
|
|
120
|
-
}
|
|
121
|
-
return Optional.ofNullable();
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new Error("Uncaught error on AsyncGenerator.");
|
|
122
27
|
}
|
|
123
28
|
}
|
|
29
|
+
count() {
|
|
30
|
+
return BigInt(this.buffer.length);
|
|
31
|
+
}
|
|
32
|
+
frequency() {
|
|
33
|
+
return useFrequency().collect(this.source());
|
|
34
|
+
}
|
|
124
35
|
}
|
|
125
36
|
;
|
|
126
37
|
export class NumericStatistics extends Statistics {
|
|
127
38
|
NumericStatistics = NumericStatisticsSymbol;
|
|
128
39
|
constructor(parameter, comparator) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
40
|
+
super(parameter, comparator || useCompare);
|
|
41
|
+
}
|
|
42
|
+
*[Symbol.iterator]() {
|
|
43
|
+
try {
|
|
44
|
+
let collector = useToGeneratorFunction();
|
|
45
|
+
yield* collector.collect(this.source());
|
|
136
46
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new Error("Uncaught error on Generator.");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
async *[Symbol.asyncIterator]() {
|
|
52
|
+
try {
|
|
53
|
+
let collector = useToAsyncGeneratorFunction();
|
|
54
|
+
yield* collector.collect(this.source());
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
throw new Error("Uncaught error on AsyncGenerator.");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
average(mapper) {
|
|
61
|
+
if (this.isEmpty()) {
|
|
62
|
+
return 0;
|
|
63
|
+
}
|
|
64
|
+
try {
|
|
65
|
+
if (isFunction(mapper)) {
|
|
66
|
+
return useNumericAverage(mapper).collect(this.source());
|
|
143
67
|
}
|
|
68
|
+
return useNumericAverage().collect(this.source());
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
throw new Error("Uncaught error on average.");
|
|
144
72
|
}
|
|
145
73
|
}
|
|
146
74
|
range(argument1) {
|
|
147
75
|
if (this.isEmpty()) {
|
|
148
76
|
return 0;
|
|
149
77
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
for (let i = 1; i < this.ordered.length; i++) {
|
|
154
|
-
let current = this.ordered[i].value;
|
|
155
|
-
if (useCompare(current, minimum) < 0) {
|
|
156
|
-
minimum = current;
|
|
157
|
-
}
|
|
158
|
-
if (useCompare(current, maximum) > 0) {
|
|
159
|
-
maximum = current;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
return useCompare(maximum, minimum);
|
|
163
|
-
}
|
|
164
|
-
else if (isFunction(argument1)) {
|
|
165
|
-
let mapper = argument1;
|
|
166
|
-
let minimum = mapper(this.ordered[0].value);
|
|
167
|
-
let maximum = mapper(this.ordered[0].value);
|
|
168
|
-
for (let i = 1; i < this.ordered.length; i++) {
|
|
169
|
-
let current = mapper(this.ordered[i].value);
|
|
170
|
-
if (current < minimum) {
|
|
171
|
-
minimum = current;
|
|
172
|
-
}
|
|
173
|
-
if (current > maximum) {
|
|
174
|
-
maximum = current;
|
|
175
|
-
}
|
|
78
|
+
try {
|
|
79
|
+
if (this.count() === 1n) {
|
|
80
|
+
return 0;
|
|
176
81
|
}
|
|
177
|
-
|
|
82
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
83
|
+
let count = this.buffer.length;
|
|
84
|
+
let minimum = this.buffer[0].element;
|
|
85
|
+
let maximum = this.buffer[count - 1].element;
|
|
86
|
+
return mapper(maximum) - mapper(minimum);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new Error("Uncaught error on range.");
|
|
178
90
|
}
|
|
179
|
-
throw new TypeError("Invalid arguments.");
|
|
180
91
|
}
|
|
181
92
|
variance(argument1) {
|
|
182
93
|
if (this.isEmpty() || this.count() === 1n) {
|
|
183
94
|
return 0;
|
|
184
95
|
}
|
|
185
|
-
|
|
186
|
-
let
|
|
187
|
-
|
|
188
|
-
return (summate / Number(this.count())) - (mean * mean);
|
|
96
|
+
try {
|
|
97
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
98
|
+
return useNumericVariance(mapper).collect(this.source());
|
|
189
99
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
let mean = this.mean(mapper);
|
|
193
|
-
let summate = this.summate(mapper);
|
|
194
|
-
return (summate / Number(this.count())) - (mean * mean);
|
|
100
|
+
catch (error) {
|
|
101
|
+
throw new Error("Uncaught error on variance.");
|
|
195
102
|
}
|
|
196
|
-
throw new TypeError("Invalid arguments.");
|
|
197
103
|
}
|
|
198
104
|
standardDeviation(argument1) {
|
|
199
|
-
|
|
200
|
-
|
|
105
|
+
try {
|
|
106
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
107
|
+
return useNumericStandardDeviation(mapper).collect(this.source());
|
|
201
108
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
let variance = this.variance(mapper);
|
|
205
|
-
return Math.sqrt(variance);
|
|
109
|
+
catch (error) {
|
|
110
|
+
throw new Error("Uncaught error on standardDeviation.");
|
|
206
111
|
}
|
|
207
|
-
throw new TypeError("Invalid arguments.");
|
|
208
112
|
}
|
|
209
113
|
mean(argument1) {
|
|
210
114
|
if (this.isEmpty()) {
|
|
211
115
|
return 0;
|
|
212
116
|
}
|
|
213
|
-
|
|
214
|
-
let
|
|
215
|
-
return
|
|
117
|
+
try {
|
|
118
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
119
|
+
return useNumericAverage(mapper).collect(this.source());
|
|
216
120
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
let summate = this.summate(mapper);
|
|
220
|
-
return summate / Number(this.count());
|
|
121
|
+
catch (error) {
|
|
122
|
+
throw new Error("Uncaught error on mean.");
|
|
221
123
|
}
|
|
222
|
-
throw new TypeError("Invalid arguments.");
|
|
223
124
|
}
|
|
224
125
|
median(argument1) {
|
|
225
126
|
if (this.isEmpty()) {
|
|
226
127
|
return 0;
|
|
227
128
|
}
|
|
228
|
-
|
|
229
|
-
let
|
|
230
|
-
|
|
231
|
-
let median = Number(this.ordered[middle].value);
|
|
232
|
-
if (count % 2 === 0) {
|
|
233
|
-
median = (median + Number(this.ordered[middle - 1].value)) / 2;
|
|
234
|
-
}
|
|
235
|
-
return median;
|
|
129
|
+
try {
|
|
130
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
131
|
+
return useNumericMedian(mapper).collect(this.source());
|
|
236
132
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
let count = Number(this.count());
|
|
240
|
-
let middle = Math.floor(count / 2);
|
|
241
|
-
let median = mapper(this.ordered[middle].value);
|
|
242
|
-
if (count % 2 === 0) {
|
|
243
|
-
median = (median + mapper(this.ordered[middle - 1].value)) / 2;
|
|
244
|
-
}
|
|
245
|
-
return median;
|
|
133
|
+
catch (error) {
|
|
134
|
+
throw new Error("Uncaught error on median.");
|
|
246
135
|
}
|
|
247
|
-
throw new TypeError("Invalid arguments.");
|
|
248
136
|
}
|
|
249
137
|
mode(argument1) {
|
|
250
138
|
if (this.isEmpty()) {
|
|
251
139
|
return 0;
|
|
252
140
|
}
|
|
253
|
-
|
|
254
|
-
let
|
|
255
|
-
|
|
256
|
-
let maxFrequency = 0n;
|
|
257
|
-
for (let [value, freq] of frequency) {
|
|
258
|
-
if (freq > maxFrequency) {
|
|
259
|
-
mode = value;
|
|
260
|
-
maxFrequency = freq;
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
return mode;
|
|
264
|
-
}
|
|
265
|
-
else if (isFunction(argument1)) {
|
|
266
|
-
let mapper = argument1;
|
|
267
|
-
let frequency = this.frequency(mapper);
|
|
268
|
-
let mode = 0;
|
|
269
|
-
let maxFrequency = 0n;
|
|
270
|
-
for (let [value, freq] of frequency) {
|
|
271
|
-
if (freq > maxFrequency) {
|
|
272
|
-
mode = value;
|
|
273
|
-
maxFrequency = freq;
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
return mode;
|
|
141
|
+
try {
|
|
142
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
143
|
+
return useNumericMode(mapper).collect(this.source());
|
|
277
144
|
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
frequency(argument1) {
|
|
281
|
-
if (this.isEmpty()) {
|
|
282
|
-
return new Map();
|
|
283
|
-
}
|
|
284
|
-
if (invalidate(argument1)) {
|
|
285
|
-
let frequency = new Map();
|
|
286
|
-
for (let i = 0; i < this.ordered.length; i++) {
|
|
287
|
-
let current = Number(this.ordered[i].value);
|
|
288
|
-
let count = frequency.get(current) || 0n;
|
|
289
|
-
frequency.set(current, count + 1n);
|
|
290
|
-
}
|
|
291
|
-
return frequency;
|
|
292
|
-
}
|
|
293
|
-
else if (isFunction(argument1)) {
|
|
294
|
-
let mapper = argument1;
|
|
295
|
-
let frequency = new Map();
|
|
296
|
-
for (let i = 0; i < this.ordered.length; i++) {
|
|
297
|
-
let current = mapper(this.ordered[i].value);
|
|
298
|
-
let count = frequency.get(current) || 0n;
|
|
299
|
-
frequency.set(current, count + 1n);
|
|
300
|
-
}
|
|
301
|
-
return frequency;
|
|
145
|
+
catch (error) {
|
|
146
|
+
throw new Error("Uncaught error on mode.");
|
|
302
147
|
}
|
|
303
|
-
throw new TypeError("Invalid arguments.");
|
|
304
148
|
}
|
|
305
149
|
summate(argument1) {
|
|
306
150
|
if (this.isEmpty()) {
|
|
307
151
|
return 0;
|
|
308
152
|
}
|
|
309
|
-
|
|
310
|
-
let
|
|
311
|
-
|
|
312
|
-
let current = Number(this.ordered[i].value);
|
|
313
|
-
summate += current;
|
|
314
|
-
}
|
|
315
|
-
return summate;
|
|
153
|
+
try {
|
|
154
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
155
|
+
return useNumericSummate(mapper).collect(this.source());
|
|
316
156
|
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
let summate = 0;
|
|
320
|
-
for (let i = 0; i < this.ordered.length; i++) {
|
|
321
|
-
let current = mapper(this.ordered[i].value);
|
|
322
|
-
summate += current;
|
|
323
|
-
}
|
|
324
|
-
return summate;
|
|
157
|
+
catch (error) {
|
|
158
|
+
throw new Error("Uncaught error on summate.");
|
|
325
159
|
}
|
|
326
|
-
throw new TypeError("Invalid arguments.");
|
|
327
160
|
}
|
|
328
161
|
quantile(quantile, argument1) {
|
|
329
162
|
if (this.isEmpty()) {
|
|
@@ -332,64 +165,40 @@ export class NumericStatistics extends Statistics {
|
|
|
332
165
|
if (quantile < 0 || quantile > 1) {
|
|
333
166
|
throw new RangeError("Invalid quantile.");
|
|
334
167
|
}
|
|
335
|
-
|
|
168
|
+
try {
|
|
169
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
336
170
|
let count = Number(this.count());
|
|
337
171
|
let index = Math.floor(quantile * count);
|
|
338
172
|
if (index === count) {
|
|
339
173
|
index--;
|
|
340
174
|
}
|
|
341
|
-
let value =
|
|
175
|
+
let value = mapper(this.buffer[index].element);
|
|
342
176
|
return value;
|
|
343
177
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
let count = Number(this.count());
|
|
347
|
-
let index = Math.floor(quantile * count);
|
|
348
|
-
if (index === count) {
|
|
349
|
-
index--;
|
|
350
|
-
}
|
|
351
|
-
let value = mapper(this.ordered[index].value);
|
|
352
|
-
return value;
|
|
178
|
+
catch (error) {
|
|
179
|
+
throw new Error("Uncaught error on quantile.");
|
|
353
180
|
}
|
|
354
|
-
throw new TypeError("Invalid arguments.");
|
|
355
181
|
}
|
|
356
182
|
interquartileRange(argument1) {
|
|
357
183
|
if (this.isEmpty()) {
|
|
358
184
|
return 0;
|
|
359
185
|
}
|
|
360
|
-
|
|
361
|
-
let
|
|
362
|
-
let upper = this.quantile(0.75);
|
|
363
|
-
return upper - lower;
|
|
364
|
-
}
|
|
365
|
-
else if (isFunction(argument1)) {
|
|
366
|
-
let mapper = argument1;
|
|
186
|
+
try {
|
|
187
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
367
188
|
let lower = this.quantile(0.25, mapper);
|
|
368
189
|
let upper = this.quantile(0.75, mapper);
|
|
369
190
|
return upper - lower;
|
|
370
191
|
}
|
|
371
|
-
|
|
192
|
+
catch (error) {
|
|
193
|
+
throw new Error("Uncaught error on interquartileRange.");
|
|
194
|
+
}
|
|
372
195
|
}
|
|
373
196
|
skewness(argument1) {
|
|
374
197
|
if (this.isEmpty()) {
|
|
375
198
|
return 0;
|
|
376
199
|
}
|
|
377
|
-
|
|
378
|
-
let
|
|
379
|
-
let standardDeviation = this.standardDeviation();
|
|
380
|
-
if (standardDeviation === 0) {
|
|
381
|
-
return 0;
|
|
382
|
-
}
|
|
383
|
-
let data = this.toArray();
|
|
384
|
-
let summate = 0;
|
|
385
|
-
for (let value of data) {
|
|
386
|
-
let z = (value - mean) / standardDeviation;
|
|
387
|
-
summate += Math.pow(z, 3);
|
|
388
|
-
}
|
|
389
|
-
return summate / data.length;
|
|
390
|
-
}
|
|
391
|
-
else if (isFunction(argument1)) {
|
|
392
|
-
let mapper = argument1;
|
|
200
|
+
try {
|
|
201
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
393
202
|
let mean = this.mean(mapper);
|
|
394
203
|
let standardDeviation = this.standardDeviation(mapper);
|
|
395
204
|
if (standardDeviation === 0) {
|
|
@@ -403,28 +212,16 @@ export class NumericStatistics extends Statistics {
|
|
|
403
212
|
}
|
|
404
213
|
return summate / data.length;
|
|
405
214
|
}
|
|
406
|
-
|
|
215
|
+
catch (error) {
|
|
216
|
+
throw new Error("Uncaught error on skewness.");
|
|
217
|
+
}
|
|
407
218
|
}
|
|
408
219
|
kurtosis(argument1) {
|
|
409
220
|
if (this.isEmpty()) {
|
|
410
221
|
return 0;
|
|
411
222
|
}
|
|
412
|
-
|
|
413
|
-
let
|
|
414
|
-
let standardDeviation = this.standardDeviation();
|
|
415
|
-
if (standardDeviation === 0) {
|
|
416
|
-
return 0;
|
|
417
|
-
}
|
|
418
|
-
let data = this.toArray();
|
|
419
|
-
let summate = 0;
|
|
420
|
-
for (let value of data) {
|
|
421
|
-
let z = (value - mean) / standardDeviation;
|
|
422
|
-
summate += Math.pow(z, 4);
|
|
423
|
-
}
|
|
424
|
-
return summate / (data.length * data.length) - 3;
|
|
425
|
-
}
|
|
426
|
-
else if (isFunction(argument1)) {
|
|
427
|
-
let mapper = argument1;
|
|
223
|
+
try {
|
|
224
|
+
let mapper = isFunction(argument1) ? argument1 : useToNumber;
|
|
428
225
|
let mean = this.mean(mapper);
|
|
429
226
|
let standardDeviation = this.standardDeviation(mapper);
|
|
430
227
|
if (standardDeviation === 0) {
|
|
@@ -438,277 +235,173 @@ export class NumericStatistics extends Statistics {
|
|
|
438
235
|
}
|
|
439
236
|
return summate / (data.length * data.length) - 3;
|
|
440
237
|
}
|
|
441
|
-
|
|
238
|
+
catch (error) {
|
|
239
|
+
throw new Error("Uncaught error on kurtosis.");
|
|
240
|
+
}
|
|
442
241
|
}
|
|
443
242
|
}
|
|
444
243
|
;
|
|
445
244
|
export class BigIntStatistics extends Statistics {
|
|
446
245
|
BigIntStatistics = BigIntStatisticsSymbol;
|
|
447
246
|
constructor(parameter, comparator) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
}
|
|
247
|
+
super(parameter, comparator || useCompare);
|
|
248
|
+
}
|
|
249
|
+
*[Symbol.iterator]() {
|
|
250
|
+
try {
|
|
251
|
+
let collector = useToGeneratorFunction();
|
|
252
|
+
yield* collector.collect(this.source());
|
|
455
253
|
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
254
|
+
catch (error) {
|
|
255
|
+
throw new Error("Uncaught error on Generator.");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async *[Symbol.asyncIterator]() {
|
|
259
|
+
try {
|
|
260
|
+
let collector = useToAsyncGeneratorFunction();
|
|
261
|
+
yield* collector.collect(this.source());
|
|
262
|
+
}
|
|
263
|
+
catch (error) {
|
|
264
|
+
throw new Error("Uncaught error on AsyncGenerator.");
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
average(argument1) {
|
|
268
|
+
if (this.isEmpty()) {
|
|
269
|
+
return 0n;
|
|
270
|
+
}
|
|
271
|
+
try {
|
|
272
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
273
|
+
return useBigIntAverage(mapper).collect(this.source());
|
|
274
|
+
}
|
|
275
|
+
catch (error) {
|
|
276
|
+
throw new Error("Uncaught error on average.");
|
|
463
277
|
}
|
|
464
278
|
}
|
|
465
279
|
range(argument1) {
|
|
466
280
|
if (this.isEmpty()) {
|
|
467
281
|
return 0n;
|
|
468
282
|
}
|
|
469
|
-
|
|
470
|
-
let
|
|
471
|
-
let
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
maximum = current;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
return BigInt(useCompare(maximum, minimum));
|
|
482
|
-
}
|
|
483
|
-
else if (isFunction(argument1)) {
|
|
484
|
-
let mapper = argument1;
|
|
485
|
-
let minimum = mapper(this.ordered[0].value);
|
|
486
|
-
let maximum = mapper(this.ordered[0].value);
|
|
487
|
-
for (let i = 1; i < this.ordered.length; i++) {
|
|
488
|
-
let current = mapper(this.ordered[i].value);
|
|
489
|
-
if (current < minimum) {
|
|
490
|
-
minimum = current;
|
|
491
|
-
}
|
|
492
|
-
if (current > maximum) {
|
|
493
|
-
maximum = current;
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
return maximum - minimum;
|
|
283
|
+
try {
|
|
284
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
285
|
+
let count = this.buffer.length;
|
|
286
|
+
let minimum = this.buffer[0].element;
|
|
287
|
+
let maximum = this.buffer[count - 1].element;
|
|
288
|
+
return mapper(maximum) - mapper(minimum);
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
throw new Error("Uncaught error on range.");
|
|
497
292
|
}
|
|
498
|
-
throw new TypeError("Invalid arguments.");
|
|
499
293
|
}
|
|
500
294
|
variance(argument1) {
|
|
501
295
|
if (this.isEmpty() || this.count() === 1n) {
|
|
502
296
|
return 0n;
|
|
503
297
|
}
|
|
504
|
-
|
|
505
|
-
let
|
|
506
|
-
|
|
507
|
-
return (summate / this.count()) - (mean * mean);
|
|
298
|
+
try {
|
|
299
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
300
|
+
return useBigIntVariance(mapper).collect(this.source());
|
|
508
301
|
}
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
let mean = this.mean(mapper);
|
|
512
|
-
let summate = this.summate(mapper);
|
|
513
|
-
return (summate / this.count()) - (mean * mean);
|
|
302
|
+
catch (error) {
|
|
303
|
+
throw new Error("Uncaught error on variance.");
|
|
514
304
|
}
|
|
515
|
-
throw new TypeError("Invalid arguments.");
|
|
516
305
|
}
|
|
517
306
|
standardDeviation(argument1) {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
}
|
|
521
|
-
else if (isFunction(argument1)) {
|
|
522
|
-
let mapper = argument1;
|
|
307
|
+
try {
|
|
308
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
523
309
|
let variance = this.variance(mapper);
|
|
524
310
|
return BigInt(Math.sqrt(Number(variance)));
|
|
525
311
|
}
|
|
526
|
-
|
|
312
|
+
catch (error) {
|
|
313
|
+
throw new Error("Uncaught error on standardDeviation.");
|
|
314
|
+
}
|
|
527
315
|
}
|
|
528
316
|
mean(argument1) {
|
|
529
317
|
if (this.isEmpty()) {
|
|
530
318
|
return 0n;
|
|
531
319
|
}
|
|
532
|
-
|
|
533
|
-
let
|
|
534
|
-
return
|
|
320
|
+
try {
|
|
321
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
322
|
+
return useBigIntAverage(mapper).collect(this.source());
|
|
535
323
|
}
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
let summate = this.summate(mapper);
|
|
539
|
-
return summate / this.count();
|
|
324
|
+
catch (error) {
|
|
325
|
+
throw new Error("Uncaught error on mean.");
|
|
540
326
|
}
|
|
541
|
-
throw new TypeError("Invalid arguments.");
|
|
542
327
|
}
|
|
543
328
|
median(argument1) {
|
|
544
329
|
if (this.isEmpty()) {
|
|
545
330
|
return 0n;
|
|
546
331
|
}
|
|
547
|
-
|
|
548
|
-
let
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
return median;
|
|
554
|
-
}
|
|
555
|
-
return median;
|
|
556
|
-
}
|
|
557
|
-
else if (isFunction(argument1)) {
|
|
558
|
-
let mapper = argument1;
|
|
559
|
-
let count = this.count();
|
|
560
|
-
let middle = count / 2n;
|
|
561
|
-
let median = mapper(this.ordered[Number(middle)].value);
|
|
562
|
-
if (count % 2n === 0n) {
|
|
563
|
-
median = (median + mapper(this.ordered[Number(middle - 1n)].value)) / 2n;
|
|
564
|
-
}
|
|
565
|
-
return median;
|
|
332
|
+
try {
|
|
333
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
334
|
+
return useBigIntMedian(mapper).collect(this.source());
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
throw new Error("Uncaught error on median.");
|
|
566
338
|
}
|
|
567
|
-
throw new TypeError("Invalid arguments.");
|
|
568
339
|
}
|
|
569
340
|
mode(argument1) {
|
|
570
341
|
if (this.isEmpty()) {
|
|
571
342
|
return 0n;
|
|
572
343
|
}
|
|
573
|
-
|
|
574
|
-
let
|
|
575
|
-
|
|
576
|
-
let maxFrequency = 0n;
|
|
577
|
-
for (let [value, freq] of frequency) {
|
|
578
|
-
if (freq > maxFrequency) {
|
|
579
|
-
mode = value;
|
|
580
|
-
maxFrequency = freq;
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
return mode;
|
|
584
|
-
}
|
|
585
|
-
else if (isFunction(argument1)) {
|
|
586
|
-
let mapper = argument1;
|
|
587
|
-
let frequency = this.frequency(mapper);
|
|
588
|
-
let mode = 0n;
|
|
589
|
-
let maxFrequency = 0n;
|
|
590
|
-
for (let [value, frequence] of frequency) {
|
|
591
|
-
if (frequence > maxFrequency) {
|
|
592
|
-
mode = value;
|
|
593
|
-
maxFrequency = frequence;
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
return mode;
|
|
344
|
+
try {
|
|
345
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
346
|
+
return useBigIntMode(mapper).collect(this.source());
|
|
597
347
|
}
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
frequency(argument1) {
|
|
601
|
-
if (this.isEmpty()) {
|
|
602
|
-
return new Map();
|
|
603
|
-
}
|
|
604
|
-
if (invalidate(argument1)) {
|
|
605
|
-
let frequency = new Map();
|
|
606
|
-
for (let i = 0; i < this.ordered.length; i++) {
|
|
607
|
-
let current = BigInt(Number(this.ordered[i].value));
|
|
608
|
-
let count = frequency.get(current) || 0n;
|
|
609
|
-
frequency.set(current, count + 1n);
|
|
610
|
-
}
|
|
611
|
-
return frequency;
|
|
612
|
-
}
|
|
613
|
-
else if (isFunction(argument1)) {
|
|
614
|
-
let mapper = argument1;
|
|
615
|
-
let frequency = new Map();
|
|
616
|
-
for (let i = 0; i < this.ordered.length; i++) {
|
|
617
|
-
let current = mapper(this.ordered[i].value);
|
|
618
|
-
let count = frequency.get(current) || 0n;
|
|
619
|
-
frequency.set(current, count + 1n);
|
|
620
|
-
}
|
|
621
|
-
return frequency;
|
|
348
|
+
catch (error) {
|
|
349
|
+
throw new Error("Uncaught error on mode.");
|
|
622
350
|
}
|
|
623
|
-
throw new TypeError("Invalid arguments.");
|
|
624
351
|
}
|
|
625
352
|
summate(argument1) {
|
|
626
353
|
if (this.isEmpty()) {
|
|
627
354
|
return 0n;
|
|
628
355
|
}
|
|
629
|
-
|
|
630
|
-
let
|
|
631
|
-
|
|
632
|
-
let current = BigInt(Number(this.ordered[i].value));
|
|
633
|
-
summate += current;
|
|
634
|
-
}
|
|
635
|
-
return summate;
|
|
356
|
+
try {
|
|
357
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
358
|
+
return useBigIntSummate(mapper).collect(this.source());
|
|
636
359
|
}
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
let summate = 0n;
|
|
640
|
-
for (let i = 0; i < this.ordered.length; i++) {
|
|
641
|
-
let current = mapper(this.ordered[i].value);
|
|
642
|
-
summate += current;
|
|
643
|
-
}
|
|
644
|
-
return summate;
|
|
360
|
+
catch (error) {
|
|
361
|
+
throw new Error("Uncaught error on summate.");
|
|
645
362
|
}
|
|
646
|
-
throw new TypeError("Invalid arguments.");
|
|
647
363
|
}
|
|
648
|
-
quantile(quantile,
|
|
364
|
+
quantile(quantile, argument1) {
|
|
649
365
|
if (this.isEmpty()) {
|
|
650
366
|
return 0n;
|
|
651
367
|
}
|
|
652
368
|
if (typeof quantile !== "number" || quantile < 0 || quantile > 1) {
|
|
653
369
|
throw new RangeError("Invalid quantile.");
|
|
654
370
|
}
|
|
655
|
-
|
|
371
|
+
try {
|
|
372
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
656
373
|
let count = Number(this.count());
|
|
657
374
|
let index = Math.floor(quantile * count);
|
|
658
375
|
if (index === count) {
|
|
659
376
|
index--;
|
|
660
377
|
}
|
|
661
|
-
let value =
|
|
378
|
+
let value = mapper(this.buffer[index].element);
|
|
662
379
|
return value;
|
|
663
380
|
}
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
let index = Math.floor(quantile * count);
|
|
667
|
-
if (index === count) {
|
|
668
|
-
index--;
|
|
669
|
-
}
|
|
670
|
-
let value = mapper(this.ordered[index].value);
|
|
671
|
-
return value;
|
|
381
|
+
catch (error) {
|
|
382
|
+
throw new Error("Uncaught error on quantile.");
|
|
672
383
|
}
|
|
673
|
-
throw new TypeError("Invalid arguments.");
|
|
674
384
|
}
|
|
675
385
|
interquartileRange(argument1) {
|
|
676
386
|
if (this.isEmpty()) {
|
|
677
387
|
return 0n;
|
|
678
388
|
}
|
|
679
|
-
|
|
680
|
-
let
|
|
681
|
-
let upper = this.quantile(0.75);
|
|
682
|
-
return upper - lower;
|
|
683
|
-
}
|
|
684
|
-
else if (isFunction(argument1)) {
|
|
685
|
-
let mapper = argument1;
|
|
389
|
+
try {
|
|
390
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
686
391
|
let lower = this.quantile(0.25, mapper);
|
|
687
392
|
let upper = this.quantile(0.75, mapper);
|
|
688
393
|
return upper - lower;
|
|
689
394
|
}
|
|
690
|
-
|
|
395
|
+
catch (error) {
|
|
396
|
+
throw new Error("Uncaught error on interquartileRange.");
|
|
397
|
+
}
|
|
691
398
|
}
|
|
692
399
|
skewness(argument1) {
|
|
693
400
|
if (this.isEmpty()) {
|
|
694
401
|
return 0n;
|
|
695
402
|
}
|
|
696
|
-
|
|
697
|
-
let
|
|
698
|
-
let standardDeviation = this.standardDeviation();
|
|
699
|
-
if (standardDeviation === 0n) {
|
|
700
|
-
return 0n;
|
|
701
|
-
}
|
|
702
|
-
let data = this.toArray();
|
|
703
|
-
let summate = 0n;
|
|
704
|
-
for (let value of data) {
|
|
705
|
-
let z = BigInt(Number(value)) - mean;
|
|
706
|
-
summate += z * z * z;
|
|
707
|
-
}
|
|
708
|
-
return summate / BigInt(data.length);
|
|
709
|
-
}
|
|
710
|
-
else if (isFunction(argument1)) {
|
|
711
|
-
let mapper = argument1;
|
|
403
|
+
try {
|
|
404
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
712
405
|
let mean = this.mean(mapper);
|
|
713
406
|
let standardDeviation = this.standardDeviation(mapper);
|
|
714
407
|
if (standardDeviation === 0n) {
|
|
@@ -722,29 +415,16 @@ export class BigIntStatistics extends Statistics {
|
|
|
722
415
|
}
|
|
723
416
|
return summate / BigInt(data.length);
|
|
724
417
|
}
|
|
725
|
-
|
|
418
|
+
catch (error) {
|
|
419
|
+
throw new Error("Uncaught error on skewness.");
|
|
420
|
+
}
|
|
726
421
|
}
|
|
727
422
|
kurtosis(argument1) {
|
|
728
423
|
if (this.isEmpty()) {
|
|
729
424
|
return 0n;
|
|
730
425
|
}
|
|
731
|
-
|
|
732
|
-
let
|
|
733
|
-
let standardDeviation = this.standardDeviation();
|
|
734
|
-
if (standardDeviation === 0n) {
|
|
735
|
-
return 0n;
|
|
736
|
-
}
|
|
737
|
-
let data = this.toArray();
|
|
738
|
-
let summate = 0n;
|
|
739
|
-
let count = data.length;
|
|
740
|
-
for (let value of data) {
|
|
741
|
-
let z = BigInt(Number(value)) - mean;
|
|
742
|
-
summate += z * z * z * z;
|
|
743
|
-
}
|
|
744
|
-
return summate / BigInt(count * count) - 3n;
|
|
745
|
-
}
|
|
746
|
-
else if (isFunction(argument1)) {
|
|
747
|
-
let mapper = argument1;
|
|
426
|
+
try {
|
|
427
|
+
let mapper = isFunction(argument1) ? argument1 : useToBigInt;
|
|
748
428
|
let mean = this.mean(mapper);
|
|
749
429
|
let standardDeviation = this.standardDeviation(mapper);
|
|
750
430
|
if (standardDeviation === 0n) {
|
|
@@ -759,7 +439,9 @@ export class BigIntStatistics extends Statistics {
|
|
|
759
439
|
}
|
|
760
440
|
return summate / BigInt(count * count) - 3n;
|
|
761
441
|
}
|
|
762
|
-
|
|
442
|
+
catch (error) {
|
|
443
|
+
throw new Error("Uncaught error on kurtosis.");
|
|
444
|
+
}
|
|
763
445
|
}
|
|
764
446
|
}
|
|
765
447
|
;
|