Lineaarisessa regressiossa sovitetaan suora pistejoukkoon. Monesti mitattava data on kohinaista ja tapahtumaa kuvaava suora on "haettava" pisteiden seasta.
Code Block |
---|
double Statistic::LineFit(const double *const x, const double *const y, const size_t n, double *m, double *b) { int error = 0; double sumx = 0, sumy = 0, sumx2 = 0, sumxy = 0; double dn = (double) n; size_t i; if (n <= 1) { *m = 0; *b = 0; error = 1; } else { double divisor; error = 0; for (i = 0; i < n; i++) { sumx += x[i]; sumy += y[i]; sumx2 += (x[i] * x[i]); sumxy += (x[i] * y[i]); } divisor = (sumx2 - ((sumx * sumx) / dn)); if (divisor != 0) { *m = (sumxy - ((sumx * sumy) / dn)) / divisor; *b = (sumy - ((*m) * sumx)) / dn; } else { *m = 0; *b = 0; error = 2; } } return *m; }; |
...