head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.38; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.54; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.54.00; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.57; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# leastsqs.cxx

    00001 // leastsqs.c -- Implements a simple linear least squares best fit routine
    00002 //
    00003 // Written by Curtis Olson, started September 1997.
    00004 //
    00005 // Copyright (C) 1997  Curtis L. Olson  - curt@@infoplane.com
    00006 //
    00007 // This library is free software; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Library General Public
    00009 // License as published by the Free Software Foundation; either
    00010 // version 2 of the License, or (at your option) any later version.
    00011 //
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015 // Library General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Library General Public
    00018 // License along with this library; if not, write to the
    00019 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00020 // Boston, MA  02111-1307, USA.
    00021 //
    00022 // $Id: leastsqs.cxx,v 1.2 2000/07/08 19:16:13 curt Exp $
    00023 //
    00024
    00025
    00026 #include <stdio.h>
    00027
    00028 #include "leastsqs.hxx"
    00029
    00030
    00031 /*
    00032 Least squares fit:
    00033
    00034 y = b0 + b1x
    00035
    00036      n*sum(xi*yi) - (sum(xi)*sum(yi))
    00037 b1 = --------------------------------
    00038      n*sum(xi^2) - (sum(xi))^2
    00039
    00040
    00041 b0 = sum(yi)/n - b1*(sum(xi)/n)
    00042 */
    00043
    00044 double sum_xi, sum_yi, sum_xi_2, sum_xi_yi;
    00045 int sum_n;
    00046
    00047 void least_squares(double *x, double *y, int n, double *m, double *b) {
    00048     int i;
    00049
    00050     sum_xi = sum_yi = sum_xi_2 = sum_xi_yi = 0.0;
    00051     sum_n = n;
    00052
    00053     for ( i = 0; i < n; i++ ) {
    00054         sum_xi += x[i];
    00055         sum_yi += y[i];
    00056         sum_xi_2 += x[i] * x[i];
    00057         sum_xi_yi += x[i] * y[i];
    00058     }
    00059
    00060     /* printf("sum(xi)=%.2f  sum(yi)=%.2f  sum(xi^2)=%.2f  sum(xi*yi)=%.2f\n",
    00061            sum_xi, sum_yi, sum_xi_2, sum_xi_yi); */
    00062
    00063     *m = ( (double)sum_n * sum_xi_yi - sum_xi * sum_yi ) /
    00064         ( (double)sum_n * sum_xi_2 - sum_xi * sum_xi );
    00065     *b = (sum_yi / (double)sum_n) - (*m) * (sum_xi / (double)sum_n);
    00066
    00067     /* printf("slope = %.2f  intercept = %.2f\n", *m, *b); */
    00068 }
    00069
    00070
    00071 /* incrimentally update existing values with a new data point */
    00072 void least_squares_update(double x, double y, double *m, double *b) {
    00073     ++sum_n;
    00074
    00075     sum_xi += x;
    00076     sum_yi += y;
    00077     sum_xi_2 += x * x;
    00078     sum_xi_yi += x * y;
    00079
    00080     /* printf("sum(xi)=%.2f  sum(yi)=%.2f  sum(xi^2)=%.2f  sum(xi*yi)=%.2f\n",
    00081            sum_xi, sum_yi, sum_xi_2, sum_xi_yi); */
    00082
    00083     *m = ( (double)sum_n * sum_xi_yi - sum_xi * sum_yi ) /
    00084         ( (double)sum_n * sum_xi_2 - sum_xi * sum_xi );
    00085     *b = (sum_yi / (double)sum_n) - (*m) * (sum_xi / (double)sum_n);
    00086
    00087     /* printf("slope = %.2f  intercept = %.2f\n", *m, *b); */
    00088 }
    00089
    00090
    00091 /*
    00092   return the least squares error:
    00093
    00094               (y[i] - y_hat[i])^2
    00095               -------------------
    00096                       n
    00097  */
    00098 double least_squares_error(double *x, double *y, int n, double m, double b) {
    00099     int i;
    00100     double error, sum;
    00101
    00102     sum = 0.0;
    00103
    00104     for ( i = 0; i < n; i++ ) {
    00105         error = y[i] - (m * x[i] + b);
    00106         sum += error * error;
    00107         // printf("%.2f %.2f\n", error, sum);
    00108     }
    00109
    00110     return ( sum / (double)n );
    00111 }
    00112
    00113
    00114 /*
    00115   return the maximum least squares error:
    00116
    00117               (y[i] - y_hat[i])^2
    00118  */
    00119 double least_squares_max_error(double *x, double *y, int n, double m, double b){
    00120     int i;
    00121     double error, max_error;
    00122
    00123     max_error = 0.0;
    00124
    00125     for ( i = 0; i < n; i++ ) {
    00126         error = y[i] - (m * x[i] + b);
    00127         error = error * error;
    00128         if ( error > max_error ) {
    00129             max_error = error;
    00130         }
    00131     }
    00132
    00133     return ( max_error );
    00134 }
    00135
    00136

@ 1.3 log @Oops, try again. @ text @d145 1 a145 1