blob: 2208997bbc0198929953b1ceb4daa5b773438725 [file] [log] [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Get parameter of generated line using linear regression formula,
// using last n data points of values.
export function linearRegression(values, n) {
let sumValues = 0;
let indices = 0;
let sqIndices = 0;
let multiply = 0;
let temp;
for (let i = 0; i < n; i++) {
temp = values[values.length-n+i];
sumValues += temp;
indices += i;
sqIndices += i * i;
multiply += i * temp;
}
// Calculate linear regression formula for values.
const slope = (n * multiply - sumValues * indices) /
(n * sqIndices - indices * indices);
const intercept = (sumValues * sqIndices - indices * multiply) /
(n * sqIndices - indices * indices);
return [slope, intercept];
}