OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2012, 2013 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 - sum * sum / (numberOfSamples - 1) / numberOfSamples); | 58 - sum * sum / (numberOfSamples - 1) / numberOfSamples); |
59 } | 59 } |
60 | 60 |
61 this.supportedConfidenceLevels = function () { | 61 this.supportedConfidenceLevels = function () { |
62 var supportedLevels = []; | 62 var supportedLevels = []; |
63 for (var quantile in tDistributionInverseCDF) | 63 for (var quantile in tDistributionInverseCDF) |
64 supportedLevels.push((1 - (1 - quantile) * 2).toFixed(2)); | 64 supportedLevels.push((1 - (1 - quantile) * 2).toFixed(2)); |
65 return supportedLevels; | 65 return supportedLevels; |
66 } | 66 } |
67 | 67 |
68 // Computes the delta d s.t. (mean - d, mean + d) is the confidence interval
with the specified confidence level in O(1). | 68 this.quantile = function (confidenceLevel, numberOfSamples, opt_degreesOfFre
edom) { |
69 this.confidenceIntervalDelta = function (confidenceLevel, numberOfSamples, s
um, squareSum) { | |
70 var probability = (1 - (1 - confidenceLevel) / 2); | 69 var probability = (1 - (1 - confidenceLevel) / 2); |
71 if (!(probability in tDistributionInverseCDF)) { | 70 if (!(probability in tDistributionInverseCDF)) { |
72 console.warn('We only support ' + this.supportedConfidenceLevels().m
ap( | 71 console.warn('We only support ' + this.supportedConfidenceLevels().m
ap( |
73 function (level) { return level * 100 + '%'; } ).join(', ') + '
confidence intervals.'); | 72 function (level) { return level * 100 + '%'; } ).join(', ') + '
confidence intervals.'); |
74 return NaN; | 73 return NaN; |
75 } | 74 } |
76 if (numberOfSamples < 2) | 75 if (numberOfSamples < 2) |
77 return Number.POSITIVE_INFINITY; | 76 return Number.POSITIVE_INFINITY; |
78 | 77 |
79 var cdfForProbability = tDistributionInverseCDF[probability]; | 78 var cdfForProbability = tDistributionInverseCDF[probability]; |
80 var degreesOfFreedom = numberOfSamples - 1; | 79 var degreesOfFreedom = opt_degreesOfFreedom; |
| 80 if (degreesOfFreedom === undefined) |
| 81 degreesOfFreedom = numberOfSamples - 1; |
81 | 82 |
82 // tDistributionQuantile(degreesOfFreedom, confidenceLevel) * sampleStan
dardDeviation / sqrt(numberOfSamples) * S/sqrt(numberOfSamples) | 83 // tDistributionQuantile(degreesOfFreedom, confidenceLevel) * sampleStan
dardDeviation / sqrt(numberOfSamples) * S/sqrt(numberOfSamples) |
83 if (degreesOfFreedom <= 100) | 84 if (degreesOfFreedom <= 100) |
84 var quantile = cdfForProbability[degreesOfFreedom - 1]; // The first e
ntry is for the one degree of freedom. | 85 return cdfForProbability[degreesOfFreedom - 1]; // The first entry is
for the one degree of freedom. |
85 else if (degreesOfFreedom <= 300) | 86 else if (degreesOfFreedom <= 300) |
86 var quantile = cdfForProbability[Math.round(degreesOfFreedom / 10) + 1
00 - 10 - 1]; | 87 return cdfForProbability[Math.round(degreesOfFreedom / 10) + 100 - 10
- 1]; |
87 else if (degreesOfFreedom <= 1300) | 88 else if (degreesOfFreedom <= 1300) |
88 var quantile = cdfForProbability[Math.round(degreesOfFreedom / 100) +
120 - 3 - 1]; | 89 return cdfForProbability[Math.round(degreesOfFreedom / 100) + 120 - 3
- 1]; |
89 else | 90 else |
90 var quantile = cdfForProbability[cdfForProbability.length - 1]; | 91 return cdfForProbability[cdfForProbability.length - 1]; |
91 return quantile * this.sampleStandardDeviation(numberOfSamples, sum, squ
areSum) / Math.sqrt(numberOfSamples); | |
92 } | 92 } |
93 | 93 |
| 94 // Computes the delta d s.t. (mean - d, mean + d) is the confidence interval
with the specified confidence level in O(1). |
| 95 this.confidenceIntervalDelta = function (confidenceLevel, numberOfSamples, s
um, squareSum) { |
| 96 var sampleStandardDeviation = this.sampleStandardDeviation(numberOfSampl
es, sum, squareSum); |
| 97 return this.confidenceIntervalDeltaFromStd(confidenceLevel, numberOfSamp
les, sampleStandardDeviation); |
| 98 } |
| 99 |
| 100 this.confidenceIntervalDeltaFromStd = function (confidenceLevel, numberOfSam
ples, sampleStandardDeviation, opt_degreesOfFreedom) { |
| 101 var quantile = this.quantile(confidenceLevel, numberOfSamples, opt_degre
esOfFreedom); |
| 102 return quantile * sampleStandardDeviation / Math.sqrt(numberOfSamples); |
| 103 } |
| 104 |
| 105 |
94 this.confidenceInterval = function (values, probability) { | 106 this.confidenceInterval = function (values, probability) { |
95 var sum = this.sum(values); | 107 var sum = this.sum(values); |
96 var mean = sum / values.length; | 108 var mean = sum / values.length; |
97 var delta = this.confidenceIntervalDelta(probability || 0.95, values.len
gth, sum, this.squareSum(values)); | 109 var delta = this.confidenceIntervalDelta(probability || 0.95, values.len
gth, sum, this.squareSum(values)); |
98 return [mean - delta, mean + delta]; | 110 return [mean - delta, mean + delta]; |
99 } | 111 } |
100 | 112 |
101 // See http://en.wikipedia.org/wiki/Student's_t-distribution#Table_of_select
ed_values | 113 // See http://en.wikipedia.org/wiki/Student's_t-distribution#Table_of_select
ed_values |
102 // This table contains one sided (a.k.a. tail) values. | 114 // This table contains one sided (a.k.a. tail) values. |
103 // Use TINV((1 - probability) * 2, df) in your favorite spreadsheet software
to compute these. | 115 // Use TINV((1 - probability) * 2, df) in your favorite spreadsheet software
to compute these. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 // Infinity | 192 // Infinity |
181 2.326348], | 193 2.326348], |
182 }; | 194 }; |
183 | 195 |
184 })(); | 196 })(); |
185 | 197 |
186 if (typeof module != 'undefined') { | 198 if (typeof module != 'undefined') { |
187 for (var key in Statistics) | 199 for (var key in Statistics) |
188 module.exports[key] = Statistics[key]; | 200 module.exports[key] = Statistics[key]; |
189 } | 201 } |
OLD | NEW |