# commons-user mailing list archives

##### Site index · List index
Message view
Top
From Phil Steitz <phil.ste...@gmail.com>
Subject [math] Re: Fwd: problem to use OLSMultipleLinearRegression
Date Sun, 29 Aug 2010 01:54:29 GMT
sahar mohebi wrote:
> Hello
>
> I have a cubic equation with known arrays of X and Y.the number of arrays
> change between 10 t0 100.
> Now I need to get equation coefficients .
>
> known x=>double[50]
> known y=>double[50]
>
> a*x^3+b*x^2+c*x+d -->a=?
>                                      b=?
>                                      c=?
>                                      d=?
>
> Please guide me to use common math library to solve my problem.
>
> sahar
>

If you want to use ordinary least squares to estimate the
coefficients, you can do this using the OLSMultipleRegression class
as follows:

// Load data into a flat array of concatenated
// rows, each of the form
// y, x, x^2, x^3
double[] design = new double[200];
int j = 0;
for (int i = 0; i < 50; i++) {
design[j] = y[i];
double v = x[i];
design[++j] = v;
design[++j] = v * v;
design[++j] = v * v * v;
j++;
}
// Estimate the model
OLSMultipleLinearRegression model = new OLSMultipleLinearRegression();
model.newSampleData(design, 50, 3);
double[] coef = model.estimateRegressionParameters();

coef[0] is d
coef[1] <-> c
coef[2] <-> b
coef[3] <-> a

Phil

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org