ridge_regression_util.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00032 #ifndef RIDGE_REGRESSION_UTIL_H
00033 #define RIDGE_REGRESSION_UTIL_H
00034 
00035 #include "ridge_regression.h"
00036 
00037 class RidgeRegressionUtil {
00038 
00039  public:
00040 
00041   template<typename T>
00042   static void CopyVectorExceptOneIndex_(const GenVector<T> &source,
00043                                         index_t exclude_index,
00044                                         GenVector<T> *destination) {
00045     destination->Init(source.length() - 1);
00046     index_t current_index = 0;
00047 
00048     for(index_t j = 0; j < source.length(); j++) {
00049       if(source[j] != exclude_index) {
00050         (*destination)[current_index] = source[j];
00051         current_index++;
00052       }
00053     }
00054   }
00055 
00056   static double SquaredCorrelationCoefficient(const Vector &observations,
00057                                               const Vector &predictions) {
00058     
00059     // Compute the average of the observed values.
00060     double avg_observed_value = 0;
00061     
00062     for(index_t i = 0; i < observations.length(); i++) {
00063       avg_observed_value += observations[i];
00064     }
00065     avg_observed_value /= ((double) observations.length());
00066 
00067     // Compute something proportional to the variance of the observed
00068     // values, and the sum of squared residuals of the predictions
00069     // against the observations.
00070     double variance = 0;
00071     double residual = 0;
00072     for(index_t i = 0; i < observations.length(); i++) {
00073       variance += math::Sqr(observations[i] - avg_observed_value);
00074       residual += math::Sqr(observations[i] - predictions[i]);
00075     }
00076     return (variance - residual) / variance;
00077   }
00078 
00079   static double VarianceInflationFactor(const Vector &observations,
00080                                         const Vector &predictions) {
00081     
00082     return 1.0 / 
00083       (1.0 - SquaredCorrelationCoefficient(observations, predictions));
00084   }
00085 
00086 };
00087 
00088 #endif
Generated on Mon Jan 24 12:04:38 2011 for FASTlib by  doxygen 1.6.3