spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[VARA] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 00:40:10
stats
8 file(s) changed, 113 insertions(+), 7 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 6e9e2c7..973d47b 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2002,6 +2002,15 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### VARA 
 11+
 12+```
 13+  Estimate the variance based on a sample. 
 14+@param values 
 15+@returns {number} 
 16+@constructor
 17+```
 18 ## Text
 19 
 20 
 21diff --git a/TODO.md b/TODO.md
 22index 3787a69..005f5af 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -80,7 +80,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 26 * TINV
 27 * TTEST
 28 * VAR
 29-* VARA
 30 * ZTEST
 31 * CLEAN
 32 * FIND
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index 8e896db..a587219 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -185,6 +185,7 @@ exports.COVAR = Statistical_1.COVAR;
 38 exports.WEIBULL = Statistical_1.WEIBULL;
 39 exports.VARPA = Statistical_1.VARPA;
 40 exports.VARP = Statistical_1.VARP;
 41+exports.VARA = Statistical_1.VARA;
 42 var Text_1 = require("./Text");
 43 exports.ARABIC = Text_1.ARABIC;
 44 exports.CHAR = Text_1.CHAR;
 45diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 46index 3194b87..6ea051f 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -1595,3 +1595,41 @@ var VARP = function () {
 50     return sigma / count;
 51 };
 52 exports.VARP = VARP;
 53+/**
 54+ * Estimate the variance based on a sample.
 55+ * @param values
 56+ * @returns {number}
 57+ * @constructor
 58+ */
 59+var VARA = function () {
 60+    var values = [];
 61+    for (var _i = 0; _i < arguments.length; _i++) {
 62+        values[_i] = arguments[_i];
 63+    }
 64+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "VARA");
 65+    var range = Filter_1.Filter.flattenAndThrow(values).map(TypeConverter_1.TypeConverter.valueToNumber);
 66+    var n = range.length;
 67+    if (n < 2) {
 68+        throw new Errors_1.DivZeroError("Evaluation of function VARA caused a divide by zero error.");
 69+    }
 70+    var sigma = 0;
 71+    var count = 0;
 72+    var mean = AVERAGEA(range);
 73+    for (var i = 0; i < n; i++) {
 74+        var el = range[i];
 75+        if (typeof el === 'number') {
 76+            sigma += Math.pow(el - mean, 2);
 77+        }
 78+        else if (el === true) {
 79+            sigma += Math.pow(1 - mean, 2);
 80+        }
 81+        else {
 82+            sigma += Math.pow(0 - mean, 2);
 83+        }
 84+        if (el !== null) {
 85+            count++;
 86+        }
 87+    }
 88+    return sigma / (count - 1);
 89+};
 90+exports.VARA = VARA;
 91diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 92index b107d67..d0d4f17 100644
 93--- a/src/Formulas/AllFormulas.ts
 94+++ b/src/Formulas/AllFormulas.ts
 95@@ -190,7 +190,8 @@ import {
 96   COVAR,
 97   WEIBULL,
 98   VARPA,
 99-  VARP
100+  VARP,
101+  VARA
102 } from "./Statistical";
103 import {
104   ARABIC,
105@@ -455,5 +456,6 @@ export {
106   PPMT,
107   WEIBULL,
108   VARPA,
109-  VARP
110+  VARP,
111+  VARA
112 }
113\ No newline at end of file
114diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
115index 5863284..3efa273 100644
116--- a/src/Formulas/Statistical.ts
117+++ b/src/Formulas/Statistical.ts
118@@ -1585,6 +1585,42 @@ var VARP =  function (...values) {
119   return sigma / count;
120 };
121 
122+
123+/**
124+ * Estimate the variance based on a sample.
125+ * @param values
126+ * @returns {number}
127+ * @constructor
128+ */
129+var VARA = function (...values) {
130+  ArgsChecker.checkAtLeastLength(arguments, 1, "VARA");
131+  var range = Filter.flattenAndThrow(values).map(TypeConverter.valueToNumber);
132+  var n = range.length;
133+  if (n < 2) {
134+    throw new DivZeroError("Evaluation of function VARA caused a divide by zero error.");
135+  }
136+  var sigma = 0;
137+  var count = 0;
138+  var mean = AVERAGEA(range);
139+  for (var i = 0; i < n; i++) {
140+    var el = range[i];
141+    if (typeof el === 'number') {
142+      sigma += Math.pow(el - mean, 2);
143+    } else if (el === true) {
144+      sigma += Math.pow(1 - mean, 2);
145+    } else {
146+      sigma += Math.pow(0 - mean, 2);
147+    }
148+
149+    if (el !== null) {
150+      count++;
151+    }
152+  }
153+  return sigma / (count - 1);
154+};
155+
156+
157+
158 export {
159   AVERAGE,
160   AVERAGEA,
161@@ -1634,5 +1670,6 @@ export {
162   COVAR,
163   WEIBULL,
164   VARPA,
165-  VARP
166+  VARP,
167+  VARA
168 }
169\ No newline at end of file
170diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
171index 4a661eb..5d14aa6 100644
172--- a/tests/Formulas/StatisticalTest.ts
173+++ b/tests/Formulas/StatisticalTest.ts
174@@ -47,7 +47,8 @@ import {
175   COVAR,
176   WEIBULL,
177   VARPA,
178-  VARP
179+  VARP,
180+  VARA
181 } from "../../src/Formulas/Statistical";
182 import * as ERRORS from "../../src/Errors";
183 import {
184@@ -988,4 +989,20 @@ test("VARP", function() {
185   catchAndAssertEquals(function() {
186     VARP.apply(this, []);
187   }, ERRORS.NA_ERROR);
188+});
189+
190+test("VARA", function() {
191+  assertEquals(VARA(1, 2, 3, 4, 5, 6, 7, 8), 6);
192+  assertEquals(VARA(1, 2, 3, 4, 5, 6, 7, 8, "0"), 7.5);
193+  assertEquals(VARA(1, 2, 3, 4, 5, 6, 7, 8, false), 7.5);
194+  assertEquals(VARA(1, 2, 3, 4, 5, 6, 7, 8, "10"), 8.611111111111112);
195+  catchAndAssertEquals(function() {
196+    VARA(1);
197+  }, ERRORS.DIV_ZERO_ERROR);
198+  catchAndAssertEquals(function() {
199+    VARA(1, 2, 3, 4, 5, 6, 7, 8, "ignore");
200+  }, ERRORS.VALUE_ERROR);
201+  catchAndAssertEquals(function() {
202+    VARA.apply(this, []);
203+  }, ERRORS.NA_ERROR);
204 });
205\ No newline at end of file
206diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
207index 9f8b300..3cb8352 100644
208--- a/tests/SheetFormulaTest.ts
209+++ b/tests/SheetFormulaTest.ts
210@@ -898,6 +898,10 @@ test("Sheet VARP", function(){
211   assertFormulaEquals('=VARP(1, 2, 3, 4, 5, 6, 7, 8)', 5.25);
212 });
213 
214+test("Sheet VARA", function(){
215+  assertFormulaEquals('=VARA(1, 2, 3, 4, 5, 6, 7, 8)', 6);
216+});
217+
218 test("Sheet *", function(){
219   assertFormulaEquals('= 10 * 10', 100);
220   assertFormulaEquals('= 10 * 0', 0);