3 * Implements a simple linear least squares best fit routine.
6 // Written by Curtis Olson, started September 1997.
8 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
10 // This library is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU Library General Public
12 // License as published by the Free Software Foundation; either
13 // version 2 of the License, or (at your option) any later version.
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Library General Public License for more details.
20 // You should have received a copy of the GNU Library General Public
21 // License along with this library; if not, write to the
22 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 // Boston, MA 02111-1307, USA.
33 # error This library requires C++
38 Classical least squares fit:
45 b_1 = \frac{n * \sum_0^{i-1} (x_i*y_i) - \sum_0^{i-1} x_i* \sum_0^{i-1} y_i}
46 {n*\sum_0^{i-1} x_i^2 - (\sum_0^{i-1} x_i)^2}
50 b_0 = \frac{\sum_0^{i-1} y_i}{n} - b_1 * \frac{\sum_0^{i-1} x_i}{n}
53 void least_squares(double *x, double *y, int n, double *m, double *b);
57 * Incrimentally update existing values with a new data point.
59 void least_squares_update(double x, double y, double *m, double *b);
63 @return the least squares error:.
66 \frac{(y_i - \hat{y}_i)^2}{n}
69 double least_squares_error(double *x, double *y, int n, double m, double b);
73 @return the maximum least squares error.
79 double least_squares_max_error(double *x, double *y, int n, double m, double b);