spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[CONFIDENCE] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-07 02:07:01
stats
8 file(s) changed, 206 insertions(+), 5 deletions(-)
files
DOCS.md
TODO.md
dist/Formulas/AllFormulas.js
dist/Formulas/Statistical.js
src/Formulas/AllFormulas.ts
src/Formulas/Statistical.ts
tests/Formulas/StatisticalTest.ts
tests/SheetFormulaTest.ts
  1diff --git a/DOCS.md b/DOCS.md
  2index 5ab0c03..1139362 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1810,6 +1810,17 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### ONFIDENCE 
 11+
 12+```
 13+  Returns the (1-alpha) confidence interval for a normal distribution. 
 14+@param alpha - The level of the confidence interval 
 15+@param standDev - The standard deviation for the total population 
 16+@param size - The size of the population. 
 17+@returns {number} 
 18+@constructor
 19+```
 20 ## Text
 21 
 22 
 23diff --git a/TODO.md b/TODO.md
 24index edd14e7..b56d220 100644
 25--- a/TODO.md
 26+++ b/TODO.md
 27@@ -58,7 +58,6 @@ For example 64 tbs to a qt.
 28 * TO_PERCENT - Contingent upon cells having display formats derived from type-hierarchy
 29 * TO_TEXT - Contingent upon cells having display formats derived from type-hierarchy
 30 * BINOMDIST
 31-* CONFIDENCE
 32 * COVAR
 33 * CRITBINOM
 34 * F.DIST.RT
 35diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 36index 6ba5e6b..ab72a64 100644
 37--- a/dist/Formulas/AllFormulas.js
 38+++ b/dist/Formulas/AllFormulas.js
 39@@ -165,6 +165,7 @@ exports.NORMINV = Statistical_1.NORMINV;
 40 exports.NEGBINOMDIST = Statistical_1.NEGBINOMDIST;
 41 exports.GEOMEAN = Statistical_1.GEOMEAN;
 42 exports.HARMEAN = Statistical_1.HARMEAN;
 43+exports.CONFIDENCE = Statistical_1.CONFIDENCE;
 44 var Text_1 = require("./Text");
 45 exports.ARABIC = Text_1.ARABIC;
 46 exports.CHAR = Text_1.CHAR;
 47diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 48index b9bf072..b1a0f8e 100644
 49--- a/dist/Formulas/Statistical.js
 50+++ b/dist/Formulas/Statistical.js
 51@@ -1333,3 +1333,85 @@ var HARMEAN = function () {
 52     return n / den;
 53 };
 54 exports.HARMEAN = HARMEAN;
 55+/**
 56+ * Returns the (1-alpha) confidence interval for a normal distribution.
 57+ * @param alpha - The level of the confidence interval
 58+ * @param standDev - The standard deviation for the total population
 59+ * @param size - The size of the population.
 60+ * @returns {number}
 61+ * @constructor
 62+ */
 63+var CONFIDENCE = function (alpha, standDev, size) {
 64+    alpha = TypeConverter_1.TypeConverter.firstValueAsNumber(alpha);
 65+    standDev = TypeConverter_1.TypeConverter.firstValueAsNumber(standDev);
 66+    size = TypeConverter_1.TypeConverter.firstValueAsNumber(size);
 67+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 3, "CONFIDENCE");
 68+    if (alpha <= 0 || alpha >= 1) {
 69+        throw new Errors_1.NumError("Function CONFIDENCE parameter 1 value is " + alpha
 70+            + ". Valid values are between 0 and 1 exclusively.");
 71+    }
 72+    if (standDev <= 0) {
 73+        throw new Errors_1.NumError("Function CONFIDENCE parameter 2 value is " + standDev + ". It should be greater than 0.");
 74+    }
 75+    if (size <= 0) {
 76+        throw new Errors_1.NumError("Function CONFIDENCE parameter 3 value is " + size + ". It should be at least 1.");
 77+    }
 78+    function _erfc(x) {
 79+        return 1 - MathHelpers_1.erf(x);
 80+    }
 81+    function _erfcinv(p) {
 82+        var j = 0;
 83+        var x, err, t, pp;
 84+        if (p >= 2)
 85+            return -100;
 86+        if (p <= 0)
 87+            return 100;
 88+        pp = (p < 1) ? p : 2 - p;
 89+        t = Math.sqrt(-2 * Math.log(pp / 2));
 90+        x = -0.70711 * ((2.30753 + t * 0.27061) /
 91+            (1 + t * (0.99229 + t * 0.04481)) - t);
 92+        for (; j < 2; j++) {
 93+            err = _erfc(x) - pp;
 94+            x += err / (1.12837916709551257 * Math.exp(-x * x) - x * err);
 95+        }
 96+        return (p < 1) ? x : -x;
 97+    }
 98+    function _normalInv(p, m, std) {
 99+        return -1.41421356237309505 * std * _erfcinv(2 * p) + m;
100+    }
101+    function _sumsqerr(arr) {
102+        var mean = mean(arr);
103+        var sum = 0;
104+        var i = arr.length;
105+        var tmp;
106+        while (--i >= 0) {
107+            tmp = arr[i] - mean;
108+            sum += tmp * tmp;
109+        }
110+        return sum;
111+    }
112+    function _variance(arr, flag) {
113+        return _sumsqerr(arr) / (arr.length - (flag ? 1 : 0));
114+    }
115+    function _normalci() {
116+        var values = [];
117+        for (var _i = 0; _i < arguments.length; _i++) {
118+            values[_i] = arguments[_i];
119+        }
120+        var ans = new Array(2);
121+        var change;
122+        if (values.length === 4) {
123+            change = Math.abs(_normalInv(values[1] / 2, 0, 1) *
124+                values[2] / Math.sqrt(values[3]));
125+        }
126+        else {
127+            change = Math.abs(_normalInv(values[1] / 2, 0, 1) *
128+                Math.sqrt(_variance(arguments[2])) / Math.sqrt(values[2].length));
129+        }
130+        ans[0] = values[0] - change;
131+        ans[1] = values[0] + change;
132+        return ans;
133+    }
134+    return _normalci(1, alpha, standDev, size)[1] - 1;
135+};
136+exports.CONFIDENCE = CONFIDENCE;
137diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
138index 80b6f5a..4a864ce 100644
139--- a/src/Formulas/AllFormulas.ts
140+++ b/src/Formulas/AllFormulas.ts
141@@ -170,7 +170,8 @@ import {
142   NORMINV,
143   NEGBINOMDIST,
144   GEOMEAN,
145-  HARMEAN
146+  HARMEAN,
147+  CONFIDENCE
148 } from "./Statistical";
149 import {
150   ARABIC,
151@@ -412,5 +413,6 @@ export {
152   NORMINV,
153   NEGBINOMDIST,
154   GEOMEAN,
155-  HARMEAN
156+  HARMEAN,
157+  CONFIDENCE
158 }
159\ No newline at end of file
160diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
161index 3efacfe..c4adb45 100644
162--- a/src/Formulas/Statistical.ts
163+++ b/src/Formulas/Statistical.ts
164@@ -1330,6 +1330,84 @@ var HARMEAN = function (...values) {
165 };
166 
167 
168+/**
169+ * Returns the (1-alpha) confidence interval for a normal distribution.
170+ * @param alpha - The level of the confidence interval
171+ * @param standDev - The standard deviation for the total population
172+ * @param size - The size of the population.
173+ * @returns {number}
174+ * @constructor
175+ */
176+var CONFIDENCE = function (alpha, standDev, size) {
177+  alpha = TypeConverter.firstValueAsNumber(alpha);
178+  standDev = TypeConverter.firstValueAsNumber(standDev);
179+  size = TypeConverter.firstValueAsNumber(size);
180+  ArgsChecker.checkLength(arguments, 3, "CONFIDENCE");
181+  if (alpha <= 0 || alpha >= 1) {
182+    throw new NumError("Function CONFIDENCE parameter 1 value is " + alpha
183+        + ". Valid values are between 0 and 1 exclusively.");
184+  }
185+  if (standDev <= 0) {
186+    throw new NumError("Function CONFIDENCE parameter 2 value is " + standDev + ". It should be greater than 0.");
187+  }
188+  if (size <= 0) {
189+    throw new NumError("Function CONFIDENCE parameter 3 value is " + size +". It should be at least 1.");
190+  }
191+  function _erfc(x) {
192+    return 1 - erf(x);
193+  }
194+  function _erfcinv(p) {
195+    var j = 0;
196+    var x, err, t, pp;
197+    if (p >= 2)
198+      return -100;
199+    if (p <= 0)
200+      return 100;
201+    pp = (p < 1) ? p : 2 - p;
202+    t = Math.sqrt(-2 * Math.log(pp / 2));
203+    x = -0.70711 * ((2.30753 + t * 0.27061) /
204+      (1 + t * (0.99229 + t * 0.04481)) - t);
205+    for (; j < 2; j++) {
206+      err = _erfc(x) - pp;
207+      x += err / (1.12837916709551257 * Math.exp(-x * x) - x * err);
208+    }
209+    return (p < 1) ? x : -x;
210+  }
211+  function _normalInv(p, m, std) {
212+    return -1.41421356237309505 * std * _erfcinv(2 * p) + m;
213+  }
214+  function _sumsqerr(arr) {
215+    var mean = mean(arr);
216+    var sum = 0;
217+    var i = arr.length;
218+    var tmp;
219+    while (--i >= 0) {
220+      tmp = arr[i] - mean;
221+      sum += tmp * tmp;
222+    }
223+    return sum;
224+  }
225+  function _variance(arr, flag?) {
226+    return _sumsqerr(arr) / (arr.length - (flag ? 1 : 0));
227+  }
228+  function _normalci(...values) {
229+      var ans = new Array(2);
230+      var change;
231+    if (values.length === 4) {
232+      change = Math.abs(_normalInv(values[1] / 2, 0, 1) *
233+        values[2] / Math.sqrt(values[3]));
234+    } else {
235+      change = Math.abs(_normalInv(values[1] / 2, 0, 1) *
236+        Math.sqrt(_variance(arguments[2])) / Math.sqrt(values[2].length));
237+    }
238+    ans[0] = values[0] - change;
239+    ans[1] = values[0] + change;
240+    return ans;
241+  }
242+  return _normalci(1, alpha, standDev, size)[1] - 1;
243+};
244+
245+
246 export {
247   AVERAGE,
248   AVERAGEA,
249@@ -1373,5 +1451,6 @@ export {
250   NORMINV,
251   NEGBINOMDIST,
252   GEOMEAN,
253-  HARMEAN
254+  HARMEAN,
255+  CONFIDENCE
256 }
257\ No newline at end of file
258diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
259index cc6fb5a..4dcf84a 100644
260--- a/tests/Formulas/StatisticalTest.ts
261+++ b/tests/Formulas/StatisticalTest.ts
262@@ -41,7 +41,8 @@ import {
263   NORMINV,
264   NEGBINOMDIST,
265   GEOMEAN,
266-  HARMEAN
267+  HARMEAN,
268+  CONFIDENCE
269 } from "../../src/Formulas/Statistical";
270 import * as ERRORS from "../../src/Errors";
271 import {
272@@ -871,4 +872,25 @@ test("HARMEAN", function() {
273   catchAndAssertEquals(function() {
274     HARMEAN.apply(this, []);
275   }, ERRORS.NA_ERROR);
276+});
277+
278+test("CONFIDENCE", function() {
279+  assertEquals(CONFIDENCE(0.04, 6.48, 25), 2.6616585881788426);
280+  assertEquals(CONFIDENCE(0.0001, 101.1, 24281), 2.5242568756291566);
281+  assertEquals(CONFIDENCE(0.8, 101.1, 24281), 0.1643742612132182);
282+  catchAndAssertEquals(function() {
283+    CONFIDENCE(0, 101.1, 24281);
284+  }, ERRORS.NUM_ERROR);
285+  catchAndAssertEquals(function() {
286+    CONFIDENCE(0.1, 0, 24281);
287+  }, ERRORS.NUM_ERROR);
288+  catchAndAssertEquals(function() {
289+    CONFIDENCE(0.1, 2.1, 0);
290+  }, ERRORS.NUM_ERROR);
291+  catchAndAssertEquals(function() {
292+    CONFIDENCE.apply(this, [0.8, 101.1]);
293+  }, ERRORS.NA_ERROR);
294+  catchAndAssertEquals(function() {
295+    CONFIDENCE.apply(this, [0.8, 101.1, 24281, 22]);
296+  }, ERRORS.NA_ERROR);
297 });
298\ No newline at end of file
299diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
300index 78d8f24..512052d 100644
301--- a/tests/SheetFormulaTest.ts
302+++ b/tests/SheetFormulaTest.ts
303@@ -805,6 +805,10 @@ test("Sheet HARMEAN", function(){
304   assertFormulaEquals('=HARMEAN(10, 4, 6, 3, 6, 7, 1, 1)', 2.532027128862095);
305 });
306 
307+test("Sheet HARMEAN", function(){
308+  assertFormulaEquals('=CONFIDENCE(0.04, 6.48, 25)', 2.6616585881788426);
309+});
310+
311 test("Sheet *", function(){
312   assertFormulaEquals('= 10 * 10', 100);
313   assertFormulaEquals('= 10 * 0', 0);