Last active
May 22, 2020 14:24
-
-
Save danielherrerohernando/0c1ad8649b4be8776c6705711664dc31 to your computer and use it in GitHub Desktop.
Set of functions used for building polynomial regression models
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const buildArray = size => new Array(size).fill(''); | |
const buildBase = degree => buildArray(degree+1).map((_,i) => x => x**i); | |
const buildGramMatrix = (data, base) => buildArray(base.length) | |
.map((_,i) => buildArray(base.length) | |
.map((_,j) => data.reduce((acc,[x]) => acc + base[i](x)*base[j](x),0))); | |
const buildIndependentTerm = (data, base) => buildArray(base.length) | |
.map((_,i) => [].concat(data.reduce((acc,[x,y]) => acc + base[i](x)*y,0))); | |
const buildSystem = (data, degree) => { | |
const base = buildBase(degree); | |
const gramMatrix = buildGramMatrix(data,base); | |
const independentTerm = buildIndependentTerm(data,base); | |
return [gramMatrix, independentTerm]; | |
}; | |
module.exports = { | |
buildSystem | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment