1
0
mirror of https://github.com/beestat/app.git synced 2026-04-19 23:28:44 -04:00
beestat/js/beestat/math.js
Jon Ziebell 887f252e2b Added "More Info" temperature profiles modal with trendline formulas
Fixed for °C Hope all you Canadians and Australians are happy now.
2023-01-28 08:05:19 -05:00

42 lines
851 B
JavaScript

beestat.math = {};
/**
* Get a linear trendline from a set of data.
*
* @param {Object} data The data; at least two points required.
*
* @return {Object} The slope and intercept of the trendline.
*/
beestat.math.get_linear_trendline = function(data) {
// Requires at least two points.
if (Object.keys(data).length < 2) {
return null;
}
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_x_squared = 0;
var n = 0;
for (var x in data) {
x = parseFloat(x);
var y = parseFloat(data[x]);
sum_x += x;
sum_y += y;
sum_xy += (x * y);
sum_x_squared += Math.pow(x, 2);
n++;
}
var slope = ((n * sum_xy) - (sum_x * sum_y)) /
((n * sum_x_squared) - (Math.pow(sum_x, 2)));
var intercept = ((sum_y) - (slope * sum_x)) / (n);
return {
'slope': slope,
'intercept': intercept
};
};