-
Notifications
You must be signed in to change notification settings - Fork 2
/
stat.js
100 lines (80 loc) · 2.66 KB
/
stat.js
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
var _ = require('underscore');
var util = require('util');
module.exports = function (numbers) {
this.numArr = numbers;
}
module.exports.prototype = {
_dec:function (num, numOfDec) {
var pow10s = Math.pow(10, numOfDec || 0);
return (numOfDec) ? Math.round(pow10s * num) / pow10s : num;
},
set_data:function (d) {
this.numArr = d;
},
avg:function (numOfDec) {
if (!_.isArray(this.numArr)) {
return false;
}
var i = this.numArr.length,
sum = 0;
while (i--) {
sum += this.numArr[i];
}
var avg = (sum / this.numArr.length);
return arguments.length ? this._dec(avg, numOfDec) : avg;
},
variance:function (numOfDec) {
if (!_.isArray(this.numArr)) {
return false;
}
var avg = this.avg(numOfDec),
i = this.numArr.length,
v = 0;
while (i--) {
v += Math.pow((this.numArr[i] - avg), 2);
}
v /= this.numArr.length;
return arguments.length ? this._dec(v, numOfDec) : v;
},
std_dev:function (numOfDec) {
var stdDev = Math.sqrt(this.variance(numOfDec));
return arguments.length ? this._dec(stdDev, numOfDec) : stdDev;
},
med_ratio:function (ratio) {
var end_count = Math.max(1, Math.ceil(this.numArr.length * ratio));
var dists = [];
var last_dist = false;
var dist;
var sorted = _.sortBy(this.numArr, function (v) {
return v;
});
// console.log('sorted dists: %s', util.inspect(sorted));
sorted.forEach(function (v) {
dists.push({value:v, last_gap: -1, next_gap: -1});
})
dists.forEach(function(dist, i){
if (i > 0){
var last_dist = dists[i - 1];
last_dist.next_gap = dist.value - last_dist.value;
dist.last_gap = last_dist.next_gap;
}
})
dists.forEach(function (dist) {
if (dist.last_gap == -1){
dist.gap = 2 * dist.next_gap;
} else if (dist.next_gap == -1){
dist.gap = 2 * dist.last_gap;
} else {
dist.gap = (dist.last_gap + dist.next_gap);
}
})
dists = _.sortBy(dists, function (dist) {
return dist.gap;
});
var sum = 0;
var slice = dists.slice(0, end_count);
// console.log('gap rated dists: %s', util.inspect(slice));
slice.forEach(function(d){ sum += d.value;});
return sum / (1.0 * end_count);
}
}