[Very] Simple Linear Regression on Java

15 октября 2012
double[] sx = {0.0, 1.0, 2.0, 3.0, 4.0};
double[] sy = {3.4, 5.2, 7, 8.8, 10.6};
double M = sx.length;

double sumXY = 0, sumX = 0, sumY = 0, sumX2 = 0;

for (int i = 0; i < M; i++) {
    sumX += sx[i];
    sumX2 += sx[i] * sx[i];

    sumY += sy[i];
    sumXY += sx[i] * sy[i];
}

double w1 = (M * sumXY - sumX * sumY) / (M * sumX2 - sumX * sumX);
double w0 = 1 / M * (sumY - w1 * sumX);

System.out.println("y = " + w1 + " * x + " + w0);

Оставить ответ