phantomas 2.2.0 → 2.5.0

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/lib/fast-stats.js DELETED
@@ -1,634 +0,0 @@
1
- /**
2
- * Taken from https://github.com/bluesmoon/node-faststats
3
- */
4
-
5
- /*
6
- Note that if your data is too large, there _will_ be overflow.
7
- */
8
-
9
- function asc(a, b) {
10
- return a - b;
11
- }
12
-
13
- var config_params = {
14
- bucket_precision: function (o, s) {
15
- if (typeof s != "number" || s <= 0) {
16
- throw new Error("bucket_precision must be a positive number");
17
- }
18
- o._config.bucket_precision = s;
19
- o.buckets = [];
20
- },
21
-
22
- buckets: function (o, b) {
23
- if (!Array.isArray(b) || b.length == 0) {
24
- throw new Error("buckets must be an array of bucket limits");
25
- }
26
-
27
- o._config.buckets = b;
28
- o.buckets = [];
29
- },
30
-
31
- bucket_extension_interval: function (o, s) {
32
- if (s === undefined) return;
33
- if (typeof s != "number" || s <= 0) {
34
- throw new Error("bucket_extension_interval must be a positive number");
35
- }
36
- o._config.bucket_extension_interval = s;
37
- },
38
-
39
- store_data: function (o, s) {
40
- if (typeof s != "boolean") {
41
- throw new Error("store_data must be a true or false");
42
- }
43
- o._config.store_data = s;
44
- },
45
-
46
- sampling: function (o, s) {
47
- if (typeof s != "boolean") {
48
- throw new Error("sampling must be a true or false");
49
- }
50
- o._config.sampling = s;
51
- },
52
- };
53
-
54
- function Stats(c) {
55
- this._config = {
56
- store_data: true,
57
- };
58
-
59
- if (c) {
60
- for (var k in config_params) {
61
- if (c.hasOwnProperty(k)) {
62
- config_params[k](this, c[k]);
63
- }
64
- }
65
- }
66
-
67
- this.reset();
68
-
69
- return this;
70
- }
71
-
72
- Stats.prototype = {
73
- reset: function () {
74
- if (this._config.store_data) this.data = [];
75
-
76
- this.length = 0;
77
-
78
- this.sum = 0;
79
- this.sum_of_squares = 0;
80
- this.sum_of_logs = 0;
81
- this.sum_of_square_of_logs = 0;
82
- this.zeroes = 0;
83
- this.max = this.min = null;
84
-
85
- this._reset_cache();
86
-
87
- return this;
88
- },
89
-
90
- _reset_cache: function () {
91
- this._stddev = null;
92
-
93
- if (this._config.store_data) this._data_sorted = null;
94
- },
95
-
96
- _find_bucket: function (a) {
97
- var b = 0,
98
- e,
99
- l;
100
- if (this._config.buckets) {
101
- l = this._config.buckets.length;
102
- if (
103
- this._config.bucket_extension_interval &&
104
- a >= this._config.buckets[l - 1]
105
- ) {
106
- e = a - this._config.buckets[l - 1];
107
- b = parseInt(e / this._config.bucket_extension_interval) + l;
108
- if (this._config.buckets[b] === undefined)
109
- this._config.buckets[b] =
110
- this._config.buckets[l - 1] +
111
- (parseInt(e / this._config.bucket_extension_interval) + 1) *
112
- this._config.bucket_extension_interval;
113
- if (this._config.buckets[b - 1] === undefined)
114
- this._config.buckets[b - 1] =
115
- this._config.buckets[l - 1] +
116
- parseInt(e / this._config.bucket_extension_interval) *
117
- this._config.bucket_extension_interval;
118
- }
119
- for (; b < l; b++) {
120
- if (a < this._config.buckets[b]) {
121
- break;
122
- }
123
- }
124
- } else if (this._config.bucket_precision) {
125
- b = Math.floor(a / this._config.bucket_precision);
126
- }
127
-
128
- return b;
129
- },
130
-
131
- _add_cache: function (a) {
132
- var tuple = [1],
133
- i;
134
- if (a instanceof Array) {
135
- tuple = a;
136
- a = tuple.shift();
137
- }
138
-
139
- this.sum += a * tuple[0];
140
- this.sum_of_squares += a * a * tuple[0];
141
- if (a === 0) {
142
- this.zeroes++;
143
- } else {
144
- this.sum_of_logs += Math.log(a) * tuple[0];
145
- this.sum_of_square_of_logs += Math.pow(Math.log(a), 2) * tuple[0];
146
- }
147
- this.length += tuple[0];
148
-
149
- if (tuple[0] > 0) {
150
- if (this.max === null || this.max < a) this.max = a;
151
- if (this.min === null || this.min > a) this.min = a;
152
- }
153
-
154
- if (this.buckets) {
155
- var b = this._find_bucket(a);
156
- if (!this.buckets[b]) this.buckets[b] = [0];
157
- this.buckets[b][0] += tuple.shift();
158
-
159
- for (i = 0; i < tuple.length; i++)
160
- this.buckets[b][i + 1] = (this.buckets[b][i + 1] | 0) + (tuple[i] | 0);
161
- }
162
-
163
- this._reset_cache();
164
- },
165
-
166
- _del_cache: function (a) {
167
- var tuple = [1],
168
- i;
169
- if (a instanceof Array) {
170
- tuple = a;
171
- a = tuple.shift();
172
- }
173
-
174
- this.sum -= a * tuple[0];
175
- this.sum_of_squares -= a * a * tuple[0];
176
- if (a === 0) {
177
- this.zeroes--;
178
- } else {
179
- this.sum_of_logs -= Math.log(a) * tuple[0];
180
- this.sum_of_square_of_logs -= Math.pow(Math.log(a), 2) * tuple[0];
181
- }
182
- this.length -= tuple[0];
183
-
184
- if (this._config.store_data) {
185
- if (this.length === 0) {
186
- this.max = this.min = null;
187
- }
188
- if (this.length === 1) {
189
- this.max = this.min = this.data[0];
190
- } else if (tuple[0] > 0 && (this.max === a || this.min === a)) {
191
- i = this.length - 1;
192
- if (i >= 0) {
193
- this.max = this.min = this.data[i--];
194
- while (i-- >= 0) {
195
- if (this.max < this.data[i]) this.max = this.data[i];
196
- if (this.min > this.data[i]) this.min = this.data[i];
197
- }
198
- }
199
- }
200
- }
201
-
202
- if (this.buckets) {
203
- var b = this._find_bucket(a);
204
- if (this.buckets[b]) {
205
- this.buckets[b][0] -= tuple.shift();
206
-
207
- if (this.buckets[b][0] === 0) delete this.buckets[b];
208
- else
209
- for (i = 0; i < tuple.length; i++)
210
- this.buckets[b][i + 1] =
211
- (this.buckets[b][i + 1] | 0) - (tuple[i] | 0);
212
- }
213
- }
214
-
215
- this._reset_cache();
216
- },
217
-
218
- push: function () {
219
- var i,
220
- a,
221
- args = Array.prototype.slice.call(arguments, 0);
222
- if (args.length && args[0] instanceof Array) args = args[0];
223
- for (i = 0; i < args.length; i++) {
224
- a = args[i];
225
- if (this._config.store_data) this.data.push(a);
226
- this._add_cache(a);
227
- }
228
-
229
- return this;
230
- },
231
-
232
- push_tuple: function (tuple) {
233
- if (!this.buckets) {
234
- throw new Error("push_tuple is only valid when using buckets");
235
- }
236
- this._add_cache(tuple);
237
- },
238
-
239
- pop: function () {
240
- if (this.length === 0 || this._config.store_data === false)
241
- return undefined;
242
-
243
- var a = this.data.pop();
244
- this._del_cache(a);
245
-
246
- return a;
247
- },
248
-
249
- remove_tuple: function (tuple) {
250
- if (!this.buckets) {
251
- throw new Error("remove_tuple is only valid when using buckets");
252
- }
253
- this._del_cache(tuple);
254
- },
255
-
256
- reset_tuples: function (tuple) {
257
- var b,
258
- l,
259
- t,
260
- ts = tuple.length;
261
- if (!this.buckets) {
262
- throw new Error("reset_tuple is only valid when using buckets");
263
- }
264
-
265
- for (b = 0, l = this.buckets.length; b < l; b++) {
266
- if (!this.buckets[b] || this.buckets[b].length <= 1) {
267
- continue;
268
- }
269
- for (t = 0; t < ts; t++) {
270
- if (typeof tuple[t] !== "undefined") {
271
- this.buckets[b][t] = tuple[t];
272
- }
273
- }
274
- }
275
- },
276
-
277
- unshift: function () {
278
- var i,
279
- a,
280
- args = Array.prototype.slice.call(arguments, 0);
281
- if (args.length && args[0] instanceof Array) args = args[0];
282
- i = args.length;
283
- while (i--) {
284
- a = args[i];
285
- if (this._config.store_data) this.data.unshift(a);
286
- this._add_cache(a);
287
- }
288
-
289
- return this;
290
- },
291
-
292
- shift: function () {
293
- if (this.length === 0 || this._config.store_data === false)
294
- return undefined;
295
-
296
- var a = this.data.shift();
297
- this._del_cache(a);
298
-
299
- return a;
300
- },
301
-
302
- amean: function () {
303
- if (this.length === 0) return NaN;
304
- return this.sum / this.length;
305
- },
306
-
307
- gmean: function () {
308
- if (this.length === 0) return NaN;
309
- if (this.zeroes > 0) return NaN;
310
- return Math.exp(this.sum_of_logs / this.length);
311
- },
312
-
313
- stddev: function () {
314
- if (this.length === 0) return NaN;
315
- var n = this.length;
316
- if (this._config.sampling) n--;
317
- if (this._stddev === null)
318
- this._stddev = Math.sqrt(
319
- (this.length * this.sum_of_squares - this.sum * this.sum) /
320
- (this.length * n)
321
- );
322
-
323
- return this._stddev;
324
- },
325
-
326
- gstddev: function () {
327
- if (this.length === 0) return NaN;
328
- if (this.zeroes > 0) return NaN;
329
- var n = this.length;
330
- if (this._config.sampling) n--;
331
- return Math.exp(
332
- Math.sqrt(
333
- (this.length * this.sum_of_square_of_logs -
334
- this.sum_of_logs * this.sum_of_logs) /
335
- (this.length * n)
336
- )
337
- );
338
- },
339
-
340
- moe: function () {
341
- if (this.length === 0) return NaN;
342
- // see http://en.wikipedia.org/wiki/Standard_error_%28statistics%29
343
- return (1.96 * this.stddev()) / Math.sqrt(this.length);
344
- },
345
-
346
- range: function () {
347
- if (this.length === 0) return [NaN, NaN];
348
- return [this.min, this.max];
349
- },
350
-
351
- distribution: function () {
352
- if (this.length === 0) return [];
353
- if (!this.buckets)
354
- throw new Error("bucket_precision or buckets not configured.");
355
-
356
- var d = [],
357
- i,
358
- j,
359
- l;
360
-
361
- if (this._config.buckets) {
362
- j = this.min;
363
- l = Math.min(this.buckets.length, this._config.buckets.length);
364
-
365
- for (i = 0; i < l; j = this._config.buckets[i++]) {
366
- // this has to be i++ and not ++i
367
- if (
368
- this._config.buckets[i] === undefined &&
369
- this._config.bucket_extension_interval
370
- )
371
- this._config.buckets[i] =
372
- this._config.buckets[i - 1] +
373
- this._config.bucket_extension_interval;
374
- if (this.min > this._config.buckets[i]) continue;
375
-
376
- d[i] = {
377
- bucket: (j + this._config.buckets[i]) / 2,
378
- range: [j, this._config.buckets[i]],
379
- count: this.buckets[i] ? this.buckets[i][0] : 0,
380
- tuple: this.buckets[i] ? this.buckets[i].slice(1) : [],
381
- };
382
-
383
- if (this.max < this._config.buckets[i]) break;
384
- }
385
- if (i == l && this.buckets[i]) {
386
- d[i] = {
387
- bucket: (j + this.max) / 2,
388
- range: [j, this.max],
389
- count: this.buckets[i][0],
390
- tuple: this.buckets[i] ? this.buckets[i].slice(1) : [],
391
- };
392
- }
393
- } else if (this._config.bucket_precision) {
394
- i = Math.floor(this.min / this._config.bucket_precision);
395
- l = Math.floor(this.max / this._config.bucket_precision) + 1;
396
- for (j = 0; i < l && i < this.buckets.length; i++, j++) {
397
- if (!this.buckets[i]) {
398
- continue;
399
- }
400
- d[j] = {
401
- bucket: (i + 0.5) * this._config.bucket_precision,
402
- range: [
403
- i * this._config.bucket_precision,
404
- (i + 1) * this._config.bucket_precision,
405
- ],
406
- count: this.buckets[i][0],
407
- tuple: this.buckets[i] ? this.buckets[i].slice(1) : [],
408
- };
409
- }
410
- }
411
-
412
- return d;
413
- },
414
-
415
- percentile: function (p) {
416
- if (this.length === 0 || (!this._config.store_data && !this.buckets))
417
- return NaN;
418
-
419
- // If we come here, we either have sorted data or sorted buckets
420
-
421
- var v;
422
-
423
- if (p <= 0) v = 0;
424
- else if (p == 25)
425
- v = [
426
- Math.floor((this.length - 1) * 0.25),
427
- Math.ceil((this.length - 1) * 0.25),
428
- ];
429
- else if (p == 50)
430
- v = [
431
- Math.floor((this.length - 1) * 0.5),
432
- Math.ceil((this.length - 1) * 0.5),
433
- ];
434
- else if (p == 75)
435
- v = [
436
- Math.floor((this.length - 1) * 0.75),
437
- Math.ceil((this.length - 1) * 0.75),
438
- ];
439
- else if (p >= 100) v = this.length - 1;
440
- else v = Math.floor((this.length * p) / 100);
441
-
442
- if (v === 0) return this.min;
443
- if (v === this.length - 1) return this.max;
444
-
445
- if (this._config.store_data) {
446
- if (this._data_sorted === null)
447
- this._data_sorted = this.data.slice(0).sort(asc);
448
-
449
- if (typeof v == "number") return this._data_sorted[v];
450
- else return (this._data_sorted[v[0]] + this._data_sorted[v[1]]) / 2;
451
- } else {
452
- var j;
453
- if (typeof v != "number") v = (v[0] + v[1]) / 2;
454
-
455
- if (this._config.buckets) j = 0;
456
- else if (this._config.bucket_precision)
457
- j = Math.floor(this.min / this._config.bucket_precision);
458
-
459
- for (; j < this.buckets.length; j++) {
460
- if (!this.buckets[j]) continue;
461
- if (v <= this.buckets[j][0]) {
462
- break;
463
- }
464
- v -= this.buckets[j][0];
465
- }
466
-
467
- return this._get_nth_in_bucket(v, j);
468
- }
469
- },
470
-
471
- _get_nth_in_bucket: function (n, b) {
472
- var range = [];
473
- if (this._config.buckets) {
474
- range[0] = b > 0 ? this._config.buckets[b - 1] : this.min;
475
- range[1] =
476
- b < this._config.buckets.length ? this._config.buckets[b] : this.max;
477
- } else if (this._config.bucket_precision) {
478
- range[0] = Math.max(b * this._config.bucket_precision, this.min);
479
- range[1] = Math.min((b + 1) * this._config.bucket_precision, this.max);
480
- }
481
- return range[0] + ((range[1] - range[0]) * n) / this.buckets[b][0];
482
- },
483
-
484
- median: function () {
485
- return this.percentile(50);
486
- },
487
-
488
- iqr: function () {
489
- var q1, q3, fw;
490
-
491
- q1 = this.percentile(25);
492
- q3 = this.percentile(75);
493
-
494
- fw = (q3 - q1) * 1.5;
495
-
496
- return this.band_pass(q1 - fw, q3 + fw, true);
497
- },
498
-
499
- band_pass: function (low, high, open, config) {
500
- var i, j, b, b_val, i_val;
501
-
502
- if (!config) config = this._config;
503
-
504
- b = new Stats(config);
505
-
506
- if (this.length === 0) return b;
507
-
508
- if (this._config.store_data) {
509
- if (this._data_sorted === null)
510
- this._data_sorted = this.data.slice(0).sort(asc);
511
-
512
- for (
513
- i = 0;
514
- i < this.length &&
515
- (this._data_sorted[i] < high ||
516
- (!open && this._data_sorted[i] === high));
517
- i++
518
- ) {
519
- if (
520
- this._data_sorted[i] > low ||
521
- (!open && this._data_sorted[i] === low)
522
- ) {
523
- b.push(this._data_sorted[i]);
524
- }
525
- }
526
- } else if (this._config.buckets) {
527
- for (i = 0; i <= this._config.buckets.length; i++) {
528
- if (this._config.buckets[i] < this.min) continue;
529
-
530
- b_val = i == 0 ? this.min : this._config.buckets[i - 1];
531
- if (b_val < this.min) b_val = this.min;
532
- if (b_val > this.max) b_val = this.max;
533
-
534
- if (high < b_val || (open && high === b_val)) {
535
- break;
536
- }
537
- if (low < b_val || (!open && low === b_val)) {
538
- for (j = 0; j < (this.buckets[i] ? this.buckets[i][0] : 0); j++) {
539
- i_val = Stats.prototype._get_nth_in_bucket.call(this, j, i);
540
- if (
541
- (i_val > low || (!open && i_val === low)) &&
542
- (i_val < high || (!open && i_val === high))
543
- ) {
544
- b.push(i_val);
545
- }
546
- }
547
- }
548
- }
549
-
550
- b.min = Math.max(low, b.min);
551
- b.max = Math.min(high, b.max);
552
- } else if (this._config.bucket_precision) {
553
- var low_i = Math.floor(low / this._config.bucket_precision),
554
- high_i = Math.floor(high / this._config.bucket_precision) + 1;
555
-
556
- for (i = low_i; i < Math.min(this.buckets.length, high_i); i++) {
557
- for (j = 0; j < (this.buckets[i] ? this.buckets[i][0] : 0); j++) {
558
- i_val = Stats.prototype._get_nth_in_bucket.call(this, j, i);
559
- if (
560
- (i_val > low || (!open && i_val === low)) &&
561
- (i_val < high || (!open && i_val === high))
562
- ) {
563
- b.push(i_val);
564
- }
565
- }
566
- }
567
-
568
- b.min = Math.max(low, b.min);
569
- b.max = Math.min(high, b.max);
570
- }
571
-
572
- return b;
573
- },
574
-
575
- copy: function (config) {
576
- var b = Stats.prototype.band_pass.call(
577
- this,
578
- this.min,
579
- this.max,
580
- false,
581
- config
582
- );
583
-
584
- b.sum = this.sum;
585
- b.sum_of_squares = this.sum_of_squares;
586
- b.sum_of_logs = this.sum_of_logs;
587
- b.sum_of_square_of_logs = this.sum_of_square_of_logs;
588
- b.zeroes = this.zeroes;
589
-
590
- return b;
591
- },
592
-
593
- Σ: function () {
594
- return this.sum;
595
- },
596
-
597
- Π: function () {
598
- return this.zeroes > 0 ? 0 : Math.exp(this.sum_of_logs);
599
- },
600
- };
601
-
602
- Stats.prototype.σ = Stats.prototype.stddev;
603
- Stats.prototype.μ = Stats.prototype.amean;
604
-
605
- exports.Stats = Stats;
606
-
607
- /**
608
- if(process.argv[1] && process.argv[1].match(__filename)) {
609
- var s = new Stats({store_data:false, buckets: [ 1, 5, 10, 15, 20, 25, 30, 35 ]}).push(1, 2, 3);
610
- var l = process.argv.slice(2);
611
- if(!l.length) l = [10, 11, 15, 8, 13, 12, 19, 32, 17, 16];
612
- l.forEach(function(e, i, a) { a[i] = parseFloat(e, 10); });
613
- Stats.prototype.push.apply(s, l);
614
- console.log(s.data);
615
- console.log(s.amean().toFixed(2), s.μ().toFixed(2), s.stddev().toFixed(2), s.σ().toFixed(2), s.gmean().toFixed(2), s.median().toFixed(2), s.moe().toFixed(2), s.distribution());
616
- var t=s.copy({buckets: [0, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 25, 30, 35] });
617
- console.log(t.amean().toFixed(2), t.μ().toFixed(2), t.stddev().toFixed(2), t.σ().toFixed(2), t.gmean().toFixed(2), t.median().toFixed(2), t.moe().toFixed(2), t.distribution());
618
-
619
- s = new Stats({store_data: false, buckets: [1, 5, 10, 15, 20, 25, 30, 35]});
620
- s.push_tuple([1, 1, 3, 4]);
621
- s.push_tuple([2, 1, 5, 8]);
622
- s.push_tuple([3, 1, 4, 9]);
623
- s.push_tuple([1, 1, 13, 14]);
624
-
625
- console.log(s.amean(), s.median());
626
- console.log(s.distribution());
627
-
628
- s.remove_tuple([1, 1, 3, 4]);
629
- s.push_tuple([4, 1, 3, 3]);
630
- console.log(s.amean(), s.median());
631
- console.log(s.distribution());
632
-
633
- }
634
- **/