spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[STDEVP] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-15 01:33:20
stats
8 file(s) changed, 102 insertions(+), 12 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 f0e64df..ccd8418 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1387,7 +1387,7 @@
  6 
  7 ```
  8   Calculates the standard deviation of a range, ignoring string values, regardless of whether they can be converted to numbers. 
  9-@param values - Range of sample 
 10+@param values - Range of sample. 
 11 @returns {number} 
 12 @constructor
 13 ```
 14@@ -1395,11 +1395,20 @@
 15 ### STDEVA 
 16 
 17 ```
 18-  Calculcates the standard deviation of a range, converting string values to numbers, if possible. If a value cannot be converted to a number, formula will throw a value error. 
 19+  Calculates the standard deviation of a range, converting string values to numbers, if possible. If a value cannot be converted to a number, formula will throw a value error. 
 20 @param values - Range of sample. 
 21 @returns {number} 
 22 @constructor
 23 ```
 24+
 25+### STDEVP 
 26+
 27+```
 28+  Calculates the standard deviation of an entire population, ignoring string values, regardless of whether they can be converted to numbers. 
 29+@param values - Entire sample. 
 30+@returns {number} 
 31+@constructor
 32+```
 33 ## Text
 34 
 35 
 36diff --git a/TODO.md b/TODO.md
 37index 18d12e5..7458763 100644
 38--- a/TODO.md
 39+++ b/TODO.md
 40@@ -99,7 +99,6 @@ For example 64 tbs to a qt.
 41 * SLOPE
 42 * SMALL
 43 * STANDARDIZE
 44-* STDEVP
 45 * STDEVPA
 46 * STEYX
 47 * T.INV
 48diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 49index 9a8bc6a..14c7aa6 100644
 50--- a/dist/Formulas/AllFormulas.js
 51+++ b/dist/Formulas/AllFormulas.js
 52@@ -124,6 +124,7 @@ exports.QUARTILE = Statistical_1.QUARTILE;
 53 exports.PERCENTILE = Statistical_1.PERCENTILE;
 54 exports.STDEV = Statistical_1.STDEV;
 55 exports.STDEVA = Statistical_1.STDEVA;
 56+exports.STDEVP = Statistical_1.STDEVP;
 57 var Text_1 = require("./Text");
 58 exports.ARABIC = Text_1.ARABIC;
 59 exports.CHAR = Text_1.CHAR;
 60diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 61index 78e3a3b..3fbff6c 100644
 62--- a/dist/Formulas/Statistical.js
 63+++ b/dist/Formulas/Statistical.js
 64@@ -586,7 +586,7 @@ exports.QUARTILE = QUARTILE;
 65 /**
 66  * Calculates the standard deviation of a range, ignoring string values, regardless of whether they can be converted to
 67  * numbers.
 68- * @param values - Range of sample
 69+ * @param values - Range of sample.
 70  * @returns {number}
 71  * @constructor
 72  */
 73@@ -612,7 +612,7 @@ var STDEV = function () {
 74 };
 75 exports.STDEV = STDEV;
 76 /**
 77- * Calculcates the standard deviation of a range, converting string values to numbers, if possible. If a value cannot
 78+ * Calculates the standard deviation of a range, converting string values to numbers, if possible. If a value cannot
 79  * be converted to a number, formula will throw a value error.
 80  * @param values - Range of sample.
 81  * @returns {number}
 82@@ -636,3 +636,31 @@ var STDEVA = function () {
 83     return Math.sqrt(sigma / (n - 1));
 84 };
 85 exports.STDEVA = STDEVA;
 86+/**
 87+ * Calculates the standard deviation of an entire population, ignoring string values, regardless of whether they can be
 88+ * converted to numbers.
 89+ * @param values - Entire sample.
 90+ * @returns {number}
 91+ * @constructor
 92+ */
 93+var STDEVP = function () {
 94+    var values = [];
 95+    for (var _i = 0; _i < arguments.length; _i++) {
 96+        values[_i] = arguments[_i];
 97+    }
 98+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "STDEVP");
 99+    var range = Filter_1.Filter.flattenAndThrow(values);
100+    var n = range.length;
101+    var sigma = 0;
102+    var count = 0;
103+    var mean = AVERAGE(range);
104+    for (var i = 0; i < n; i++) {
105+        var value = TypeConverter_1.TypeConverter.firstValue(range[i]);
106+        if (typeof value !== "string") {
107+            sigma += Math.pow(value - mean, 2);
108+            count++;
109+        }
110+    }
111+    return Math.sqrt(sigma / count);
112+};
113+exports.STDEVP = STDEVP;
114diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
115index cb81a57..dc209cb 100644
116--- a/src/Formulas/AllFormulas.ts
117+++ b/src/Formulas/AllFormulas.ts
118@@ -128,7 +128,8 @@ import {
119   QUARTILE,
120   PERCENTILE,
121   STDEV,
122-  STDEVA
123+  STDEVA,
124+  STDEVP
125 } from "./Statistical";
126 import {
127   ARABIC,
128@@ -323,5 +324,6 @@ export {
129   UPLUS,
130   UMINUS,
131   STDEV,
132-  STDEVA
133+  STDEVA,
134+  STDEVP
135 }
136\ No newline at end of file
137diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
138index 308db54..53f123b 100644
139--- a/src/Formulas/Statistical.ts
140+++ b/src/Formulas/Statistical.ts
141@@ -574,7 +574,7 @@ var QUARTILE = function (data, quartile) {
142 /**
143  * Calculates the standard deviation of a range, ignoring string values, regardless of whether they can be converted to
144  * numbers.
145- * @param values - Range of sample
146+ * @param values - Range of sample.
147  * @returns {number}
148  * @constructor
149  */
150@@ -597,7 +597,7 @@ var STDEV = function (...values) {
151 
152 
153 /**
154- * Calculcates the standard deviation of a range, converting string values to numbers, if possible. If a value cannot
155+ * Calculates the standard deviation of a range, converting string values to numbers, if possible. If a value cannot
156  * be converted to a number, formula will throw a value error.
157  * @param values - Range of sample.
158  * @returns {number}
159@@ -617,6 +617,32 @@ var STDEVA = function (...values) {
160   return Math.sqrt(sigma / (n - 1));
161 };
162 
163+
164+/**
165+ * Calculates the standard deviation of an entire population, ignoring string values, regardless of whether they can be
166+ * converted to numbers.
167+ * @param values - Entire sample.
168+ * @returns {number}
169+ * @constructor
170+ */
171+var STDEVP = function (...values) {
172+  ArgsChecker.checkAtLeastLength(arguments, 1, "STDEVP");
173+  var range = Filter.flattenAndThrow(values);
174+  var n = range.length;
175+  var sigma = 0;
176+  var count = 0;
177+  var mean = AVERAGE(range);
178+  for (var i = 0; i < n; i++) {
179+    var value = TypeConverter.firstValue(range[i]);
180+    if (typeof value !== "string") {
181+      sigma += Math.pow(value - mean, 2);
182+      count++;
183+    }
184+  }
185+  return Math.sqrt(sigma / count);
186+};
187+
188+
189 export {
190   AVERAGE,
191   AVERAGEA,
192@@ -640,5 +666,6 @@ export {
193   QUARTILE,
194   PERCENTILE,
195   STDEV,
196-  STDEVA
197+  STDEVA,
198+  STDEVP
199 }
200\ No newline at end of file
201diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
202index e3444a3..2e831fe 100644
203--- a/tests/Formulas/StatisticalTest.ts
204+++ b/tests/Formulas/StatisticalTest.ts
205@@ -21,7 +21,8 @@ import {
206   QUARTILE,
207   PERCENTILE,
208   STDEV,
209-  STDEVA
210+  STDEVA,
211+  STDEVP
212 } from "../../src/Formulas/Statistical";
213 import * as ERRORS from "../../src/Errors";
214 import {
215@@ -514,3 +515,20 @@ test("STDEVA", function(){
216     STDEVA([10, 10, [], 10]);
217   }, ERRORS.REF_ERROR);
218 });
219+
220+test("STDEVP", function(){
221+  assertEquals(STDEVP(1, 2, 3, 4, 5, 6, 7, 123), 39.39999206852712);
222+  assertEquals(STDEVP(1, 2, 3, 4, 5, 6, 7, "132321"), 2);
223+  assertEquals(STDEVP(1, 2, 3, 4, 5, 6, 7), 2);
224+  assertEquals(STDEVP([1, 2, 3, 4, 5, 6, 7]), 2);
225+  assertEquals(STDEVP(1, 2, 3, [4, 5], 6, 7), 2);
226+  assertEquals(STDEVP(33, 44), 5.5);
227+  assertEquals(STDEVP(33, 44, 0, 1, 0, 1), 18.197222010210485);
228+  assertEquals(STDEVP(33, 44, false, true, false, true), 18.197222010210485);
229+  catchAndAssertEquals(function() {
230+    STDEVP();
231+  }, ERRORS.NA_ERROR);
232+  catchAndAssertEquals(function() {
233+    STDEVP([10, 10, [], 10]);
234+  }, ERRORS.REF_ERROR);
235+});
236diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
237index d41d9f2..1df10f1 100644
238--- a/tests/SheetFormulaTest.ts
239+++ b/tests/SheetFormulaTest.ts
240@@ -283,6 +283,10 @@ test("Sheet STDEVA", function(){
241   assertFormulaEquals('=STDEVA(33, 44)', 7.7781745930520225);
242 });
243 
244+test("Sheet STDEVP", function(){
245+  assertFormulaEquals('=STDEVP(33, 44)', 5.5);
246+});
247+
248 test("Sheet PERCENTILE", function(){
249   assertFormulaEquals('=PERCENTILE([10], 0)', 10);
250 });