00001 // -*- C++ -*- 00002 // VecStatsCollector.h 00003 // 00004 // Copyright (C) 2002 Pascal Vincent 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 /* ******************************************************* 00035 * $Id: VecStatsCollector.h,v 1.19 2004/07/21 16:30:53 chrish42 Exp $ 00036 ******************************************************* */ 00037 00039 #ifndef VecStatsCollector_INC 00040 #define VecStatsCollector_INC 00041 00042 #include <plearn/base/Object.h> 00043 #include "StatsCollector.h" 00044 00045 namespace PLearn { 00046 using namespace std; 00047 00048 class VecStatsCollector: public Object 00049 { 00050 private: 00051 typedef Object inherited; 00052 00053 public: 00054 // ************************ 00055 // * public build options * 00056 // ************************ 00057 00059 int maxnvalues; 00060 00062 TVec<string> fieldnames; 00063 00065 bool compute_covariance; 00066 00067 // * "learnt" options * 00068 TVec<StatsCollector> stats; // the stats for each element 00069 Mat cov; // the uncentered covariance matrix (mean not subtracted: X'.X) 00070 00071 // **************** 00072 // * Constructors * 00073 // **************** 00074 00075 VecStatsCollector(); 00076 00077 00078 // ****************** 00079 // * Object methods * 00080 // ****************** 00081 00082 private: 00084 // (Please implement in .cc) 00085 void build_(); 00086 00087 protected: 00089 static void declareOptions(OptionList& ol); 00090 00091 public: 00092 00093 int length() const { return stats.length(); } 00094 int size() const { return length(); } 00095 00097 virtual void build(); 00098 00100 virtual void forget(); 00101 00104 virtual void update(const Vec& x, real weight = 1.0); 00105 00107 void setFieldNames(TVec<string> the_fieldnames) 00108 { fieldnames = the_fieldnames; } 00109 00111 TVec<string> getFieldNames() const 00112 { return fieldnames; } 00113 00116 int getFieldNum(const string& fieldname_or_num) const; 00117 00119 00125 virtual double getStat(const string& statspec); 00126 00128 void update(const Mat& m); 00129 00132 void update(const Mat& m, const Vec& weights); 00133 00135 virtual void finalize(); 00136 00138 const StatsCollector& getStats(int i) const 00139 { return stats[i]; } 00140 00142 Vec getMean() const; 00143 00145 Vec getVariance() const; 00146 00148 Vec getStdDev() const; 00149 00151 Vec getStdError() const; 00152 00154 const Mat& getXtX() const 00155 { return cov; } 00156 00158 Mat getCovariance() const; 00159 00161 Mat getCorrelation() const; 00162 00166 Vec getAllStats(Vec& st) const; 00167 00171 int getIndexInAllStats(int fieldindex, const string& statname) const; 00172 00174 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00175 00177 PLEARN_DECLARE_OBJECT(VecStatsCollector); 00178 }; 00179 00180 // Declares a few other classes and functions related to this class 00181 DECLARE_OBJECT_PTR(VecStatsCollector); 00182 00183 00184 } // end of namespace PLearn 00185 00186 #endif