Skip to content

Commit

Permalink
fix: fix export issues (close #18)
Browse files Browse the repository at this point in the history
  • Loading branch information
opatiny committed Jun 21, 2022
1 parent b5734c9 commit 64ad6fc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 67 deletions.
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@
},
"devDependencies": {
"@babel/plugin-transform-modules-commonjs": "^7.7.4",
"eslint": "^6.7.2",
"eslint-config-cheminfo": "^2.0.4",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-jest": "^23.1.1",
"eslint-plugin-prettier": "^3.1.1",
"eslint": "^8.18.0",
"eslint-config-cheminfo": "^8.0.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.5.3",
"jest": "^24.9.0",
"papaparse": "^5.1.0",
"prettier": "^1.19.1",
"prettier": "^2.7.1",
"rollup": "^1.27.8"
},
"dependencies": {
Expand Down
44 changes: 22 additions & 22 deletions regression-multivariate-linear.d.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import { AbstractMatrix, Matrix } from 'ml-matrix';
import { AbstractMatrix, Matrix } from "ml-matrix";

declare module 'ml-regression-multivariate-linear' {
declare namespace MultivariateLinearRegression {
export interface MLRModel {
name: 'multivariateLinearRegression';
name: "multivariateLinearRegression";
}

export interface MLROptions {
intercept?: boolean;
statistics?: boolean;
}
}

class MultivariateLinearRegression {
stdError: number;
stdErrorMatrix: Matrix;
stdErrors: number[];
tStats: number[];
weights: number[][];

constructor(
x: number[][] | AbstractMatrix,
y: number[][] | AbstractMatrix,
options?: MLROptions
);
declare class MultivariateLinearRegression {
stdError: number;
stdErrorMatrix: Matrix;
stdErrors: number[];
tStats: number[];
weights: number[][];

static load(model: MLRModel): MultivariateLinearRegression;
constructor(
x: number[][] | AbstractMatrix,
y: number[][] | AbstractMatrix,
options?: MultivariateLinearRegression.MLROptions
);

predict(x: number[]): number[];
predict(x: number[][]): number[][];
predict(x: AbstractMatrix): Matrix;
toJSON(): MLRModel;
}
static load(model: MultivariateLinearRegression.MLRModel): MultivariateLinearRegression;

export = MultivariateLinearRegression;
predict(x: number[]): number[];
predict(x: number[][]): number[][];
predict(x: AbstractMatrix): Matrix;
toJSON(): MultivariateLinearRegression.MLRModel;
}

export = MultivariateLinearRegression;
51 changes: 21 additions & 30 deletions src/__tests__/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import x02Data from '../../data/x02';
import x42Data from '../../data/x42';
import MLR from "..";
import x02Data from "../../data/x02";
import x42Data from "../../data/x42";

import MLR from '..';

describe('multivariate linear regression', () => {
it('should work with 2 inputs and 3 outputs', () => {
describe("multivariate linear regression", () => {
it("should work with 2 inputs and 3 outputs", () => {
const mlr = new MLR(
[
[0, 0],
Expand All @@ -18,13 +17,13 @@ describe('multivariate linear regression', () => {
[2, 4, 3],
[4, 6, 5],
[6, 8, 7],
],
]
);
expect(mlr.predict([2, 3]).map(Math.round)).toStrictEqual([4, 6, 5]);
expect(mlr.predict([4, 4]).map(Math.round)).toStrictEqual([8, 8, 8]);
});

it('should work with 2 inputs and 3 outputs - intercept is 0', () => {
it("should work with 2 inputs and 3 outputs - intercept is 0", () => {
const mlr = new MLR(
[
[0, 0],
Expand All @@ -39,13 +38,13 @@ describe('multivariate linear regression', () => {
[4, 6, 5],
[6, 8, 7],
],
{ intercept: true },
{ intercept: true }
);
expect(mlr.predict([2, 3]).map(Math.round)).toStrictEqual([4, 6, 5]);
expect(mlr.predict([4, 4]).map(Math.round)).toStrictEqual([8, 8, 8]);
});

it('should work with 2 inputs and 3 outputs - intercept is not 0', () => {
it("should work with 2 inputs and 3 outputs - intercept is not 0", () => {
const mlr = new MLR(
[
[0, 0],
Expand All @@ -60,37 +59,29 @@ describe('multivariate linear regression', () => {
[3, 8, 15],
[5, 10, 17],
],
{ intercept: true },
{ intercept: true }
);
expect(mlr.predict([2, 3]).map(Math.round)).toStrictEqual([3, 8, 15]);
expect(mlr.predict([4, 4]).map(Math.round)).toStrictEqual([7, 10, 18]);
});

it('should work with 2 inputs and 1 output (x02)', () => {
it("should work with 2 inputs and 1 output (x02)", () => {
const mlr = new MLR(x02Data.x, x02Data.y, { intercept: false });
const prediction = mlr.predict(x02Data.x);
expect(prediction[0][0]).toBeCloseTo(38.05);
});

it('should work with 2 inputs and 1 output (x42)', () => {
it("should work with 2 inputs and 1 output (x42)", () => {
const mlr = new MLR(x42Data.x, x42Data.y, { intercept: false });
const expectedWeights = [
83.125,
2.625,
3.125,
3.75,
-2.0,
-4.375,
0.0,
1.5,
-0.25,
83.125, 2.625, 3.125, 3.75, -2.0, -4.375, 0.0, 1.5, -0.25,
];
for (let i = 0; i < mlr.weights.length; i++) {
expect(mlr.weights[i][0]).toBeCloseTo(expectedWeights[i]);
}
});

it('toJSON and load', () => {
it("toJSON and load", () => {
const mlr = new MLR(
[
[0, 0],
Expand All @@ -103,23 +94,23 @@ describe('multivariate linear regression', () => {
[2, 4, 3],
[4, 6, 5],
[6, 8, 7],
],
]
);
const json = JSON.parse(JSON.stringify(mlr));
const newMlr = MLR.load(json);
expect(newMlr.predict([2, 3]).map(Math.round)).toStrictEqual([4, 6, 5]);
expect(newMlr.predict([4, 4]).map(Math.round)).toStrictEqual([8, 8, 8]);
});

it('datamining test 1-1', () => {
it("datamining test 1-1", () => {
const X = [[4.47], [208.3], [3400.0]];
const Y = [[0.51], [105.66], [1800.0]];
const mlr = new MLR(X, Y, { intercept: true });
expect(mlr.weights[0][0]).toBeCloseTo(0.53);
expect(mlr.weights[1][0]).toBeCloseTo(-3.29);
});

it('datamining test 1-2', () => {
it("datamining test 1-2", () => {
const X = [
[4.47, 1],
[208.3, 1],
Expand All @@ -131,7 +122,7 @@ describe('multivariate linear regression', () => {
expect(mlr.weights[1][0]).toBeCloseTo(-3.29);
});

it('datamining test 2', () => {
it("datamining test 2", () => {
const X = [
[1, 1, 1],
[2, 1, 1],
Expand All @@ -151,7 +142,7 @@ describe('multivariate linear regression', () => {
expect(mlr.weights[2][1]).toBeCloseTo(0);
});

it('datamining statistics test', () => {
it("datamining statistics test", () => {
const X = [
[3, 1],
[4, 2],
Expand All @@ -168,13 +159,13 @@ describe('multivariate linear regression', () => {
expect(mlr.summary.variables[1].coefficients[0]).toBeCloseTo(5.25);
expect(mlr.summary.variables[1].standardError).toBeCloseTo(2.43);
expect(mlr.summary.variables[1].tStat).toBeCloseTo(2.16);
expect(mlr.summary.variables[2].label).toStrictEqual('Intercept');
expect(mlr.summary.variables[2].label).toBe("Intercept");
expect(mlr.summary.variables[2].coefficients[0]).toBeCloseTo(13.75);
expect(mlr.summary.variables[2].standardError).toBeCloseTo(7.81);
expect(mlr.summary.variables[2].tStat).toBeCloseTo(1.76);
});

it('should optionally return statistics', () => {
it("should optionally return statistics", () => {
const X = [
[3, 1],
[4, 2],
Expand Down
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Matrix, { SVD, pseudoInverse } from 'ml-matrix';
import { Matrix, SVD, pseudoInverse } from "ml-matrix";

export default class MultivariateLinearRegression {
constructor(x, y, options = {}) {
Expand Down Expand Up @@ -49,15 +49,15 @@ export default class MultivariateLinearRegression {
.diagonal()
.map((d) => Math.sqrt(d));
this.tStats = this.weights.map((d, i) =>
this.stdErrors[i] === 0 ? 0 : d[0] / this.stdErrors[i],
this.stdErrors[i] === 0 ? 0 : d[0] / this.stdErrors[i]
);
}
}
}

predict(x) {
if (Array.isArray(x)) {
if (typeof x[0] === 'number') {
if (typeof x[0] === "number") {
return this._predict(x);
} else if (Array.isArray(x[0])) {
const y = new Array(x.length);
Expand All @@ -73,7 +73,7 @@ export default class MultivariateLinearRegression {
}
return y;
}
throw new TypeError('x must be a matrix or array of numbers');
throw new TypeError("x must be a matrix or array of numbers");
}

_predict(x) {
Expand All @@ -94,12 +94,12 @@ export default class MultivariateLinearRegression {
}

score() {
throw new Error('score method is not implemented yet');
throw new Error("score method is not implemented yet");
}

toJSON() {
return {
name: 'multivariateLinearRegression',
name: "multivariateLinearRegression",
weights: this.weights,
inputs: this.inputs,
outputs: this.outputs,
Expand All @@ -114,7 +114,7 @@ export default class MultivariateLinearRegression {
return {
label:
i === this.weights.length - 1
? 'Intercept'
? "Intercept"
: `X Variable ${i + 1}`,
coefficients: d,
standardError: this.stdErrors[i],
Expand All @@ -127,8 +127,8 @@ export default class MultivariateLinearRegression {
}

static load(model) {
if (model.name !== 'multivariateLinearRegression') {
throw new Error('not a MLR model');
if (model.name !== "multivariateLinearRegression") {
throw new Error("not a MLR model");
}
return new MultivariateLinearRegression(true, model);
}
Expand Down

0 comments on commit 64ad6fc

Please sign in to comment.