spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[WEIBULL] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 00:05:02
stats
8 file(s) changed, 106 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 f6cffd6..cb91ccb 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1972,6 +1972,18 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### WEIBULL 
 11+
 12+```
 13+  Returns the values of the Weibull distribution for the given number. 
 14+@param x - Number to use in calculation. 
 15+@param shape - The Alpha parameter of the Weibull distribution. Should be greater than 0. 
 16+@param scale - The Beta parameter of the Weibull distribution. Should be greater than 0. 
 17+@param cumulative - Indicates the type of function: If 0 the form of the function is calculated, if 1 then the distribution is calculated. 
 18+@returns {number} 
 19+@constructor
 20+```
 21 ## Text
 22 
 23 
 24diff --git a/TODO.md b/TODO.md
 25index a1779a2..f2137f4 100644
 26--- a/TODO.md
 27+++ b/TODO.md
 28@@ -83,7 +83,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 29 * VARA
 30 * VARP
 31 * VARPA
 32-* WEIBULL
 33 * ZTEST
 34 * CLEAN
 35 * FIND
 36diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 37index 16a8de3..d89939f 100644
 38--- a/dist/Formulas/AllFormulas.js
 39+++ b/dist/Formulas/AllFormulas.js
 40@@ -182,6 +182,7 @@ exports.HARMEAN = Statistical_1.HARMEAN;
 41 exports.CONFIDENCE = Statistical_1.CONFIDENCE;
 42 exports.BINOMDIST = Statistical_1.BINOMDIST;
 43 exports.COVAR = Statistical_1.COVAR;
 44+exports.WEIBULL = Statistical_1.WEIBULL;
 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 c064564..3bfa5cc 100644
 50--- a/dist/Formulas/Statistical.js
 51+++ b/dist/Formulas/Statistical.js
 52@@ -1501,3 +1501,32 @@ var COVAR = function (dataY, dataX) {
 53     return result / n;
 54 };
 55 exports.COVAR = COVAR;
 56+/**
 57+ * Returns the values of the Weibull distribution for the given number.
 58+ * @param x - Number to use in calculation.
 59+ * @param shape - The Alpha parameter of the Weibull distribution. Should be greater than 0.
 60+ * @param scale - The Beta parameter of the Weibull distribution. Should be greater than 0.
 61+ * @param cumulative - Indicates the type of function: If 0 the form of the function is calculated, if 1 then the
 62+ * distribution is calculated.
 63+ * @returns {number}
 64+ * @constructor
 65+ */
 66+var WEIBULL = function (x, shape, scale, cumulative) {
 67+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 4, "WEIBULL");
 68+    x = TypeConverter_1.TypeConverter.firstValueAsNumber(x);
 69+    if (x < 0) {
 70+        throw new Errors_1.NumError("Function WEIBULL parameter 1 value is " + x + ", but should be greater than or equal to 0.");
 71+    }
 72+    shape = TypeConverter_1.TypeConverter.firstValueAsNumber(shape);
 73+    if (shape <= 0) {
 74+        throw new Errors_1.NumError("Function WEIBULL parameter 2 value is " + shape + ", but should be greater than 0.");
 75+    }
 76+    scale = TypeConverter_1.TypeConverter.firstValueAsNumber(scale);
 77+    if (scale <= 0) {
 78+        throw new Errors_1.NumError("Function WEIBULL parameter 2 value is " + scale + ", but should be greater than 0.");
 79+    }
 80+    cumulative = TypeConverter_1.TypeConverter.firstValueAsNumber(cumulative);
 81+    return (cumulative) ? 1 - Math.exp(-Math.pow(x / scale, shape)) : Math.pow(x, shape - 1)
 82+        * Math.exp(-Math.pow(x / scale, shape)) * shape / Math.pow(scale, shape);
 83+};
 84+exports.WEIBULL = WEIBULL;
 85diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 86index 3b4c70a..2eff062 100644
 87--- a/src/Formulas/AllFormulas.ts
 88+++ b/src/Formulas/AllFormulas.ts
 89@@ -187,7 +187,8 @@ import {
 90   HARMEAN,
 91   CONFIDENCE,
 92   BINOMDIST,
 93-  COVAR
 94+  COVAR,
 95+  WEIBULL
 96 } from "./Statistical";
 97 import {
 98   ARABIC,
 99@@ -449,5 +450,6 @@ export {
100   COLUMN,
101   ROW,
102   T,
103-  PPMT
104+  PPMT,
105+  WEIBULL
106 }
107\ No newline at end of file
108diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
109index 1592b64..a01a62c 100644
110--- a/src/Formulas/Statistical.ts
111+++ b/src/Formulas/Statistical.ts
112@@ -1498,6 +1498,36 @@ var COVAR = function (dataY, dataX) {
113 };
114 
115 
116+/**
117+ * Returns the values of the Weibull distribution for the given number.
118+ * @param x - Number to use in calculation.
119+ * @param shape - The Alpha parameter of the Weibull distribution. Should be greater than 0.
120+ * @param scale - The Beta parameter of the Weibull distribution. Should be greater than 0.
121+ * @param cumulative - Indicates the type of function: If 0 the form of the function is calculated, if 1 then the
122+ * distribution is calculated.
123+ * @returns {number}
124+ * @constructor
125+ */
126+var WEIBULL = function (x, shape, scale, cumulative) {
127+  ArgsChecker.checkLength(arguments, 4, "WEIBULL");
128+  x = TypeConverter.firstValueAsNumber(x);
129+  if (x < 0) {
130+    throw new NumError("Function WEIBULL parameter 1 value is " + x + ", but should be greater than or equal to 0.");
131+  }
132+  shape = TypeConverter.firstValueAsNumber(shape);
133+  if (shape <= 0) {
134+    throw new NumError("Function WEIBULL parameter 2 value is " + shape + ", but should be greater than 0.");
135+  }
136+  scale = TypeConverter.firstValueAsNumber(scale);
137+  if (scale <= 0) {
138+    throw new NumError("Function WEIBULL parameter 2 value is " + scale + ", but should be greater than 0.");
139+  }
140+  cumulative = TypeConverter.firstValueAsNumber(cumulative);
141+  return (cumulative) ? 1 - Math.exp(-Math.pow(x / scale, shape)) : Math.pow(x, shape - 1)
142+      * Math.exp(-Math.pow(x / scale, shape)) * shape / Math.pow(scale, shape);
143+};
144+
145+
146 export {
147   AVERAGE,
148   AVERAGEA,
149@@ -1544,5 +1574,6 @@ export {
150   HARMEAN,
151   CONFIDENCE,
152   BINOMDIST,
153-  COVAR
154+  COVAR,
155+  WEIBULL
156 }
157\ No newline at end of file
158diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
159index ce85a03..4287b7f 100644
160--- a/tests/Formulas/StatisticalTest.ts
161+++ b/tests/Formulas/StatisticalTest.ts
162@@ -44,7 +44,8 @@ import {
163   HARMEAN,
164   CONFIDENCE,
165   BINOMDIST,
166-  COVAR
167+  COVAR,
168+  WEIBULL
169 } from "../../src/Formulas/Statistical";
170 import * as ERRORS from "../../src/Errors";
171 import {
172@@ -930,4 +931,25 @@ test("COVAR", function() {
173   catchAndAssertEquals(function() {
174     COVAR.apply(this, [[10, 10, 10], [1, 2, 4], 1000000]);
175   }, ERRORS.NA_ERROR);
176+});
177+
178+test("WEIBULL", function() {
179+  assertEquals(WEIBULL(2.4, 2, 4, true), 0.30232367392896886);
180+  assertEquals(WEIBULL(3.1, 4, 4, true), 0.3028470073265427);
181+  assertEquals(WEIBULL(0.16, 1, 4, false), 0.2401973597880808);
182+  catchAndAssertEquals(function() {
183+    WEIBULL(-10, 2, 4, true);
184+  }, ERRORS.NUM_ERROR);
185+  catchAndAssertEquals(function() {
186+    WEIBULL(10, 0, 4, true);
187+  }, ERRORS.NUM_ERROR);
188+  catchAndAssertEquals(function() {
189+    WEIBULL(10, 1, 0, true);
190+  }, ERRORS.NUM_ERROR);
191+  catchAndAssertEquals(function() {
192+    WEIBULL.apply(this, [10, 10, 10]);
193+  }, ERRORS.NA_ERROR);
194+  catchAndAssertEquals(function() {
195+    WEIBULL.apply(this, [10, 10, 10, 10, 10]);
196+  }, ERRORS.NA_ERROR);
197 });
198\ No newline at end of file
199diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
200index 90c9d1c..249c297 100644
201--- a/tests/SheetFormulaTest.ts
202+++ b/tests/SheetFormulaTest.ts
203@@ -886,6 +886,10 @@ test("Sheet PPMT", function(){
204   assertFormulaEquals('=PPMT(0, 3, 24, 33000, 0, 1)', -1375.00);
205 });
206 
207+test("Sheet WEIBULL", function(){
208+  assertFormulaEquals('=WEIBULL(2.4, 2, 4, true)', 0.30232367392896886);
209+});
210+
211 test("Sheet *", function(){
212   assertFormulaEquals('= 10 * 10', 100);
213   assertFormulaEquals('= 10 * 0', 0);