Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

lregress Class Reference

Linear regression and related functions. More...

#include <lregress.h>

List of all members.

Public Methods

 lregress ()
 ~lregress ()
double meanX (std::vector< point > &data) const
 Calculate the mean of the x values in a vector of point objects. More...

double meanY (std::vector< point > &data) const
 Calculate the mean of the y values in a vector of point objects. More...

void lr (std::vector< point > &data, lineInfo &info) const
 Calculate the linear regression on a set of N points, {Xi, Yi}. More...


Private Methods

 lregress (const lregress &rhs)


Detailed Description

Linear regression and related functions.

The lregress class packages the lr function, which calculates a linear regression line given paired vectors of X and Y points.

Copyright and Use

You may use this source code without limitation and without fee as long as you include:

This software was written and is copyrighted by Ian Kaplan, Bear Products International, www.bearcave.com, 2001.

This software is provided "as is", without any warranty or claim as to its usefulness. Anyone who uses this source code uses it at their own risk. Nor is any support provided by Ian Kaplan and Bear Products International.

Please send any bug fixes or suggested source changes to:

     iank@bearcave.com

Definition at line 40 of file lregress.h.


Constructor & Destructor Documentation

lregress::lregress ( const lregress & rhs ) [private]
 

lregress::lregress ( ) [inline]
 

Definition at line 46 of file lregress.h.

00046 {}

lregress::~lregress ( ) [inline]
 

Definition at line 47 of file lregress.h.

00047 {}


Member Function Documentation

void lregress::lr ( std::vector< point > & data,
lregress::lineInfo & info ) const
 

Calculate the linear regression on a set of N points, {Xi, Yi}.

A regression line is described by the equation y' = a + bx. The coefficients a and b are returned in a lineInfo object, along with the other values.

Formally, linear regression of a set of {x,y} points is described in terms of independent and dependent variables. The array x contains the independent variable, which is exactly known. The array y contains the dependent variable, which is "measured". The y values are assumed to be random values, dependent on the x values.

Definition at line 64 of file lregress.cpp.

Referenced by autocorr::acorrSlope().

00066 {
00067   const size_t N = data.size();
00068   if (N > 0) {
00069 
00070     double muX = meanX( data );
00071 
00072     double muY = meanY( data );
00073 
00074     //     N-1
00075     //     ---
00076     //     \   (Xi - meanX)(Yi - meanY)
00077     //     /__ 
00078     //     i=0
00079     // b = -----------------------------
00080     //     N-1
00081     //     ---             2
00082     //     \   (Xi - meanX)
00083     //     /__ 
00084     //     i=0
00085     //
00086 
00087     double SSxy = 0;
00088     double SSxx = 0;
00089     double SSyy = 0;
00090     double Sx = 0;
00091     double Sy = 0;
00092     double Sxy = 0;
00093     double SSy = 0;
00094     double SSx = 0;
00095     for (size_t i = 0; i < N; i++) {
00096       Sx = Sx + data[i].x();
00097       Sy = Sy + data[i].y();
00098       Sxy = Sxy + (data[i].x() * data[i].y());
00099       SSx = SSx + (data[i].x() * data[i].x());
00100       SSy = SSy + (data[i].y() * data[i].y());
00101       double subX = (data[i].x() - muX);
00102       double subY = (data[i].y() - muY);
00103       SSyy = SSyy + subY * subY;
00104       SSxy = SSxy + subX * subY;
00105       SSxx = SSxx + subX * subX;
00106     }
00107     
00108     // slope
00109     double b = SSxy / SSxx;
00110     // intercept
00111     double a = muY - b * muX;
00112 
00113     // standard deviation of the points
00114     double stddevPoints = sqrt( (SSyy - b * SSxy)/(N-2) );
00115 
00116     // Error of the slope
00117     double bError = stddevPoints / sqrt( SSxx );
00118 
00119     double r2Numerator = (N * Sxy) - (Sx * Sy);
00120     double r2Denominator = ((N*SSx) - (Sx * Sx))*((N*SSy) - (Sy * Sy));
00121     double r2 = (r2Numerator * r2Numerator) / r2Denominator;
00122 
00123     double signB = (b < 0) ? -1.0 : 1.0;
00124 
00125     double r = signB * sqrt( r2 );
00126   
00127     info.corr( r );
00128     info.stddev( stddevPoints );
00129     info.slopeErr( bError );
00130     info.slope( b );
00131     info.intercept( a );
00132   } // if N > 0
00133 
00134 } // lineInfo

double lregress::meanX ( std::vector< point > & data ) const
 

Calculate the mean of the x values in a vector of point objects.

Definition at line 13 of file lregress.cpp.

Referenced by lr().

00014 {
00015   double mean = 0.0;
00016   double sum = 0.0;
00017 
00018   const size_t N = data.size();
00019   size_t i;
00020   for (i = 0; i < N; i++) {
00021     sum = sum + data[i].x();
00022   }
00023   mean = sum / N;
00024   return mean;
00025 } // meanX

double lregress::meanY ( std::vector< point > & data ) const
 

Calculate the mean of the y values in a vector of point objects.

Definition at line 32 of file lregress.cpp.

Referenced by lr().

00033 {
00034   double mean = 0.0;
00035   double sum = 0.0;
00036 
00037   const size_t N = data.size();
00038   size_t i;
00039   for (i = 0; i < N; i++) {
00040     sum = sum + data[i].y();
00041   }
00042   mean = sum / N;
00043   return mean;
00044 } // meanY


The documentation for this class was generated from the following files:
Generated at Tue May 27 21:56:17 2003 for Wavelet compression, determinism and time series forecasting by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001