Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

MovingAverageVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // MovingAverageVMatrix.cc 00004 // 00005 // Copyright (C) 2004 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: MovingAverageVMatrix.cc,v 1.2 2004/06/29 19:52:56 tihocan Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Yoshua Bengio 00040 00044 #include "MovingAverageVMatrix.h" 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 00050 MovingAverageVMatrix::MovingAverageVMatrix() 00051 :inherited(), centered_windows(true) 00052 /* ### Initialise all fields to their default value */ 00053 { 00054 // ... 00055 00056 // ### You may or may not want to call build_() to finish building the object 00057 // build_(); 00058 } 00059 00060 PLEARN_IMPLEMENT_OBJECT(MovingAverageVMatrix, "Perform moving average of given columns", 00061 "The user specifies one or more columns and for each such <column-name>\n" 00062 "a moving average window size: a ma<windowsize>-<column-name> column is\n" 00063 "created which will contain at row t the moving average from row t-<windowsize>+1\n" 00064 "to t inclusively of <column-name>.\n"); 00065 00066 void MovingAverageVMatrix::getNewRow(int i, Vec& v) const 00067 { 00068 source->getRow(i,sourcerow); 00069 int max_target = centered_windows?min(i+max_window_size/2,length()-1):i; 00070 if (is_missing(sums(max_target,0))) 00071 { 00072 int k=max_target-1; 00073 while (k>=0 && is_missing(sums(k,0))) k--; 00074 if (k<0) 00075 { 00076 k=0; 00077 source->getRow(k,previous_sourcerow); 00078 for (int j=0;j<columns.length();j++) 00079 { 00080 real new_value = previous_sourcerow[columns[j]]; 00081 if (is_missing(new_value)) 00082 { 00083 sums(k,j) = 0; 00084 ma(k,j) = MISSING_VALUE; 00085 } 00086 else 00087 { 00088 sums(k,j) = new_value; 00089 nnonmissing(k,j) = 1; 00090 ma(k,j) = new_value; 00091 } 00092 } 00093 } 00094 for (;k<max_target;k++) 00095 { 00096 source->getRow(k+1,previous_sourcerow); 00097 for (int j=0;j<columns.length();j++) 00098 { 00099 real new_value = previous_sourcerow[columns[j]]; 00100 if (!is_missing(new_value)) 00101 { 00102 sums(k+1,j) = sums(k,j) + new_value; 00103 nnonmissing(k+1,j) = nnonmissing(k,j) + 1; 00104 } 00105 else sums(k+1,j) = sums(k,j); 00106 int delta = k+1-window_sizes[j]; 00107 int n_at_window_start = (delta>=0)?nnonmissing(delta,j):0; 00108 int n = nnonmissing(k+1,j) - n_at_window_start; 00109 if (n>0) 00110 { 00111 if (delta>=0) 00112 ma(k+1,j) = (sums(k+1,j) - sums(delta,j))/n; 00113 else 00114 ma(k+1,j) = sums(k+1,j)/n; 00115 } 00116 else 00117 ma(k+1,j) = MISSING_VALUE; 00118 } 00119 } 00120 } 00121 for (int j=0;j<columns_to_average.length();j++) 00122 { 00123 int target = centered_windows?min(i+window_sizes[j]/2,length()-1):i; 00124 row[sourcerow.length()+j]=ma(target,j); 00125 } 00126 v << row; 00127 } 00128 00129 void MovingAverageVMatrix::declareOptions(OptionList& ol) 00130 { 00131 // ### Declare all of this object's options here 00132 // ### For the "flags" of each option, you should typically specify 00133 // ### one of OptionBase::buildoption, OptionBase::learntoption or 00134 // ### OptionBase::tuningoption. Another possible flag to be combined with 00135 // ### is OptionBase::nosave 00136 00137 declareOption(ol, "columns_to_average", &MovingAverageVMatrix::columns_to_average, OptionBase::buildoption, 00138 "Names of the columns to average."); 00139 00140 declareOption(ol, "window_sizes", &MovingAverageVMatrix::window_sizes, OptionBase::buildoption, 00141 "Sizes (in number of rows) of the moving average windows for each column to average."); 00142 00143 declareOption(ol, "centered_windows", &MovingAverageVMatrix::centered_windows, OptionBase::buildoption, 00144 "Wether or not to center the window around the current example or to average only over previous examples"); 00145 00146 // Now call the parent class' declareOptions 00147 inherited::declareOptions(ol); 00148 } 00149 00150 void MovingAverageVMatrix::build_() 00151 { 00152 int nc=columns_to_average.length(); 00153 if (source) 00154 { 00155 row.resize(source->width()+nc); 00156 sourcerow = row.subVec(0,source->width()); 00157 previous_sourcerow.resize(source->width()); 00158 columns.resize(nc); 00159 max_window_size=0; 00160 if (window_sizes.length()!=nc) 00161 PLERROR("MovingAverageVMatrix: the window_sizes option should have the same length as the columns_to_average option (got %d and %d)", 00162 window_sizes.length(),nc); 00163 for (int j=0;j<nc;j++) 00164 { 00165 if ((columns[j] = source->fieldIndex(columns_to_average[j])) == -1) 00166 PLERROR("MovingAverageVMatrix: provided field name %s not found in source VMatrix",columns_to_average[j].c_str()); 00167 if (window_sizes[j]>max_window_size) 00168 max_window_size=window_sizes[j]; 00169 } 00170 00171 setMtime(max(getMtime(),source->getMtime())); 00172 00173 // copy length and width from source if not set 00174 if(length_<0) 00175 length_ = source->length(); 00176 if(width_<0) 00177 width_ = source->width() + nc; 00178 00179 sums.resize(length_,nc); 00180 sums.fill(MISSING_VALUE); 00181 nnonmissing.resize(length_,nc); 00182 nnonmissing.clear(); 00183 ma.resize(length_,nc); 00184 ma.fill(MISSING_VALUE); 00185 00186 // copy fieldnames from source if not set and they look good 00187 if(!hasFieldInfos() && source->hasFieldInfos() ) 00188 { 00189 Array<VMField>& sinfo = source->getFieldInfos(); 00190 int w=sinfo.size(); 00191 sinfo.resize(w+nc); 00192 for (int j=0;j<nc;j++) 00193 { 00194 sinfo[w+j]=sinfo[columns[j]]; 00195 sinfo[w+j].name = "ma"+tostring(window_sizes[j])+"-"+sinfo[w+j].name; 00196 } 00197 setFieldInfos(sinfo); 00198 } 00199 } 00200 } 00201 00202 // ### Nothing to add here, simply calls build_ 00203 void MovingAverageVMatrix::build() 00204 { 00205 inherited::build(); 00206 build_(); 00207 } 00208 00209 void MovingAverageVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00210 { 00211 inherited::makeDeepCopyFromShallowCopy(copies); 00212 00213 deepCopyField(columns, copies); 00214 deepCopyField(columns_to_average, copies); 00215 deepCopyField(sums, copies); 00216 deepCopyField(window_sizes, copies); 00217 00218 } 00219 00220 } // end of namespace PLearn 00221

Generated on Tue Aug 17 15:59:04 2004 for PLearn by doxygen 1.3.7