PLearn 0.1
|
00001 // -*- C++ -*- 00002 // VecStatsCollector.h 00003 // 00004 // Copyright (C) 2002 Pascal Vincent 00005 // Copyright (C) 2005 Université de Montréal 00006 // Copyright (C) 2007 Xavier Saint-Mleux, ApSTAT Technologies inc. 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 /* ******************************************************* 00037 * $Id: VecStatsCollector.h 10282 2009-07-21 21:22:48Z plearner $ 00038 ******************************************************* */ 00039 00041 #ifndef VecStatsCollector_INC 00042 #define VecStatsCollector_INC 00043 00044 #include <plearn/base/Object.h> 00045 00046 // From C++ stdlib 00047 #include <map> 00048 00049 // From PLearn 00050 #include "StatsCollector.h" 00051 #include "ObservationWindow.h" 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 class VecStatsCollector: public Object 00057 { 00058 typedef Object inherited; 00059 00060 public: 00061 // ************************ 00062 // * public build options * 00063 // ************************ 00064 00065 int maxnvalues; 00066 00068 bool compute_covariance; 00069 00076 double epsilon; 00077 00086 int m_window; 00087 00106 int m_full_update_frequency; 00107 00120 int m_window_nan_code; 00121 00131 bool no_removal_warnings; 00132 00133 00134 // ****************** 00135 // * learnt options * 00136 // ****************** 00137 00139 TVec<StatsCollector> stats; 00140 00142 Mat cov; 00143 00144 Mat sum_cross; 00145 Mat sum_cross_weights; 00146 Mat sum_cross_square_weights; 00147 real sum_non_missing_weights; 00148 real sum_non_missing_square_weights; 00149 00150 public: 00151 00152 // **************** 00153 // * Constructors * 00154 // **************** 00155 00156 VecStatsCollector(); 00157 00158 virtual int length() const; 00159 int size() const { return length(); } 00160 00162 virtual void build(); 00163 00165 virtual void forget(); 00166 00169 virtual void remote_update(const Vec& x, real weight = 1.0); 00170 00173 virtual void update(const Vec& x, real weight = 1.0); 00174 00176 bool shouldUpdateWindow(const Vec& x); 00177 00182 virtual void remove_observation(const Vec& x, real weight = 1.0); 00183 00185 void setFieldNames(TVec<string> the_fieldnames); 00186 00188 TVec<string> getFieldNames() const 00189 { return fieldnames; } 00190 00193 int getFieldNum(const string& fieldname_or_num) const; 00194 00196 00202 virtual double getStat(const string& statspec); 00203 00205 void update(const Mat& m); 00206 00209 void update(const Mat& m, const Vec& weights); 00210 00212 virtual void finalize(); 00213 00215 const StatsCollector& getStats(int i) const 00216 { return stats[i]; } 00217 00219 StatsCollector& getStats(int i) { return stats[i]; } 00220 00222 Vec getMean() const { 00223 Vec mean; 00224 getMean(mean); 00225 return mean; 00226 } 00227 00229 Vec remote_getMean() 00230 { return getMean(); } 00231 00233 void getMean(Vec& mean) const; 00234 00236 Vec getVariance() const; 00237 00239 Vec getStdDev() const; 00240 00242 Vec getStdError() const; 00243 00246 const Mat& getXtX() const 00247 { return cov; } 00248 00253 void getCovariance(Mat& covar) const; 00254 Mat getCovariance() const; 00255 00257 Mat remote_getCovariance() 00258 { return getCovariance(); } 00259 00261 Mat getCorrelation() const; 00262 00266 Vec getAllStats(Vec& st) const; 00267 00269 Vec getAllStats(const string& statname) const; 00270 00273 void getAllStats(const string& statname, Vec& result) const; 00274 00278 int getIndexInAllStats(int fieldindex, const string& statname) const; 00279 00286 void append(const VecStatsCollector& vsc, const string fieldname_prefix="", 00287 const TVec<string>& new_fieldnames = TVec<string>() ); 00288 00290 void remote_append(const VecStatsCollector* vsc, const string fieldname_prefix, 00291 const TVec<string>& new_fieldnames); 00292 00294 virtual void setWindowSize(int sz); 00295 00296 const Mat& getObservations() const; 00297 const PP<ObservationWindow> getObservationWindow() const; 00298 00300 virtual void merge(VecStatsCollector& other); 00301 00303 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00304 00306 PLEARN_DECLARE_OBJECT(VecStatsCollector); 00307 00308 protected: 00311 map<string,int> fieldnames_num; 00312 00315 TVec<string> fieldnames; 00316 00318 PP<ObservationWindow> m_observation_window; 00319 00322 int m_num_incremental; 00323 00324 protected: 00326 static void declareOptions(OptionList& ol); 00327 00329 static void declareMethods(RemoteMethodMap& rmm); 00330 00331 private: 00333 // (Please implement in .cc) 00334 void build_(); 00335 }; 00336 00337 // Declares a few other classes and functions related to this class 00338 DECLARE_OBJECT_PTR(VecStatsCollector); 00339 00340 } // end of namespace PLearn 00341 00342 #endif 00343 00344 00345 /* 00346 Local Variables: 00347 mode:c++ 00348 c-basic-offset:4 00349 c-file-style:"stroustrup" 00350 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00351 indent-tabs-mode:nil 00352 fill-column:79 00353 End: 00354 */ 00355 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :