commit
message
Added Formulas.EXPONDIST
author
Ben Vogt <[email protected]>
date
2017-02-20 18:41:30
stats
3 file(s) changed,
52 insertions(+),
1 deletions(-)
files
src/RawFormulas/Math.ts
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
1diff --git a/src/RawFormulas/Math.ts b/src/RawFormulas/Math.ts
2index 1933bb9..466d7ba 100644
3--- a/src/RawFormulas/Math.ts
4+++ b/src/RawFormulas/Math.ts
5@@ -1819,6 +1819,31 @@ var EFFECT = function (...values) : number {
6 };
7
8
9+/**
10+ * Returns the value of the exponential distribution function with a specified lambda at a specified value.
11+ * @param values[0] x - The input to the exponential distribution function. If cumulative is TRUE then EXPONDIST returns
12+ * the cumulative probability of all values up to x.
13+ * @param values[1] lambda - The lambda to specify the exponential distribution function.
14+ * @param values[2] cumulative - Whether to use the exponential cumulative distribution.
15+ * @returns {number} value of the exponential distribution function.
16+ * @constructor
17+ */
18+var EXPONDIST = function (...values) : number {
19+ function cdf(x, rate) {
20+ return x < 0 ? 0 : 1 - Math.exp(-rate * x);
21+ }
22+ function pdf(x, rate) {
23+ return x < 0 ? 0 : rate * Math.exp(-rate * x);
24+ }
25+ ArgsChecker.checkLength(values, 3);
26+ var x = TypeCaster.firstValueAsNumber(values[0]);
27+ var lambda = TypeCaster.firstValueAsNumber(values[1]);
28+ var cumulative = TypeCaster.firstValueAsBoolean(values[2]);
29+ return (cumulative) ? cdf(x, lambda) : pdf(x, lambda);
30+};
31+
32+
33+
34 export {
35 ABS,
36 ACOS,
37@@ -1845,6 +1870,7 @@ export {
38 EVEN,
39 ERF,
40 ERFC,
41+ EXPONDIST,
42 FDIST$LEFTTAILED,
43 FINV,
44 FISHER,
45diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
46index dd375f1..c995254 100644
47--- a/src/RawFormulas/RawFormulas.ts
48+++ b/src/RawFormulas/RawFormulas.ts
49@@ -27,6 +27,7 @@ import {
50 EVEN,
51 ERF,
52 ERFC,
53+ EXPONDIST,
54 FDIST$LEFTTAILED,
55 FINV,
56 FISHER,
57@@ -126,7 +127,6 @@ var EOMONTH = function (start_date, months) {
58 var edate = moment(start_date).add(months, 'months');
59 return new Date(edate.year(), edate.month(), edate.daysInMonth());
60 };
61-var EXPONDIST = Formula["EXPONDIST"];
62 var __COMPLEX = {
63 "F.DIST": FDIST$LEFTTAILED
64 };
65diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
66index a10646d..8eab87a 100644
67--- a/tests/FormulasTest.ts
68+++ b/tests/FormulasTest.ts
69@@ -1038,10 +1038,34 @@ catchAndAssertEquals(function() {
70 }, ERRORS.NA_ERROR);
71
72
73+// Test EXPONDIST
74 assertEquals(EXPONDIST(4, 0.5, false), 0.06766764161830635);
75+assertEquals(EXPONDIST(4, 0.5, 0), 0.06766764161830635);
76+assertEquals(EXPONDIST(4, 0.5, true), 0.8646647167633873);
77+assertEquals(EXPONDIST(4, 0.5, 1), 0.8646647167633873);
78+assertEquals(EXPONDIST(4, 0.5, -1), 0.8646647167633873);
79+assertEquals(EXPONDIST([4, "str"], ["0.5"], [false]), 0.06766764161830635);
80+catchAndAssertEquals(function() {
81+ EXPONDIST("str", 0.5, "1");
82+}, ERRORS.VALUE_ERROR);
83+catchAndAssertEquals(function() {
84+ EXPONDIST(4, 0.5, "1");
85+}, ERRORS.VALUE_ERROR);
86+catchAndAssertEquals(function() {
87+ EXPONDIST();
88+}, ERRORS.NA_ERROR);
89+catchAndAssertEquals(function() {
90+ EXPONDIST(4, 0.5);
91+}, ERRORS.NA_ERROR);
92+catchAndAssertEquals(function() {
93+ EXPONDIST(4, 0.5, true, 1);
94+}, ERRORS.NA_ERROR);
95+
96
97+// Test FALSE
98 assertEquals(FALSE(), false);
99
100+
101 // Test F.DIST
102 assertEquals(__COMPLEX["F.DIST"](15.35, 7, 6, false), 0.0003451054686025578);
103 assertEquals(__COMPLEX["F.DIST"](15.35, 7, 6, true), 0.9980694465675269);