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

feat!: throw error if insufficient number of points #35

Closed
wants to merge 2 commits into from
Closed
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
43 changes: 24 additions & 19 deletions src/__tests__/2d-polinomial-fit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ describe('2D polinomial fit', () => {
}

const pf = new Polyfit(X, y, {
order: 2
order: 2,
});

it('Training coefficients', () => {
const estimatedCoefficients = [
1.5587e1,
3.8873e-1,
5.2582e-3,
4.8498e-1,
2.1127e-3,
-7.3709e-3
1.5587e1, 3.8873e-1, 5.2582e-3, 4.8498e-1, 2.1127e-3, -7.3709e-3,
];
for (let i = 0; i < estimatedCoefficients.length; ++i) {
expect(pf.coefficients.get(i, 0)).toBeCloseTo(estimatedCoefficients[i], 1e-2);
expect(pf.coefficients.get(i, 0)).toBeCloseTo(
estimatedCoefficients[i],
1e-2,
);
}
});

Expand All @@ -44,16 +42,8 @@ describe('2D polinomial fit', () => {

it('Other function test', () => {
var testValues = [
15.041667,
9.375,
5.041667,
2.041667,
0.375,
0.041667,
1.041667,
3.375,
7.041667,
12.041667
15.041667, 9.375, 5.041667, 2.041667, 0.375, 0.041667, 1.041667, 3.375,
7.041667, 12.041667,
];

var len = 21;
Expand All @@ -67,7 +57,7 @@ describe('2D polinomial fit', () => {
}

var polyFit = new Polyfit(X, y, {
order: 2
order: 2,
});

var test = 10;
Expand All @@ -85,4 +75,19 @@ describe('2D polinomial fit', () => {
expect(predict[i]).toBeCloseTo(testValues[i], 1e-2);
}
});
it('must throw error', () => {
const X = new Array(5);
const y = new Array(5);
for (let i = 0; i < 5; ++i) {
X[i] = [i, i + 10];
y[i] = i + 20;
}

expect(() => {
const polyfit = new Polyfit(X, y, {
order: 4,
});
return polyfit;
}).toThrow('Insufficient number of points to create regression model.');
});
});
40 changes: 14 additions & 26 deletions src/regression/poly-fit-regression2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Matrix, SVD } from 'ml-matrix';
import BaseRegression from 'ml-regression-base';

const defaultOptions = {
order: 2
order: 2,
};
// Implements the Kernel ridge regression algorithm.
// http://www.ics.uci.edu/~welling/classnotes/papers_class/Kernel-Ridge.pdf
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class PolynomialFitRegression2D extends BaseRegression {

if (X.columns !== 2) {
throw new RangeError(
`You give X with ${X.columns} columns and it must be 2`
`You give X with ${X.columns} columns and it must be 2`,
);
}
if (X.rows !== y.rows) {
Expand All @@ -66,29 +66,19 @@ export default class PolynomialFitRegression2D extends BaseRegression {

var examples = X.rows;
var coefficients = ((this.order + 2) * (this.order + 1)) / 2;
if (examples < coefficients) {
throw new Error(
'Insufficient number of points to create regression model.',
stropitek marked this conversation as resolved.
Show resolved Hide resolved
);
}
this.coefficients = new Array(coefficients);

var x1 = X.getColumnVector(0);
var x2 = X.getColumnVector(1);

var scaleX1 =
1.0 /
x1
.clone()
.abs()
.max();
var scaleX2 =
1.0 /
x2
.clone()
.abs()
.max();
var scaleY =
1.0 /
y
.clone()
.abs()
.max();
var scaleX1 = 1.0 / x1.clone().abs().max();
var scaleX2 = 1.0 / x2.clone().abs().max();
var scaleY = 1.0 / y.clone().abs().max();

x1.mulColumn(0, scaleX1);
x2.mulColumn(0, scaleX2);
Expand All @@ -109,7 +99,7 @@ export default class PolynomialFitRegression2D extends BaseRegression {
var svd = new SVD(A.transpose(), {
computeLeftSingularVectors: true,
computeRightSingularVectors: true,
autoTranspose: false
autoTranspose: false,
});

var qqs = Matrix.rowVector(svd.diagonal);
Expand All @@ -128,9 +118,7 @@ export default class PolynomialFitRegression2D extends BaseRegression {
var U = svd.rightSingularVectors;
var V = svd.leftSingularVectors;

this.coefficients = V.mmul(qqs.transpose())
.mmul(U.transpose())
.mmul(y);
this.coefficients = V.mmul(qqs.transpose()).mmul(U.transpose()).mmul(y);

col = 0;

Expand All @@ -143,7 +131,7 @@ export default class PolynomialFitRegression2D extends BaseRegression {
(this.coefficients.get(col, 0) *
Math.pow(scaleX1, i) *
Math.pow(scaleX2, j)) /
scaleY
scaleY,
);
col++;
}
Expand Down Expand Up @@ -172,7 +160,7 @@ export default class PolynomialFitRegression2D extends BaseRegression {
return {
name: 'polyfit2D',
order: this.order,
coefficients: this.coefficients
coefficients: this.coefficients,
};
}

Expand Down