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