spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[SKEW] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-07-12 02:33:54
stats
8 file(s) changed, 90 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 0e497a6..21bc223 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -2039,6 +2039,15 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### SKEW 
 11+
 12+```
 13+  Returns the skewness of a distribution. 
 14+@param values - The numerical values or range. 
 15+@returns {number} 
 16+@constructor
 17+```
 18 ## Text
 19 
 20 
 21diff --git a/TODO.md b/TODO.md
 22index d26cc78..1237d07 100644
 23--- a/TODO.md
 24+++ b/TODO.md
 25@@ -70,7 +70,6 @@ Many of these formulas can be written by allowing the Sheet and Parser to return
 26 * RANK
 27 * RANK.AVG
 28 * RANK.EQ
 29-* SKEW
 30 * STEYX
 31 * T.INV
 32 * T.INV.2T
 33diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 34index a8f463a..7a2480d 100644
 35--- a/dist/Formulas/AllFormulas.js
 36+++ b/dist/Formulas/AllFormulas.js
 37@@ -189,6 +189,7 @@ exports.VARA = Statistical_1.VARA;
 38 exports.VAR = Statistical_1.VAR;
 39 exports.PERMUT = Statistical_1.PERMUT;
 40 exports.RSQ = Statistical_1.RSQ;
 41+exports.SKEW = Statistical_1.SKEW;
 42 var Text_1 = require("./Text");
 43 exports.ARABIC = Text_1.ARABIC;
 44 exports.CHAR = Text_1.CHAR;
 45diff --git a/dist/Formulas/Statistical.js b/dist/Formulas/Statistical.js
 46index 49b5474..284584a 100644
 47--- a/dist/Formulas/Statistical.js
 48+++ b/dist/Formulas/Statistical.js
 49@@ -1725,3 +1725,32 @@ var RSQ = function (rangeY, rangeX) {
 50     return Math.pow(PEARSON(dataX, dataY), 2);
 51 };
 52 exports.RSQ = RSQ;
 53+/**
 54+ * Returns the skewness of a distribution.
 55+ * @param values - The numerical values or range.
 56+ * @returns {number}
 57+ * @constructor
 58+ */
 59+var SKEW = function () {
 60+    var values = [];
 61+    for (var _i = 0; _i < arguments.length; _i++) {
 62+        values[_i] = arguments[_i];
 63+    }
 64+    ArgsChecker_1.ArgsChecker.checkAtLeastLength(arguments, 1, "SKEW");
 65+    var range = Filter_1.Filter.flattenAndThrow(values).map(TypeConverter_1.TypeConverter.valueToNumber);
 66+    var n = range.length;
 67+    if (n < 3) {
 68+        throw new Errors_1.DivZeroError("SKEW requires at least 3 data points.");
 69+    }
 70+    var meanValue = MathHelpers_1.mean(range);
 71+    var sigma = 0;
 72+    for (var i = 0; i < n; i++) {
 73+        sigma += Math.pow(range[i] - meanValue, 3);
 74+    }
 75+    var d = ((n - 1) * (n - 2) * Math.pow(MathHelpers_1.stdev(range, true), 3));
 76+    if (d === 0) {
 77+        throw new Errors_1.DivZeroError("Evaluation of function SKEW caused a divide by zero error.");
 78+    }
 79+    return n * sigma / d;
 80+};
 81+exports.SKEW = SKEW;
 82diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 83index 76c0ffb..5dba7a9 100644
 84--- a/src/Formulas/AllFormulas.ts
 85+++ b/src/Formulas/AllFormulas.ts
 86@@ -194,7 +194,8 @@ import {
 87   VARA,
 88   VAR,
 89   PERMUT,
 90-  RSQ
 91+  RSQ,
 92+  SKEW
 93 } from "./Statistical";
 94 import {
 95   ARABIC,
 96@@ -463,5 +464,6 @@ export {
 97   VARA,
 98   VAR,
 99   PERMUT,
100-  RSQ
101+  RSQ,
102+  SKEW
103 }
104\ No newline at end of file
105diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
106index 9bf7558..95c9968 100644
107--- a/src/Formulas/Statistical.ts
108+++ b/src/Formulas/Statistical.ts
109@@ -1709,6 +1709,32 @@ var RSQ = function (rangeY, rangeX) {
110 };
111 
112 
113+/**
114+ * Returns the skewness of a distribution.
115+ * @param values - The numerical values or range.
116+ * @returns {number}
117+ * @constructor
118+ */
119+var SKEW = function (...values) {
120+  ArgsChecker.checkAtLeastLength(arguments, 1, "SKEW");
121+  var range = Filter.flattenAndThrow(values).map(TypeConverter.valueToNumber);
122+  var n = range.length;
123+  if (n < 3) {
124+    throw new DivZeroError("SKEW requires at least 3 data points.");
125+  }
126+  var meanValue = mean(range);
127+  var sigma = 0;
128+  for (var i = 0; i < n; i++) {
129+    sigma += Math.pow(range[i] - meanValue, 3);
130+  }
131+  var d = ((n - 1) * (n - 2) * Math.pow(stdev(range, true), 3));
132+  if (d === 0) {
133+    throw new DivZeroError("Evaluation of function SKEW caused a divide by zero error.");
134+  }
135+  return n * sigma / d;
136+};
137+
138+
139 export {
140   AVERAGE,
141   AVERAGEA,
142@@ -1762,5 +1788,6 @@ export {
143   VARA,
144   VAR,
145   PERMUT,
146-  RSQ
147+  RSQ,
148+  SKEW
149 }
150\ No newline at end of file
151diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
152index ea666a3..5caedd1 100644
153--- a/tests/Formulas/StatisticalTest.ts
154+++ b/tests/Formulas/StatisticalTest.ts
155@@ -51,7 +51,8 @@ import {
156   VARA,
157   VAR,
158   PERMUT,
159-  RSQ
160+  RSQ,
161+  SKEW
162 } from "../../src/Formulas/Statistical";
163 import * as ERRORS from "../../src/Errors";
164 import {
165@@ -1058,4 +1059,16 @@ test("RSQ", function() {
166   catchAndAssertEquals(function() {
167     RSQ.apply(this, [[1]]);
168   }, ERRORS.NA_ERROR);
169+});
170+
171+test("SKEW", function() {
172+  assertEquals(SKEW(1, 2, 3, 4, 5, 6), 0);
173+  assertEquals(SKEW(1, 2, 3,[4, 5], 6), 0);
174+  assertEquals(SKEW(1, 2, 3, 4, 5, 6, 100), 2.6336050735387375);
175+  catchAndAssertEquals(function() {
176+    SKEW(1)
177+  }, ERRORS.DIV_ZERO_ERROR);
178+  catchAndAssertEquals(function() {
179+    SKEW.apply(this, []);
180+  }, ERRORS.NA_ERROR);
181 });
182\ No newline at end of file
183diff --git a/tests/SheetFormulaTest.ts b/tests/SheetFormulaTest.ts
184index f6d8518..6356ae3 100644
185--- a/tests/SheetFormulaTest.ts
186+++ b/tests/SheetFormulaTest.ts
187@@ -914,6 +914,10 @@ test("Sheet RSQ", function(){
188   assertFormulaEquals('=RSQ([10, 22, 4], [1, 3, 7])', 0.2500000000000001);
189 });
190 
191+test("Sheet SKEW", function(){
192+  assertFormulaEquals('=SKEW(1, 2, 3, 4, 5, 6, 100)', 2.6336050735387375);
193+});
194+
195 test("Sheet *", function(){
196   assertFormulaEquals('= 10 * 10', 100);
197   assertFormulaEquals('= 10 * 0', 0);