Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute least squares using java

I am trying to find a java code to compute the least squares solution (x) in the Ax=b equation. Suppose that

A = [1 0 0;1 0 0];
b = [1; 2];

x = A\b

returns the

x =

    1.5000
         0
         0

I found Class LeastSquares,

public LeastSquares(double[] a, double[] b, int degree)

but in the input both A and B are one dimensional arrays, however, in above example, A is a matrix and B is an array.

In Class NonNegativeLeastSquares

public NonNegativeLeastSquares(int M, int N, double a[][],double b[])

A is a matrix and B is an array, but the description of the class says that it finds an approximate solution to the linear system of equations Ax = b, such that ||Ax - b||2 is minimized, and such that x >= 0. Which means that x must be always positive.

I need a similar class as NonNegativeLeastSquares, however with out the x>=0 constraint. Could someone please help me?
thanks a lot.

like image 515
Pegah Avatar asked Jan 30 '13 16:01

Pegah


1 Answers

See the Apache Commons Math library, specifically the SimpleRegression class.

like image 105
maerics Avatar answered Sep 23 '22 10:09

maerics