Skip to content

Instantly share code, notes, and snippets.

@danielherrerohernando
Last active May 22, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielherrerohernando/0c1ad8649b4be8776c6705711664dc31 to your computer and use it in GitHub Desktop.
Save danielherrerohernando/0c1ad8649b4be8776c6705711664dc31 to your computer and use it in GitHub Desktop.
Set of functions used for building polynomial regression models
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