# commons-user mailing list archives

##### Site index · List index
Message view
Top
From Luc Maisonobe <Luc.Maison...@free.fr>
Subject Re: [math] - Least squares appx. of quadratic equations
Date Fri, 31 Dec 2010 19:32:13 GMT
```Le 31/12/2010 19:17, Vladimir Blagojevic a écrit :
> Mikkel,
>
> I looked at the example but I was not able to load my sample data and
> find coefficients of a quadratic equation. Lets say that I have data
> points for quadratic equation:
> double data[][] = { { 0, 1 }, { 1, 6 }, { 2, 17 } }; which is in fact
> y(x) = 1 + 2x + 3x^2
>
> How would I setup either OLS and/or GLS MultipleLinearRegression
> objects to load this data and find the coefficients.

For univariate polynomials, there is also a dedicated curve fitter
available in Apache commons-math. It is the class PolynomialFitter in
package org.apache.commons.math.optimization.fitting.

The way this fitter work is as follows:

- first you set up the optimizer by specifying the polynomial degree
and an optimizing engine to use for solving. In your case (degree 2)
this can be done as follows:

PolynomialFitter fitter =
new PolynomialFitter(2, new LevenbergMarquardtOptimizer());

- then you add the observed data points (the first argument is a
weight):

- last, you perform the fit and get the solution as a polynomial
function object:

PolynomialFunction fitted = fitter.fit();

- you can now use the fitted function as you want. For example to
display is between 0.0 and 10.0 with a 0.1 step, you would do
something along these lines:

for (double x = 0.0; x < 10.0; x += 0.1) {
System.out.println(x + " " + fitted.value(x));
}

hope this helps,
Luc

>
> Much appreciated,
>
> 2010/12/30 Mikkel Meyer Andersen <mikl@mikl.dk>:
>> Hi,
>>
>> Try to have a look at the examples provided at
>> http://commons.apache.org/math/userguide/stat.html#a1.5_Multiple_linear_regression
>> . Both OLS and GLS are supported.
>>
>> If you have further questions, do not hesitate to ask!
>>
>> Cheers, Mikkel.
>>
>> 2010/12/30 Vladimir Blagojevic <dovlex@gmail.com>:
>>> Hi,
>>>
>>> I am not sure how, if at all, I can use math package to apply
>>> least-squares approximation to find coefficients to n-order equation:
>>> y = a0*X^0 + a1*X^1 + ... + an*X^n.
>>> More specifically, given a set of x,y data points I would like to find
>>> quadratic equations that best fits the input data points.
>>>
>>> Regards,
>>>
>>> ---------------------------------------------------------------------
>>> 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
>>
>>
>
> ---------------------------------------------------------------------
> 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