Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/add prettier #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/*
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This disables linting errors in code editors for built files.

4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "airbnb-base",
"extends": ["airbnb-base", "plugin:prettier/recommended"],
"rules": {
"import/prefer-default-export": 0,
"no-plusplus": 0
},
}
}
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"arrowParens": "avoid",
"printWidth": 100,
"singleQuote": true,
"trailingComma": "es5"
}
58 changes: 29 additions & 29 deletions dist/regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@
var DEFAULT_OPTIONS = { order: 2, precision: 2, period: null };

/**
* Determine the coefficient of determination (r^2) of a fit from the observations
* and predictions.
*
* @param {Array<Array<number>>} data - Pairs of observed x-y values
* @param {Array<Array<number>>} results - Pairs of observed predicted x-y values
*
* @return {number} - The r^2 value, or NaN if one cannot be calculated.
*/
* Determine the coefficient of determination (r^2) of a fit from the observations
* and predictions.
*
* @param {Array<Array<number>>} data - Pairs of observed x-y values
* @param {Array<Array<number>>} results - Pairs of observed predicted x-y values
*
* @return {number} - The r^2 value, or NaN if one cannot be calculated.
*/
function determinationCoefficient(data, results) {
var predictions = [];
var observations = [];
Expand Down Expand Up @@ -96,14 +96,14 @@
}

/**
* Determine the solution of a system of linear equations A * x = b using
* Gaussian elimination.
*
* @param {Array<Array<number>>} input - A 2-d matrix of data in row-major form [ A | b ]
* @param {number} order - How many degrees to solve for
*
* @return {Array<number>} - Vector of normalized solution coefficients matrix (x)
*/
* Determine the solution of a system of linear equations A * x = b using
* Gaussian elimination.
*
* @param {Array<Array<number>>} input - A 2-d matrix of data in row-major form [ A | b ]
* @param {number} order - How many degrees to solve for
*
* @return {Array<number>} - Vector of normalized solution coefficients matrix (x)
*/
function gaussianElimination(input, order) {
var matrix = input;
var n = input.length - 1;
Expand Down Expand Up @@ -143,25 +143,25 @@
}

/**
* Round a number to a precision, specificed in number of decimal places
*
* @param {number} number - The number to round
* @param {number} precision - The number of decimal places to round to:
* > 0 means decimals, < 0 means powers of 10
*
*
* @return {numbr} - The number, rounded
*/
* Round a number to a precision, specificed in number of decimal places
*
* @param {number} number - The number to round
* @param {number} precision - The number of decimal places to round to:
* > 0 means decimals, < 0 means powers of 10
*
*
* @return {number} - The number, rounded
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected a typo.

*/
function round(number, precision) {
var factor = Math.pow(10, precision);
return Math.round(number * factor) / factor;
}

/**
* The set of all fitting methods
*
* @namespace
*/
* The set of all fitting methods
*
* @namespace
*/
var methods = {
linear: function linear(data, options) {
var sum = [0, 0, 0, 0, 0];
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"keywords": [
"regression",
"data",
"fiting",
"fitting",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected a typo.

"modeling",
"analysis"
],
Expand All @@ -33,9 +33,12 @@
"chai": "^3.5.0",
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.2.0",
"eslint-config-prettier": "^4.2.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-prettier": "^3.1.0",
"mocha": "^3.2.0",
"nyc": "^11.0.3",
"prettier": "1.17.0",
"uglify-js": "3"
},
"author": "Tom Alexander <[email protected]>"
Expand Down
120 changes: 60 additions & 60 deletions src/regression.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const DEFAULT_OPTIONS = { order: 2, precision: 2, period: null };

/**
* Determine the coefficient of determination (r^2) of a fit from the observations
* and predictions.
*
* @param {Array<Array<number>>} data - Pairs of observed x-y values
* @param {Array<Array<number>>} results - Pairs of observed predicted x-y values
*
* @return {number} - The r^2 value, or NaN if one cannot be calculated.
*/
* Determine the coefficient of determination (r^2) of a fit from the observations
* and predictions.
*
* @param {Array<Array<number>>} data - Pairs of observed x-y values
* @param {Array<Array<number>>} results - Pairs of observed predicted x-y values
*
* @return {number} - The r^2 value, or NaN if one cannot be calculated.
*/
function determinationCoefficient(data, results) {
const predictions = [];
const observations = [];
Expand All @@ -25,27 +25,27 @@ function determinationCoefficient(data, results) {

const ssyy = observations.reduce((a, observation) => {
const difference = observation[1] - mean;
return a + (difference * difference);
return a + difference * difference;
}, 0);

const sse = observations.reduce((accum, observation, index) => {
const prediction = predictions[index];
const residual = observation[1] - prediction[1];
return accum + (residual * residual);
return accum + residual * residual;
}, 0);

return 1 - (sse / ssyy);
return 1 - sse / ssyy;
}

/**
* Determine the solution of a system of linear equations A * x = b using
* Gaussian elimination.
*
* @param {Array<Array<number>>} input - A 2-d matrix of data in row-major form [ A | b ]
* @param {number} order - How many degrees to solve for
*
* @return {Array<number>} - Vector of normalized solution coefficients matrix (x)
*/
* Determine the solution of a system of linear equations A * x = b using
* Gaussian elimination.
*
* @param {Array<Array<number>>} input - A 2-d matrix of data in row-major form [ A | b ]
* @param {number} order - How many degrees to solve for
*
* @return {Array<number>} - Vector of normalized solution coefficients matrix (x)
*/
function gaussianElimination(input, order) {
const matrix = input;
const n = input.length - 1;
Expand Down Expand Up @@ -85,25 +85,25 @@ function gaussianElimination(input, order) {
}

/**
* Round a number to a precision, specificed in number of decimal places
*
* @param {number} number - The number to round
* @param {number} precision - The number of decimal places to round to:
* > 0 means decimals, < 0 means powers of 10
*
*
* @return {numbr} - The number, rounded
*/
* Round a number to a precision, specificed in number of decimal places
*
* @param {number} number - The number to round
* @param {number} precision - The number of decimal places to round to:
* > 0 means decimals, < 0 means powers of 10
*
*
* @return {number} - The number, rounded
*/
function round(number, precision) {
const factor = 10 ** precision;
return Math.round(number * factor) / factor;
}

/**
* The set of all fitting methods
*
* @namespace
*/
* The set of all fitting methods
*
* @namespace
*/
const methods = {
linear(data, options) {
const sum = [0, 0, 0, 0, 0];
Expand All @@ -120,15 +120,15 @@ const methods = {
}
}

const run = ((len * sum[2]) - (sum[0] * sum[0]));
const rise = ((len * sum[3]) - (sum[0] * sum[1]));
const run = len * sum[2] - sum[0] * sum[0];
const rise = len * sum[3] - sum[0] * sum[1];
const gradient = run === 0 ? 0 : round(rise / run, options.precision);
const intercept = round((sum[1] / len) - ((gradient * sum[0]) / len), options.precision);
const intercept = round(sum[1] / len - (gradient * sum[0]) / len, options.precision);

const predict = x => ([
const predict = x => [
round(x, options.precision),
round((gradient * x) + intercept, options.precision)]
);
round(gradient * x + intercept, options.precision),
];

const points = data.map(point => predict(point[0]));

Expand All @@ -155,15 +155,15 @@ const methods = {
}
}

const denominator = ((sum[1] * sum[2]) - (sum[5] * sum[5]));
const a = Math.exp(((sum[2] * sum[3]) - (sum[5] * sum[4])) / denominator);
const b = ((sum[1] * sum[4]) - (sum[5] * sum[3])) / denominator;
const denominator = sum[1] * sum[2] - sum[5] * sum[5];
const a = Math.exp((sum[2] * sum[3] - sum[5] * sum[4]) / denominator);
const b = (sum[1] * sum[4] - sum[5] * sum[3]) / denominator;
const coeffA = round(a, options.precision);
const coeffB = round(b, options.precision);
const predict = x => ([
const predict = x => [
round(x, options.precision),
round(coeffA * Math.exp(coeffB * x), options.precision),
]);
];

const points = data.map(point => predict(point[0]));

Expand All @@ -185,18 +185,18 @@ const methods = {
sum[0] += Math.log(data[n][0]);
sum[1] += data[n][1] * Math.log(data[n][0]);
sum[2] += data[n][1];
sum[3] += (Math.log(data[n][0]) ** 2);
sum[3] += Math.log(data[n][0]) ** 2;
}
}

const a = ((len * sum[1]) - (sum[2] * sum[0])) / ((len * sum[3]) - (sum[0] * sum[0]));
const a = (len * sum[1] - sum[2] * sum[0]) / (len * sum[3] - sum[0] * sum[0]);
const coeffB = round(a, options.precision);
const coeffA = round((sum[2] - (coeffB * sum[0])) / len, options.precision);
const coeffA = round((sum[2] - coeffB * sum[0]) / len, options.precision);

const predict = x => ([
const predict = x => [
round(x, options.precision),
round(round(coeffA + (coeffB * Math.log(x)), options.precision), options.precision),
]);
round(round(coeffA + coeffB * Math.log(x), options.precision), options.precision),
];

const points = data.map(point => predict(point[0]));

Expand All @@ -218,19 +218,19 @@ const methods = {
sum[0] += Math.log(data[n][0]);
sum[1] += Math.log(data[n][1]) * Math.log(data[n][0]);
sum[2] += Math.log(data[n][1]);
sum[3] += (Math.log(data[n][0]) ** 2);
sum[3] += Math.log(data[n][0]) ** 2;
}
}

const b = ((len * sum[1]) - (sum[0] * sum[2])) / ((len * sum[3]) - (sum[0] ** 2));
const a = ((sum[2] - (b * sum[0])) / len);
const b = (len * sum[1] - sum[0] * sum[2]) / (len * sum[3] - sum[0] ** 2);
const a = (sum[2] - b * sum[0]) / len;
const coeffA = round(Math.exp(a), options.precision);
const coeffB = round(b, options.precision);

const predict = x => ([
const predict = x => [
round(x, options.precision),
round(round(coeffA * (x ** coeffB), options.precision), options.precision),
]);
round(round(coeffA * x ** coeffB, options.precision), options.precision),
];

const points = data.map(point => predict(point[0]));

Expand All @@ -254,7 +254,7 @@ const methods = {
for (let i = 0; i < k; i++) {
for (let l = 0; l < len; l++) {
if (data[l][1] !== null) {
a += (data[l][0] ** i) * data[l][1];
a += data[l][0] ** i * data[l][1];
}
}

Expand All @@ -277,13 +277,13 @@ const methods = {

const coefficients = gaussianElimination(rhs, k).map(v => round(v, options.precision));

const predict = x => ([
const predict = x => [
round(x, options.precision),
round(
coefficients.reduce((sum, coeff, power) => sum + (coeff * (x ** power)), 0),
options.precision,
coefficients.reduce((sum, coeff, power) => sum + coeff * x ** power, 0),
options.precision
),
]);
];

const points = data.map(point => predict(point[0]));

Expand Down
5 changes: 0 additions & 5 deletions test/data.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export const linear = {

zeroGradient: {
r2: NaN,
equation: [0, 10],
Expand Down Expand Up @@ -44,11 +43,9 @@ export const linear = {
data: [[10, 21], [100, null], [1000, 2001], [10000, null]],
points: [[10, 21], [100, 201], [1000, 2001], [10000, 20001]],
},

};

export const exponential = {

growthGreaterThanZero: {
r2: 1,
equation: [2, 2],
Expand Down Expand Up @@ -118,7 +115,6 @@ export const power = {
};

export const polynomial = {

positiveLinearGradient: {
config: { order: 1 },
r2: 1,
Expand Down Expand Up @@ -208,5 +204,4 @@ export const polynomial = {
data: [[1, 6], [2, 11], [3, 18], [4, 27], [5, 38], [6, 51]],
points: [[1, 6], [2, 11], [3, 18], [4, 27], [5, 38], [6, 51]],
},

};
6 changes: 3 additions & 3 deletions test/regression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ describe('round', () => {
});

describe('models', () => {
Object.keys(models).forEach((model) => {
Object.keys(models).forEach(model => {
describe(model, () => {
Object.keys(models[model]).forEach((name) => {
Object.keys(models[model]).forEach(name => {
const example = models[model][name];
describe(name, () => {
it(`correctly predicts ${name}`, () => {
let result = regression[model](example.data, example.config);
const result = regression[model](example.data, example.config);
delete result.predict;
expect(result).to.deep.equal({
r2: example.r2,
Expand Down
Loading