spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
[INTERCEPT] formula added and tested
author
Ben Vogt <[email protected]>
date
2017-06-27 01:01:38
stats
7 file(s) changed, 119 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
  1diff --git a/DOCS.md b/DOCS.md
  2index 11ee145..eff56de 100644
  3--- a/DOCS.md
  4+++ b/DOCS.md
  5@@ -1554,6 +1554,16 @@
  6 @returns {number} 
  7 @constructor
  8 ```
  9+
 10+### INTERCEPT 
 11+
 12+```
 13+  Calculates the y-value at which a line will intersect the y-axis by using known x-values and y-values. Any text values will be ignored. 
 14+@param rangeY - Dependent range of values. 
 15+@param rangeX - Independent range of values. 
 16+@returns {number} 
 17+@constructor
 18+```
 19 ## Text
 20 
 21 
 22diff --git a/TODO.md b/TODO.md
 23index ee0ea41..29ceb4f 100644
 24--- a/TODO.md
 25+++ b/TODO.md
 26@@ -68,7 +68,6 @@ For example 64 tbs to a qt.
 27 * GEOMEAN
 28 * HARMEAN
 29 * HYPGEOMDIST
 30-* INTERCEPT
 31 * LOGINV
 32 * LOGNORMDIST
 33 * MODE
 34diff --git a/dist/Formulas/AllFormulas.js b/dist/Formulas/AllFormulas.js
 35index c95b202..e6ee74c 100644
 36--- a/dist/Formulas/AllFormulas.js
 37+++ b/dist/Formulas/AllFormulas.js
 38@@ -141,6 +141,7 @@ exports.STANDARDIZE = Statistical_1.STANDARDIZE;
 39 exports.SMALL = Statistical_1.SMALL;
 40 exports.LARGE = Statistical_1.LARGE;
 41 exports.KURT = Statistical_1.KURT;
 42+exports.INTERCEPT = Statistical_1.INTERCEPT;
 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 c44d47d..601f723 100644
 48--- a/dist/Formulas/Statistical.js
 49+++ b/dist/Formulas/Statistical.js
 50@@ -850,3 +850,42 @@ var KURT = function () {
 51     return ((n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3))) * sigma - 3 * (n - 1) * (n - 1) / ((n - 2) * (n - 3));
 52 };
 53 exports.KURT = KURT;
 54+/**
 55+ * Calculates the y-value at which a line will intersect the y-axis by using known x-values and y-values. Any text
 56+ * values will be ignored.
 57+ * @param rangeY - Dependent range of values.
 58+ * @param rangeX - Independent range of values.
 59+ * @returns {number}
 60+ * @constructor
 61+ */
 62+var INTERCEPT = function (rangeY, rangeX) {
 63+    ArgsChecker_1.ArgsChecker.checkLength(arguments, 2, "INTERCEPT");
 64+    var dataX = Filter_1.Filter.flattenAndThrow(rangeX).filter(function (value) {
 65+        return typeof value !== "string";
 66+    }).map(function (value) {
 67+        return TypeConverter_1.TypeConverter.valueToNumber(value);
 68+    });
 69+    var dataY = Filter_1.Filter.flattenAndThrow(rangeY).filter(function (value) {
 70+        return typeof value !== "string";
 71+    }).map(function (value) {
 72+        return TypeConverter_1.TypeConverter.valueToNumber(value);
 73+    });
 74+    if (dataX.length !== dataY.length) {
 75+        throw new Errors_1.NAError("INTERCEPT has mismatched argument count " + dataX.length + " vs " + dataY.length + ".");
 76+    }
 77+    var xMean = MathHelpers_1.mean(dataX);
 78+    var yMean = MathHelpers_1.mean(dataY);
 79+    var n = dataX.length;
 80+    var num = 0;
 81+    var den = 0;
 82+    for (var i = 0; i < n; i++) {
 83+        num += (dataX[i] - xMean) * (dataY[i] - yMean);
 84+        den += Math.pow(dataX[i] - xMean, 2);
 85+    }
 86+    if (den === 0) {
 87+        throw new Errors_1.DivZeroError("Evaluation of function INTERCEPT caused a divide by zero error.");
 88+    }
 89+    var b = num / den;
 90+    return yMean - b * xMean;
 91+};
 92+exports.INTERCEPT = INTERCEPT;
 93diff --git a/src/Formulas/AllFormulas.ts b/src/Formulas/AllFormulas.ts
 94index 88de407..9e5031f 100644
 95--- a/src/Formulas/AllFormulas.ts
 96+++ b/src/Formulas/AllFormulas.ts
 97@@ -146,7 +146,8 @@ import {
 98   STANDARDIZE,
 99   SMALL,
100   LARGE,
101-  KURT
102+  KURT,
103+  INTERCEPT
104 } from "./Statistical";
105 import {
106   ARABIC,
107@@ -361,5 +362,6 @@ export {
108   STANDARDIZE,
109   SMALL,
110   LARGE,
111-  KURT
112+  KURT,
113+  INTERCEPT
114 }
115\ No newline at end of file
116diff --git a/src/Formulas/Statistical.ts b/src/Formulas/Statistical.ts
117index 3a27215..735463c 100644
118--- a/src/Formulas/Statistical.ts
119+++ b/src/Formulas/Statistical.ts
120@@ -831,6 +831,48 @@ var KURT = function (...values) {
121 };
122 
123 
124+/**
125+ * Calculates the y-value at which a line will intersect the y-axis by using known x-values and y-values. Any text
126+ * values will be ignored.
127+ * @param rangeY - Dependent range of values.
128+ * @param rangeX - Independent range of values.
129+ * @returns {number}
130+ * @constructor
131+ */
132+var INTERCEPT = function (rangeY, rangeX) {
133+  ArgsChecker.checkLength(arguments, 2, "INTERCEPT");
134+  var dataX = Filter.flattenAndThrow(rangeX).filter(function (value) {
135+    return typeof value !== "string";
136+  }).map(function (value) {
137+    return TypeConverter.valueToNumber(value);
138+  });
139+  var dataY = Filter.flattenAndThrow(rangeY).filter(function (value) {
140+    return typeof value !== "string";
141+  }).map(function (value) {
142+    return TypeConverter.valueToNumber(value);
143+  });
144+
145+  if (dataX.length !== dataY.length) {
146+    throw new NAError("INTERCEPT has mismatched argument count " + dataX.length + " vs " + dataY.length + ".");
147+  }
148+
149+  var xMean = mean(dataX);
150+  var yMean = mean(dataY);
151+  var n = dataX.length;
152+  var num = 0;
153+  var den = 0;
154+  for (var i = 0; i < n; i++) {
155+    num += (dataX[i] - xMean) * (dataY[i] - yMean);
156+    den += Math.pow(dataX[i] - xMean, 2);
157+  }
158+  if (den === 0) {
159+    throw new DivZeroError("Evaluation of function INTERCEPT caused a divide by zero error.");
160+  }
161+  var b = num / den;
162+  return yMean - b * xMean;
163+};
164+
165+
166 export {
167   AVERAGE,
168   AVERAGEA,
169@@ -862,5 +904,6 @@ export {
170   STANDARDIZE,
171   SMALL,
172   LARGE,
173-  KURT
174+  KURT,
175+  INTERCEPT
176 }
177\ No newline at end of file
178diff --git a/tests/Formulas/StatisticalTest.ts b/tests/Formulas/StatisticalTest.ts
179index ca78622..f02874a 100644
180--- a/tests/Formulas/StatisticalTest.ts
181+++ b/tests/Formulas/StatisticalTest.ts
182@@ -29,7 +29,8 @@ import {
183   STANDARDIZE,
184   SMALL,
185   LARGE,
186-  KURT
187+  KURT,
188+  INTERCEPT
189 } from "../../src/Formulas/Statistical";
190 import * as ERRORS from "../../src/Errors";
191 import {
192@@ -652,4 +653,22 @@ test("KURT", function() {
193   catchAndAssertEquals(function() {
194     KURT(1, 2, 3, "ignore");
195   }, ERRORS.DIV_ZERO_ERROR);
196+});
197+
198+test("INTERCEPT", function() {
199+  assertEquals(INTERCEPT([1, 2, 3, 4], [10, 20, 33, 44]), 0.1791776688042246);
200+  assertEquals(INTERCEPT([true, 2, 3, 4], [10, 20, 33, "ignore", 44]), 0.1791776688042246);
201+  assertEquals(INTERCEPT([1, 2], [10, 20]), 0);
202+  catchAndAssertEquals(function() {
203+    INTERCEPT([1], [10])
204+  }, ERRORS.DIV_ZERO_ERROR);
205+  catchAndAssertEquals(function() {
206+    INTERCEPT([1, "ignore"], [10, "ignore"])
207+  }, ERRORS.DIV_ZERO_ERROR);
208+  catchAndAssertEquals(function() {
209+    INTERCEPT.apply(this, [[1, 2, 3]]);
210+  }, ERRORS.NA_ERROR);
211+  catchAndAssertEquals(function() {
212+    INTERCEPT.apply(this, [[1, 2, 3], [1, 2, 3], [1, 2, 3]]);
213+  }, ERRORS.NA_ERROR);
214 });
215\ No newline at end of file