PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // CorrelationProfiler.cc 00004 // 00005 // Copyright (C) 2007 Pierre-Antoine Manzagol 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 // Authors: Pierre-Antoine Manzagol 00036 00040 #include "CorrelationProfiler.h" 00041 #include <plearn/math/TMat_maths.h> 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 PLEARN_IMPLEMENT_OBJECT( 00047 CorrelationProfiler, 00048 "Object used to profile the correlation between the elements of a vector", 00049 "\n" 00050 ); 00051 00052 CorrelationProfiler::CorrelationProfiler() : its_dim(0), 00053 its_name("noname") 00054 /* ### Initialize all fields to their default value */ 00055 { 00056 // ... 00057 00058 // ### You may (or not) want to call build_() to finish building the object 00059 // ### (doing so assumes the parent classes' build_() have been called too 00060 // ### in the parent classes' constructors, something that you must ensure) 00061 } 00062 00063 00064 CorrelationProfiler::CorrelationProfiler(int dim, string name) : its_dim(dim), 00065 its_name(name) 00066 { 00067 } 00068 00069 00070 void CorrelationProfiler::reset() 00071 { 00072 A.clear(); 00073 sum_v.clear(); 00074 n = 0; 00075 } 00076 00077 // Accumulates statistics for computing the correlation 00078 void CorrelationProfiler::operator()(Vec& v) 00079 { 00080 PLASSERT( A.length() == v.length() ); 00081 externalProductAcc(A, v, v); 00082 sum_v += v; 00083 n++; 00084 } 00085 00086 00087 void CorrelationProfiler::printAndReset() 00088 { 00089 00090 cout << its_name << " - stats based on " << n << " samples." << endl; 00091 00092 // *** Get mean 00093 sum_v /= n; 00094 00095 // *** Divide by n-1 to get non-centered covariance 00096 A /= (real)(n-1); 00097 00098 // *** Get the centered covariance 00099 externalProductScaleAcc(A, sum_v, sum_v, -1.0); 00100 00101 // TODO check opening 00102 string file_name; 00103 file_name = its_name + "_covariance.txt"; 00104 ofstream fd; 00105 fd.open( file_name.c_str() ); 00106 A.print(fd); 00107 fd.close(); 00108 00109 /* // *** Get correlation by dividing by the product of the standard deviations 00110 Vec diagA = diag( A ); 00111 // TODO Not very efficient. Isn't there a lapack function for this. 00112 for(int i=0; i<A.length(); i++) { 00113 for(int j=0; j<i; j++) { 00114 A(i,j) /= sqrt( diagA[i]*diagA[j] ); 00115 A(j,i) = A(i,j); 00116 } 00117 // the diagonal 00118 A(i,i) = 1.0; 00119 } 00120 00121 // *** Open file, print correlation and close. 00122 // TODO check opening 00123 string file_name; 00124 file_name = its_name + ".txt"; 00125 ofstream fd; 00126 fd.open( file_name.c_str() ); 00127 A.print(fd); 00128 fd.close();*/ 00129 00130 // *** Reset 00131 reset(); 00132 } 00133 00134 00135 // ### Nothing to add here, simply calls build_ 00136 void CorrelationProfiler::build() 00137 { 00138 inherited::build(); 00139 build_(); 00140 } 00141 00142 void CorrelationProfiler::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00143 { 00144 inherited::makeDeepCopyFromShallowCopy(copies); 00145 00146 // ### Call deepCopyField on all "pointer-like" fields 00147 // ### that you wish to be deepCopied rather than 00148 // ### shallow-copied. 00149 // ### ex: 00150 deepCopyField(A, copies); 00151 deepCopyField(sum_v, copies); 00152 00153 // ### Remove this line when you have fully implemented this method. 00154 //PLERROR("CorrelationProfiler::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00155 } 00156 00157 void CorrelationProfiler::declareOptions(OptionList& ol) 00158 { 00159 // ### Declare all of this object's options here. 00160 // ### For the "flags" of each option, you should typically specify 00161 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00162 // ### OptionBase::tuningoption. If you don't provide one of these three, 00163 // ### this option will be ignored when loading values from a script. 00164 // ### You can also combine flags, for example with OptionBase::nosave: 00165 // ### (OptionBase::buildoption | OptionBase::nosave) 00166 00167 declareOption(ol, "dimension", &CorrelationProfiler::its_dim, 00168 OptionBase::buildoption, 00169 "Dimension of the vector whose correlantion we want (integer)."); 00170 declareOption(ol, "name", &CorrelationProfiler::its_name, 00171 OptionBase::buildoption, 00172 "Name of the vector whose correlantion we want (string)." 00173 "Used in determining the output file's name in" 00174 "printAndReset()."); 00175 00176 // Now call the parent class' declareOptions 00177 inherited::declareOptions(ol); 00178 } 00179 00180 void CorrelationProfiler::build_() 00181 { 00182 // ### This method should do the real building of the object, 00183 // ### according to set 'options', in *any* situation. 00184 // ### Typical situations include: 00185 // ### - Initial building of an object from a few user-specified options 00186 // ### - Building of a "reloaded" object: i.e. from the complete set of 00187 // ### all serialised options. 00188 // ### - Updating or "re-building" of an object after a few "tuning" 00189 // ### options have been modified. 00190 // ### You should assume that the parent class' build_() has already been 00191 // ### called. 00192 PLASSERT( its_dim > 0 ); 00193 A.resize(its_dim, its_dim); 00194 sum_v.resize(its_dim); 00195 reset(); 00196 } 00197 00198 00199 } // end of namespace PLearn 00200 00201 00202 /* 00203 Local Variables: 00204 mode:c++ 00205 c-basic-offset:4 00206 c-file-style:"stroustrup" 00207 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00208 indent-tabs-mode:nil 00209 fill-column:79 00210 End: 00211 */ 00212 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :