Compute least squares using java

天大地大妈咪最大 提交于 2019-12-07 06:11:55

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/14608998/compute-least-squares-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!