-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdistributions.ts
272 lines (235 loc) · 6.47 KB
/
distributions.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
import { thresholdSturges } from 'd3-array';
import { processNumber } from 'number-helper-functions';
import dlv from 'dlv';
import { getSimpleArray } from './arrays';
import { calcDomain } from './domain';
import { calcSum } from './operations';
import { calcPercent } from './percentages';
interface IDistribution {
labels: string[];
data: number[];
}
interface IDistributionArrayItem {
label: string;
count: number;
percentage: number;
from: number;
to: number;
}
interface IBucket {
label: string;
from: number;
to: number;
inside: (val: number) => boolean;
}
interface ISerieDistribution {
labels: string[];
data: {
name: string;
count:number[];
percentage_serie: number[];
percentage_total: number[];
}[]
}
function createArrayData(buckets: IBucket[], array: number[]): number[] {
const data = new Array(buckets.length).fill(0);
buckets.forEach((b, currentIndex) => {
data[currentIndex] = array.filter((d) => b.inside(d)).length;
});
return data;
}
/**
* Calculate the buckets given a data array and an amount
*
* @export
* @param {number[]} array The data array
* @param {boolean} [strict=false] Whether to use real or pretty domain
* @param {number} [numOfBins] Amount of desired buckets
* @return {IBucket[]} The buckets
*/
export function calcBuckets(
array: number[],
strict = false,
numOfBins?: number,
): IBucket[] {
let [minDom, maxDom] = calcDomain(array);
if (!strict) {
minDom = Math.floor(minDom);
maxDom = Math.ceil(maxDom);
}
const bins = numOfBins ?? thresholdSturges(array);
const bucketSize = (maxDom - minDom) / bins;
const buckets: IBucket[] = [];
for (let i = 0; i < bins; i++) {
const bucketMin = minDom + i * bucketSize;
const bucketMax = minDom + (i + 1) * bucketSize;
const label = `${processNumber(bucketMin)} - ${processNumber(bucketMax)}`;
buckets.push({
label,
from: bucketMin,
to: bucketMax,
inside(val: number) {
return i === bins - 1
? val >= bucketMin && val <= bucketMax
: val >= bucketMin && val < bucketMax;
},
});
}
return buckets;
}
/**
* Calculates the distribution of an arrays values
*
* @export
* @param {any[]} array Input array
* @param {boolean} strict
* @param {number} [numOfBins] Number of bins to use
* @return {IDistribution} The distribution
*/
export function calcDistribution(
array: number[],
strict = false,
numOfBins?: number,
): IDistribution {
const buckets: IBucket[] = calcBuckets(array, strict, numOfBins);
return {
labels: buckets.map((b) => b.label),
data: createArrayData(buckets, array),
};
}
/**
* Gets the min and max values for a calcDistribution bucket
*
* @export
* @param {string} bucketLabel The bucket label
* @return {number[]} [min, max]
*/
export function getMinMaxFromBucket(bucketLabel: string): number[] {
const [min, max] = bucketLabel.split(' - ');
return [Number(min.trim()), Number(max.trim())];
}
/**
* Calculates the distribution of an array of grouped objects
*
* @export
* @param {IBucket[]} buckets
* @param {Record<string, unknown[]>} dataGrouped
* @param {string} distributionProp
* @return {ISerieDistribution} The distribution with labels and data
*/
export function calcDistributionWithSeries(
buckets: IBucket[],
dataGrouped: Record<string, unknown[]>,
distributionProp: string,
): ISerieDistribution {
const totalCount = 0;
const data = Object.entries(dataGrouped).map(([key, value]) => {
let serieCount = 0;
const serieName = key;
const dataArr = buckets.map((d) => {
const bucketObj = {
interval: d.label,
data: 0,
};
value.forEach((v) => {
const valueProp = dlv(v as Record<string, number | string>, distributionProp);
if (d.inside(valueProp as number)) {
bucketObj.data++;
serieCount++;
}
});
return bucketObj;
});
return {
name: serieName,
count: dataArr.map((d) => d.data),
percentage_serie: dataArr.map((d) => calcPercent(d.data, serieCount)),
};
});
return {
labels: buckets.map((b) => b.label),
data: data.map((i) => ({
...i,
percentage_total: i.count.map((d) => calcPercent(d, totalCount)),
})),
};
}
/**
* Calculates the distribution of an arrays values and outputs an array
*
* @export
* @param {number[]} array Array to calc distribution of
* @param {boolean} [binsStrict=false] If false, buckets may be rounded [floor, ceil]
* @param {number} [numOfBins] Number of bins to use
* @return {IDistributionArrayItem[]} The distribution as an array of objects
*/
export function calcDistributionAsArray(
array: number[],
binsStrict = false,
numOfBins?: number,
): IDistributionArrayItem[] {
const distribution = calcDistribution(array, binsStrict, numOfBins);
const total = calcSum(distribution.data);
return distribution.labels.map((label, index) => {
const indexValue = distribution.data[index];
const [from, to] = getMinMaxFromBucket(label);
return {
label,
count: indexValue,
percentage: calcPercent(indexValue, total),
from,
to,
};
});
}
/**
* Gets the quartiles of an array
*
* @export
* @param {any[]} array Input array
* @param {string} [property] Property to map by
* @return {[number, number, number]} The quartiles
*/
export function calcQuartiles(
array: any[],
property?: string,
): [number, number, number] {
const len = array.length;
const simpleArray = [...getSimpleArray(array, property)];
simpleArray.sort((a, b) => a - b);
return [
simpleArray[Math.round(len * 0.25) - 1],
simpleArray[Math.round(len * 0.5) - 1],
simpleArray[Math.round(len * 0.75) - 1],
];
}
/**
* Calculates a histogram from array values
*
* @export
* @param {any[]} array Input array
* @param {number} [numberOfBins=4] Number of bins to use
* @param {string} [property] Property to map by
* @return {number[]} The histogram
*/
export function calcHistogram(
array: any[],
numberOfBins = 4,
property?: string,
): number[] {
const dataArray = getSimpleArray(array, property);
const [arrayMin, arrayMax] = calcDomain(dataArray);
const first = arrayMin;
const binWidth = (arrayMax - first) / numberOfBins;
const len = dataArray.length;
const bins = new Array(numberOfBins).fill(0);
for (let i = 0; i < len; i++) {
bins[
Math.min(
Math.floor((dataArray[i] - first) / binWidth),
numberOfBins - 1,
)
] += 1;
}
return bins;
}