mljs / regression-simple-linear

Simple Linear Regression
MIT License
28 stars 6 forks source link

SLR returns NaN when inputs and outputs are 1-element arrays #7

Open edphn opened 2 years ago

edphn commented 2 years ago

I'm reimplementing my algorithm from MLR into SLR (I don't need multiple params anymore) and I see that SLR is returning NaN when inputs & outputs are arrays of 1-element. The old implementation with MLR was producing proper results.

const SLR = require('ml-regression-simple-linear')
const MLR = require('ml-regression-multivariate-linear')

const slr1 = new SLR([ 1 ], [ 2 ])
console.log('SLR for 1 value:', slr1.predict(2))

const mlr1 = new MLR([ [ 1 ] ], [ [ 2 ] ])
console.log('MLR for 1 value:', mlr1.predict([ 2 ]))

const slr2 = new SLR([ 1, 2 ], [ 2, 3 ])
console.log('SLR for 2 values:', slr2.predict(3))

const mlr2 = new MLR([ [ 1 ], [ 2 ] ], [ [ 2 ], [ 3 ] ])
console.log('MLR for 2 values:', mlr2.predict([ 3 ]))

Output:

SLR for 1 value: NaN (should be 3)
MLR for 1 value: [ 2.9999999999999996 ]
SLR for 2 values: 4
MLR for 2 values: [ 3.999999999999991 ]
targos commented 2 years ago

What do you expect as a result with this edge case? There are infinite solutions that fulfill the input data.

edphn commented 2 years ago

Shouldn't it be just 3, MLR for the same inputs and outputs (just wrapped in array) returns nearly 3?

targos commented 2 years ago

I think it works in MLR because the intercept option is true by default. Unfortunately, I don't remember exactly what it means and how it could translate to SLR.

targos commented 2 years ago

Hello @jobo322, do you have an opinion on this issue?

maasencioh commented 2 years ago

What do you expect as a result with this edge case? There are infinite solutions that fulfill the input data.

I also agree with you, it has no sense to give an answer for this case, maybe both should throw on this cardinality