Project import generated by Copybara.
GitOrigin-RevId: d9e9e3fb4e31372ec1fb43b178994ca78fa8fe70
diff --git a/static_src/shared/math.js b/static_src/shared/math.js
new file mode 100644
index 0000000..36e2d75
--- /dev/null
+++ b/static_src/shared/math.js
@@ -0,0 +1,26 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// 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];
+}