spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[Statistical.ZTEST] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-10-07 16:56:49
stats
8 file(s) changed, 81 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 74b9b78..4a2593e 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2272,6 +2272,17 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### ZTEST 
 11+
 12+```
 13+  Returns the two-tailed P value of a z test with standard distribution. 
 14+@param range - Te array of the data. 
 15+@param value - The value to be tested. 
 16+@param stdDev - [OPTIONAL] The standard deviation of the total population. If this argument is missing, the standard deviation of the sample is processed. 
 17+@returns {number} 
 18+@constructor
 19+```
 20 ## Text
 21 
 22 
 23diff --git a/TODO.md b/TODO.md
 24index 9c074d5..cdc319e 100644
 25--- a/TODO.md
 26+++ b/TODO.md
 27@@ -39,8 +39,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 28 
 29 
 30 ### Easy formulas to write
 31-* CRITBINOM
 32-* ZTEST
 33 * CLEAN
 34 * FIND
 35 * JOIN
 36@@ -59,6 +57,7 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 37 * VALUE
 38 
 39 ### Other formulas to write
 40+* CRITBINOM
 41 * F.DIST.RT
 42 * LOGINV
 43 * T.INV
 44diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 45index 9cdf260..507582a 100644
 46--- a/dist/Formulas/AllFormulas.js
 47+++ b/dist/Formulas/AllFormulas.js
 48@@ -213,6 +213,7 @@ exports.RANK$EQ = Statistical_1.RANK$EQ;
 49 exports.LOGNORMDIST = Statistical_1.LOGNORMDIST;
 50 exports.TDIST = Statistical_1.TDIST;
 51 exports.HYPGEOMDIST = Statistical_1.HYPGEOMDIST;
 52+exports.ZTEST = Statistical_1.ZTEST;
 53 var Text_1 = require("./Text");
 54 exports.ARABIC = Text_1.ARABIC;
 55 exports.CHAR = Text_1.CHAR;
 56diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 57index b0b05c0..58b918e 100644
 58--- a/dist/Formulas/Statistical.js
 59+++ b/dist/Formulas/Statistical.js
 60@@ -7,6 +7,7 @@ var TypeConverter_1 = require("../Utilities/TypeConverter");
 61 var Errors_1 = require("../Errors");
 62 var Math_1 = require("./Math");
 63 var MathHelpers_1 = require("../Utilities/MathHelpers");
 64+var MoreUtils_1 = require("../Utilities/MoreUtils");
 65 /**
 66  * Calculates the sum of squares of deviations based on a sample.
 67  * @param values - The values or ranges of the sample.
 68@@ -2092,3 +2093,20 @@ var HYPGEOMDIST = function (numberOfSuccesses, numberOfDraws, successesInPop, po
 69         Math_1.COMBIN(populationSize, numberOfDraws);
 70 };
 71 exports.HYPGEOMDIST = HYPGEOMDIST;
 72+/**
 73+ * Returns the two-tailed P value of a z test with standard distribution.
 74+ * @param range - Te array of the data.
 75+ * @param value - The value to be tested.
 76+ * @param stdDev - [OPTIONAL] The standard deviation of the total population. If this argument is missing, the standard
 77+ * deviation of the sample is processed.
 78+ * @returns {number}
 79+ * @constructor
 80+ */
 81+var ZTEST = function (range, value, stdDev) {
 82+    ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 2, 3, "ZTEST");
 83+    range = Filter_1.Filter.flattenAndThrow(range);
 84+    value = TypeConverter_1.TypeConverter.firstValueAsNumber(value);
 85+    var sd = MoreUtils_1.isUndefined(stdDev) ? STDEV(range) : TypeConverter_1.TypeConverter.firstValueAsNumber(stdDev);
 86+    return 1 - NORMSDIST((AVERAGE(range) - value) / (sd / Math.sqrt(range.length)));
 87+};
 88+exports.ZTEST = ZTEST;
 89diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 90index a7b7337..31b36ed 100644
 91--- a/src/Formulas/AllFormulas.ts
 92+++ b/src/Formulas/AllFormulas.ts
 93@@ -219,7 +219,8 @@ import {
 94   RANK$EQ,
 95   LOGNORMDIST,
 96   TDIST,
 97-  HYPGEOMDIST
 98+  HYPGEOMDIST,
 99+  ZTEST
100 } from "./Statistical";
101 import {
102   ARABIC,
103@@ -527,5 +528,6 @@ export {
104   PV,
105   RATE,
106   SUBTOTAL,
107-  HYPGEOMDIST
108+  HYPGEOMDIST,
109+  ZTEST
110 }
111\ No newline at end of file
112diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
113index a0bd6f6..ea02275 100644
114--- a/src/Formulas/Statistical.ts
115+++ b/src/Formulas/Statistical.ts
116@@ -32,6 +32,7 @@ import {
117   erf,
118   gammaln
119 } from "../Utilities/MathHelpers";
120+import {isDefined, isUndefined} from "../Utilities/MoreUtils";
121 
122 
123 /**
124@@ -2081,6 +2082,23 @@ let HYPGEOMDIST = function (numberOfSuccesses, numberOfDraws, successesInPop, po
125       COMBIN(populationSize, numberOfDraws);
126 };
127 
128+/**
129+ * Returns the two-tailed P value of a z test with standard distribution.
130+ * @param range - Te array of the data.
131+ * @param value - The value to be tested.
132+ * @param stdDev - [OPTIONAL] The standard deviation of the total population. If this argument is missing, the standard
133+ * deviation of the sample is processed.
134+ * @returns {number}
135+ * @constructor
136+ */
137+let ZTEST = function (range, value, stdDev?) {
138+  ArgsChecker.checkLengthWithin(arguments, 2, 3, "ZTEST");
139+  range = Filter.flattenAndThrow(range);
140+  value = TypeConverter.firstValueAsNumber(value);
141+  let sd = isUndefined(stdDev) ? STDEV(range) : TypeConverter.firstValueAsNumber(stdDev);
142+  return 1 - NORMSDIST((AVERAGE(range) - value) / (sd / Math.sqrt(range.length)));
143+};
144+
145 
146 export {
147   AVERAGE,
148@@ -2145,5 +2163,6 @@ export {
149   RANK$EQ,
150   LOGNORMDIST,
151   TDIST,
152-  HYPGEOMDIST
153+  HYPGEOMDIST,
154+  ZTEST
155 }
156\ No newline at end of file
157diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
158index 4064684..3a53e19 100644
159--- a/tests/Formulas/StatisticalTest.ts
160+++ b/tests/Formulas/StatisticalTest.ts
161@@ -61,7 +61,8 @@ import {
162   RANK$EQ,
163   LOGNORMDIST,
164   TDIST,
165-  HYPGEOMDIST
166+  HYPGEOMDIST,
167+  ZTEST
168 } from "../../src/Formulas/Statistical";
169 import * as ERRORS from "../../src/Errors";
170 import {
171@@ -1267,4 +1268,17 @@ test("HYPGEOMDIST", function() {
172   catchAndAssertEquals(function() {
173     HYPGEOMDIST.apply(this, [5, 12, 20, 40, 44]);
174   }, ERRORS.NA_ERROR);
175+});
176+
177+test("ZTEST", function() {
178+  assertEquals(ZTEST([1, 2, 3, 4, 5, 6, 7], 5.6, 1.1), 0.9999405457342111);
179+  assertEquals(ZTEST([1, 2, 3, 4], 10.1, 1.1), 1);
180+  assertEquals(ZTEST([1, 2, 3, 4, 5, 6, 7], 5.6, 22), 0.5762927116053485);
181+  assertEquals(ZTEST([1, 2, 3, 4, 5, 6, 7], -100, -100), 0.9970345857641326);
182+  catchAndAssertEquals(function() {
183+    ZTEST.apply(this, [5]);
184+  }, ERRORS.NA_ERROR);
185+  catchAndAssertEquals(function() {
186+    ZTEST.apply(this, [1, 2, 3, 4]);
187+  }, ERRORS.NA_ERROR);
188 });
189\ No newline at end of file
190diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
191index 601e4f3..65f485c 100644
192--- a/tests/SheetFormulaTest.ts
193+++ b/tests/SheetFormulaTest.ts
194@@ -1037,6 +1037,14 @@ test("Sheet SUBTOTAL", function(){
195   assertFormulaEquals('=SUBTOTAL([1], [1, 2, 3, 4, 5, 6, 7])', 4);
196 });
197 
198+test("Sheet HYPGEOMDIST", function(){
199+  assertFormulaEquals('=HYPGEOMDIST(4, 12, 20, 44)', 0.16895408557348432);
200+});
201+
202+test("Sheet ZTEST", function(){
203+  assertFormulaEquals('=ZTEST([1, 2, 3, 4, 5, 6, 7], 5.6, 1.1)', 0.9999405457342111);
204+});
205+
206 test("Sheet parsing error", function(){
207   assertFormulaEqualsError('= 10e', PARSE_ERROR);
208   assertFormulaEqualsError('= SUM(', PARSE_ERROR);