Skip to content

Instantly share code, notes, and snippets.

@danielherrerohernando
Last active May 23, 2020 22:37
Show Gist options
  • Save danielherrerohernando/13395d1c6f422221e0607d77c8df59a3 to your computer and use it in GitHub Desktop.
Save danielherrerohernando/13395d1c6f422221e0607d77c8df59a3 to your computer and use it in GitHub Desktop.
Library's index file
const { writeFileSync, readFileSync } = require('fs');
const { buildSystem } = require('./systemBuilder');
const { solve } = require('./systemSolver');
const { getExpression } = require('./helpers');
const createModel = () => {
let params = {};
const fit = (data,degrees) => {
degrees.forEach(degree => {
const system = buildSystem(data, degree);
const coefficients = solve(...system);
params[degree] = coefficients;
});
};
const estimate = (degree,x) => params[degree] && params[degree].reduce((acc,c,i)=>acc+c*x**i,0);
const loadParams = path => {
const loadedData = JSON.parse(readFileSync(path));
params = { ...params, ...loadedData };
};
const saveParams = path => writeFileSync(path, JSON.stringify(params, null, 2));
const saveExpressions = path => {
const paramsWithExpressions = Object.entries(params).reduce((acc,[degree,coefficients]) => {
acc[degree] = getExpression(coefficients);
return acc;
},{});
writeFileSync(path, JSON.stringify(paramsWithExpressions, null, 2));
};
return { fit, estimate, loadParams, saveParams, saveExpressions };
};
module.exports = {
createModel
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment