PLearn 0.1
AnalyzeDond2DiscreteVariables.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // AnalyzeDond2DiscreteVariables.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 
00040 #include "AnalyzeDond2DiscreteVariables.h"
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00045 PLEARN_IMPLEMENT_OBJECT(
00046     AnalyzeDond2DiscreteVariables,
00047     "Computes correlation coefficient between various discrete values and the target.",
00048     "name of the discrete variable, of the target and the values to check are options.\n"
00049 );
00050 
00052 // AnalyzeDond2DiscreteVariables //
00054 AnalyzeDond2DiscreteVariables::AnalyzeDond2DiscreteVariables()
00055 {
00056 }
00057     
00059 // declareOptions //
00061 void AnalyzeDond2DiscreteVariables::declareOptions(OptionList& ol)
00062 {
00063 
00064     declareOption(ol, "variable_name", &AnalyzeDond2DiscreteVariables::variable_name,
00065                   OptionBase::buildoption,
00066                   "The field name of the variable to be analyzed.");
00067 
00068     declareOption(ol, "target_name", &AnalyzeDond2DiscreteVariables::target_name,
00069                   OptionBase::buildoption,
00070                   "The field name of the target.");
00071 
00072     declareOption(ol, "values_to_analyze", &AnalyzeDond2DiscreteVariables::values_to_analyze,
00073                   OptionBase::buildoption,
00074                   "The vector of values to check the correlation with the target.\n"
00075                   "The algorithm groups the values from, to of each pair specified.\n");
00076 
00077     inherited::declareOptions(ol);
00078 }
00079 
00081 // makeDeepCopyFromShallowCopy //
00083 void AnalyzeDond2DiscreteVariables::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00084 {
00085     deepCopyField(values_to_analyze, copies);
00086     deepCopyField(variable_name, copies);
00087     deepCopyField(target_name, copies);
00088     inherited::makeDeepCopyFromShallowCopy(copies);
00089 
00090 }
00091 
00093 // build //
00095 void AnalyzeDond2DiscreteVariables::build()
00096 {
00097     // ### Nothing to add here, simply calls build_().
00098     inherited::build();
00099     build_();
00100 }
00101 
00103 // build_ //
00105 void AnalyzeDond2DiscreteVariables::build_()
00106 {
00107     if (train_set)
00108     {
00109         analyzeDiscreteVariable();
00110         PLERROR("AnalyzeDond2DiscreteVariables: we are done here");
00111     }
00112 }
00113 
00114 void AnalyzeDond2DiscreteVariables::analyzeDiscreteVariable()
00115 {    
00116     // initialize primary dataset
00117     int main_length = train_set->length();
00118     int main_width = train_set->width();
00119     Vec main_input(main_width);
00120     TVec<string> main_names(main_width);
00121     main_names << train_set->fieldNames();
00122     
00123     // check for valid options
00124     int number_of_values = values_to_analyze.size();
00125     int variable_col = -1;
00126     int target_col = -1;
00127     for (int main_col = 0; main_col < main_width; main_col++)
00128     {
00129         if (variable_name == main_names[main_col]) variable_col = main_col;
00130         if (target_name == main_names[main_col]) target_col = main_col;
00131     }
00132     if (variable_col < 0) PLERROR("In AnalyzeDond2DiscreteVariables: variable name not found: %s", variable_name.c_str());
00133     if (target_col < 0) PLERROR("In AnalyzeDond2DiscreteVariables: target name not found: %s", target_name.c_str());
00134     if (number_of_values <= 0) PLERROR("In AnalyzeDond2DiscreteVariables: invalid values_to_analyze");
00135     
00136     // initialize working variables
00137     Vec value_target_sum(number_of_values);
00138     Vec value_present_count(number_of_values);
00139     value_target_sum.clear();
00140     value_present_count.clear();
00141     real target_sum = 0.0;
00142     real target_squared_sum = 0.0;
00143     real variable_present_count = 0.0;
00144     
00145     //Now, we can process the discrete variable.
00146     ProgressBar* pb = 0;
00147     pb = new ProgressBar( "Analyzing discrete variable " + variable_name, main_length);
00148     for (int main_row = 0; main_row < main_length; main_row++)
00149     {
00150         train_set->getRow(main_row, main_input);
00151         real variable_value = main_input[variable_col];
00152         if (is_missing(variable_value)) continue;
00153         real target_value = main_input[target_col];
00154         target_sum += target_value;
00155         target_squared_sum += target_value * target_value;
00156         variable_present_count += 1.0;
00157         for (int value_col = 0; value_col < number_of_values; value_col++)
00158         {
00159             if (variable_value < values_to_analyze[value_col].first || variable_value > values_to_analyze[value_col].second) continue;
00160             value_target_sum[value_col] += target_value;
00161             value_present_count[value_col] += 1.0;
00162         }
00163         pb->update( main_row );
00164     }
00165     delete pb;
00166     if (variable_present_count <= 0.0)
00167     {
00168         cout << "In AnalyzeDond2DiscreteVariables: no value present for this variable" << endl;
00169         return;
00170     }
00171     real target_mean = target_sum / variable_present_count;
00172     cout << "In AnalyzeDond2DiscreteVariables, for variable:  " << variable_name << endl;
00173     cout << variable_present_count << " values are present out of " << main_length << " samples." << endl;
00174     for (int value_col = 0; value_col < number_of_values; value_col++)
00175     {
00176         real ssxy = value_target_sum[value_col] - value_present_count[value_col] * target_mean;
00177         real ss2xy = ssxy * ssxy;
00178         real ssxx = value_present_count[value_col] * (1.0 -  value_present_count[value_col] / variable_present_count);
00179         real ssyy = target_squared_sum - target_sum * target_mean;
00180         real correlation_coefficient = ss2xy / (ssxx * ssyy);
00181         cout << "For value from: " << values_to_analyze[value_col].first << " to: " << values_to_analyze[value_col].second 
00182              << " occurence: " << value_present_count[value_col] << " correlation coefficient: " << correlation_coefficient << endl;
00183     }
00184 }
00185 
00186 int AnalyzeDond2DiscreteVariables::outputsize() const {return 0;}
00187 void AnalyzeDond2DiscreteVariables::train() {}
00188 void AnalyzeDond2DiscreteVariables::computeOutput(const Vec&, Vec&) const {}
00189 void AnalyzeDond2DiscreteVariables::computeCostsFromOutputs(const Vec&, const Vec&, const Vec&, Vec&) const {}
00190 TVec<string> AnalyzeDond2DiscreteVariables::getTestCostNames() const
00191 {
00192     TVec<string> result;
00193     result.append( "MSE" );
00194     return result;
00195 }
00196 TVec<string> AnalyzeDond2DiscreteVariables::getTrainCostNames() const
00197 {
00198     TVec<string> result;
00199     result.append( "MSE" );
00200     return result;
00201 }
00202 
00203 } // end of namespace PLearn
00204 
00205 
00206 /*
00207   Local Variables:
00208   mode:c++
00209   c-basic-offset:4
00210   c-file-style:"stroustrup"
00211   c-file-offsets:((innamespace . 0)(inline-open . 0))
00212   indent-tabs-mode:nil
00213   fill-column:79
00214   End:
00215 */
00216 // 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