commit
message
[VAR] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 00:46:22
stats
8 file(s) changed,
88 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 973d47b..970c2d1 100644
3--- a/DOCS.md
4+++ b/DOCS.md
5@@ -2011,6 +2011,14 @@
6 @returns {number}
7 @constructor
8 ```
9+
10+### VAR
11+
12+```
13+ Estimate the variance based on a sample.
14+@param values - Values in sample.
15+@constructor
16+```
17 ## Text
18
19
20diff --git a/TODO.md b/TODO.md
21index 005f5af..94e569e 100644
22--- a/TODO.md
23+++ b/TODO.md
24@@ -79,7 +79,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
25 * TDIST
26 * TINV
27 * TTEST
28-* VAR
29 * ZTEST
30 * CLEAN
31 * FIND
32diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
33index a587219..9626897 100644
34--- a/dist/Formulas/AllFormulas.js
35+++ b/dist/Formulas/AllFormulas.js
36@@ -186,6 +186,7 @@ exports.WEIBULL = Statistical_1.WEIBULL;
37 exports.VARPA = Statistical_1.VARPA;
38 exports.VARP = Statistical_1.VARP;
39 exports.VARA = Statistical_1.VARA;
40+exports.VAR = Statistical_1.VAR;
41 var Text_1 = require("./Text");
42 exports.ARABIC = Text_1.ARABIC;
43 exports.CHAR = Text_1.CHAR;
44diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
45index 6ea051f..56e1fef 100644
46--- a/dist/Formulas/Statistical.js
47+++ b/dist/Formulas/Statistical.js
48@@ -1633,3 +1633,29 @@ var VARA = function () {
49 return sigma / (count - 1);
50 };
51 exports.VARA = VARA;
52+/**
53+ * Estimate the variance based on a sample.
54+ * @param values - Values in sample.
55+ * @constructor
56+ */
57+var VAR = function () {
58+ var values = [];
59+ for (var _i = 0; _i < arguments.length; _i++) {
60+ values[_i] = arguments[_i];
61+ }
62+ ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "VAR");
63+ var range = Filter_1.Filter.flattenAndThrow(values).map(TypeConverter_1.TypeConverter.valueToNumber);
64+ var n = range.length;
65+ if (n < 2) {
66+ throw new Errors_1.DivZeroError("Evaluation of function VAR caused a divide by zero error.");
67+ }
68+ var sigma = 0;
69+ var count = 0;
70+ var mean = AVERAGE(range);
71+ for (var i = 0; i < n; i++) {
72+ sigma += Math.pow(range[i] - mean, 2);
73+ count++;
74+ }
75+ return sigma / (count - 1);
76+};
77+exports.VAR = VAR;
78diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
79index d0d4f17..a5fc798 100644
80--- a/src/Formulas/AllFormulas.ts
81+++ b/src/Formulas/AllFormulas.ts
82@@ -191,7 +191,8 @@ import {
83 WEIBULL,
84 VARPA,
85 VARP,
86- VARA
87+ VARA,
88+ VAR
89 } from "./Statistical";
90 import {
91 ARABIC,
92@@ -457,5 +458,6 @@ export {
93 WEIBULL,
94 VARPA,
95 VARP,
96- VARA
97+ VARA,
98+ VAR
99 }
100\ No newline at end of file
101diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
102index 3efa273..0abb22b 100644
103--- a/src/Formulas/Statistical.ts
104+++ b/src/Formulas/Statistical.ts
105@@ -1620,6 +1620,28 @@ var VARA = function (...values) {
106 };
107
108
109+/**
110+ * Estimate the variance based on a sample.
111+ * @param values - Values in sample.
112+ * @constructor
113+ */
114+var VAR = function (...values) {
115+ ArgsChecker.checkAtLeastLength(arguments, 1, "VAR");
116+ var range = Filter.flattenAndThrow(values).map(TypeConverter.valueToNumber);
117+ var n = range.length;
118+ if (n < 2) {
119+ throw new DivZeroError("Evaluation of function VAR caused a divide by zero error.");
120+ }
121+ var sigma = 0;
122+ var count = 0;
123+ var mean = AVERAGE(range);
124+ for (var i = 0; i < n; i++) {
125+ sigma += Math.pow(range[i] - mean, 2);
126+ count++;
127+ }
128+ return sigma / (count - 1);
129+};
130+
131
132 export {
133 AVERAGE,
134@@ -1671,5 +1693,6 @@ export {
135 WEIBULL,
136 VARPA,
137 VARP,
138- VARA
139+ VARA,
140+ VAR
141 }
142\ No newline at end of file
143diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
144index 5d14aa6..ffba91a 100644
145--- a/tests/Formulas/StatisticalTest.ts
146+++ b/tests/Formulas/StatisticalTest.ts
147@@ -48,7 +48,8 @@ import {
148 WEIBULL,
149 VARPA,
150 VARP,
151- VARA
152+ VARA,
153+ VAR
154 } from "../../src/Formulas/Statistical";
155 import * as ERRORS from "../../src/Errors";
156 import {
157@@ -1005,4 +1006,20 @@ test("VARA", function() {
158 catchAndAssertEquals(function() {
159 VARA.apply(this, []);
160 }, ERRORS.NA_ERROR);
161+});
162+
163+test("VAR", function() {
164+ assertEquals(VAR(1, 2, 3, 4, 5, 6, 7, 8), 6);
165+ assertEquals(VAR(1, 2, 3, 4, 5, 6, 7, 8, "0"), 7.5);
166+ assertEquals(VAR(1, 2, 3, 4, 5, 6, 7, 8, false), 7.5);
167+ assertEquals(VAR(1, 2, 3, 4, 5, 6, 7, 8, "10"), 8.611111111111112);
168+ catchAndAssertEquals(function() {
169+ VAR(1);
170+ }, ERRORS.DIV_ZERO_ERROR);
171+ catchAndAssertEquals(function() {
172+ VAR(1, 2, 3, 4, 5, 6, 7, 8, "ignore");
173+ }, ERRORS.VALUE_ERROR);
174+ catchAndAssertEquals(function() {
175+ VAR.apply(this, []);
176+ }, ERRORS.NA_ERROR);
177 });
178\ No newline at end of file
179diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
180index 3cb8352..139967b 100644
181--- a/tests/SheetFormulaTest.ts
182+++ b/tests/SheetFormulaTest.ts
183@@ -902,6 +902,11 @@ test("Sheet VARA", function(){
184 assertFormulaEquals('=VARA(1, 2, 3, 4, 5, 6, 7, 8)', 6);
185 });
186
187+test("Sheet VAR", function(){
188+ assertFormulaEquals('=VAR(1, 2, 3, 4, 5, 6, 7, 8)', 6);
189+});
190+
191+
192 test("Sheet *", function(){
193 assertFormulaEquals('= 10 * 10', 100);
194 assertFormulaEquals('= 10 * 0', 0);