mirror of
https://github.com/Karaka-Management/jsOMS.git
synced 2026-02-10 22:58:40 +00:00
Regression/forecast for charting
This commit is contained in:
parent
59ef044f54
commit
5c02524873
31
Math/Stochastic/Average.js
Normal file
31
Math/Stochastic/Average.js
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
/**
|
||||||
|
* Average class.
|
||||||
|
*
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @copyright 2013 Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0 * @since 1.0.0
|
||||||
|
*/
|
||||||
|
(function (jsOMS, undefined)
|
||||||
|
{
|
||||||
|
"use strict";
|
||||||
|
/** @namespace jsOMS.Math.Stochastic.Average */
|
||||||
|
jsOMS.Autoloader.defineNamespace('jsOMS.Math.Stochastic.Average');
|
||||||
|
|
||||||
|
jsOMS.Math.Stochastic.Average.arithmeticMean = function (values, offset = 0)
|
||||||
|
{
|
||||||
|
Array.sort(values);
|
||||||
|
let length = values.length;
|
||||||
|
|
||||||
|
if (offset > 0) {
|
||||||
|
values = Array.splice(offset, length - offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length === 0) {
|
||||||
|
throw 'Division zero';
|
||||||
|
}
|
||||||
|
|
||||||
|
return values.reduce((a, b) => a + b, 0) / count;
|
||||||
|
};
|
||||||
|
}(window.jsOMS = window.jsOMS || {}));
|
||||||
35
Math/Stochastic/Forecast/LinearRegression.js
Normal file
35
Math/Stochastic/Forecast/LinearRegression.js
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
/**
|
||||||
|
* Linear regression class.
|
||||||
|
*
|
||||||
|
* @author OMS Development Team <dev@oms.com>
|
||||||
|
* @author Dennis Eichhorn <d.eichhorn@oms.com>
|
||||||
|
* @copyright 2013 Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0 * @since 1.0.0
|
||||||
|
*/
|
||||||
|
(function (jsOMS, undefined)
|
||||||
|
{
|
||||||
|
"use strict";
|
||||||
|
/** @namespace jsOMS.Math.Stochastic.Forecast.LinearRegression */
|
||||||
|
jsOMS.Autoloader.defineNamespace('jsOMS.Math.Stochastic.Forecast.LinearRegression');
|
||||||
|
|
||||||
|
jsOMS.Math.Stochastic.Forecast.LinearRegression.getLinearRegresseion = function (x, y)
|
||||||
|
{
|
||||||
|
let count = x.length,
|
||||||
|
meanX = jsOMS.Math.Stochastic.Average.arithmeticMean(x),
|
||||||
|
meanY = jsOMS.Math.Stochastic.Average.arithmeticMean(y),
|
||||||
|
sum1 = 0,
|
||||||
|
sum2 = 0,
|
||||||
|
b0, b1;
|
||||||
|
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
sum1 += (y[i] - meanY) * (x[i] - meanX);
|
||||||
|
sum2 += (x[i] - meanX) ** 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
b1 = sum1 / sum2;
|
||||||
|
b0 = meanY - b1 * meanX;
|
||||||
|
|
||||||
|
return {b0: b0, b1: b1};
|
||||||
|
};
|
||||||
|
}(window.jsOMS = window.jsOMS || {}));
|
||||||
Loading…
Reference in New Issue
Block a user