1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * modification, are permitted provided that the following conditions
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * are met:
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    documentation and/or other materials provided with the distribution.
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THE POSSIBILITY OF SUCH DAMAGE.
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)var Statistics = new (function () {
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.max = function (values) {
29926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return Math.max.apply(Math, values);
30926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
31926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.min = function (values) {
33926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return Math.min.apply(Math, values);
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.sum = function (values) {
37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return values.reduce(function (a, b) { return a + b; }, 0);
38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.squareSum = function (values) {
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return values.reduce(function (sum, value) { return sum + value * value;}, 0);
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // With sum and sum of squares, we can compute the sample standard deviation in O(1).
45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // See https://rniwa.com/2012-11-10/sample-standard-deviation-in-terms-of-sum-and-square-sum-of-samples/
46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.sampleStandardDeviation = function (numberOfSamples, sum, squareSum) {
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (numberOfSamples < 2)
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            return 0;
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return Math.sqrt(squareSum / (numberOfSamples - 1)
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            - sum * sum / (numberOfSamples - 1) / numberOfSamples);
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
53926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.supportedConfidenceLevels = function () {
54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var supportedLevels = [];
55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        for (var quantile in tDistributionInverseCDF)
56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            supportedLevels.push((1 - (1 - quantile) * 2).toFixed(2));
57926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return supportedLevels;
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
60926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // Computes the delta d s.t. (mean - d, mean + d) is the confidence interval with the specified confidence level in O(1).
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.confidenceIntervalDelta = function (confidenceLevel, numberOfSamples, sum, squareSum) {
62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var probability = (1 - (1 - confidenceLevel) / 2);
63926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (!(probability in tDistributionInverseCDF)) {
64e6d4491e48613634a83c1957c72759da80987961Ben Murdoch            console.warn('We only support ' + this.supportedConfidenceLevels().map(
65e6d4491e48613634a83c1957c72759da80987961Ben Murdoch                function (level) { return level * 100 + '%'; } ).join(', ') + ' confidence intervals.');
66e6d4491e48613634a83c1957c72759da80987961Ben Murdoch            return NaN;
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        }
68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        if (numberOfSamples - 2 < 0)
69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            return NaN;
70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
71926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var cdfForProbability = tDistributionInverseCDF[probability];
72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var degreesOfFreedom = numberOfSamples - 1;
73e6d4491e48613634a83c1957c72759da80987961Ben Murdoch        if (degreesOfFreedom > cdfForProbability.length) {
74e6d4491e48613634a83c1957c72759da80987961Ben Murdoch            console.warn('We only support up to ' + cdfForProbability.length + ' degrees of freedom');
75e6d4491e48613634a83c1957c72759da80987961Ben Murdoch            return NaN;
76e6d4491e48613634a83c1957c72759da80987961Ben Murdoch        }
77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        // tDistributionQuantile(degreesOfFreedom, confidenceLevel) * sampleStandardDeviation / sqrt(numberOfSamples) * S/sqrt(numberOfSamples)
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var quantile = cdfForProbability[degreesOfFreedom - 1]; // The first entry is for the one degree of freedom.
80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return quantile * this.sampleStandardDeviation(numberOfSamples, sum, squareSum) / Math.sqrt(numberOfSamples);
81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
82926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    this.confidenceInterval = function (values, probability) {
84926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var sum = this.sum(values);
85926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var mean = sum / values.length;
86926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        var delta = this.confidenceIntervalDelta(probability || 0.95, values.length, sum, this.squareSum(values));
87926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        return [mean - delta, mean + delta];
88926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    }
89926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // See http://en.wikipedia.org/wiki/Student's_t-distribution#Table_of_selected_values
91926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // This table contains one sided (a.k.a. tail) values.
92926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    var tDistributionInverseCDF = {
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        0.9: [
94926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            3.077684, 1.885618, 1.637744, 1.533206, 1.475884, 1.439756, 1.414924, 1.396815, 1.383029, 1.372184,
95926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.363430, 1.356217, 1.350171, 1.345030, 1.340606, 1.336757, 1.333379, 1.330391, 1.327728, 1.325341,
96926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.323188, 1.321237, 1.319460, 1.317836, 1.316345, 1.314972, 1.313703, 1.312527, 1.311434, 1.310415,
97926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.309464, 1.308573, 1.307737, 1.306952, 1.306212, 1.305514, 1.304854, 1.304230, 1.303639, 1.303077,
98926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.302543, 1.302035, 1.301552, 1.301090, 1.300649, 1.300228, 1.299825, 1.299439, 1.299069, 1.298714,
99926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
100926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.298373, 1.298045, 1.297730, 1.297426, 1.297134, 1.296853, 1.296581, 1.296319, 1.296066, 1.295821,
101926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.295585, 1.295356, 1.295134, 1.294920, 1.294712, 1.294511, 1.294315, 1.294126, 1.293942, 1.293763,
102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.293589, 1.293421, 1.293256, 1.293097, 1.292941, 1.292790, 1.292643, 1.292500, 1.292360, 1.292224,
103926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.292091, 1.291961, 1.291835, 1.291711, 1.291591, 1.291473, 1.291358, 1.291246, 1.291136, 1.291029,
104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.290924, 1.290821, 1.290721, 1.290623, 1.290527, 1.290432, 1.290340, 1.290250, 1.290161, 1.290075],
105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        0.95: [
106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            6.313752, 2.919986, 2.353363, 2.131847, 2.015048, 1.943180, 1.894579, 1.859548, 1.833113, 1.812461,
107926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.795885, 1.782288, 1.770933, 1.761310, 1.753050, 1.745884, 1.739607, 1.734064, 1.729133, 1.724718,
108926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.720743, 1.717144, 1.713872, 1.710882, 1.708141, 1.705618, 1.703288, 1.701131, 1.699127, 1.697261,
109926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.695519, 1.693889, 1.692360, 1.690924, 1.689572, 1.688298, 1.687094, 1.685954, 1.684875, 1.683851,
110926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.682878, 1.681952, 1.681071, 1.680230, 1.679427, 1.678660, 1.677927, 1.677224, 1.676551, 1.675905,
111926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.675285, 1.674689, 1.674116, 1.673565, 1.673034, 1.672522, 1.672029, 1.671553, 1.671093, 1.670649,
113926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.670219, 1.669804, 1.669402, 1.669013, 1.668636, 1.668271, 1.667916, 1.667572, 1.667239, 1.666914,
114926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.666600, 1.666294, 1.665996, 1.665707, 1.665425, 1.665151, 1.664885, 1.664625, 1.664371, 1.664125,
115926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.663884, 1.663649, 1.663420, 1.663197, 1.662978, 1.662765, 1.662557, 1.662354, 1.662155, 1.661961,
116926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.661771, 1.661585, 1.661404, 1.661226, 1.661052, 1.660881, 1.660715, 1.660551, 1.660391, 1.660234],
117926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        0.975: [
118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            12.706205, 4.302653, 3.182446, 2.776445, 2.570582, 2.446912, 2.364624, 2.306004, 2.262157, 2.228139,
119926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.200985, 2.178813, 2.160369, 2.144787, 2.131450, 2.119905, 2.109816, 2.100922, 2.093024, 2.085963,
120926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.079614, 2.073873, 2.068658, 2.063899, 2.059539, 2.055529, 2.051831, 2.048407, 2.045230, 2.042272,
121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.039513, 2.036933, 2.034515, 2.032245, 2.030108, 2.028094, 2.026192, 2.024394, 2.022691, 2.021075,
122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.019541, 2.018082, 2.016692, 2.015368, 2.014103, 2.012896, 2.011741, 2.010635, 2.009575, 2.008559,
123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
124926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.007584, 2.006647, 2.005746, 2.004879, 2.004045, 2.003241, 2.002465, 2.001717, 2.000995, 2.000298,
125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.999624, 1.998972, 1.998341, 1.997730, 1.997138, 1.996564, 1.996008, 1.995469, 1.994945, 1.994437,
126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.993943, 1.993464, 1.992997, 1.992543, 1.992102, 1.991673, 1.991254, 1.990847, 1.990450, 1.990063,
127926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.989686, 1.989319, 1.988960, 1.988610, 1.988268, 1.987934, 1.987608, 1.987290, 1.986979, 1.986675,
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            1.986377, 1.986086, 1.985802, 1.985523, 1.985251, 1.984984, 1.984723, 1.984467, 1.984217, 1.983972],
129926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        0.99: [
130926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            31.820516, 6.964557, 4.540703, 3.746947, 3.364930, 3.142668, 2.997952, 2.896459, 2.821438, 2.763769,
131926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.718079, 2.680998, 2.650309, 2.624494, 2.602480, 2.583487, 2.566934, 2.552380, 2.539483, 2.527977,
132926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.517648, 2.508325, 2.499867, 2.492159, 2.485107, 2.478630, 2.472660, 2.467140, 2.462021, 2.457262,
133926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.452824, 2.448678, 2.444794, 2.441150, 2.437723, 2.434494, 2.431447, 2.428568, 2.425841, 2.423257,
134926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.420803, 2.418470, 2.416250, 2.414134, 2.412116, 2.410188, 2.408345, 2.406581, 2.404892, 2.403272,
135926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
136926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.401718, 2.400225, 2.398790, 2.397410, 2.396081, 2.394801, 2.393568, 2.392377, 2.391229, 2.390119,
137926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.389047, 2.388011, 2.387008, 2.386037, 2.385097, 2.384186, 2.383302, 2.382446, 2.381615, 2.380807,
138926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.380024, 2.379262, 2.378522, 2.377802, 2.377102, 2.376420, 2.375757, 2.375111, 2.374482, 2.373868,
139926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.373270, 2.372687, 2.372119, 2.371564, 2.371022, 2.370493, 2.369977, 2.369472, 2.368979, 2.368497,
140926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)            2.368026, 2.367566, 2.367115, 2.366674, 2.366243, 2.365821, 2.365407, 2.365002, 2.364606, 2.364217]
141926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    };
142926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
143926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)})();
144926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
145926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)if (typeof module != 'undefined') {
146926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    for (var key in Statistics)
147926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        module.exports[key] = Statistics[key];
148926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
149