ml-regression-simple-linear
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -0,1 +1,10 @@ | ||
## [2.0.2](https://github.com/mljs/regression-simple-linear/compare/v2.0.1...v2.0.2) (2019-06-19) | ||
### Bug Fixes | ||
* correct TS definitions ([27ad52f](https://github.com/mljs/regression-simple-linear/commit/27ad52f)) | ||
## [2.0.1](https://github.com/mljs/regression-simple-linear/compare/v2.0.0...v2.0.1) (2019-05-02) | ||
@@ -2,0 +11,0 @@ |
@@ -42,3 +42,3 @@ 'use strict'; | ||
result += `${xFactor === '1' ? '' : `${xFactor} * `}x`; | ||
if (this.intercept) { | ||
if (this.intercept !== 0) { | ||
const absIntercept = Math.abs(this.intercept); | ||
@@ -45,0 +45,0 @@ const operator = absIntercept === this.intercept ? '+' : '-'; |
{ | ||
"name": "ml-regression-simple-linear", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "Simple Linear Regression", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -8,3 +8,7 @@ import BaseRegression from 'ml-regression-base'; | ||
export default class SimpleLinearRegression extends BaseRegression { | ||
class SimpleLinearRegression extends BaseRegression { | ||
slope: number; | ||
intercept: number; | ||
coefficients: [number, number]; | ||
constructor(x: number[], y: number[]); | ||
@@ -17,2 +21,4 @@ | ||
} | ||
export = SimpleLinearRegression; | ||
} |
@@ -10,4 +10,4 @@ import SLR from '..'; | ||
expect(regression.slope).toStrictEqual(regression.coefficients[1]); | ||
expect(regression.intercept).toStrictEqual(regression.coefficients[0]); | ||
expect(regression.slope).toBe(regression.coefficients[1]); | ||
expect(regression.intercept).toBe(regression.coefficients[0]); | ||
@@ -18,6 +18,6 @@ expect(regression.slope).toBeCloseTo(-0.264706, 1e-5); | ||
const y = regression.predict(85); | ||
expect(regression.computeX(y)).toStrictEqual(85); | ||
expect(regression.computeX(y)).toBe(85); | ||
expect(y).toBeCloseTo(28.088235294117649, 1e-10); | ||
expect(regression.toString(3)).toStrictEqual('f(x) = - 0.265 * x + 50.6'); | ||
expect(regression.toString(3)).toBe('f(x) = - 0.265 * x + 50.6'); | ||
}); | ||
@@ -69,3 +69,3 @@ | ||
expect(score.r).toBeCloseTo(0.9945, 1e-4); | ||
expect(score.r2).toStrictEqual(score.r * score.r); | ||
expect(score.r2).toBe(score.r * score.r); | ||
expect(score.chi2).toBeLessThan(1); | ||
@@ -81,18 +81,18 @@ expect(score.rmsd).toBeLessThan(1); | ||
expect(regression.slope).toStrictEqual(-2); | ||
expect(regression.intercept).toStrictEqual(10); | ||
expect(regression.predict(6)).toStrictEqual(-2); | ||
expect(regression.predict(-1)).toStrictEqual(12); | ||
expect(regression.predict(2.5)).toStrictEqual(5); | ||
expect(regression.computeX(5)).toStrictEqual(2.5); | ||
expect(regression.computeX(9)).toStrictEqual(0.5); | ||
expect(regression.computeX(-12)).toStrictEqual(11); | ||
expect(regression.slope).toBe(-2); | ||
expect(regression.intercept).toBe(10); | ||
expect(regression.predict(6)).toBe(-2); | ||
expect(regression.predict(-1)).toBe(12); | ||
expect(regression.predict(2.5)).toBe(5); | ||
expect(regression.computeX(5)).toBe(2.5); | ||
expect(regression.computeX(9)).toBe(0.5); | ||
expect(regression.computeX(-12)).toBe(11); | ||
const score = regression.score(inputs, outputs); | ||
expect(score.r).toBeGreaterThan(0); | ||
expect(score.r2).toStrictEqual(1); | ||
expect(score.chi2).toStrictEqual(0); | ||
expect(score.rmsd).toStrictEqual(0); | ||
expect(score.r2).toBe(1); | ||
expect(score.chi2).toBe(0); | ||
expect(score.rmsd).toBe(0); | ||
expect(regression.toString(3)).toStrictEqual('f(x) = - 2.00 * x + 10.0'); | ||
expect(regression.toString(3)).toBe('f(x) = - 2.00 * x + 10.0'); | ||
}); | ||
@@ -105,7 +105,6 @@ | ||
const regression = new SLR(inputs, outputs); | ||
expect(regression.toLaTeX()).toStrictEqual('f(x) = 2'); | ||
expect(regression.toString()).toStrictEqual('f(x) = 2'); | ||
expect(regression.toString(1)).toStrictEqual('f(x) = 2'); | ||
expect(regression.toString(5)).toStrictEqual('f(x) = 2.0000'); | ||
expect(regression.toLaTeX()).toBe('f(x) = 2'); | ||
expect(regression.toString()).toBe('f(x) = 2'); | ||
expect(regression.toString(1)).toBe('f(x) = 2'); | ||
expect(regression.toString(5)).toBe('f(x) = 2.0000'); | ||
}); | ||
@@ -119,5 +118,12 @@ | ||
expect(regression.toString()).toStrictEqual('f(x) = x - 1'); | ||
expect(regression.toString()).toBe('f(x) = x - 1'); | ||
}); | ||
it('intercept = 0', () => { | ||
const regression = new SLR([0, 1, 2], [0, 1, 2]); | ||
expect(regression.slope).toBe(1); | ||
expect(regression.intercept).toBe(0); | ||
expect(regression.toString()).toBe('f(x) = x'); | ||
}); | ||
it('different size on input and output', () => { | ||
@@ -137,11 +143,15 @@ const inputs = [0, 1, 2]; | ||
}); | ||
expect(regression.slope).toStrictEqual(1); | ||
expect(regression.intercept).toStrictEqual(1); | ||
expect(regression.slope).toBe(1); | ||
expect(regression.intercept).toBe(1); | ||
expect(regression.coefficients).toStrictEqual([1, 1]); | ||
const model = regression.toJSON(); | ||
expect(model.name).toStrictEqual('simpleLinearRegression'); | ||
expect(model.slope).toStrictEqual(1); | ||
expect(model.intercept).toStrictEqual(1); | ||
expect(model.name).toBe('simpleLinearRegression'); | ||
expect(model.slope).toBe(1); | ||
expect(model.intercept).toBe(1); | ||
}); | ||
it('wrong model', () => { | ||
expect(() => SLR.load({})).toThrow(/^not a SLR model$/); | ||
}); | ||
}); |
@@ -40,3 +40,3 @@ import BaseRegression, { | ||
result += `${xFactor === '1' ? '' : `${xFactor} * `}x`; | ||
if (this.intercept) { | ||
if (this.intercept !== 0) { | ||
const absIntercept = Math.abs(this.intercept); | ||
@@ -43,0 +43,0 @@ const operator = absIntercept === this.intercept ? '+' : '-'; |
13569
284