2007年12月02日

線形最小二乗法での傾き算出処理


/**
* 線形最小二乗法での傾き算出処理.
* @param x_val X軸方向の値の配列.
* @param y_val Y軸方向の値の配列.
* @return 傾きを戻します。.
*/
static public double calulateGradientByLinearLeastSquares(double x_val[], double y_val[]) {

double x_sum = 0;
for( double d : x_val ) {
x_sum += d;
}

int num = x_val.length;
double x_avg = x_sum/num;
double tmp = 0;
for( double d : x_val ) {
tmp += Math.pow( d - x_avg, 2 );
}

double y_sum = 0;
for( double d : y_val ) {
y_sum +=d;
}

double y_avg = y_sum/num;
double xt = 0;
for (int i = 0; i < num; i++) {
xt += ( x_val[i] - x_avg ) * (y_val[i] - y_avg);
}
return( xt/tmp );
}
posted by 台北猫々 at 20:56| Comment(0) | TrackBack(0) | 技術メモ(Java)