spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[VARPA] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 00:20:18
stats
8 file(s) changed, 104 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 cb91ccb..9d72256 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1984,6 +1984,15 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### VARPA 
 11+
 12+```
 13+  Estimate the variance based on the entire population. Text will be converted to numbers, if possible. 
 14+@param values - Values of population. 
 15+@returns {number} 
 16+@constructor
 17+```
 18 ## Text
 19 
 20 
 21diff --git a/TODO.md b/TODO.md
 22index f2137f4..f885ede 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -82,7 +82,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 26 * VAR
 27 * VARA
 28 * VARP
 29-* VARPA
 30 * ZTEST
 31 * CLEAN
 32 * FIND
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index d89939f..64e19f5 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -183,6 +183,7 @@ exports.CONFIDENCE = Statistical_1.CONFIDENCE;
 38 exports.BINOMDIST = Statistical_1.BINOMDIST;
 39 exports.COVAR = Statistical_1.COVAR;
 40 exports.WEIBULL = Statistical_1.WEIBULL;
 41+exports.VARPA = Statistical_1.VARPA;
 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 3bfa5cc..e5b392f 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -1530,3 +1530,38 @@ var WEIBULL = function (x, shape, scale, cumulative) {
 50         * Math.exp(-Math.pow(x / scale, shape)) * shape / Math.pow(scale, shape);
 51 };
 52 exports.WEIBULL = WEIBULL;
 53+/**
 54+ * Estimate the variance based on the entire population. Text will be converted to numbers, if possible.
 55+ * @param values - Values of population.
 56+ * @returns {number}
 57+ * @constructor
 58+ */
 59+var VARPA = 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, "VARPA");
 65+    var range = Filter_1.Filter.flattenAndThrow(values).map(TypeConverter_1.TypeConverter.valueToNumber);
 66+    var n = range.length;
 67+    var sigma = 0;
 68+    var count = 0;
 69+    var mean = AVERAGEA(range);
 70+    for (var i = 0; i < n; i++) {
 71+        var el = range[i];
 72+        if (typeof el === 'number') {
 73+            sigma += Math.pow(el - mean, 2);
 74+        }
 75+        else if (el === true) {
 76+            sigma += Math.pow(1 - mean, 2);
 77+        }
 78+        else {
 79+            sigma += Math.pow(0 - mean, 2);
 80+        }
 81+        if (el !== null) {
 82+            count++;
 83+        }
 84+    }
 85+    return sigma / count;
 86+};
 87+exports.VARPA = VARPA;
 88diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 89index 2eff062..04d2f68 100644
 90--- a/src/Formulas/AllFormulas.ts
 91+++ b/src/Formulas/AllFormulas.ts
 92@@ -188,7 +188,8 @@ import {
 93   CONFIDENCE,
 94   BINOMDIST,
 95   COVAR,
 96-  WEIBULL
 97+  WEIBULL,
 98+  VARPA
 99 } from "./Statistical";
100 import {
101   ARABIC,
102@@ -451,5 +452,6 @@ export {
103   ROW,
104   T,
105   PPMT,
106-  WEIBULL
107+  WEIBULL,
108+  VARPA
109 }
110\ No newline at end of file
111diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
112index a01a62c..71b4c86 100644
113--- a/src/Formulas/Statistical.ts
114+++ b/src/Formulas/Statistical.ts
115@@ -1528,6 +1528,36 @@ var WEIBULL = function (x, shape, scale, cumulative) {
116 };
117 
118 
119+/**
120+ * Estimate the variance based on the entire population. Text will be converted to numbers, if possible.
121+ * @param values - Values of population.
122+ * @returns {number}
123+ * @constructor
124+ */
125+var VARPA = function (...values) {
126+  ArgsChecker.checkAtLeastLength(arguments, 1, "VARPA");
127+  var range = Filter.flattenAndThrow(values).map(TypeConverter.valueToNumber);
128+  var n = range.length;
129+  var sigma = 0;
130+  var count = 0;
131+  var mean = AVERAGEA(range);
132+  for (var i = 0; i < n; i++) {
133+    var el = range[i];
134+    if (typeof el === 'number') {
135+      sigma += Math.pow(el - mean, 2);
136+    } else if (el === true) {
137+      sigma += Math.pow(1 - mean, 2);
138+    } else {
139+      sigma += Math.pow(0 - mean, 2);
140+    }
141+
142+    if (el !== null) {
143+      count++;
144+    }
145+  }
146+  return sigma / count;
147+};
148+
149 export {
150   AVERAGE,
151   AVERAGEA,
152@@ -1575,5 +1605,6 @@ export {
153   CONFIDENCE,
154   BINOMDIST,
155   COVAR,
156-  WEIBULL
157+  WEIBULL,
158+  VARPA
159 }
160\ No newline at end of file
161diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
162index 4287b7f..098153f 100644
163--- a/tests/Formulas/StatisticalTest.ts
164+++ b/tests/Formulas/StatisticalTest.ts
165@@ -45,7 +45,8 @@ import {
166   CONFIDENCE,
167   BINOMDIST,
168   COVAR,
169-  WEIBULL
170+  WEIBULL,
171+  VARPA
172 } from "../../src/Formulas/Statistical";
173 import * as ERRORS from "../../src/Errors";
174 import {
175@@ -952,4 +953,18 @@ test("WEIBULL", function() {
176   catchAndAssertEquals(function() {
177     WEIBULL.apply(this, [10, 10, 10, 10, 10]);
178   }, ERRORS.NA_ERROR);
179+});
180+
181+test("VARPA", function() {
182+  assertEquals(VARPA(1, 2, 3, 4, 5, 6, 7, 8), 5.25);
183+  assertEquals(VARPA(1, 2, 3, 4, [5, [6]], 7, 8), 5.25);
184+  assertEquals(VARPA(1, 2, 3, 4, 5, 6, 7, 8, "0"), 6.666666666666667);
185+  assertEquals(VARPA(1, 2, 3, 4, 5, 6, 7, 8, "10"), 7.654320987654322);
186+  assertEquals(VARPA(1, 2, 3, 4, 5, 6, 7, 8, false), 6.666666666666667);
187+  catchAndAssertEquals(function() {
188+    VARPA(1, 2, 3, 4, 5, 6, 7, 8, "ignore");
189+  }, ERRORS.VALUE_ERROR);
190+  catchAndAssertEquals(function() {
191+    VARPA.apply(this, []);
192+  }, ERRORS.NA_ERROR);
193 });
194\ No newline at end of file
195diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
196index 249c297..b7f6e5c 100644
197--- a/tests/SheetFormulaTest.ts
198+++ b/tests/SheetFormulaTest.ts
199@@ -890,6 +890,10 @@ test("Sheet WEIBULL", function(){
200   assertFormulaEquals('=WEIBULL(2.4, 2, 4, true)', 0.30232367392896886);
201 });
202 
203+test("Sheet VARPA", function(){
204+  assertFormulaEquals('=VARPA(1, 2, 3, 4, 5, 6, 7, 8)', 5.25);
205+});
206+
207 test("Sheet *", function(){
208   assertFormulaEquals('= 10 * 10', 100);
209   assertFormulaEquals('= 10 * 0', 0);