commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Phil Steitz <phil.ste...@gmail.com>
Subject Re: [math] Jama Matrix to Math Commons (Least squares and Recursive least squares)
Date Wed, 27 Apr 2011 16:57:00 GMT
On 4/27/11 8:23 AM, Luc Maisonobe wrote:
> Le 27/04/2011 15:52, Cedric Martin a écrit :
>> Hi all,
> Hi Cédric,
>
>> I have a simplistic implementation of least squares using Jama Matrix which
>> I would like to migrate to Math Commons if possible.  I'm running into
>> issues where the LUDecomposition requires a square matrix, and what I want
>> to offer is a rectangular matrix.  The following code will hopefully clarify
>> (note that the following code does produce the expected results, using Jama
>> Matrix)):
>>
>>
>>  Matrix matrixX = new Matrix(*length, order*);
>>
>> Matrix matrixY = new Matrix(length, 1);
>>
>>   for (int j = 0; j < this.order; j++) {
>>
>>  for (int i = 0; i < length; i++) {
>>
>>  matrixX.set(i, j, Math.pow(i + offset, j));
>>
>>  }
>>
>> }
>>
>>
>>  double[] values = super.getValues();
>>
>>
>>  for (int i = 0; i < length; i++) {
>>
>>  matrixY.set(i, 0, values[i]);
>>
>> }
>>
>>
>>  double[] result = { 0.0, 0.0 };
>>
>>
>>  if (length >= this.order) {
>>
>>  result = matrixX.solve(matrixY).getRowPackedCopy();
>>
>> }
>>
>>
>> Would anyone be able to help me with this?  Am I simply be using the wrong
>> objects?
> Yes, you are using the wrong classes.
> LU decomposition only works with square matrices and it only solves
> problemns when an exact solution does exist.
>
> As you need a least square approximation when the number of free
> variables and the number of equations do not match, you should use
> another type of decomposition. The most effective one in this case is QR
> decomposition. You can use it just as LU decomposition, the API is
> really similar.
>
> In fact, you can even use QR decomposition for square matrices too. In
> this case it would involve twice as much operations as LU decomposition,
> but would be numerically more stable. This numericall stability explains
> that in many cases, QR decomposition is used even if LU decomposition
> could theoretically also be used.
>
You may also be able to just use the OLSMultipleLinearRegression
class in o.a.c.m.stat.regression.  In any case, you can imitate what
the implementation in that class does, which uses QR decomposition.

Phil
>> I also have to build a simple Recursive least squares implementation using
>> similar objects (RealMatrix I'm assuming), would anyone be able to point me
>> in the right direction for this as well, while using the Commons Math
>> library?
> We don't have a recursive least square implementation now.
>
> best regards,
> Luc
>
>>
>> Thank you very much!
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message