-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
data.ts
325 lines (297 loc) · 8.26 KB
/
data.ts
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import {
boxplot as boxplots,
quantilesFivenum,
quantilesHigher,
quantilesHinges,
quantilesLinear,
quantilesLower,
quantilesMidpoint,
quantilesNearest,
quantilesType7,
} from '@sgratzl/boxplots';
export {
quantilesFivenum,
quantilesHigher,
quantilesHinges,
quantilesLinear,
quantilesLower,
quantilesMidpoint,
quantilesNearest,
quantilesType7,
} from '@sgratzl/boxplots';
export interface IBaseStats {
min: number;
max: number;
q1: number;
q3: number;
median: number;
mean: number;
items: readonly number[];
outliers: readonly number[];
}
export interface IBoxPlot extends IBaseStats {
whiskerMax: number;
whiskerMin: number;
}
export interface IKDEPoint {
v: number;
estimate: number;
}
export interface IViolin extends IBaseStats {
maxEstimate: number;
coords: IKDEPoint[];
}
/**
* compute the whiskers
* @param boxplot
* @param {number[]} arr sorted array
* @param {number} coef
*/
export function whiskers(
boxplot: IBoxPlot,
arr: number[] | null,
coef = 1.5
): { whiskerMin: number; whiskerMax: number } {
const iqr = boxplot.q3 - boxplot.q1;
// since top left is max
const coefValid = typeof coef === 'number' && coef > 0;
let whiskerMin = coefValid ? Math.max(boxplot.min, boxplot.q1 - coef * iqr) : boxplot.min;
let whiskerMax = coefValid ? Math.min(boxplot.max, boxplot.q3 + coef * iqr) : boxplot.max;
if (Array.isArray(arr)) {
// compute the closest real element
for (let i = 0; i < arr.length; i += 1) {
const v = arr[i];
if (v >= whiskerMin) {
whiskerMin = v;
break;
}
}
for (let i = arr.length - 1; i >= 0; i -= 1) {
const v = arr[i];
if (v <= whiskerMax) {
whiskerMax = v;
break;
}
}
}
return {
whiskerMin,
whiskerMax,
};
}
export type QuantileMethod =
| 7
| 'quantiles'
| 'hinges'
| 'fivenum'
| 'linear'
| 'lower'
| 'higher'
| 'nearest'
| 'midpoint'
| ((arr: ArrayLike<number>, length?: number | undefined) => { q1: number; median: number; q3: number });
export interface IBaseOptions {
/**
* statistic measure that should be used for computing the minimal data limit
* @default 'min'
*/
minStats?: 'min' | 'q1' | 'whiskerMin';
/**
* statistic measure that should be used for computing the maximal data limit
* @default 'max'
*/
maxStats?: 'max' | 'q3' | 'whiskerMax';
/**
* from the R doc: this determines how far the plot ‘whiskers’ extend out from
* the box. If coef is positive, the whiskers extend to the most extreme data
* point which is no more than coef times the length of the box away from the
* box. A value of zero causes the whiskers to extend to the data extremes
* @default 1.5
*/
coef?: number;
/**
* the method to compute the quantiles.
*
* 7, 'quantiles': the type-7 method as used by R 'quantiles' method.
* 'hinges' and 'fivenum': the method used by R 'boxplot.stats' method.
* 'linear': the interpolation method 'linear' as used by 'numpy.percentile' function
* 'lower': the interpolation method 'lower' as used by 'numpy.percentile' function
* 'higher': the interpolation method 'higher' as used by 'numpy.percentile' function
* 'nearest': the interpolation method 'nearest' as used by 'numpy.percentile' function
* 'midpoint': the interpolation method 'midpoint' as used by 'numpy.percentile' function
* @default 7
*/
quantiles?: QuantileMethod;
/**
* the method to compute the whiskers.
*
* 'nearest': with this mode computed whisker values will be replaced with nearest real data points
* 'exact': with this mode exact computed whisker values will be displayed on chart
* @default 'nearest'
*/
whiskersMode?: 'nearest' | 'exact';
}
export type IBoxplotOptions = IBaseOptions;
export interface IViolinOptions extends IBaseOptions {
/**
* number of points that should be samples of the KDE
* @default 100
*/
points: number;
}
/**
* @hidden
*/
export const defaultStatsOptions: Required<Omit<IBaseOptions, 'minStats' | 'maxStats'>> = {
coef: 1.5,
quantiles: 7,
whiskersMode: 'nearest',
};
function determineQuantiles(q: QuantileMethod) {
if (typeof q === 'function') {
return q;
}
const lookup = {
hinges: quantilesHinges,
fivenum: quantilesFivenum,
7: quantilesType7,
quantiles: quantilesType7,
linear: quantilesLinear,
lower: quantilesLower,
higher: quantilesHigher,
nearest: quantilesNearest,
midpoint: quantilesMidpoint,
};
return lookup[q] || quantilesType7;
}
function determineStatsOptions(options?: IBaseOptions) {
const coef = options == null || typeof options.coef !== 'number' ? defaultStatsOptions.coef : options.coef;
const q = options == null || options.quantiles == null ? quantilesType7 : options.quantiles;
const quantiles = determineQuantiles(q);
const whiskersMode =
options == null || typeof options.whiskersMode !== 'string'
? defaultStatsOptions.whiskersMode
: options.whiskersMode;
return {
coef,
quantiles,
whiskersMode,
};
}
/**
* @hidden
*/
export function boxplotStats(arr: readonly number[] | Float32Array | Float64Array, options: IBaseOptions): IBoxPlot {
const vs =
typeof Float64Array !== 'undefined' && !(arr instanceof Float32Array || arr instanceof Float64Array)
? Float64Array.from(arr)
: arr;
const r = boxplots(vs, determineStatsOptions(options));
return {
items: Array.from(r.items),
outliers: r.outlier,
whiskerMax: r.whiskerHigh,
whiskerMin: r.whiskerLow,
max: r.max,
median: r.median,
mean: r.mean,
min: r.min,
q1: r.q1,
q3: r.q3,
};
}
function computeSamples(min: number, max: number, points: number) {
// generate coordinates
const range = max - min;
const samples: number[] = [];
const inc = range / points;
for (let v = min; v <= max && inc > 0; v += inc) {
samples.push(v);
}
if (samples[samples.length - 1] !== max) {
samples.push(max);
}
return samples;
}
/**
* @hidden
*/
export function violinStats(arr: readonly number[], options: IViolinOptions): IViolin | undefined {
// console.assert(Array.isArray(arr));
if (arr.length === 0) {
return undefined;
}
const vs =
typeof Float64Array !== 'undefined' && !(arr instanceof Float32Array || arr instanceof Float64Array)
? Float64Array.from(arr)
: arr;
const stats = boxplots(vs, determineStatsOptions(options));
// generate coordinates
const samples = computeSamples(stats.min, stats.max, options.points);
const coords = samples.map((v) => ({ v, estimate: stats.kde(v) }));
const maxEstimate = coords.reduce((a, d) => Math.max(a, d.estimate), Number.NEGATIVE_INFINITY);
return {
max: stats.max,
min: stats.min,
mean: stats.mean,
median: stats.median,
q1: stats.q1,
q3: stats.q3,
items: Array.from(stats.items),
coords,
outliers: [], // items.filter((d) => d < stats.q1 || d > stats.q3),
maxEstimate,
};
}
/**
* @hidden
*/
export function asBoxPlotStats(value: any, options: IBoxplotOptions): IBoxPlot | undefined {
if (!value) {
return undefined;
}
if (typeof value.median === 'number' && typeof value.q1 === 'number' && typeof value.q3 === 'number') {
// sounds good, check for helper
if (typeof value.whiskerMin === 'undefined') {
const { coef } = determineStatsOptions(options);
const { whiskerMin, whiskerMax } = whiskers(
value,
Array.isArray(value.items) ? (value.items as number[]).slice().sort((a, b) => a - b) : null,
coef
);
value.whiskerMin = whiskerMin;
value.whiskerMax = whiskerMax;
}
return value;
}
if (!Array.isArray(value)) {
return undefined;
}
return boxplotStats(value, options);
}
/**
* @hidden
*/
export function asViolinStats(value: any, options: IViolinOptions): IViolin | undefined {
if (!value) {
return undefined;
}
if (typeof value.median === 'number' && Array.isArray(value.coords)) {
return value;
}
if (!Array.isArray(value)) {
return undefined;
}
return violinStats(value, options);
}
/**
* @hidden
*/
export function rnd(seed = Date.now()): () => number {
// Adapted from http://indiegamr.com/generate-repeatable-random-numbers-in-js/
let s = seed;
return () => {
s = (s * 9301 + 49297) % 233280;
return s / 233280;
};
}