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

YMDDatedVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux 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 /* ******************************************************* 00038 * $Id: YMDDatedVMatrix.cc,v 1.6 2004/06/29 19:31:27 tihocan Exp $ 00039 ******************************************************* */ 00040 00041 #include "YMDDatedVMatrix.h" 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00046 PLEARN_IMPLEMENT_OBJECT(YMDDatedVMatrix, "ONE LINE DESC", "NO HELP"); 00047 00048 YMDDatedVMatrix::YMDDatedVMatrix() 00049 { 00050 } 00051 00052 YMDDatedVMatrix::YMDDatedVMatrix(VMat data_, Mat years_, Mat months_, Mat days_) 00053 : inherited(data_->length(),data_->width()),data(data_), years(years_), 00054 months(months_), days(days_), day_of_ith_pos(days_.length()) 00055 { 00056 build(); 00057 } 00058 00059 YMDDatedVMatrix::YMDDatedVMatrix(Mat& YMD_and_data) 00060 : inherited(YMD_and_data.length(),YMD_and_data.width()-3), 00061 data(YMD_and_data.subMatColumns(3,YMD_and_data.width()-3)), 00062 years(YMD_and_data.subMatColumns(0,1)), 00063 months(YMD_and_data.subMatColumns(1,1)), 00064 days(YMD_and_data.subMatColumns(2,1)), day_of_ith_pos(YMD_and_data.length()) 00065 { 00066 build(); 00067 } 00068 00069 void 00070 YMDDatedVMatrix::build() 00071 { 00072 inherited::build(); 00073 build_(); 00074 } 00075 00076 void YMDDatedVMatrix::build_() 00077 { 00078 if ( data.isNotNull() && years.isNotEmpty() && months.isNotEmpty() && days.isNotEmpty() && !day_of_ith_pos.isEmpty()) { 00079 // check that the dates are in increasing chronological order and 00080 // compute the pos_of_ith_{year,month,day} vectors 00081 if (years.length()!=data->length() || 00082 months.length()!=data->length() || 00083 days.length()!=data->length()) 00084 PLERROR("YMDDatedVMatrix: arguments should all have the same length"); 00085 00086 // build pos_of_date 00087 int first_year = (int)years(0,0); 00088 int last_year = (int)years(data->length()-1,0); 00089 int ny=last_year-first_year+1; 00090 pos_of_date.resize(ny); 00091 for (int y=0;y<ny;y++) 00092 { 00093 pos_of_date[y].resize(12,31); 00094 for (int m=0;m<12;m++) 00095 for (int d=0;d<31;d++) 00096 pos_of_date[y](m,d)= -1; // -1 will mean unseen date 00097 } 00098 00099 int n_different_years=1; 00100 int n_different_months=1; 00101 int n_different_days=1; 00102 for (int i=1;i<years.length();i++) 00103 { 00104 if (years(i,0)>years(i-1,0)) 00105 { 00106 n_different_years++; 00107 n_different_months++; 00108 n_different_days++; 00109 pos_of_date[int(years(i,0)-first_year)](int(months(i,0)-1), int(days(i,0)-1)) = i; 00110 } 00111 else 00112 { 00113 if (years(i,0)<years(i-1,0)) 00114 PLERROR("YMDDatedVMatrix: %d-th year = %d < %d-th year= %d", 00115 i,(int)years(i,0),i-1,(int)years(i-1,0)); 00116 if (months(i,0)>months(i-1,0)) 00117 { 00118 n_different_months++; 00119 n_different_days++; 00120 pos_of_date[int(years(i,0)-first_year)](int(months(i,0)-1),int(days(i,0)-1)) = i; 00121 } 00122 else 00123 { 00124 if (months(i,0)<months(i-1,0)) 00125 PLERROR("YMDDatedVMatrix: %d-th month = %d < %d-th month= %d", 00126 i,(int)months(i,0),i-1,(int)months(i-1,0)); 00127 if (days(i,0)>days(i-1,0)) 00128 { 00129 n_different_days++; 00130 pos_of_date[int(years(i,0)-first_year)](int(months(i,0)-1),int(days(i,0)-1)) = i; 00131 } 00132 else if (days(i,0)<days(i-1,0)) 00133 PLERROR("YMDDatedVMatrix: %d-th day = %d < %d-th day= %d", 00134 i,(int)days(i,0),i-1,(int)days(i-1,0)); 00135 } 00136 } 00137 } 00138 pos_of_ith_year.resize(n_different_years+1); 00139 pos_of_ith_month.resize(n_different_months+1); 00140 pos_of_ith_day.resize(n_different_days+1); 00141 int y=1; 00142 int m=1; 00143 int d=1; 00144 day_of_ith_pos[0]=0; 00145 for (int i=1;i<years.length();i++) 00146 { 00147 if (years(i,0)>years(i-1,0)) 00148 { 00149 pos_of_ith_year[y++]=i; 00150 pos_of_ith_month[m++]=i; 00151 pos_of_ith_day[d++]=i; 00152 } 00153 else 00154 { 00155 if (years(i,0)<years(i-1,0)) 00156 PLERROR("YMDDatedVMatrix: %d-th year = %d < %d-th year= %d", 00157 i,(int)years(i,0),i-1,(int)years(i-1,0)); 00158 if (months(i,0)>months(i-1,0)) 00159 { 00160 pos_of_ith_month[m++]=i; 00161 pos_of_ith_day[d++]=i; 00162 } 00163 else 00164 { 00165 if (months(i,0)<months(i-1,0)) 00166 PLERROR("YMDDatedVMatrix: %d-th month = %d < %d-th month= %d", 00167 i,(int)months(i,0),i-1,(int)months(i-1,0)); 00168 if (days(i,0)>days(i-1,0)) 00169 pos_of_ith_day[d++]=i; 00170 else if (days(i,0)<days(i-1,0)) 00171 PLERROR("YMDDatedVMatrix: %d-th day = %d < %d-th day= %d", 00172 i,(int)days(i,0),i-1,(int)days(i-1,0)); 00173 } 00174 } 00175 day_of_ith_pos[i]=d-1; 00176 } 00177 pos_of_ith_year[y]=data->length(); 00178 pos_of_ith_month[m]=data->length(); 00179 pos_of_ith_day[d]=data->length(); 00180 } 00181 } 00182 00183 void 00184 YMDDatedVMatrix::declareOptions(OptionList &ol) 00185 { 00186 declareOption(ol, "data", &YMDDatedVMatrix::data, OptionBase::buildoption, ""); 00187 declareOption(ol, "years", &YMDDatedVMatrix::years, OptionBase::buildoption, ""); 00188 declareOption(ol, "months", &YMDDatedVMatrix::months, OptionBase::buildoption, ""); 00189 declareOption(ol, "days", &YMDDatedVMatrix::days, OptionBase::buildoption, ""); 00190 declareOption(ol, "day_of_ith_pos", &YMDDatedVMatrix::day_of_ith_pos, OptionBase::buildoption, ""); 00191 inherited::declareOptions(ol); 00192 } 00193 00194 VMat YMDDatedVMatrix::subDistrRelativeYears(int first_relative_year, int n_years) 00195 { 00196 if (first_relative_year<0 || n_years<0 || 00197 first_relative_year+n_years >=pos_of_ith_year.length()) 00198 PLERROR("YMDDatedVMatrix::subDistrRelativeYears(%d,%d) : incorrect arguments", 00199 first_relative_year, n_years); 00200 return data.subMatRows(int(pos_of_ith_year[first_relative_year]), 00201 int(pos_of_ith_year[first_relative_year+n_years]- 00202 pos_of_ith_year[first_relative_year])); 00203 } 00204 00205 VMat YMDDatedVMatrix::subDistrRelativeMonths(int first_relative_month, int n_months) 00206 { 00207 if (first_relative_month<0 || n_months<0 || 00208 first_relative_month+n_months >=pos_of_ith_month.length()) 00209 PLERROR("YMDDatedVMatrix::subDistrRelativeMonths(%d,%d) : incorrect arguments", 00210 first_relative_month, n_months); 00211 return data.subMatRows(int(pos_of_ith_month[first_relative_month]), 00212 int(pos_of_ith_month[first_relative_month+n_months]- 00213 pos_of_ith_month[first_relative_month])); 00214 } 00215 00216 VMat YMDDatedVMatrix::subDistrRelativeDays(int first_relative_day, int n_days) 00217 { 00218 if (first_relative_day<0 || n_days<0 || 00219 first_relative_day+n_days >=pos_of_ith_day.length()) 00220 PLERROR("YMDDatedVMatrix::subDistrRelativeDays(%d,%d) : incorrect arguments", 00221 first_relative_day, n_days); 00222 return data.subMatRows(int(pos_of_ith_day[first_relative_day]), 00223 int(pos_of_ith_day[first_relative_day+n_days]- 00224 pos_of_ith_day[first_relative_day])); 00225 } 00226 00227 int YMDDatedVMatrix::positionOfDate(int year, int month, int day) 00228 { 00229 if (year<years(0,0)) 00230 PLERROR("YMDDatedVMatrix::positionOfDate(%d,%d,%d): year %d < first year %d", 00231 year,month,day,year,(int)years(0,0)); 00232 if (year>years(years.length()-1,0)) 00233 return years.length(); 00234 if (month<1 || month>12) 00235 PLERROR("YMDDatedVMatrix::positionOfDate(%d,%d,%d): month %d should be in [1,12]", 00236 year,month,day,month); 00237 if (day<1 || day>31) 00238 PLERROR("YMDDatedVMatrix::positionOfDate(%d,%d,%d): day %d should be in [1,31]", 00239 year,month,day,day); 00240 return int(pos_of_date[year-int(years(0,0))](month-1,day-1)); 00241 } 00242 00243 VMat YMDDatedVMatrix::subDistrAbsoluteYears(int year, int month, int day, int n_years) 00244 { 00245 int first_pos = positionOfDate(year,month,day); 00246 int last_pos = positionOfDate(year+n_years,month,day); 00247 return data.subMatRows(first_pos,last_pos-first_pos); 00248 } 00249 00250 VMat YMDDatedVMatrix::subDistrAbsoluteMonths(int year, int month, int day, int n_months) 00251 { 00252 int first_pos = positionOfDate(year,month,day); 00253 int extra_years = (month + n_months) / 12; 00254 int new_month = (month + n_months) - extra_years*12; 00255 int last_pos = positionOfDate(year+extra_years,new_month,day); 00256 return data.subMatRows(first_pos,last_pos-first_pos); 00257 } 00258 00259 VMat YMDDatedVMatrix::subDistrAbsoluteDays(int year, int month, int day, int n_days) 00260 { 00261 int first_pos = positionOfDate(year,month,day); 00262 int nthday = (int)day_of_ith_pos[first_pos]; 00263 int last_pos = (int)pos_of_ith_day[nthday+n_days]; 00264 return data.subMatRows(first_pos,last_pos-first_pos); 00265 } 00266 00267 VMat YMDDatedVMatrix::subDistrAbsoluteUnits(int year, int month, int day, int n_units, const string& units) 00268 { 00269 if (units[0]=='y' || units[0]=='Y') 00270 return subDistrAbsoluteYears(year,month,day,n_units); 00271 if (units[0]=='m' || units[0]=='M') 00272 return subDistrAbsoluteMonths(year,month,day,n_units); 00273 if (units[0]=='d' || units[0]=='D') 00274 return subDistrAbsoluteDays(year,month,day,n_units); 00275 else 00276 PLERROR("YMDDatedVMatrix::subDistrAbsoluteUnits, unknown units, expected years,months or days!", 00277 units.c_str()); 00278 Mat m; 00279 return VMat(m); 00280 } 00281 00282 VMat YMDDatedVMatrix::subDistrRelativeDates(int first, int n, const string& units) 00283 { 00284 if (units[0]=='y' || units[0]=='Y') 00285 return subDistrRelativeYears(first,n); 00286 if (units[0]=='m' || units[0]=='M') 00287 return subDistrRelativeMonths(first,n); 00288 if (units[0]=='d' || units[0]=='D') 00289 return subDistrRelativeDays(first,n); 00290 else 00291 PLERROR("YMDDatedVMatrix::subDistrRelativeDates(%d,%d,%s), unknown units, expected years,months or days!", 00292 first,n,units.c_str()); 00293 return VMat(Mat()); 00294 } 00295 00296 int YMDDatedVMatrix::lengthInDates(const string& units) 00297 { 00298 if (units[0]=='y' || units[0]=='Y') 00299 return pos_of_ith_year.length()-1; 00300 if (units[0]=='m' || units[0]=='M') 00301 return pos_of_ith_month.length()-1; 00302 if (units[0]=='d' || units[0]=='D') 00303 return pos_of_ith_day.length()-1; 00304 else 00305 PLERROR("YMDDatedVMatrix::lengthInDates(%s), unknown units, expected years,months or days!", 00306 units.c_str()); 00307 return 0; 00308 } 00309 00310 int YMDDatedVMatrix::positionOfRelativeDate(int first, const string& units) 00311 { 00312 if (units[0]=='y' || units[0]=='Y') 00313 return (int)pos_of_ith_year[first]; 00314 if (units[0]=='m' || units[0]=='M') 00315 return (int)pos_of_ith_month[first]; 00316 if (units[0]=='d' || units[0]=='D') 00317 return (int)pos_of_ith_day[first]; 00318 else 00319 PLERROR("YMDDatedVMatrix::positionOfRelativeDate(%s), unknown units, expected years,months or days!", 00320 units.c_str()); 00321 return 0; 00322 } 00323 00324 void YMDDatedVMatrix::copyDatesOfRows(int from_row, int n_rows, Mat& dates) 00325 { 00326 dates.resize(n_rows,3); 00327 for (int i=from_row;i<from_row+n_rows;i++) 00328 { 00329 dates(i-from_row,0)=years(i,0); 00330 dates(i-from_row,1)=months(i,0); 00331 dates(i-from_row,2)=days(i,0); 00332 } 00333 } 00334 00335 Vec YMDDatedVMatrix::copyRowDataAndDate(int row, int &year, int &month, int &day) 00336 { 00337 year = (int)years(row,0); 00338 month = (int)months(row,0); 00339 day = (int)days(row,0); 00340 return data(row); 00341 } 00342 void YMDDatedVMatrix::copyDateOfRow(int row, int &year, int &month, int &day) 00343 { 00344 year = (int)years(row,0); 00345 month = (int)months(row,0); 00346 day = (int)days(row,0); 00347 } 00348 00349 } // end of namespcae PLearn

Generated on Tue Aug 17 16:11:22 2004 for PLearn by doxygen 1.3.7