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) { |
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 = numberOfSamples - 1; |
petrcermak
2015/09/09 18:44:40
The degrees of freedom should be changed as well.
nednguyen
2015/09/09 22:29:06
Done. But this is no-op change for now. I will hoo
petrcermak
2015/09/10 09:04:08
Great, thanks!
| |
81 | 80 |
82 // tDistributionQuantile(degreesOfFreedom, confidenceLevel) * sampleStan dardDeviation / sqrt(numberOfSamples) * S/sqrt(numberOfSamples) | 81 // tDistributionQuantile(degreesOfFreedom, confidenceLevel) * sampleStan dardDeviation / sqrt(numberOfSamples) * S/sqrt(numberOfSamples) |
83 if (degreesOfFreedom <= 100) | 82 if (degreesOfFreedom <= 100) |
84 var quantile = cdfForProbability[degreesOfFreedom - 1]; // The first e ntry is for the one degree of freedom. | 83 return cdfForProbability[degreesOfFreedom - 1]; // The first entry is for the one degree of freedom. |
85 else if (degreesOfFreedom <= 300) | 84 else if (degreesOfFreedom <= 300) |
86 var quantile = cdfForProbability[Math.round(degreesOfFreedom / 10) + 1 00 - 10 - 1]; | 85 return cdfForProbability[Math.round(degreesOfFreedom / 10) + 100 - 10 - 1]; |
87 else if (degreesOfFreedom <= 1300) | 86 else if (degreesOfFreedom <= 1300) |
88 var quantile = cdfForProbability[Math.round(degreesOfFreedom / 100) + 120 - 3 - 1]; | 87 return cdfForProbability[Math.round(degreesOfFreedom / 100) + 120 - 3 - 1]; |
89 else | 88 else |
90 var quantile = cdfForProbability[cdfForProbability.length - 1]; | 89 return cdfForProbability[cdfForProbability.length - 1]; |
90 } | |
91 | |
92 // Computes the delta d s.t. (mean - d, mean + d) is the confidence interval with the specified confidence level in O(1). | |
93 this.confidenceIntervalDelta = function (confidenceLevel, numberOfSamples, s um, squareSum) { | |
petrcermak
2015/09/09 18:44:40
To reduce code duplication, this function should u
nednguyen
2015/09/09 22:29:06
Done.
| |
94 var quantile = this.quantile(confidenceLevel, numberOfSamples); | |
91 return quantile * this.sampleStandardDeviation(numberOfSamples, sum, squ areSum) / Math.sqrt(numberOfSamples); | 95 return quantile * this.sampleStandardDeviation(numberOfSamples, sum, squ areSum) / Math.sqrt(numberOfSamples); |
92 } | 96 } |
93 | 97 |
98 this.confidenceIntervalDeltaFromStd = function (confidenceLevel, numberOfSam ples, sampleStandardDeviation) { | |
99 var quantile = this.quantile(confidenceLevel, numberOfSamples); | |
100 return quantile * sampleStandardDeviation / Math.sqrt(numberOfSamples); | |
101 } | |
102 | |
103 | |
94 this.confidenceInterval = function (values, probability) { | 104 this.confidenceInterval = function (values, probability) { |
95 var sum = this.sum(values); | 105 var sum = this.sum(values); |
96 var mean = sum / values.length; | 106 var mean = sum / values.length; |
97 var delta = this.confidenceIntervalDelta(probability || 0.95, values.len gth, sum, this.squareSum(values)); | 107 var delta = this.confidenceIntervalDelta(probability || 0.95, values.len gth, sum, this.squareSum(values)); |
98 return [mean - delta, mean + delta]; | 108 return [mean - delta, mean + delta]; |
99 } | 109 } |
100 | 110 |
101 // See http://en.wikipedia.org/wiki/Student's_t-distribution#Table_of_select ed_values | 111 // 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. | 112 // 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. | 113 // 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 | 190 // Infinity |
181 2.326348], | 191 2.326348], |
182 }; | 192 }; |
183 | 193 |
184 })(); | 194 })(); |
185 | 195 |
186 if (typeof module != 'undefined') { | 196 if (typeof module != 'undefined') { |
187 for (var key in Statistics) | 197 for (var key in Statistics) |
188 module.exports[key] = Statistics[key]; | 198 module.exports[key] = Statistics[key]; |
189 } | 199 } |
OLD | NEW |