PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // LinearRegressor.h 00004 // 00005 // Copyright (C) 2003 Yoshua Bengio 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: LinearRegressor.h 9695 2008-11-18 20:51:09Z ducharme $ 00037 ******************************************************* */ 00038 00040 #ifndef LinearRegressor_INC 00041 #define LinearRegressor_INC 00042 00043 #include <plearn_learners/generic/PLearner.h> 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 class LinearRegressor: public PLearner 00049 { 00050 typedef PLearner inherited; 00051 00052 // Global storage used to save memory allocations. 00053 mutable Vec extendedinput; 00054 mutable Vec input; 00055 Vec train_costs; 00056 00057 protected: 00058 Mat XtX; 00059 Mat XtY; 00060 real sum_squared_y; 00061 real sum_gammas; 00062 00064 real weights_norm; 00065 00066 // ********************* 00067 // * protected options * 00068 // ********************* 00069 00070 // ### declare protected option fields (such as learnt parameters) here 00071 00074 Mat weights; 00075 00078 real AIC; 00079 00082 real BIC; 00083 00087 Vec resid_variance; 00088 00089 public: 00090 00091 // ************************ 00092 // * public build options * 00093 // ************************ 00094 00096 bool include_bias; 00097 00100 bool cholesky; 00101 00103 real weight_decay; 00104 00110 bool output_learned_weights; 00111 00112 // **************** 00113 // * Constructors * 00114 // **************** 00115 00116 // Default constructor, make sure the implementation in the .cc 00117 // initializes all fields to reasonable default values. 00118 LinearRegressor(); 00119 00120 00121 // ****************** 00122 // * PLearner methods * 00123 // ****************** 00124 00125 private: 00127 void build_(); 00128 void resetAccumulators(); 00129 00130 public: 00131 00132 // ************************ 00133 // **** Object methods **** 00134 // ************************ 00135 00137 virtual void build(); 00138 00140 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00141 00142 // Declares other standard object methods 00143 // If your class is not instantiatable (it has pure virtual methods) 00144 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS 00145 PLEARN_DECLARE_OBJECT(LinearRegressor); 00146 00147 00148 // ************************** 00149 // **** PLearner methods **** 00150 // ************************** 00151 00154 virtual int outputsize() const; 00155 00163 virtual void forget(); 00164 00168 virtual void train(); 00169 00172 virtual void computeOutput(const Vec& input, Vec& output) const; 00173 00175 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00176 const Vec& target, Vec& costs) const; 00177 00179 virtual 00180 bool computeConfidenceFromOutput(const Vec& input, const Vec& output, 00181 real probability, 00182 TVec< pair<real,real> >& intervals) const; 00183 00185 virtual TVec<string> getTestCostNames() const; 00186 00189 virtual TVec<string> getTrainCostNames() const; 00190 00191 protected: 00193 static void declareOptions(OptionList& ol); 00194 00198 void computeInformationCriteria(real squared_error, int n); 00199 00202 void computeResidualsVariance(const Vec& outputwise_sum_squared_Y); 00203 00205 int effective_inputsize() const 00206 { return inputsize() + (include_bias? 1 : 0); } 00207 }; 00208 00209 // Declares a few other classes and functions related to this class 00210 DECLARE_OBJECT_PTR(LinearRegressor); 00211 00212 } // end of namespace PLearn 00213 00214 #endif 00215 00216 00217 /* 00218 Local Variables: 00219 mode:c++ 00220 c-basic-offset:4 00221 c-file-style:"stroustrup" 00222 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00223 indent-tabs-mode:nil 00224 fill-column:79 00225 End: 00226 */ 00227 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :