spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[NORMDIST] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-03 20:53:55
stats
8 file(s) changed, 105 insertions(+), 10 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 e6c168b..52576c9 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1758,6 +1758,18 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### NORMDIST 
 11+
 12+```
 13+  Returns the normal distribution for the given number in the distribution. 
 14+@param x - Value to use. 
 15+@param meanValue - The mean value of the distribution. 
 16+@param standDev - The standard deviation of the distribution. 
 17+@param cumulative - 0 calculates the density function, 1 calculates the distribution. 
 18+@returns {number} 
 19+@constructor
 20+```
 21 ## Text
 22 
 23 
 24diff --git a/TODO.md b/TODO.md
 25index f591d1d..8d17884 100644
 26--- a/TODO.md
 27+++ b/TODO.md
 28@@ -69,7 +69,6 @@ For example 64 tbs to a qt.
 29 * LOGNORMDIST
 30 * MODE
 31 * NEGBINOMDIST
 32-* NORMDIST
 33 * NORMINV
 34 * PERMUT
 35 * PROB
 36diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 37index 5f8892e..9e2fec4 100644
 38--- a/dist/Formulas/AllFormulas.js
 39+++ b/dist/Formulas/AllFormulas.js
 40@@ -160,6 +160,7 @@ exports.PERCENTRANK = Statistical_1.PERCENTRANK;
 41 exports.PERCENTRANK$EXC = Statistical_1.PERCENTRANK$EXC;
 42 exports.NORMSINV = Statistical_1.NORMSINV;
 43 exports.NORMSDIST = Statistical_1.NORMSDIST;
 44+exports.NORMDIST = Statistical_1.NORMDIST;
 45 var Text_1 = require("./Text");
 46 exports.ARABIC = Text_1.ARABIC;
 47 exports.CHAR = Text_1.CHAR;
 48diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 49index b7ccb11..fdd2d0d 100644
 50--- a/dist/Formulas/Statistical.js
 51+++ b/dist/Formulas/Statistical.js
 52@@ -1101,6 +1101,13 @@ var NORMSINV = function (probability) {
 53     return inv(probability, 0, 1);
 54 };
 55 exports.NORMSINV = NORMSINV;
 56+function _cdf(x, mValue, stdVal) {
 57+    return 0.5 * (1 + MathHelpers_1.erf((x - mValue) / Math.sqrt(2 * stdVal * stdVal)));
 58+}
 59+function _pdf(x, meanVal, std) {
 60+    return Math.exp(-0.5 * Math.log(2 * Math.PI) -
 61+        Math.log(std) - Math.pow(x - meanVal, 2) / (2 * std * std));
 62+}
 63 /**
 64  * Returns the standard normal cumulative distribution for the given number.
 65  * @param z - Value to use in calculation.
 66@@ -1110,9 +1117,27 @@ exports.NORMSINV = NORMSINV;
 67 var NORMSDIST = function (z) {
 68     ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "NORMSDIST");
 69     z = TypeConverter_1.TypeConverter.firstValueAsNumber(z);
 70-    function _cdf(x, mValue, stdVal) {
 71-        return 0.5 * (1 + MathHelpers_1.erf((x - mValue) / Math.sqrt(2 * stdVal * stdVal)));
 72-    }
 73     return _cdf(z, 0, 1);
 74 };
 75 exports.NORMSDIST = NORMSDIST;
 76+/**
 77+ * Returns the normal distribution for the given number in the distribution.
 78+ * @param x - Value to use.
 79+ * @param meanValue - The mean value of the distribution.
 80+ * @param standDev - The standard deviation of the distribution.
 81+ * @param cumulative - 0 calculates the density function, 1 calculates the distribution.
 82+ * @returns {number}
 83+ * @constructor
 84+ */
 85+var NORMDIST = function (x, meanValue, standDev, cumulative) {
 86+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 4, "NORMDIST");
 87+    x = TypeConverter_1.TypeConverter.firstValueAsNumber(x);
 88+    meanValue = TypeConverter_1.TypeConverter.firstValueAsNumber(meanValue);
 89+    standDev = TypeConverter_1.TypeConverter.firstValueAsNumber(standDev);
 90+    cumulative = TypeConverter_1.TypeConverter.firstValueAsNumber(cumulative);
 91+    if (standDev <= 0) {
 92+        throw new Errors_1.NumError("Function NORMDIST parameter 3 value should be greater than 0. It is " + standDev + ".");
 93+    }
 94+    return (cumulative === 0) ? _pdf(x, meanValue, standDev) : _cdf(x, meanValue, standDev);
 95+};
 96+exports.NORMDIST = NORMDIST;
 97diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 98index 4f18269..867af80 100644
 99--- a/src/Formulas/AllFormulas.ts
100+++ b/src/Formulas/AllFormulas.ts
101@@ -165,7 +165,8 @@ import {
102   PERCENTRANK,
103   PERCENTRANK$EXC,
104   NORMSINV,
105-  NORMSDIST
106+  NORMSDIST,
107+  NORMDIST
108 } from "./Statistical";
109 import {
110   ARABIC,
111@@ -402,5 +403,6 @@ export {
112   PERCENTRANK,
113   PERCENTRANK$EXC,
114   NORMSINV,
115-  NORMSDIST
116+  NORMSDIST,
117+  NORMDIST
118 }
119\ No newline at end of file
120diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
121index f45a4ab..f03e836 100644
122--- a/src/Formulas/Statistical.ts
123+++ b/src/Formulas/Statistical.ts
124@@ -1095,6 +1095,15 @@ var NORMSINV = function (probability) {
125 };
126 
127 
128+function _cdf(x, mValue, stdVal) {
129+  return 0.5 * (1 + erf((x - mValue) / Math.sqrt(2 * stdVal * stdVal)));
130+}
131+
132+function _pdf(x, meanVal, std) {
133+  return Math.exp(-0.5 * Math.log(2 * Math.PI) -
134+    Math.log(std) - Math.pow(x - meanVal, 2) / (2 * std * std));
135+}
136+
137 /**
138  * Returns the standard normal cumulative distribution for the given number.
139  * @param z - Value to use in calculation.
140@@ -1104,13 +1113,32 @@ var NORMSINV = function (probability) {
141 var NORMSDIST = function (z) {
142   ArgsChecker.checkLength(arguments, 1, "NORMSDIST");
143   z = TypeConverter.firstValueAsNumber(z);
144-  function _cdf(x, mValue, stdVal) {
145-    return 0.5 * (1 + erf((x - mValue) / Math.sqrt(2 * stdVal * stdVal)));
146-  }
147   return _cdf(z, 0, 1);
148 };
149 
150 
151+/**
152+ * Returns the normal distribution for the given number in the distribution.
153+ * @param x - Value to use.
154+ * @param meanValue - The mean value of the distribution.
155+ * @param standDev - The standard deviation of the distribution.
156+ * @param cumulative - 0 calculates the density function, 1 calculates the distribution.
157+ * @returns {number}
158+ * @constructor
159+ */
160+var NORMDIST =  function (x, meanValue, standDev, cumulative) {
161+  ArgsChecker.checkLength(arguments, 4, "NORMDIST");
162+  x = TypeConverter.firstValueAsNumber(x);
163+  meanValue = TypeConverter.firstValueAsNumber(meanValue);
164+  standDev = TypeConverter.firstValueAsNumber(standDev);
165+  cumulative = TypeConverter.firstValueAsNumber(cumulative);
166+  if (standDev <= 0) {
167+    throw new NumError("Function NORMDIST parameter 3 value should be greater than 0. It is " + standDev + ".");
168+  }
169+  return (cumulative === 0) ? _pdf(x, meanValue, standDev) : _cdf(x, meanValue, standDev);
170+};
171+
172+
173 export {
174   AVERAGE,
175   AVERAGEA,
176@@ -1149,5 +1177,6 @@ export {
177   PERCENTRANK,
178   PERCENTRANK$EXC,
179   NORMSINV,
180-  NORMSDIST
181+  NORMSDIST,
182+  NORMDIST
183 }
184\ No newline at end of file
185diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
186index 7550095..b6b462b 100644
187--- a/tests/Formulas/StatisticalTest.ts
188+++ b/tests/Formulas/StatisticalTest.ts
189@@ -36,7 +36,8 @@ import {
190   PERCENTRANK,
191   PERCENTRANK$EXC,
192   NORMSINV,
193-  NORMSDIST
194+  NORMSDIST,
195+  NORMDIST
196 } from "../../src/Formulas/Statistical";
197 import * as ERRORS from "../../src/Errors";
198 import {
199@@ -780,4 +781,23 @@ test("NORMSDIST", function() {
200   catchAndAssertEquals(function() {
201     NORMSDIST.apply(this, [1, 2]);
202   }, ERRORS.NA_ERROR);
203+});
204+
205+
206+test("NORMDIST", function() {
207+  assertEquals(NORMDIST(1, 0, 6, true), 0.5661838326109037);
208+  assertEquals(NORMDIST(1, 0, 6, false), 0.06557328601698999);
209+  assertEquals(NORMDIST(0.5, 0.44, 8, true), 0.5029920390526184);
210+  assertEquals(NORMDIST(0.5, 0.44, 8, false), 0.04986638253844748);
211+  assertEquals(NORMDIST(-0.5, 0.44, 8, true), 0.45323192202214374);
212+  assertEquals(NORMDIST(-0.5, -100, 100, true), 0.840131867824506);
213+  catchAndAssertEquals(function() {
214+    NORMDIST(-0.5, 0.44, 0, true);
215+  }, ERRORS.NUM_ERROR);
216+  catchAndAssertEquals(function() {
217+    NORMDIST.apply(this, [1, 0, 6]);
218+  }, ERRORS.NA_ERROR);
219+  catchAndAssertEquals(function() {
220+    NORMDIST.apply(this, [1, 0, 6, true, 5]);
221+  }, ERRORS.NA_ERROR);
222 });
223\ No newline at end of file
224diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
225index 910dd0c..c52ad7d 100644
226--- a/tests/SheetFormulaTest.ts
227+++ b/tests/SheetFormulaTest.ts
228@@ -785,6 +785,10 @@ test("Sheet NORMSINV", function(){
229   assertFormulaEquals('=NORMSINV(0.1)', -1.2815515655446006);
230 });
231 
232+test("Sheet NORMSINV", function(){
233+  assertFormulaEquals('=NORMDIST(1, 0, 6, true)', 0.5661838326109037);
234+});
235+
236 test("Sheet *", function(){
237   assertFormulaEquals('= 10 * 10', 100);
238   assertFormulaEquals('= 10 * 0', 0);