ridge_regression_util.h
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
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     
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     
00068     
00069     
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