spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[TRIMMEAN] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-22 02:20:18
stats
8 file(s) changed, 105 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 bf2004c..6dced99 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1497,6 +1497,16 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### TRIMMEAN 
 11+
 12+```
 13+  Returns the mean value of a range excluding some percentage of the range on the high and low ends of the range. 
 14+@param range - Array or range to consider. 
 15+@param percent - The portion of the data to exclude on both ends of the range. 
 16+@returns {number} 
 17+@constructor
 18+```
 19 ## Text
 20 
 21 
 22diff --git a/TODO.md b/TODO.md
 23index 81ab8e0..80e1b53 100644
 24--- a/TODO.md
 25+++ b/TODO.md
 26@@ -98,7 +98,6 @@ For example 64 tbs to a qt.
 27 * T.INV.2T
 28 * TDIST
 29 * TINV
 30-* TRIMMEAN
 31 * TTEST
 32 * VAR
 33 * VARA
 34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 35index a167626..aeabfdb 100644
 36--- a/dist/Formulas/AllFormulas.js
 37+++ b/dist/Formulas/AllFormulas.js
 38@@ -135,6 +135,7 @@ exports.STDEV = Statistical_1.STDEV;
 39 exports.STDEVA = Statistical_1.STDEVA;
 40 exports.STDEVP = Statistical_1.STDEVP;
 41 exports.STDEVPA = Statistical_1.STDEVPA;
 42+exports.TRIMMEAN = Statistical_1.TRIMMEAN;
 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 ed6e4af..d8e123a 100644
 48--- a/dist/Formulas/Statistical.js
 49+++ b/dist/Formulas/Statistical.js
 50@@ -694,3 +694,32 @@ var STDEVPA = function () {
 51     return Math.sqrt(sigma / count);
 52 };
 53 exports.STDEVPA = STDEVPA;
 54+/**
 55+ * Returns the mean value of a range excluding some percentage of the range on the high and low ends of the range.
 56+ * @param range - Array or range to consider.
 57+ * @param percent - The portion of the data to exclude on both ends of the range.
 58+ * @returns {number}
 59+ * @constructor
 60+ */
 61+var TRIMMEAN = function (range, percent) {
 62+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "TRIMMEAN");
 63+    var p = TypeConverter_1.TypeConverter.firstValueAsNumber(percent);
 64+    if (p < 0) {
 65+        throw new Errors_1.NumError("Function TRIMMEAN parameter 2 value is " + p + ". It should be greater than or equal to 0.");
 66+    }
 67+    if (p >= 1) {
 68+        throw new Errors_1.NumError("Function TRIMMEAN parameter 2 value is " + p + ". It should be less than 1.");
 69+    }
 70+    var data = Filter_1.Filter.flattenAndThrow(range).sort(function (a, b) {
 71+        return a - b;
 72+    }).map(function (value) {
 73+        return TypeConverter_1.TypeConverter.valueToNumber(value);
 74+    });
 75+    if (data.length === 0) {
 76+        throw new Errors_1.RefError("TRIMMEAN has no valid input data.");
 77+    }
 78+    var trim = Math_1.FLOOR(data.length * p, 2) / 2;
 79+    var tmp = data.slice(trim, data.length);
 80+    return MathHelpers_1.mean(tmp.slice(0, tmp.length - trim));
 81+};
 82+exports.TRIMMEAN = TRIMMEAN;
 83diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 84index 692b758..70c9a0d 100644
 85--- a/src/Formulas/AllFormulas.ts
 86+++ b/src/Formulas/AllFormulas.ts
 87@@ -140,7 +140,8 @@ import {
 88   STDEV,
 89   STDEVA,
 90   STDEVP,
 91-  STDEVPA
 92+  STDEVPA,
 93+  TRIMMEAN
 94 } from "./Statistical";
 95 import {
 96   ARABIC,
 97@@ -345,5 +346,6 @@ export {
 98   MROUND,
 99   FACTDOUBLE,
100   FREQUENCY,
101-  GROWTH
102+  GROWTH,
103+  TRIMMEAN
104 }
105\ No newline at end of file
106diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
107index edb8903..b6bbe82 100644
108--- a/src/Formulas/Statistical.ts
109+++ b/src/Formulas/Statistical.ts
110@@ -15,7 +15,8 @@ import {
111 } from "../Errors";
112 import {
113   SUM,
114-  ABS
115+  ABS,
116+  FLOOR
117 } from "./Math";
118 import {
119   cdf,
120@@ -670,6 +671,38 @@ var STDEVPA = function (...values) {
121 };
122 
123 
124+/**
125+ * Returns the mean value of a range excluding some percentage of the range on the high and low ends of the range.
126+ * @param range - Array or range to consider.
127+ * @param percent - The portion of the data to exclude on both ends of the range.
128+ * @returns {number}
129+ * @constructor
130+ */
131+var TRIMMEAN = function (range, percent) {
132+  ArgsChecker.checkLength(arguments, 2, "TRIMMEAN");
133+  var p = TypeConverter.firstValueAsNumber(percent);
134+  if (p < 0) {
135+    throw new NumError("Function TRIMMEAN parameter 2 value is " + p + ". It should be greater than or equal to 0.");
136+  }
137+  if (p >= 1) {
138+    throw new NumError("Function TRIMMEAN parameter 2 value is " + p + ". It should be less than 1.");
139+  }
140+  var data = Filter.flattenAndThrow(range).sort(function (a, b) {
141+    return a - b;
142+  }).map(function (value) {
143+    return TypeConverter.valueToNumber(value);
144+  });
145+
146+  if (data.length === 0) {
147+    throw new RefError("TRIMMEAN has no valid input data.");
148+  }
149+
150+  var trim = FLOOR(data.length * p, 2) / 2;
151+  var tmp = data.slice(trim, data.length);
152+  return mean(tmp.slice(0, tmp.length - trim));
153+};
154+
155+
156 export {
157   AVERAGE,
158   AVERAGEA,
159@@ -695,5 +728,6 @@ export {
160   STDEV,
161   STDEVA,
162   STDEVP,
163-  STDEVPA
164+  STDEVPA,
165+  TRIMMEAN
166 }
167\ No newline at end of file
168diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
169index 35105ca..65c5aaa 100644
170--- a/tests/Formulas/StatisticalTest.ts
171+++ b/tests/Formulas/StatisticalTest.ts
172@@ -23,7 +23,8 @@ import {
173   STDEV,
174   STDEVA,
175   STDEVP,
176-  STDEVPA
177+  STDEVPA,
178+  TRIMMEAN
179 } from "../../src/Formulas/Statistical";
180 import * as ERRORS from "../../src/Errors";
181 import {
182@@ -552,4 +553,19 @@ test("STDEVPA", function() {
183   catchAndAssertEquals(function() {
184     STDEVPA([10, 10, [], 10]);
185   }, ERRORS.REF_ERROR);
186+});
187+
188+test("TRIMMEAN", function() {
189+  assertEquals(TRIMMEAN([1.1, 2, 3, 44, 20, 21, 7], 0.05), 14.014285714285714);
190+  assertEquals(TRIMMEAN([1.1, 2, 3, 44, 20, 21, 7, 1, 22], 0.1), 13.455555555555556);
191+  assertEquals(TRIMMEAN([1], 0.1), 1);
192+  catchAndAssertEquals(function() {
193+    TRIMMEAN([], 0.1);
194+  }, ERRORS.REF_ERROR);
195+  catchAndAssertEquals(function() {
196+    TRIMMEAN([10], 1);
197+  }, ERRORS.NUM_ERROR);
198+  catchAndAssertEquals(function() {
199+    TRIMMEAN([10], -1);
200+  }, ERRORS.NUM_ERROR);
201 });
202\ No newline at end of file
203diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
204index 81a39d3..312d32e 100644
205--- a/tests/SheetFormulaTest.ts
206+++ b/tests/SheetFormulaTest.ts
207@@ -687,6 +687,10 @@ test("Sheet GROWTH", function(){
208   assertFormulaEqualsArray('=GROWTH([15.53, 19.99, 20.43, 21.18, 25.93, 30.00, 30.00, 34.01, 36.47],[1, 2, 3, 4, 5, 6, 7, 8, 9],[10, 11, 12])', [41.740521723275876, 46.22712349335047, 51.19598074591973]);
209 });
210 
211+test("Sheet TRIMMEAN", function(){
212+  assertFormulaEquals('=TRIMMEAN([1], 0.1)', 1);
213+});
214+
215 test("Sheet *", function(){
216   assertFormulaEquals('= 10 * 10', 100);
217   assertFormulaEquals('= 10 * 0', 0);