1
(function(){science.stats = {};
2
// Bandwidth selectors for Gaussian kernels.
3
// Based on R's implementations in `stats.bw`.
4
science.stats.bandwidth = {
6
// Silverman, B. W. (1986) Density Estimation. London: Chapman and Hall.
8
var hi = Math.sqrt(science.stats.variance(x));
9
if (!(lo = Math.min(hi, science.stats.iqr(x) / 1.34)))
10
(lo = hi) || (lo = Math.abs(x[1])) || (lo = 1);
11
return .9 * lo * Math.pow(x.length, -.2);
14
// Scott, D. W. (1992) Multivariate Density Estimation: Theory, Practice, and
15
// Visualization. Wiley.
17
var h = science.stats.iqr(x) / 1.34;
18
return 1.06 * Math.min(Math.sqrt(science.stats.variance(x)), h)
19
* Math.pow(x.length, -1/5);
22
science.stats.distance = {
23
euclidean: function(a, b) {
34
manhattan: function(a, b) {
38
while (++i < n) s += Math.abs(a[i] - b[i]);
41
minkowski: function(p) {
42
return function(a, b) {
46
while (++i < n) s += Math.pow(Math.abs(a[i] - b[i]), p);
47
return Math.pow(s, 1 / p);
50
chebyshev: function(a, b) {
56
x = Math.abs(a[i] - b[i]);
61
hamming: function(a, b) {
65
while (++i < n) if (a[i] !== b[i]) d++;
68
jaccard: function(a, b) {
72
while (++i < n) if (a[i] === b[i]) s++;
75
braycurtis: function(a, b) {
85
s0 += Math.abs(ai - bi);
86
s1 += Math.abs(ai + bi);
91
// Based on implementation in http://picomath.org/.
92
science.stats.erf = function(x) {
100
// Save the sign of x
101
var sign = x < 0 ? -1 : 1;
107
// A&S formula 7.1.26
108
var t = 1 / (1 + p * x);
110
1 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1)
111
* t * Math.exp(-x * x));
113
science.stats.phi = function(x) {
114
return .5 * (1 + science.stats.erf(x / Math.SQRT2));
116
// See <http://en.wikipedia.org/wiki/Kernel_(statistics)>.
117
science.stats.kernel = {
118
uniform: function(u) {
119
if (u <= 1 && u >= -1) return .5;
122
triangular: function(u) {
123
if (u <= 1 && u >= -1) return 1 - Math.abs(u);
126
epanechnikov: function(u) {
127
if (u <= 1 && u >= -1) return .75 * (1 - u * u);
130
quartic: function(u) {
131
if (u <= 1 && u >= -1) {
133
return (15 / 16) * tmp * tmp;
137
triweight: function(u) {
138
if (u <= 1 && u >= -1) {
140
return (35 / 32) * tmp * tmp * tmp;
144
gaussian: function(u) {
145
return 1 / Math.sqrt(2 * Math.PI) * Math.exp(-.5 * u * u);
147
cosine: function(u) {
148
if (u <= 1 && u >= -1) return Math.PI / 4 * Math.cos(Math.PI / 2 * u);
152
// http://exploringdata.net/den_trac.htm
153
science.stats.kde = function() {
154
var kernel = science.stats.kernel.gaussian,
156
bandwidth = science.stats.bandwidth.nrd;
158
function kde(points, i) {
159
var bw = bandwidth.call(this, sample);
160
return points.map(function(x) {
165
y += kernel((x - sample[i]) / bw);
167
return [x, y / bw / n];
171
kde.kernel = function(x) {
172
if (!arguments.length) return kernel;
177
kde.sample = function(x) {
178
if (!arguments.length) return sample;
183
kde.bandwidth = function(x) {
184
if (!arguments.length) return bandwidth;
185
bandwidth = science.functor(x);
191
// Based on figue implementation by Jean-Yves Delort.
192
// http://code.google.com/p/figue/
193
science.stats.kmeans = function() {
194
var distance = science.stats.distance.euclidean,
195
maxIterations = 1000,
198
function kmeans(vectors) {
199
var n = vectors.length,
204
centroids = science_stats_kmeansRandom(k, vectors),
213
while (repeat && iterations < maxIterations) {
215
j = -1; while (++j < k) {
219
i = -1; while (++i < n) {
222
j = -1; while (++j < k) {
223
d = distance.call(this, centroids[j], x);
229
clusterSizes[assignments[i] = best]++;
232
// Update centroids step.
234
i = -1; while (++i < n) {
237
if (d == null) newCentroids[x] = vectors[i].slice();
239
j = -1; while (++j < d.length) {
240
d[j] += vectors[i][j];
244
j = -1; while (++j < k) {
246
d = 1 / clusterSizes[j];
247
i = -1; while (++i < x.length) x[i] *= d;
250
// Check convergence.
252
j = -1; while (++j < k) {
253
if (!science_stats_kmeansCompare(newCentroids[j], centroids[j])) {
258
centroids = newCentroids;
261
return {assignments: assignments, centroids: centroids};
264
kmeans.k = function(x) {
265
if (!arguments.length) return k;
270
kmeans.distance = function(x) {
271
if (!arguments.length) return distance;
279
function science_stats_kmeansCompare(a, b) {
280
if (!a || !b || a.length !== b.length) return false;
283
while (++i < n) if (a[i] !== b[i]) return false;
287
// Returns an array of k distinct vectors randomly selected from the input
288
// array of vectors. Returns null if k > n or if there are less than k distinct
289
// objects in vectors.
290
function science_stats_kmeansRandom(k, vectors) {
291
var n = vectors.length;
292
if (k > n) return null;
294
var selected_vectors = [];
295
var selected_indices = [];
296
var tested_indices = {};
303
while (selected < k) {
304
if (tested === n) return null;
306
var random_index = Math.floor(Math.random() * n);
307
if (random_index in tested_indices) continue;
309
tested_indices[random_index] = 1;
311
vector = vectors[random_index];
313
for (i = 0; i < selected; i++) {
314
if (science_stats_kmeansCompare(vector, selected_vectors[i])) {
320
selected_vectors[selected] = vector;
321
selected_indices[selected] = random_index;
325
return selected_vectors;
327
science.stats.hcluster = function() {
328
var distance = science.stats.distance.euclidean,
329
linkage = "simple"; // simple, complete or average
331
function hcluster(vectors) {
332
var n = vectors.length,
346
// Initialise distance matrix and vector of closest clusters.
347
i = -1; while (++i < n) {
350
j = -1; while (++j < n) {
351
distMatrix[i][j] = i === j ? Infinity : distance(vectors[i] , vectors[j]);
352
if (distMatrix[i][dMin[i]] > distMatrix[i][j]) dMin[i] = j;
356
// create leaves of the tree
357
i = -1; while (++i < n) {
363
centroid: vectors[i],
371
for (p = 0; p < n-1; p++) {
372
// find the closest pair of clusters
374
for (i = 0; i < n; i++) {
375
if (distMatrix[i][dMin[i]] < distMatrix[c1][dMin[c1]]) c1 = i;
379
// create node to store cluster info
380
c1Cluster = clusters[c1][0];
381
c2Cluster = clusters[c2][0];
386
dist: distMatrix[c1][c2],
387
centroid: calculateCentroid(c1Cluster.size, c1Cluster.centroid,
388
c2Cluster.size, c2Cluster.centroid),
389
size: c1Cluster.size + c2Cluster.size,
390
depth: 1 + Math.max(c1Cluster.depth, c2Cluster.depth)
392
clusters[c1].splice(0, 0, newCluster);
393
cSize[c1] += cSize[c2];
395
// overwrite row c1 with respect to the linkage type
396
for (j = 0; j < n; j++) {
399
if (distMatrix[c1][j] > distMatrix[c2][j])
400
distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j];
403
if (distMatrix[c1][j] < distMatrix[c2][j])
404
distMatrix[j][c1] = distMatrix[c1][j] = distMatrix[c2][j];
407
distMatrix[j][c1] = distMatrix[c1][j] = (cSize[c1] * distMatrix[c1][j] + cSize[c2] * distMatrix[c2][j]) / (cSize[c1] + cSize[j]);
411
distMatrix[c1][c1] = Infinity;
413
// infinity out old row c2 and column c2
414
for (i = 0; i < n; i++)
415
distMatrix[i][c2] = distMatrix[c2][i] = Infinity;
417
// update dmin and replace ones that previous pointed to c2 to point to c1
418
for (j = 0; j < n; j++) {
419
if (dMin[j] == c2) dMin[j] = c1;
420
if (distMatrix[c1][j] < distMatrix[c1][dMin[c1]]) dMin[c1] = j;
423
// keep track of the last added cluster
430
hcluster.distance = function(x) {
431
if (!arguments.length) return distance;
439
function calculateCentroid(c1Size, c1Centroid, c2Size, c2Centroid) {
440
var newCentroid = [],
441
newSize = c1Size + c2Size,
442
n = c1Centroid.length,
445
newCentroid[i] = (c1Size * c1Centroid[i] + c2Size * c2Centroid[i]) / newSize;
449
science.stats.iqr = function(x) {
450
var quartiles = science.stats.quantiles(x, [.25, .75]);
451
return quartiles[1] - quartiles[0];
453
// Based on org.apache.commons.math.analysis.interpolation.LoessInterpolator
454
// from http://commons.apache.org/math/
455
science.stats.loess = function() {
460
function smooth(xval, yval, weights) {
464
if (n !== yval.length) throw {error: "Mismatched array lengths"};
465
if (n == 0) throw {error: "At least one point required."};
467
if (arguments.length < 3) {
469
i = -1; while (++i < n) weights[i] = 1;
472
science_stats_loessFiniteReal(xval);
473
science_stats_loessFiniteReal(yval);
474
science_stats_loessFiniteReal(weights);
475
science_stats_loessStrictlyIncreasing(xval);
477
if (n == 1) return [yval[0]];
478
if (n == 2) return [yval[0], yval[1]];
480
var bandwidthInPoints = Math.floor(bandwidth * n);
482
if (bandwidthInPoints < 2) throw {error: "Bandwidth too small."};
486
robustnessWeights = [];
488
// Do an initial fit and 'robustnessIters' robustness iterations.
489
// This is equivalent to doing 'robustnessIters+1' robustness iterations
490
// starting with all robustness weights set to 1.
491
i = -1; while (++i < n) {
494
robustnessWeights[i] = 1;
498
while (++iter <= robustnessIters) {
499
var bandwidthInterval = [0, bandwidthInPoints - 1];
500
// At each x, compute a local weighted linear regression
502
i = -1; while (++i < n) {
505
// Find out the interval of source points on which
506
// a regression is to be made.
508
science_stats_loessUpdateBandwidthInterval(xval, weights, i, bandwidthInterval);
511
var ileft = bandwidthInterval[0],
512
iright = bandwidthInterval[1];
514
// Compute the point of the bandwidth interval that is
516
var edge = (xval[i] - xval[ileft]) > (xval[iright] - xval[i]) ? ileft : iright;
518
// Compute a least-squares linear fit weighted by
519
// the product of robustness weights and the tricube
521
// See http://en.wikipedia.org/wiki/Linear_regression
522
// (section "Univariate linear case")
523
// and http://en.wikipedia.org/wiki/Weighted_least_squares
524
// (section "Weighted least squares")
530
denom = Math.abs(1 / (xval[edge] - x));
532
for (var k = ileft; k <= iright; ++k) {
535
dist = k < i ? x - xk : xk - x,
536
w = science_stats_loessTricube(dist * denom) * robustnessWeights[k] * weights[k],
540
sumXSquared += xk * xkw;
545
var meanX = sumX / sumWeights,
546
meanY = sumY / sumWeights,
547
meanXY = sumXY / sumWeights,
548
meanXSquared = sumXSquared / sumWeights;
550
var beta = (Math.sqrt(Math.abs(meanXSquared - meanX * meanX)) < accuracy)
551
? 0 : ((meanXY - meanX * meanY) / (meanXSquared - meanX * meanX));
553
var alpha = meanY - beta * meanX;
555
res[i] = beta * x + alpha;
556
residuals[i] = Math.abs(yval[i] - res[i]);
559
// No need to recompute the robustness weights at the last
560
// iteration, they won't be needed anymore
561
if (iter === robustnessIters) {
565
// Recompute the robustness weights.
567
// Find the median residual.
568
var sortedResiduals = residuals.slice();
569
sortedResiduals.sort();
570
var medianResidual = sortedResiduals[Math.floor(n / 2)];
572
if (Math.abs(medianResidual) < accuracy)
577
i = -1; while (++i < n) {
578
arg = residuals[i] / (6 * medianResidual);
579
robustnessWeights[i] = (arg >= 1) ? 0 : ((w = 1 - arg * arg) * w);
586
smooth.bandwidth = function(x) {
587
if (!arguments.length) return x;
592
smooth.robustnessIterations = function(x) {
593
if (!arguments.length) return x;
598
smooth.accuracy = function(x) {
599
if (!arguments.length) return x;
607
function science_stats_loessFiniteReal(values) {
608
var n = values.length,
611
while (++i < n) if (!isFinite(values[i])) return false;
616
function science_stats_loessStrictlyIncreasing(xval) {
620
while (++i < n) if (xval[i - 1] >= xval[i]) return false;
625
// Compute the tricube weight function.
626
// http://en.wikipedia.org/wiki/Local_regression#Weight_function
627
function science_stats_loessTricube(x) {
628
return (x = 1 - x * x * x) * x * x;
631
// Given an index interval into xval that embraces a certain number of
632
// points closest to xval[i-1], update the interval so that it embraces
633
// the same number of points closest to xval[i], ignoring zero weights.
634
function science_stats_loessUpdateBandwidthInterval(
635
xval, weights, i, bandwidthInterval) {
637
var left = bandwidthInterval[0],
638
right = bandwidthInterval[1];
640
// The right edge should be adjusted if the next point to the right
641
// is closer to xval[i] than the leftmost point of the current interval
642
var nextRight = science_stats_loessNextNonzero(weights, right);
643
if ((nextRight < xval.length) && (xval[nextRight] - xval[i]) < (xval[i] - xval[left])) {
644
var nextLeft = science_stats_loessNextNonzero(weights, left);
645
bandwidthInterval[0] = nextLeft;
646
bandwidthInterval[1] = nextRight;
650
function science_stats_loessNextNonzero(weights, i) {
652
while (j < weights.length && weights[j] === 0) j++;
655
// Welford's algorithm.
656
science.stats.mean = function(x) {
658
if (n === 0) return NaN;
661
while (++i < n) m += (x[i] - m) / (i + 1);
664
science.stats.median = function(x) {
665
return science.stats.quantiles(x, [.5])[0];
667
science.stats.mode = function(x) {
668
x = x.slice().sort(science.ascending);
678
if ((v = x[i]) !== last) {
679
if ((tmp = i - l) > max) {
689
// Uses R's quantile algorithm type=7.
690
science.stats.quantiles = function(d, quantiles) {
691
d = d.slice().sort(science.ascending);
692
var n_1 = d.length - 1;
693
return quantiles.map(function(q) {
694
if (q === 0) return d[0];
695
else if (q === 1) return d[n_1];
697
var index = 1 + q * n_1,
698
lo = Math.floor(index),
702
return h === 0 ? a : a + h * (d[lo] - a);
705
// Unbiased estimate of a sample's variance.
706
// Also known as the sample variance, where the denominator is n - 1.
707
science.stats.variance = function(x) {
709
if (n < 1) return NaN;
710
if (n === 1) return 0;
711
var mean = science.stats.mean(x),
b'\\ No newline at end of file'