Linear system cannot be solved since matrix is singular

241 Views Asked by At

** Trying to understand the logic of the error - not looking for help in programming **

I'm running a polynomial regression script. I first click on a grid to add points to a graph and then change the order of the polynomial function to observe changes in the regression model. However, when I change the order to around 4 or 5, I start seeing an error.

class PolynomialRegression{
    constructor(){
        this.expression = "f(x) = undefined";
        this.coefficients = []; 
        this.degree = NaN;
        this.f = (x) => 0;
    }   

    train(X,y,degree=1){
        try {
            const n = X.length;
            let x_pow_deg = [],
                s_xiy = [];
            this.degree = degree;

            // computing sum of Xs to the power of `i` -> [ X^0, X^1, ... , X^i ], and computing sums
            for(let i=0;i<=this.degree;i++){
                x_pow_deg.push(X.map(x=>x**i));
                s_xiy.push(math.multiply(x_pow_deg[i],y));
            }

            //computing remaining sums for solving the `m+1` linear equations, where `m+1` is equal to the number of coeficients; m=this.degree
            for(let i=this.degree+1; i<=2*this.degree;i++) 
                x_pow_deg.push(X.map(el=>el**i)); 

            //fixing equation; Ax = b -> A*s_xiy = coef 
            let A = math.zeros(this.degree+1,this.degree+1)._data
            for(let i=0;i<=this.degree;i++)
                for(let j=0;j<=this.degree;j++)
                    A[i][j] = math.sum(x_pow_deg[j+i]);
            A[0][0] = n;

            //solve linear equation Ax = b |  modeling f(x) in JS  |  fomating mathematical expression of the model
            this.coefficients = math.lusolve(math.lup(A),s_xiy)._data;
            this.f = (x) =>{
                return math.sum(this.coefficients.map((coef,i)=>coef*(x**i)))
            };
            this.expression = "f(x) = ".concat(
                this.coefficients.map((el,i)=> i===0? `${el}`:` + (${el}x^${i})` ).join("")
            );

        } catch (error) {
            // Error: Linear system cannot be solved since matrix is singular. |Matrix|=Det(Matrix)=0
            console.error(error);
        }
    }

    predict(X){
        const response = Array.isArray(X)? X.map((x)=>this.f(x)) : this.f(X);
        return response;
    }
}

export default PolynomialRegression;

What could be causing this?

Error: Linear system cannot be solved since matrix is singular  at _denseBackwardSubstitution 

Codesandbox for testing: https://codesandbox.io/s/opur8?file=/src/PolynomialRegression.js

Full original project: https://github.com/MenesesGHZ/polynomial-regression-js