commit
message
[PROB] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 03:14:35
stats
8 file(s) changed,
131 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 c9f4d4c..4a20aed 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -2058,6 +2058,18 @@
6 @returns {number}
7 @constructor
8 ```
9+
10+### PROB
11+
12+```
13+ Returns the probability that values in a range are between two limits. Data is the array or range of data in the ample.
14+@param range - The array or range of data in the sample.
15+@param probability - The array or range of the corresponding probabilities
16+@param start - The start value of the interval whose probabilities are to be summed.
17+@param end - [OPTIONAL] - The end value of the interval whose probabilities are to be summed. If this parameter is missing, the probability for the start value is calculated
18+@returns {number}
19+@constructor
20+```
21 ## Text
22
23
24diff --git a/TODO.md b/TODO.md
25index 5f1ecd4..7078dd2 100644
26--- a/TODO.md
27+++ b/TODO.md
28@@ -66,7 +66,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
29 * LOGINV
30 * LOGNORMDIST
31 * MODE
32-* PROB
33 * RANK
34 * RANK.AVG
35 * RANK.EQ
36diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
37index 9eafc38..5acd2b4 100644
38--- a/dist/Formulas/AllFormulas.js
39+++ b/dist/Formulas/AllFormulas.js
40@@ -191,6 +191,7 @@ exports.PERMUT = Statistical_1.PERMUT;
41 exports.RSQ = Statistical_1.RSQ;
42 exports.SKEW = Statistical_1.SKEW;
43 exports.STEYX = Statistical_1.STEYX;
44+exports.PROB = Statistical_1.PROB;
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 14196d3..046b0f9 100644
50--- a/dist/Formulas/Statistical.js
51+++ b/dist/Formulas/Statistical.js
52@@ -1787,3 +1787,43 @@ var STEYX = function (rangeY, rangeX) {
53 return Math.sqrt((lft - num * num / den) / (n - 2));
54 };
55 exports.STEYX = STEYX;
56+/**
57+ * Returns the probability that values in a range are between two limits. Data is the array or range of data in the
58+ * sample.
59+ * @param range - The array or range of data in the sample.
60+ * @param probability - The array or range of the corresponding probabilities
61+ * @param start - The start value of the interval whose probabilities are to be summed.
62+ * @param end - [OPTIONAL] - The end value of the interval whose probabilities are to be summed. If this parameter is
63+ * missing, the probability for the start value is calculated
64+ * @returns {number}
65+ * @constructor
66+ */
67+var PROB = function (range, probability, start, end) {
68+ ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 3, 4, "PROB");
69+ range = Filter_1.Filter.flattenAndThrow(range);
70+ probability = Filter_1.Filter.flattenAndThrow(probability);
71+ if (range.length !== probability.length) {
72+ throw new Errors_1.NAError("PROB has mismatched argument count " + range.length + " vs " + probability.length + ".");
73+ }
74+ var sum = Math_1.SUM(probability);
75+ if (sum <= 0 || sum > 1) {
76+ throw new Errors_1.ValueError("Function PROB parameter 2 should sum to 1, but sums to " + sum + ".");
77+ }
78+ start = TypeConverter_1.TypeConverter.firstValueAsNumber(start);
79+ end = (end === undefined) ? start : TypeConverter_1.TypeConverter.firstValueAsNumber(end);
80+ if (start === end) {
81+ return (range.indexOf(start) >= 0) ? probability[range.indexOf(start)] : 0;
82+ }
83+ var sorted = range.sort(function (a, b) {
84+ return a - b;
85+ });
86+ var n = sorted.length;
87+ var result = 0;
88+ for (var i = 0; i < n; i++) {
89+ if (sorted[i] >= start && sorted[i] <= end) {
90+ result += probability[range.indexOf(sorted[i])];
91+ }
92+ }
93+ return result;
94+};
95+exports.PROB = PROB;
96diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
97index dc1b6c7..1c85403 100644
98--- a/src/Formulas/AllFormulas.ts
99+++ b/src/Formulas/AllFormulas.ts
100@@ -196,7 +196,8 @@ import {
101 PERMUT,
102 RSQ,
103 SKEW,
104- STEYX
105+ STEYX,
106+ PROB
107 } from "./Statistical";
108 import {
109 ARABIC,
110@@ -467,5 +468,6 @@ export {
111 PERMUT,
112 RSQ,
113 SKEW,
114- STEYX
115+ STEYX,
116+ PROB
117 }
118\ No newline at end of file
119diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
120index 756f04c..6db312c 100644
121--- a/src/Formulas/Statistical.ts
122+++ b/src/Formulas/Statistical.ts
123@@ -11,7 +11,7 @@ import {
124 TypeConverter
125 } from "../Utilities/TypeConverter";
126 import {
127- RefError, NumError, DivZeroError, NAError
128+ RefError, NumError, DivZeroError, NAError, ValueError
129 } from "../Errors";
130 import {
131 SUM,
132@@ -1769,6 +1769,47 @@ var STEYX = function (rangeY, rangeX) {
133 };
134
135
136+/**
137+ * Returns the probability that values in a range are between two limits. Data is the array or range of data in the
138+ * sample.
139+ * @param range - The array or range of data in the sample.
140+ * @param probability - The array or range of the corresponding probabilities
141+ * @param start - The start value of the interval whose probabilities are to be summed.
142+ * @param end - [OPTIONAL] - The end value of the interval whose probabilities are to be summed. If this parameter is
143+ * missing, the probability for the start value is calculated
144+ * @returns {number}
145+ * @constructor
146+ */
147+var PROB = function (range, probability, start, end?) {
148+ ArgsChecker.checkLengthWithin(arguments, 3, 4, "PROB");
149+ range = Filter.flattenAndThrow(range);
150+ probability = Filter.flattenAndThrow(probability);
151+ if (range.length !== probability.length) {
152+ throw new NAError("PROB has mismatched argument count " + range.length + " vs " + probability.length + ".");
153+ }
154+ var sum = SUM(probability);
155+ if (sum <=0 || sum > 1) {
156+ throw new ValueError("Function PROB parameter 2 should sum to 1, but sums to " + sum + ".");
157+ }
158+ start = TypeConverter.firstValueAsNumber(start);
159+ end = (end === undefined) ? start : TypeConverter.firstValueAsNumber(end);
160+ if (start === end) {
161+ return (range.indexOf(start) >= 0) ? probability[range.indexOf(start)] : 0;
162+ }
163+ var sorted = range.sort(function (a, b) {
164+ return a - b;
165+ });
166+ var n = sorted.length;
167+ var result = 0;
168+ for (var i = 0; i < n; i++) {
169+ if (sorted[i] >= start && sorted[i] <= end) {
170+ result += probability[range.indexOf(sorted[i])];
171+ }
172+ }
173+ return result;
174+};
175+
176+
177 export {
178 AVERAGE,
179 AVERAGEA,
180@@ -1824,5 +1865,6 @@ export {
181 PERMUT,
182 RSQ,
183 SKEW,
184- STEYX
185+ STEYX,
186+ PROB
187 }
188\ No newline at end of file
189diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
190index 9c4287f..700b319 100644
191--- a/tests/Formulas/StatisticalTest.ts
192+++ b/tests/Formulas/StatisticalTest.ts
193@@ -53,7 +53,8 @@ import {
194 PERMUT,
195 RSQ,
196 SKEW,
197- STEYX
198+ STEYX,
199+ PROB
200 } from "../../src/Formulas/Statistical";
201 import * as ERRORS from "../../src/Errors";
202 import {
203@@ -1090,4 +1091,26 @@ test("STEYX", function() {
204 catchAndAssertEquals(function() {
205 STEYX.apply(this, [[1, 2, 3], [1, 2, 3], [1, 2, 3]]);
206 }, ERRORS.NA_ERROR);
207+});
208+
209+test("PROB", function() {
210+ assertEquals(PROB([1, 2, 3, 4], [0.25, 0.25, 0.25, 0.25], 3), 0.25);
211+ assertEquals(PROB([1], [1], 3), 0);
212+ assertEquals(PROB([1], [1], 0.1, 100), 1);
213+ assertEquals(PROB([1, 2, 3], [0.25, 0.25, 0.5], 3), 0.5);
214+ assertEquals(PROB([1, 2, 4], [0.25, 0.25, 0.5], 3), 0);
215+ assertEquals(PROB([1, 2, 3], [0.25, 0.25, 0.5], 3, 100), 0.5);
216+ assertEquals(PROB([1, 2, 3], [0.25, 0.25, 0.5], 0.1, 100), 1);
217+ catchAndAssertEquals(function() {
218+ PROB([1, 2, 3, 4], [0.25, 0.25, 0.25], 3);
219+ }, ERRORS.NA_ERROR);
220+ catchAndAssertEquals(function() {
221+ PROB([1, 2, 3, 4], [0.25, 0.25, 0.25, 0.99], 3);
222+ }, ERRORS.VALUE_ERROR);
223+ catchAndAssertEquals(function() {
224+ PROB.apply(this, [[1, 2, 3, 4], [0.25, 0.25, 0.25]]);
225+ }, ERRORS.NA_ERROR);
226+ catchAndAssertEquals(function() {
227+ PROB.apply(this, [[1, 2, 3, 4], [0.25, 0.25, 0.25], 10, 10, 10]);
228+ }, ERRORS.NA_ERROR);
229 });
230\ No newline at end of file
231diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
232index 5ed0f3a..afc6d67 100644
233--- a/tests/SheetFormulaTest.ts
234+++ b/tests/SheetFormulaTest.ts
235@@ -922,6 +922,10 @@ test("Sheet STEYX", function(){
236 assertFormulaEquals('=STEYX([1, 2, 3, 4], [1, 3, 5, 2])', 1.4638501094227998);
237 });
238
239+test("Sheet PROB", function(){
240+ assertFormulaEquals('=PROB([1, 2, 3, 4], [0.25, 0.25, 0.25, 0.25], 3)', 0.25);
241+});
242+
243 test("Sheet *", function(){
244 assertFormulaEquals('= 10 * 10', 100);
245 assertFormulaEquals('= 10 * 0', 0);