UNPKG

@nexys/math-ts

Version:

[![npm version](https://badge.fury.io/js/%40nexys%2Fmath-ts.svg)](https://www.npmjs.com/package/@nexys/math-ts) [![TavisCI](https://travis-ci.com/Nexysweb/tableau-wdc-react.svg?branch=master)](https://travis-ci.com/github/Nexysweb/math-ts) [![Deployment](

20 lines (19 loc) 761 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.linest = (series) => { const n = series.length; if (n < 1) { throw Error('series length must be greater than zero'); } const sumY = series.map(_ => _.y).reduce((a, b) => a + b); const sumX = series.map(_ => _.x).reduce((a, b) => a + b); const sumX2 = series.map(_ => Math.pow(_.x, 2)).reduce((a, b) => a + b); const sumXY = series.map(_ => _.x * _.y).reduce((a, b) => a + b); const denom = n * sumX2 - Math.pow(sumX, 2); const a = (n * sumXY - sumY * sumX) / denom; const b = (sumY * sumX2 - sumX * sumXY) / denom; return { a, b }; }; exports.estimatedY = (xs, { a, b }) => xs.map(x => { return { y: a * x + b, x }; });