commit
message
[COVAR] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-08 14:25:27
stats
8 file(s) changed,
91 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 88a439e..9635e49 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -1860,6 +1860,16 @@
6 @returns {number}
7 @constructor
8 ```
9+
10+### COVAR
11+
12+```
13+ Returns the covariance of the product of paired deviations.
14+@param dataY - The first range of data.
15+@param dataX - The second range of data.
16+@returns {number}
17+@constructor
18+```
19 ## Text
20
21
22diff --git a/TODO.md b/TODO.md
23index 91e8cfb..85bd4be 100644
24--- a/TODO.md
25+++ b/TODO.md
26@@ -54,7 +54,6 @@ For example 64 tbs to a qt.
27 * TO_DOLLARS - Contingent upon cells having display formats derived from type-hierarchy
28 * TO_PERCENT - Contingent upon cells having display formats derived from type-hierarchy
29 * TO_TEXT - Contingent upon cells having display formats derived from type-hierarchy
30-* COVAR
31 * CRITBINOM
32 * F.DIST.RT
33 * HYPGEOMDIST
34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
35index 6a837e8..fd2811d 100644
36--- a/dist/Formulas/AllFormulas.js
37+++ b/dist/Formulas/AllFormulas.js
38@@ -170,6 +170,7 @@ exports.GEOMEAN = Statistical_1.GEOMEAN;
39 exports.HARMEAN = Statistical_1.HARMEAN;
40 exports.CONFIDENCE = Statistical_1.CONFIDENCE;
41 exports.BINOMDIST = Statistical_1.BINOMDIST;
42+exports.COVAR = Statistical_1.COVAR;
43 var Text_1 = require("./Text");
44 exports.ARABIC = Text_1.ARABIC;
45 exports.CHAR = Text_1.CHAR;
46diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
47index 137bd06..c064564 100644
48--- a/dist/Formulas/Statistical.js
49+++ b/dist/Formulas/Statistical.js
50@@ -1477,3 +1477,27 @@ var BINOMDIST = function (successes, trials, probability, cumulative) {
51 return (cumulative) ? _binomialCDF(successes, trials, probability) : _binomialPDF(successes, trials, probability);
52 };
53 exports.BINOMDIST = BINOMDIST;
54+/**
55+ * Returns the covariance of the product of paired deviations.
56+ * @param dataY - The first range of data.
57+ * @param dataX - The second range of data.
58+ * @returns {number}
59+ * @constructor
60+ */
61+var COVAR = function (dataY, dataX) {
62+ ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "COVAR");
63+ dataY = Filter_1.Filter.flattenAndThrow(dataY).map(TypeConverter_1.TypeConverter.valueToNumber);
64+ dataX = Filter_1.Filter.flattenAndThrow(dataX).map(TypeConverter_1.TypeConverter.valueToNumber);
65+ if (dataX.length !== dataY.length) {
66+ throw new Errors_1.NAError("COVAR has mismatched argument count " + dataY.length + " vs " + dataX.length + ".");
67+ }
68+ var mean1 = MathHelpers_1.mean(dataY);
69+ var mean2 = MathHelpers_1.mean(dataX);
70+ var result = 0;
71+ var n = dataY.length;
72+ for (var i = 0; i < n; i++) {
73+ result += (dataY[i] - mean1) * (dataX[i] - mean2);
74+ }
75+ return result / n;
76+};
77+exports.COVAR = COVAR;
78diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
79index c30dc27..8e342d7 100644
80--- a/src/Formulas/AllFormulas.ts
81+++ b/src/Formulas/AllFormulas.ts
82@@ -175,7 +175,8 @@ import {
83 GEOMEAN,
84 HARMEAN,
85 CONFIDENCE,
86- BINOMDIST
87+ BINOMDIST,
88+ COVAR
89 } from "./Statistical";
90 import {
91 ARABIC,
92@@ -422,5 +423,6 @@ export {
93 N,
94 UNARY_PERCENT,
95 MULTINOMIAL,
96- BINOMDIST
97+ BINOMDIST,
98+ COVAR
99 }
100\ No newline at end of file
101diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
102index 24d1d79..1592b64 100644
103--- a/src/Formulas/Statistical.ts
104+++ b/src/Formulas/Statistical.ts
105@@ -1473,6 +1473,31 @@ var BINOMDIST = function (successes, trials, probability, cumulative) {
106 };
107
108
109+/**
110+ * Returns the covariance of the product of paired deviations.
111+ * @param dataY - The first range of data.
112+ * @param dataX - The second range of data.
113+ * @returns {number}
114+ * @constructor
115+ */
116+var COVAR = function (dataY, dataX) {
117+ ArgsChecker.checkLength(arguments, 2, "COVAR");
118+ dataY = Filter.flattenAndThrow(dataY).map(TypeConverter.valueToNumber);
119+ dataX = Filter.flattenAndThrow(dataX).map(TypeConverter.valueToNumber);
120+ if (dataX.length !== dataY.length) {
121+ throw new NAError("COVAR has mismatched argument count " + dataY.length + " vs " + dataX.length + ".");
122+ }
123+ var mean1 = mean(dataY);
124+ var mean2 = mean(dataX);
125+ var result = 0;
126+ var n = dataY.length;
127+ for (var i = 0; i < n; i++) {
128+ result += (dataY[i] - mean1) * (dataX[i] - mean2);
129+ }
130+ return result / n;
131+};
132+
133+
134 export {
135 AVERAGE,
136 AVERAGEA,
137@@ -1518,5 +1543,6 @@ export {
138 GEOMEAN,
139 HARMEAN,
140 CONFIDENCE,
141- BINOMDIST
142+ BINOMDIST,
143+ COVAR
144 }
145\ No newline at end of file
146diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
147index b1ae495..ce85a03 100644
148--- a/tests/Formulas/StatisticalTest.ts
149+++ b/tests/Formulas/StatisticalTest.ts
150@@ -43,7 +43,8 @@ import {
151 GEOMEAN,
152 HARMEAN,
153 CONFIDENCE,
154- BINOMDIST
155+ BINOMDIST,
156+ COVAR
157 } from "../../src/Formulas/Statistical";
158 import * as ERRORS from "../../src/Errors";
159 import {
160@@ -915,4 +916,18 @@ test("BINOMDIST", function() {
161 catchAndAssertEquals(function() {
162 BINOMDIST.apply(this, [20, 20, 1, true, 10]);
163 }, ERRORS.NA_ERROR);
164+});
165+
166+test("COVAR", function() {
167+ assertEquals(COVAR([2, 4, 5, 1, 3], [7, 3, 1, 3, 4]), -1.5999999999999999);
168+ assertEquals(COVAR([2, 4, 5, 1], [7, 3, 1, 3]), -2);
169+ catchAndAssertEquals(function() {
170+ COVAR([2, 4, 5, 1], [7, 3, 1, 3, 4]);
171+ }, ERRORS.NA_ERROR);
172+ catchAndAssertEquals(function() {
173+ COVAR.apply(this, [[10, 10, 10]]);
174+ }, ERRORS.NA_ERROR);
175+ catchAndAssertEquals(function() {
176+ COVAR.apply(this, [[10, 10, 10], [1, 2, 4], 1000000]);
177+ }, ERRORS.NA_ERROR);
178 });
179\ No newline at end of file
180diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
181index 5bd5766..f23e273 100644
182--- a/tests/SheetFormulaTest.ts
183+++ b/tests/SheetFormulaTest.ts
184@@ -821,6 +821,14 @@ test("Sheet MULTINOMIAL", function(){
185 assertFormulaEquals('=MULTINOMIAL(2, 22)', 276);
186 });
187
188+test("Sheet BINOMDIST", function(){
189+ assertFormulaEquals('=BINOMDIST(14, 22, 0.4, true)', 0.9929516025629364);
190+});
191+
192+test("Sheet COVAR", function(){
193+ assertFormulaEquals('=COVAR([2, 4, 5, 1], [7, 3, 1, 3])', -2);
194+});
195+
196 test("Sheet *", function(){
197 assertFormulaEquals('= 10 * 10', 100);
198 assertFormulaEquals('= 10 * 0', 0);