PLearn 0.1
ComputeDond2Target.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // ComputeDond2Target.cc
00004 //
00005 // Copyright (C) 2006 Dan Popovici, Pascal Lamblin
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: Dan Popovici
00036 
00039 #define PL_LOG_MODULE_NAME "ComputeDond2Target"
00040 
00041 #include "ComputeDond2Target.h"
00042 #include <plearn/io/pl_log.h>
00043 
00044 namespace PLearn {
00045 using namespace std;
00046 
00047 PLEARN_IMPLEMENT_OBJECT(
00048     ComputeDond2Target,
00049     "Computes the class target for the training and the test datasets.",
00050     "the program reorders the input variables to group them between the binary variables,\n"
00051     "the discrete variables and the continuous variables.\n"
00052     "It reorders the variables in the order specified by the input vector option.\n"
00053     "It also computes the predicted class target from the predicted annual sales figure\n"
00054     "of the financial institution and computes the real class target.\n"
00055 );
00056 
00058 // ComputeDond2Target //
00060 ComputeDond2Target::ComputeDond2Target()
00061   : unknown_sales(0)
00062 {
00063 }
00064     
00066 // declareOptions //
00068 void ComputeDond2Target::declareOptions(OptionList& ol)
00069 {
00070     declareOption(ol, "input_vector", &ComputeDond2Target::input_vector,
00071                   OptionBase::buildoption,
00072                   "The variables to assemble in the input vector by names.\n"
00073                   "To ease the following steps they are grouped with the binary variables first,\n"
00074                   "the discrete variables, the continuous variables and finally some variables unused in the training.\n");
00075 
00076     declareOption(ol, "unknown_sales", &ComputeDond2Target::unknown_sales,
00077                   OptionBase::buildoption,
00078                   "If set to 1 and annual sales attribute is missing, the class will be set to missing.");
00079 
00080     declareOption(ol, "target_sales", &ComputeDond2Target::target_sales,
00081                   OptionBase::buildoption,
00082                   "The column of the real annual sales used to compute the real class target.");
00083 
00084     declareOption(ol, "predicted_sales", &ComputeDond2Target::predicted_sales,
00085                   OptionBase::buildoption,
00086                   "The column of the predicted annual sales used to compute the predicted class target.");
00087 
00088     declareOption(ol, "margin", &ComputeDond2Target::margin,
00089                   OptionBase::buildoption,
00090                   "The column of the total authorized margins including SLA.");
00091 
00092     declareOption(ol, "loan", &ComputeDond2Target::loan,
00093                   OptionBase::buildoption,
00094                   "The column of the total loan balances excluding mortgages.");
00095 
00096     declareOption(ol, "output_path", &ComputeDond2Target::output_path,
00097                   OptionBase::buildoption,
00098                   "The file path for the targeted output file.");
00099 
00100     inherited::declareOptions(ol);
00101 }
00102 
00104 // makeDeepCopyFromShallowCopy //
00106 void ComputeDond2Target::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00107 {
00108     deepCopyField(input_vector, copies);
00109     deepCopyField(unknown_sales, copies);
00110     deepCopyField(target_sales, copies);
00111     deepCopyField(predicted_sales, copies);
00112     deepCopyField(margin, copies);
00113     deepCopyField(loan, copies);
00114     deepCopyField(output_path, copies);
00115     inherited::makeDeepCopyFromShallowCopy(copies);
00116 
00117 }
00118 
00120 // build //
00122 void ComputeDond2Target::build()
00123 {
00124     // ### Nothing to add here, simply calls build_().
00125     inherited::build();
00126     build_();
00127 }
00128 
00130 // build_ //
00132 void ComputeDond2Target::build_()
00133 {
00134     MODULE_LOG << "build_() called" << endl;
00135     if (train_set)
00136     {
00137         computeTarget();
00138     }
00139 }
00140 
00141 void ComputeDond2Target::computeTarget()
00142 {    
00143     // initialize primary dataset
00144     int main_col = 0;
00145     main_length = train_set->length();
00146     int main_width = train_set->width();
00147     main_input.resize(main_width);
00148     main_names.resize(main_width);
00149     ins_width = input_vector.size();
00150     predicted_class = ins_width;
00151     target_class = ins_width + 1;
00152     output_width = ins_width + 2;
00153     output_variable_src.resize(ins_width);
00154     output_names.resize(output_width);
00155     output_vec.resize(output_width);
00156     main_names << train_set->fieldNames();
00157     for (int ins_col = 0; ins_col < ins_width; ins_col++)
00158     {
00159         for (main_col = 0; main_col < main_width; main_col++)
00160         {
00161             if (input_vector[ins_col] == main_names[main_col]) break;
00162         }
00163         if (main_col >= main_width) PLERROR("In ComputeDond2Target: no field with this name in input dataset: %s", (input_vector[ins_col]).c_str());
00164         output_variable_src[ins_col] = main_col;
00165         output_names[ins_col] = input_vector[ins_col];
00166     }
00167     output_names[predicted_class] = "CLASSE_PRED";
00168     output_names[target_class] = "CLASSE_REEL";
00169     
00170     // initialize output datasets
00171     output_file = new FileVMatrix(output_path + ".pmat", main_length, output_names);
00172     output_file->defineSizes(output_width, 0, 0);
00173     
00174     //Now, we can group the input and compute the class target
00175     ProgressBar* pb = 0;
00176     pb = new ProgressBar( "Computing target classes", main_length);
00177     for (int main_row = 0; main_row < main_length; main_row++)
00178     {
00179         train_set->getRow(main_row, main_input);
00180         for (int ins_col = 0; ins_col < ins_width; ins_col++)
00181         {
00182             output_vec[ins_col] = main_input[output_variable_src[ins_col]];
00183         }
00184         if (is_missing(main_input[predicted_sales])) main_input[predicted_sales] = 0.0;
00185         commitment = 0.0;
00186         if (!is_missing(main_input[margin])) commitment += main_input[margin];
00187         if (!is_missing(main_input[loan])) commitment += main_input[loan];
00188         if (main_input[predicted_sales] < 1000000.0 && commitment < 200000.0) output_vec[predicted_class] = 1.0;
00189         else if (main_input[predicted_sales] < 10000000.0 && commitment < 1000000.0) output_vec[predicted_class] = 2.0;
00190         else if (main_input[predicted_sales] < 100000000.0 && commitment < 20000000.0) output_vec[predicted_class] = 3.0;
00191         else output_vec[predicted_class] = 4.0;
00192         if (is_missing(main_input[target_sales]) && unknown_sales == 0)
00193             PLERROR("In ComputeDond2Target: no target information for record: %i", main_row);
00194         commitment = 0.0;
00195         if (!is_missing(main_input[margin])) commitment += main_input[margin];
00196         if (!is_missing(main_input[loan])) commitment += main_input[loan];
00197         if (is_missing(main_input[target_sales]))  output_vec[target_class] = main_input[target_sales];
00198         else if (main_input[target_sales] < 1000000.0 && commitment < 200000.0) output_vec[target_class] = 1.0;
00199         else if (main_input[target_sales] < 10000000.0 && commitment < 1000000.0) output_vec[target_class] = 2.0;
00200         else if (main_input[target_sales] < 100000000.0 && commitment < 20000000.0) output_vec[target_class] = 3.0;
00201         else output_vec[target_class] = 4.0;
00202         output_file->putRow(main_row, output_vec);
00203         pb->update( main_row );
00204     }
00205     delete pb;
00206 }
00207 
00208 VMat ComputeDond2Target::getOutputFile()
00209 {
00210     return output_file;
00211 }
00212 
00213 int ComputeDond2Target::outputsize() const {return 0;}
00214 void ComputeDond2Target::train()
00215 {
00216     PLERROR("ComputeDond2Target: we are done here");
00217 }
00218 void ComputeDond2Target::computeOutput(const Vec&, Vec&) const {}
00219 void ComputeDond2Target::computeCostsFromOutputs(const Vec&, const Vec&, const Vec&, Vec&) const {}
00220 TVec<string> ComputeDond2Target::getTestCostNames() const
00221 {
00222     TVec<string> result;
00223     result.append( "MSE" );
00224     return result;
00225 }
00226 TVec<string> ComputeDond2Target::getTrainCostNames() const
00227 {
00228     TVec<string> result;
00229     result.append( "MSE" );
00230     return result;
00231 }
00232 
00233 } // end of namespace PLearn
00234 
00235 
00236 /*
00237   Local Variables:
00238   mode:c++
00239   c-basic-offset:4
00240   c-file-style:"stroustrup"
00241   c-file-offsets:((innamespace . 0)(inline-open . 0))
00242   indent-tabs-mode:nil
00243   fill-column:79
00244   End:
00245 */
00246 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines