Weighted Regression

Hi,

I have some difficulty to program the weighted regression in Java. I will be very thankful for anyone who could give some assistance on the implementation of weighted regression.

Actually, I find a java code about regression (not weighted regression) from Google. But I do not know how to modify it to the weighted regression that I want.

publicclass Regression{

// Apply least squares to raw data to determine the coefficients for

// an n-order equation: y = a0*X^0 + a1*X^1 + ... + an*X^n.

// Returns the coefficients for the solved equation, given a number

// of y and x data points. The rawData input is given in the form of

// {{y0, x0}, {y1, x1},...,{yn, xn}}.The coefficients returned by

// the regression are {a0, a1,...,an} which corresponds to

// {X^0, X^1,...,X^n}. The number of coefficients returned is the

// requested equation order (norder) plus 1.

staticdouble[] linear_equation(double rawData[][],int norder){

double a[][] =newdouble[norder+1][norder+1];

double b[] =newdouble[norder+1];

double term[] =newdouble[norder+1];

double ysquare = 0;

// step through each of the raw data entries

for (int i = 0; i < rawData.length; i++){

b[0] += rawData[i][0];//gives the sum of the y observations in b[0];

ysquare += rawData[i][0] * rawData[i][0];// sums the squares of the y-values.

// sum the x power values

double xpower = 1;//initializes xpower to value 1

for (int j = 0; j < norder+1; j++){

term[j] = xpower;

a[0][j] += xpower;

xpower = xpower * rawData[i][1];

}

// now set up the rest of rows in the matrix - multiplying each row by each term

for (int j = 1; j < norder+1; j++){

b[j] += rawData[i][0] * term[j];

for (int k = 0; k < b.length; k++){

a[j][k] += term[j] * term[k];

}

}

}

// solve for the coefficients

double coef[] = gauss(a, b);

// calculate the r-squared statistic

double ss = 0;

double yaverage = b[0] / rawData.length;

for (int i = 0; i < norder+1; i++){

double xaverage = a[0][i] / rawData.length;

ss += coef[i] * (b[i] - (rawData.length * xaverage * yaverage));

}

rsquared = ss / (ysquare - (rawData.length * yaverage * yaverage));

// solved the simultaneous equations via gauss

double [] newcoef =newdouble [coef.length + 1];

for (int i = 0; i < newcoef.length; i++){

System.arraycopy(coef,0,newcoef,0,i);

}

coef = newcoef;

coef[coef.length - 1] = rsquared;

return coef;

}

// it's been so long since I wrote this, that I don't recall the math

// logic behind it. IIRC, it's just a standard gaussian technique for

// solving simultaneous equations of the form: |A| = |B| * |C| where we

// know the values of |A| and |B|, and we are solving for the coefficients

// in |C|

staticdouble[] gauss(double ax[][],double bx[]){

double a[][] =newdouble[ax.length][ax[0].length];

double b[] =newdouble[bx.length];

double pivot;

double mult;

double top;

int n = b.length;

double coef[] =newdouble[n];

// copy over the array values - inplace solution changes values

for (int i = 0; i < ax.length; i++){

for (int j = 0; j < ax[i].length; j++){

a[i][j] = ax[i][j];

}

b[i] = bx[i];

}

for (int j = 0; j < (n-1); j++){

pivot = a[j][j];

for (int i = j+1; i < n; i++){

mult = a[i][j] / pivot;

for (int k = j+1; k < n; k++){

a[i][k] = a[i][k] - mult * a[j][k];

}

b[i] = b[i] - mult * b[j];

}

}

coef[n-1] = b[n-1] / a[n-1][n-1];

for (int i = n-2; i >= 0; i--){

top = b[i];

for (int k = i+1; k < n; k ++){

top = top - a[i][k] * coef[k];

}

coef[i] = top / a[i][i];

}

return coef;

}

}

Any help from you will be very appreciated!!!

R.

[7966 byte] By [regression_seekera] at [2007-10-2 18:47:15]
# 1

I posted my weighted regression code here modified based on the original regression code. Please give me some suggestions if the modified regression code is correctly used to weighted regression. Or give me some testing data to let me test it. Thank for any help!!!

// Apply least squares to raw data to determine the coefficients for

// an n-order equation: y = a0*X^0 + a1*X^1 + ... + an*X^n.

// Returns the coefficients for the solved equation, given a number

// of y and x data points. The rawData input is given in the form of

// {{y0, x0}, {y1, x1},...,{yn, xn}}.The coefficients returned by

// the regression are {a0, a1,...,an} which corresponds to

// {X^0, X^1,...,X^n}. The number of coefficients returned is the

// requested equation order (norder) plus 1.

static double[] linear_equation(double rawData[][], int norder, double [] weights, int length) {

double a[][] = new double[norder+1][norder+1];

double b[] = new double[norder+1];

double term[] = new double[norder+1];

double ysquare = 0;

double weight = 0;

// step through each raw data entries

for (int i = 0; i < rawData.length; i++) {

// sum the y values

b[0] += rawData[i][0];

ysquare += rawData[i][0] * rawData[i][0];

weight += weights[i];

// sum the x power values

double xpower = 1;

for (int j = 0; j < norder+1; j++) {

term[j] = xpower;

a[0][j] += xpower;

xpower = xpower * rawData[i][1];

}

// now set up the rest of rows in the matrix - multiplying each row by each term

for (int j = 1; j < norder+1; j++) {

b[j] += rawData[i][0] * term[j];

for (int k = 0; k < b.length; k++) {

a[j][k] += term[j] * term[k];

}

}

}

// solve for the coefficients

double coef[] = gauss(a, b);

//Squares

double ss = 0;

double yaverage = b[0] / length;

double waverage = weight / length;

for (int i = 0; i < norder+1; i++) {

double xaverage = a[0][i] / length;

ss += coef[i] * (b[i] - (length * xaverage * yaverage));

}

double rsquared = ss / (waverage * (ysquare - (length * yaverage * yaverage)));

double [] newcoef = new double [coef.length + 1];

for (int i = 0; i < newcoef.length; i++){

System.arraycopy(coef,0,newcoef,0,i);

}

coef = newcoef;

coef[coef.length - 1] = rsquared;

return coef;

}

static double[] gauss(double ax[][], double bx[]) {

double a[][] = new double[ax.length][ax[0].length];

double b[] = new double[bx.length];

double pivot;

double mult;

double top;

int n = b.length;

double coef[] = new double[n];

// copy over the array values - inplace solution changes values

for (int i = 0; i < ax.length; i++) {

for (int j = 0; j < ax[i].length; j++) {

a[i][j] = ax[i][j];

}

b[i] = bx[i];

}

for (int j = 0; j < (n-1); j++) {

pivot = a[j][j];

for (int i = j+1; i < n; i++) {

mult = a[i][j] / pivot;

for (int k = j+1; k < n; k++) {

a[i][k] = a[i][k] - mult * a[j][k];

}

b[i] = b[i] - mult * b[j];

}

}

coef[n-1] = b[n-1] / a[n-1][n-1];

for (int i = n-2; i >= 0; i--) {

top = b[i];

for (int k = i+1; k < n; k ++) {

top = top - a[i][k] * coef[k];

}

coef[i] = top / a[i][i];

}

return coef;

}

regression_seekera at 2007-7-13 20:09:53 > top of Java-index,Other Topics,Algorithms...