aboutsummaryrefslogtreecommitdiff
path: root/gn2/wqflask/static/new/javascript/stats.js
blob: 6c443ab3fcee3f88da2141e2df4ae64cbc02b4cb (plain)
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
// Generated by CoffeeScript 1.8.0
var Stats, bxd_only;

Stats = (function() {
  function Stats(the_values) {
    this.the_values = the_values;
  }

  Stats.prototype.add_value = function(value) {
    return this.the_values.push(value);
  };

  Stats.prototype.n_of_samples = function() {
    return this.the_values.length;
  };

  Stats.prototype.sum = function() {
    var total, value, _i, _len, _ref;
    total = 0;
    _ref = this.the_values;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      value = _ref[_i];
      total += value;
    }
    return total;
  };

  Stats.prototype.mean = function() {
    return this.sum() / this.n_of_samples();
  };

  Stats.prototype.median = function() {
    var is_odd, median_position, the_values_sorted;
    is_odd = this.the_values.length % 2;
    median_position = Math.floor(this.the_values.length / 2);
    the_values_sorted = this.the_values.sort(function(a, b) {
      return a - b;
    });
    if (is_odd) {
      return the_values_sorted[median_position];
    } else {
      return (the_values_sorted[median_position] + the_values_sorted[median_position - 1]) / 2;
    }
  };

  Stats.prototype.std_dev = function() {
    var step_a, step_b, sum, value, _i, _len, _ref;
    sum = 0;
    _ref = this.the_values;
    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
      value = _ref[_i];
      step_a = Math.pow(value - this.mean(), 2);
      sum += step_a;
    }
    step_b = sum / this.the_values.length;
    return Math.sqrt(step_b);
  };

  Stats.prototype.std_error = function() {
    return this.std_dev() / Math.sqrt(this.n_of_samples());
  };

  Stats.prototype.min = function() {
    return Math.min.apply(Math, this.the_values);
  };

  Stats.prototype.max = function() {
    return Math.max.apply(Math, this.the_values);
  };

  Stats.prototype.range = function() {
    if (js_data.dataset_type == "ProbeSet"){
      if (js_data.data_scale == "linear_positive"){
        return Math.log2(this.max()) - Math.log2(this.min());
      } else {
        return this.max() - this.min()
      }
    } else {
      return this.max() - this.min()
    }
  };

  Stats.prototype.range_fold = function() {
    if (js_data.dataset_type == "ProbeSet"){
      return Math.pow(2, this.range());
    } else {
      return this.range()
    }
  };

  Stats.prototype.interquartile = function() {
    var iq, length, q1, q3;
    length = this.the_values.length;
    if (js_data.dataset_type == "ProbeSet" && js_data.data_scale == "linear_positive") {
      q1 = Math.log2(this.the_values[Math.floor(length * .25)]);
      q3 = Math.log2(this.the_values[Math.floor(length * .75)]);
    } else {
      q1 = this.the_values[Math.floor(length * .25)];
      q3 = this.the_values[Math.floor(length * .75)];
    }
    iq = q3 - q1;
    if (js_data.dataset_type == "ProbeSet") {
        return Math.pow(2, iq);
    } else {
        return iq;
    }
  };

  Stats.prototype.skewness = function() {
    var len = this.the_values.length,
        delta = 0,
        delta_n = 0,
        term1 = 0,
        N = 0,
        mean = 0,
        M2 = 0,
        M3 = 0,
        g;

    for ( var i = 0; i < len; i++ ) {
      N += 1;

      delta = this.the_values[ i ] - mean;
      delta_n = delta / N;

      term1 = delta * delta_n * (N-1);

      M3 += term1*delta_n*(N-2) - 3*delta_n*M2;
      M2 += term1;
      mean += delta_n;
    }
    // Calculate the population skewness:
    g = Math.sqrt( N )*M3 / Math.pow( M2, 3/2 );

    // Return the corrected sample skewness:
    return Math.sqrt( N*(N-1))*g / (N-2);
  };

  Stats.prototype.kurtosis = function() {
    var len = this.the_values.length,
        delta = 0,
        delta_n = 0,
        delta_n2 = 0,
        term1 = 0,
        N = 0,
        mean = 0,
        M2 = 0,
        M3 = 0,
        M4 = 0,
        g;

    for ( var i = 0; i < len; i++ ) {
      N += 1;

      delta = this.the_values[ i ] - mean;
      delta_n = delta / N;
      delta_n2 = delta_n * delta_n;

      term1 = delta * delta_n * (N-1);

      M4 += term1*delta_n2*(N*N - 3*N + 3) + 6*delta_n2*M2 - 4*delta_n*M3;
      M3 += term1*delta_n*(N-2) - 3*delta_n*M2;
      M2 += term1;
      mean += delta_n;
    }
    // Calculate the population excess kurtosis:
    g = N*M4 / (M2*M2) - 3;
    //Return the corrected sample excess kurtosis:
    return (N-1) / ( (N-2)*(N-3) ) * ( (N+1)*g + 6 );
  };

  return Stats;

})();

window.Stats = Stats;