contrib/rpl/rrel/rrel_linear_regression.h
Go to the documentation of this file.
00001 #ifndef rrel_linear_regression_h_
00002 #define rrel_linear_regression_h_
00003 
00004 //:
00005 // \file
00006 // \author Chuck Stewart
00007 // \date March 2001
00008 // Class to maintain data and optimization model for single linear regression problems.
00009 
00010 #include <rrel/rrel_estimation_problem.h>
00011 
00012 //: Class to maintain data and optimization model for single linear regression problems.
00013 //  The linear regression problem is to estimate the parameter vector \f$ a \f$ in
00014 //  \f[
00015 //      y = Xa + \epsilon
00016 //  \f]
00017 //  where \f$ \epsilon \f$ is a zero-mean random error variable.
00018 //
00019 //  Each sample is a vnl_vector.  Denoting the length of the
00020 //  vector by m, the first m-1 terms are assumed to be the independent
00021 //  variables, while the last term is the dependent (random) variable.
00022 //  This is suitable for surface estimation from range data ---
00023 //  planar, quadratics, etc --- where most of the error is in the
00024 //  depth direction.  Assuming a data point vector \f$x = (x_1, ...,
00025 //  x_m)\f$, if the "use_intercept" parameter sent to the constructor is
00026 //  true and then the regression model is 
00027 //  \f[
00028 //      x_m = a_1 + a_2 x_1 + ... + a_{m} x_{m-1}
00029 //  \f]
00030 //  Otherwise, the regression model is
00031 //  \f[
00032 //      x_m = a_1 x_1 + ... + a_{m-1} x_{m-1}
00033 //  \f]
00034 //  The parameter vector is \f$a = (a_1, ..., a_{m})\f$ for the former and
00035 //  \f$a = (a_1,...,a_{m-1})\f$ for the latter.
00036 //  
00037 //  To illustrate, consider range data, where each measured point is
00038 //  p = (x,y,z).  For planar fits, the input pts to the class
00039 //  constructor are then just p, so the regression model is
00040 //  \f[
00041 //      z = a_0 + a_1 x + a_2 y
00042 //  \f]
00043 //  For quadratic fits, the data points (vnl_vectors provided as input
00044 //  to the constructor) are \f$(x, y, x^2, xy, y^2, z)\f$ and the regression
00045 //  model is
00046 //  \f[
00047 //      z = a_0 + a_1 x + a_2 y + a_3 x^2 + a_4 xy + a_5 y^2
00048 //  \f]
00049 
00050 class rrel_linear_regression : public rrel_estimation_problem {
00051 public:
00052   //: Constructor that includes all information in the sample vectors.
00053   //  For each sample, the first m-1 entries are the independent
00054   //  variables, and the last entry is the dependent variable.
00055   rrel_linear_regression( const vcl_vector<vnl_vector<double> >& pts,
00056                           bool use_intercept=true);
00057 
00058   //: Constructor with data pre-separated into arrays of independent and dependent variables. 
00059   rrel_linear_regression( const vcl_vector< vnl_vector<double> >&  ind_vars,
00060                           const vcl_vector< double >&  dep_vars );
00061 
00062   //: Destructor.
00063   virtual ~rrel_linear_regression();
00064 
00065   //: Total number of data points.
00066   unsigned int num_samples( ) const; 
00067 
00068   //: Generate a parameter estimate from a minimal sample set.
00069   bool fit_from_minimal_set( const vcl_vector<int>& point_indices,
00070                              vnl_vector<double>& params ) const;
00071 
00072   //: Compute signed fit residuals relative to the parameter estimate.
00073   void compute_residuals( const vnl_vector<double>& params,
00074                           vcl_vector<double>& residuals ) const;
00075 
00076   //: \brief Weighted least squares parameter estimate. 
00077   bool weighted_least_squares_fit( vnl_vector<double>& params,
00078                                    vnl_matrix<double>& norm_covar,
00079                                    const vcl_vector<double>* weights=0 ) const;
00080 
00081 public:  // testing / debugging utility
00082     //: \brief Print information as a test utility.
00083   void print_points() const;
00084 
00085 protected:
00086   vcl_vector<double> rand_vars_;               // e.g. the z or depth values
00087   vcl_vector<vnl_vector<double> > ind_vars_;   // e.g. the image coordinates (plus 1.0
00088                                                // for intercept parameters)
00089 };
00090   
00091 #endif