spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[LOGNORMDIST] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-30 13:04:18
stats
8 file(s) changed, 96 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 1122db0..a17eef6 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2112,6 +2112,17 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### GNORMDIST 
 11+
 12+```
 13+  Returns the cumulative lognormal distribution for the given number. 
 14+@param x - The probability value. 
 15+@param meanValue - The mean value of the standard logarithmic distribution. 
 16+@param standardDev - The standard deviation of the standard logarithmic distribution. 
 17+@returns {number} 
 18+@constructor
 19+```
 20 ## Text
 21 
 22 
 23diff --git a/TODO.md b/TODO.md
 24index 7ade243..16ccfc2 100644
 25--- a/TODO.md
 26+++ b/TODO.md
 27@@ -64,7 +64,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 28 * F.DIST.RT
 29 * HYPGEOMDIST
 30 * LOGINV
 31-* LOGNORMDIST
 32 * T.INV
 33 * T.INV.2T
 34 * TDIST
 35diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 36index 7d44b26..daf55a8 100644
 37--- a/dist/Formulas/AllFormulas.js
 38+++ b/dist/Formulas/AllFormulas.js
 39@@ -196,6 +196,7 @@ exports.MODE = Statistical_1.MODE;
 40 exports.RANK = Statistical_1.RANK;
 41 exports.RANK$AVG = Statistical_1.RANK$AVG;
 42 exports.RANK$EQ = Statistical_1.RANK$EQ;
 43+exports.LOGNORMDIST = Statistical_1.LOGNORMDIST;
 44 var Text_1 = require("./Text");
 45 exports.ARABIC = Text_1.ARABIC;
 46 exports.CHAR = Text_1.CHAR;
 47diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 48index 8f5dbaf..a14a3c4 100644
 49--- a/dist/Formulas/Statistical.js
 50+++ b/dist/Formulas/Statistical.js
 51@@ -1955,3 +1955,26 @@ var RANK$EQ = function (value, data, isAscending) {
 52     return range.indexOf(value) + 1;
 53 };
 54 exports.RANK$EQ = RANK$EQ;
 55+/**
 56+ * Returns the cumulative lognormal distribution for the given number.
 57+ * @param x - The probability value.
 58+ * @param meanValue - The mean value of the standard logarithmic distribution.
 59+ * @param standardDev - The standard deviation of the standard logarithmic distribution.
 60+ * @returns {number}
 61+ * @constructor
 62+ */
 63+var LOGNORMDIST = function (x, meanValue, standardDev) {
 64+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 3, "LOGNORMDIST");
 65+    x = TypeConverter_1.TypeConverter.firstValueAsNumber(x);
 66+    meanValue = TypeConverter_1.TypeConverter.firstValueAsNumber(meanValue);
 67+    standardDev = TypeConverter_1.TypeConverter.firstValueAsNumber(standardDev);
 68+    if (x <= 0) {
 69+        throw new Errors_1.NumError("Function LOGNORMDIST parameter 1 value is " + x + ", but should be greater than 0.");
 70+    }
 71+    if (standardDev <= 0) {
 72+        throw new Errors_1.NumError("Function LOGNORMDIST parameter 3 value is " + standardDev + ", but should be greater than 0.");
 73+    }
 74+    var a = (Math.log(x) - meanValue) / Math.sqrt(2 * standardDev * standardDev);
 75+    return 0.5 + 0.5 * MathHelpers_1.erf(a);
 76+};
 77+exports.LOGNORMDIST = LOGNORMDIST;
 78diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 79index f882bdd..427e42e 100644
 80--- a/src/Formulas/AllFormulas.ts
 81+++ b/src/Formulas/AllFormulas.ts
 82@@ -201,7 +201,8 @@ import {
 83   MODE,
 84   RANK,
 85   RANK$AVG,
 86-  RANK$EQ
 87+  RANK$EQ,
 88+  LOGNORMDIST
 89 } from "./Statistical";
 90 import {
 91   ARABIC,
 92@@ -479,5 +480,6 @@ export {
 93   MODE,
 94   RANK,
 95   RANK$AVG,
 96-  RANK$EQ
 97+  RANK$EQ,
 98+  LOGNORMDIST
 99 }
100\ No newline at end of file
101diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
102index 11d6285..0c1a530 100644
103--- a/src/Formulas/Statistical.ts
104+++ b/src/Formulas/Statistical.ts
105@@ -1939,6 +1939,29 @@ var RANK$EQ =  function (value, data, isAscending?) {
106   return range.indexOf(value) + 1;
107 };
108 
109+/**
110+ * Returns the cumulative lognormal distribution for the given number.
111+ * @param x - The probability value.
112+ * @param meanValue - The mean value of the standard logarithmic distribution.
113+ * @param standardDev - The standard deviation of the standard logarithmic distribution.
114+ * @returns {number}
115+ * @constructor
116+ */
117+var LOGNORMDIST = function (x, meanValue, standardDev) {
118+  ArgsChecker.checkLength(arguments, 3, "LOGNORMDIST");
119+  x = TypeConverter.firstValueAsNumber(x);
120+  meanValue = TypeConverter.firstValueAsNumber(meanValue);
121+  standardDev = TypeConverter.firstValueAsNumber(standardDev);
122+  if (x <= 0) {
123+    throw new NumError("Function LOGNORMDIST parameter 1 value is " + x + ", but should be greater than 0.");
124+  }
125+  if (standardDev <= 0) {
126+    throw new NumError("Function LOGNORMDIST parameter 3 value is " + standardDev + ", but should be greater than 0.");
127+  }
128+  var a = (Math.log(x) - meanValue)/Math.sqrt(2 * standardDev * standardDev);
129+  return 0.5 + 0.5 * erf(a);
130+};
131+
132 
133 export {
134   AVERAGE,
135@@ -2000,5 +2023,6 @@ export {
136   MODE,
137   RANK,
138   RANK$AVG,
139-  RANK$EQ
140+  RANK$EQ,
141+  LOGNORMDIST
142 }
143\ No newline at end of file
144diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
145index 5218046..8bd796b 100644
146--- a/tests/Formulas/StatisticalTest.ts
147+++ b/tests/Formulas/StatisticalTest.ts
148@@ -58,7 +58,8 @@ import {
149   MODE,
150   RANK,
151   RANK$AVG,
152-  RANK$EQ
153+  RANK$EQ,
154+  LOGNORMDIST
155 } from "../../src/Formulas/Statistical";
156 import * as ERRORS from "../../src/Errors";
157 import {
158@@ -1185,4 +1186,29 @@ test("RANK.EQ", function() {
159   catchAndAssertEquals(function() {
160     RANK$EQ.apply(this, [44, [7, 1], true, false]);
161   }, ERRORS.NA_ERROR);
162+});
163+
164+test("LOGNORMDIST", function() {
165+  assertEquals(LOGNORMDIST(4, 4, 6), 0.33155709720516946);
166+  assertEquals(LOGNORMDIST(1, 4, 6), 0.2524925375469229);
167+  assertEquals(LOGNORMDIST(2, 4, 6), 0.29076812115284056);
168+  assertEquals(LOGNORMDIST(20, 100, 6), 0);
169+  catchAndAssertEquals(function() {
170+    LOGNORMDIST(0, 4, 6);
171+  }, ERRORS.NUM_ERROR);
172+  catchAndAssertEquals(function() {
173+    LOGNORMDIST(-10, 4, 6);
174+  }, ERRORS.NUM_ERROR);
175+  catchAndAssertEquals(function() {
176+    LOGNORMDIST(1, 4, -6);
177+  }, ERRORS.NUM_ERROR);
178+  catchAndAssertEquals(function() {
179+    LOGNORMDIST(1, 4, 0);
180+  }, ERRORS.NUM_ERROR);
181+  catchAndAssertEquals(function() {
182+    LOGNORMDIST.apply(this, []);
183+  }, ERRORS.NA_ERROR);
184+  catchAndAssertEquals(function() {
185+    LOGNORMDIST.apply(this, [4, 4, 4, 4]);
186+  }, ERRORS.NA_ERROR);
187 });
188\ No newline at end of file
189diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
190index ec0f3cc..7f626c0 100644
191--- a/tests/SheetFormulaTest.ts
192+++ b/tests/SheetFormulaTest.ts
193@@ -942,6 +942,10 @@ test("Sheet RANK.EQ", function(){
194   assertFormulaEquals('=RANK.EQ([2], [1, 2, 3, 4, 5, 6, 7, 8, 9], true)', 2);
195 });
196 
197+test("Sheet LOGNORMDIST", function(){
198+  assertFormulaEquals('=LOGNORMDIST(4, 4, 6)', 0.33155709720516946);
199+});
200+
201 test("Sheet *", function(){
202   assertFormulaEquals('= 10 * 10', 100);
203   assertFormulaEquals('= 10 * 0', 0);