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

FinancePreprocVMatrix.cc

Go to the documentation of this file.
00001 00002 // -*- C++ -*- 00003 00004 // FinancePreprocVMatrix.cc 00005 // 00006 // Copyright (C) 2003 Rejean Ducharme 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: FinancePreprocVMatrix.cc,v 1.12 2004/07/21 16:30:55 chrish42 Exp $ 00038 ******************************************************* */ 00039 00041 #include "FinancePreprocVMatrix.h" 00042 #include <plearn/base/PDate.h> 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00047 PLEARN_IMPLEMENT_OBJECT(FinancePreprocVMatrix, "ONE LINE DESCR", 00048 "FinancePreprocVMatrix implements a VMatrix with extra preprocessing columns."); 00049 00050 FinancePreprocVMatrix::FinancePreprocVMatrix() 00051 :inherited(), add_tradable(false), add_last_day_of_month(false), 00052 add_moving_average(false), add_rollover_info(false) 00053 {} 00054 00055 FinancePreprocVMatrix::FinancePreprocVMatrix(VMat vm, TVec<string> the_asset_names, 00056 bool add_tradable_info, bool add_last_day, bool add_moving_average_stats, 00057 bool add_roll_over_info, int threshold, TVec<string> the_price_tags, 00058 TVec<int> moving_average_window_length, 00059 string the_volume_tag, string the_date_tag, string the_expiration_tag, 00060 int the_last_day_cutoff, bool last_date_is_a_last_day) 00061 :inherited(vm->length(), vm->width()+(add_tradable_info?the_asset_names.size():0) + (add_last_day?1:0) + (add_moving_average_stats?the_asset_names.size()*the_price_tags.size()*moving_average_window_length.size():0)+(add_roll_over_info?the_asset_names.size():0)), 00062 underlying(vm), asset_name(the_asset_names), 00063 add_tradable(add_tradable_info), add_last_day_of_month(add_last_day), 00064 add_moving_average(add_moving_average_stats), 00065 add_rollover_info(add_roll_over_info), 00066 min_volume_threshold(threshold), prices_tag(the_price_tags), 00067 moving_average_window(moving_average_window_length), 00068 volume_tag(the_volume_tag), date_tag(the_date_tag), 00069 expiration_tag(the_expiration_tag), last_day_cutoff(the_last_day_cutoff), 00070 last_date_is_last_day(last_date_is_a_last_day), 00071 rollover_date(asset_name.size()), row_buffer(vm->width()) 00072 { 00073 build(); 00074 } 00075 00076 void FinancePreprocVMatrix::getNewRow(int i, const Vec& v) const 00077 { 00078 Vec row_buffer = v.subVec(0, underlying.width()); 00079 underlying->getRow(i, row_buffer); 00080 00081 int pos = underlying.width(); 00082 if (add_tradable) 00083 { 00084 for (int k=0; k<asset_name.size(); ++k, ++pos) 00085 { 00086 real volume = row_buffer[volume_index[k]]; 00087 if (!is_missing(volume) && (int)volume>=min_volume_threshold) 00088 v[pos] = 1.0; 00089 else 00090 v[pos] = 0.0; 00091 } 00092 } 00093 00094 if (add_last_day_of_month) 00095 v[pos++] = (last_day_of_month_index.contains(i)) ? 1.0 : 0.0; 00096 00097 if (add_moving_average) 00098 { 00099 int price_pos = 0; 00100 for (int j=0; j<asset_name.length(); j++) 00101 { 00102 for (int k=0; k<prices_tag.size(); k++) 00103 { 00104 int index = price_index[price_pos++]; 00105 int prices_length = MIN(max_moving_average_window, i+1); 00106 int prices_start = i+1 - prices_length; 00107 Vec prices(prices_length); 00108 for (int l=0; l<prices_length; l++) 00109 prices[l] = underlying->get(l+prices_start,index); 00110 00111 for (int l=0; l<moving_average_window.size(); l++) 00112 { 00113 int start = MAX(prices.length()-moving_average_window[l], 0); 00114 int len = prices.length() - start; 00115 v[pos++] = mean(prices.subVec(start,len),true); 00116 } 00117 } 00118 } 00119 } 00120 00121 if (add_rollover_info) 00122 { 00123 for (int k=0; k<asset_name.size(); ++k, ++pos) 00124 { 00125 v[pos] = (rollover_date[k].find(i)==-1 ? 0.0 : 1.0); 00126 } 00127 } 00128 } 00129 00130 void FinancePreprocVMatrix::declareOptions(OptionList& ol) 00131 { 00132 declareOption(ol, "vmat", &FinancePreprocVMatrix::underlying, OptionBase::buildoption, 00133 "The underlying VMat."); 00134 00135 declareOption(ol, "add_tradable", &FinancePreprocVMatrix::add_tradable, OptionBase::buildoption, 00136 "Do we include the information telling if this day is tradable or not."); 00137 00138 declareOption(ol, "add_last_day_of_month", &FinancePreprocVMatrix::add_last_day_of_month, OptionBase::buildoption, 00139 "Do we include the information about the last tradable day of the month or not."); 00140 00141 declareOption(ol, "add_moving_average", &FinancePreprocVMatrix::add_moving_average, OptionBase::buildoption, 00142 "Do we include the moving average statistics on the price_tag indexes."); 00143 00144 declareOption(ol, "add_rollover_info", &FinancePreprocVMatrix::add_rollover_info, OptionBase::buildoption, 00145 "Do we include the boolean information on whether or not this is a new time series (new expiration date)."); 00146 00147 declareOption(ol, "min_volume_threshold", &FinancePreprocVMatrix::min_volume_threshold, OptionBase::buildoption, 00148 "The threshold saying if the asset is tradable or not."); 00149 00150 declareOption(ol, "moving_average_window", &FinancePreprocVMatrix::moving_average_window, OptionBase::buildoption, 00151 "The window size of the moving average."); 00152 00153 declareOption(ol, "prices_tag", &FinancePreprocVMatrix::prices_tag, OptionBase::buildoption, 00154 "The fieldInfo name for the prices columns."); 00155 00156 declareOption(ol, "volume_tag", &FinancePreprocVMatrix::volume_tag, OptionBase::buildoption, 00157 "The fieldInfo name for the volume column."); 00158 00159 declareOption(ol, "date_tag", &FinancePreprocVMatrix::date_tag, OptionBase::buildoption, 00160 "The fieldInfo name of the date column."); 00161 00162 declareOption(ol, "expiration_tag", &FinancePreprocVMatrix::expiration_tag, OptionBase::buildoption, 00163 "The fieldInfo name of the expiration-date column."); 00164 00165 declareOption(ol, "last_day_cutoff", &FinancePreprocVMatrix::last_day_cutoff, OptionBase::buildoption, 00166 "Cutoff for the add_last_day_of_month flag (default=0)."); 00167 00168 // Now call the parent class' declareOptions 00169 inherited::declareOptions(ol); 00170 } 00171 00172 void FinancePreprocVMatrix::setVMFields() 00173 { 00174 Array<VMField>& orig_fields = underlying->getFieldInfos(); 00175 00176 for (int i=0; i<orig_fields.size(); i++) 00177 declareField(i, orig_fields[i].name, orig_fields[i].fieldtype); 00178 00179 int pos = underlying.width(); 00180 if (add_tradable) 00181 { 00182 for (int i=0; i<asset_name.size(); ++i) 00183 { 00184 string name = asset_name[i]+":is_tradable"; 00185 declareField(pos++, name, VMField::DiscrGeneral); 00186 } 00187 } 00188 00189 if (add_last_day_of_month) 00190 declareField(pos++, "is_last_day_of_month", VMField::DiscrGeneral); 00191 00192 if (add_moving_average) 00193 { 00194 for (int i=0; i<asset_name.size(); i++) 00195 { 00196 for (int j=0; j<prices_tag.size(); j++) 00197 { 00198 for (int k=0; k<moving_average_window.size(); k++) 00199 { 00200 string moving_average_name_col = asset_name[i]+":"+prices_tag[j]+":moving_average:w="+tostring(moving_average_window[k]); 00201 declareField(pos++, moving_average_name_col, VMField::DiscrGeneral); 00202 } 00203 } 00204 } 00205 } 00206 00207 if (add_rollover_info) 00208 { 00209 for (int i=0; i<asset_name.size(); ++i) 00210 { 00211 string name = asset_name[i]+":rollover"; 00212 declareField(pos++, name, VMField::DiscrGeneral); 00213 } 00214 } 00215 } 00216 00217 void FinancePreprocVMatrix::build_() 00218 { 00219 if(length_ == -1 || width_ == -1) 00220 { 00221 length_ = underlying->length(); 00222 width_ = ( underlying->width() + 00223 (add_tradable?asset_name.size():0) + 00224 (add_last_day_of_month?1:0) + 00225 (add_moving_average?asset_name.size()*prices_tag.size()*moving_average_window.size():0) + 00226 (add_rollover_info?asset_name.size():0) ); 00227 } 00228 00229 // stuff about the tradable information 00230 int nb_assets = asset_name.size(); 00231 if (add_tradable) 00232 { 00233 volume_index.resize(nb_assets); 00234 for (int i=0; i<nb_assets; i++) 00235 { 00236 string volume_name_col = asset_name[i]+":"+volume_tag; 00237 volume_index[i] = underlying->fieldIndex(volume_name_col); 00238 } 00239 } 00240 00241 if (add_last_day_of_month) 00242 { 00243 int date_col = underlying->fieldIndex(date_tag); 00244 int julian_day = int(underlying->get(0,date_col)); 00245 PDate first_date(julian_day-last_day_cutoff); 00246 int previous_month = first_date.month; 00247 for (int i=1; i<underlying.length(); i++) 00248 { 00249 julian_day = int(underlying->get(i,date_col)); 00250 PDate today(julian_day-last_day_cutoff); 00251 int this_month = today.month; 00252 if (this_month != previous_month) last_day_of_month_index.append(i-1); 00253 previous_month = this_month; 00254 } 00255 // if needed, we set the last day as a last tradable day of month 00256 if (last_date_is_last_day) 00257 last_day_of_month_index.append(underlying.length()-1); 00258 } 00259 00260 if (add_moving_average) 00261 { 00262 max_moving_average_window = max(moving_average_window); 00263 00264 int price_index_size = nb_assets*prices_tag.size(); 00265 price_index.resize(price_index_size); 00266 int k = 0; 00267 for (int i=0; i<nb_assets; i++) 00268 { 00269 for (int j=0; j<prices_tag.size(); j++) 00270 { 00271 string moving_average_name_col = asset_name[i]+":"+prices_tag[j]; 00272 price_index[k++] = underlying->fieldIndex(moving_average_name_col); 00273 } 00274 } 00275 } 00276 00277 if (add_rollover_info) 00278 { 00279 expiration_index.resize(nb_assets); 00280 for (int i=0; i<nb_assets; i++) 00281 { 00282 string expiration_name_col = asset_name[i]+":"+expiration_tag; 00283 expiration_index[i] = underlying->fieldIndex(expiration_name_col); 00284 00285 rollover_date[i].resize(0); 00286 real last_expiration_date = underlying->get(0,expiration_index[i]); 00287 for (int j=1; j<underlying.length(); j++) 00288 { 00289 real expiration_date = underlying->get(j,expiration_index[i]); 00290 if (!is_missing(expiration_date) && expiration_date!=last_expiration_date) 00291 { 00292 if (!is_missing(last_expiration_date)) 00293 rollover_date[i].append(j); 00294 last_expiration_date = expiration_date; 00295 } 00296 } 00297 } 00298 } 00299 00300 setVMFields(); 00301 saveFieldInfos(); 00302 } 00303 00304 // ### Nothing to add here, simply calls build_ 00305 void FinancePreprocVMatrix::build() 00306 { 00307 inherited::build(); 00308 build_(); 00309 } 00310 00311 void FinancePreprocVMatrix::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies) 00312 { 00313 inherited::makeDeepCopyFromShallowCopy(copies); 00314 00315 deepCopyField(prices_tag, copies); 00316 deepCopyField(moving_average_window, copies); 00317 deepCopyField(asset_name, copies); 00318 deepCopyField(volume_index, copies); 00319 deepCopyField(price_index, copies); 00320 deepCopyField(price_index, copies); 00321 deepCopyField(expiration_index, copies); 00322 } 00323 00324 } // end of namespace PLearn 00325

Generated on Tue Aug 17 15:53:19 2004 for PLearn by doxygen 1.3.7