PLearn 0.1
StabilisationLearner.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // StabilisationLearner.cc
00004 //
00005 // Copyright (C) 2009 Frederic Bastien
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: Frederic Bastien
00036 
00040 #include "StabilisationLearner.h"
00041 #include <plearn/io/pl_log.h>
00042 namespace PLearn {
00043 using namespace std;
00044 
00045 PLEARN_IMPLEMENT_OBJECT(
00046     StabilisationLearner,
00047     "Stabilise the prediction to the old one if the confidence of the new one is under a threshold.",
00048     "This class is used to don't have example that ping-pong between too"
00049     " different class prediction. If the new prediction if different from"
00050     " the old one, we need to have at least the 'thresold' as *confidence*. The"
00051     " confidence is not well grouned in the theory, but is a good euristic.");
00052 
00053 StabilisationLearner::StabilisationLearner()
00054     :threshold(0)
00055 {
00056 }
00057 
00058 void StabilisationLearner::declareOptions(OptionList& ol)
00059 {
00060     declareOption(ol, "threshold", &StabilisationLearner::threshold,
00061                   OptionBase::buildoption,
00062                   "The distance needed from 0.5 to accept the change");
00063 
00064     inherited::declareOptions(ol);
00065 }
00066 
00067 void StabilisationLearner::build_()
00068 {
00069 }
00070 
00071 // ### Nothing to add here, simply calls build_
00072 void StabilisationLearner::build()
00073 {
00074     inherited::build();
00075     build_();
00076 }
00077 
00078 
00079 void StabilisationLearner::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00080 {
00081     inherited::makeDeepCopyFromShallowCopy(copies);
00082 }
00083 
00084 
00085 int StabilisationLearner::outputsize() const
00086 {
00087     return 1;
00088 }
00089 
00090 void StabilisationLearner::forget()
00091 {
00092     inherited::forget();
00093 }
00094 
00095 void StabilisationLearner::train()
00096 {
00097 }
00098 
00099 
00100 void StabilisationLearner::computeOutput(const Vec& input, Vec& output) const
00101 {
00102     real pred_=input[0];
00103     
00104     real l1=input[1];
00105     real l2=input[2];
00106     real old_=input[3];
00107     int old,pred;
00108     real ret;
00109 
00110     pred=int(pred_);
00111     old=int(old_);
00112     if(old==3)                old=2;
00113 
00114     if (is_missing(old_))       ret=pred;
00115     else if(old==pred)          ret = pred;
00116     else if(old==0 and pred==2) ret = 1;
00117     else if(old==2 and pred==0) ret = 1;
00118     else if(old==0 and pred==1)
00119         ret = (l1-threshold)>=0.5;//#(l1-0.5)>threshold
00120     else if(old==1 and pred==0)
00121         ret = (l1+threshold)>=0.5;
00122     else if(old==1 and pred==2)
00123         ret = ((l2-threshold)>=0.5)+1;
00124     else if(old==2 and pred==1)
00125         ret = ((l2+threshold)>=0.5)+1;
00126     else{
00127         ret = pred;
00128         NORMAL_LOG<< "We don't know what to do with old="<<old<<" and pred="<<pred<<endl;    
00129     }
00130     output[0]=ret;
00131 
00132 }
00133 
00134 void StabilisationLearner::computeCostsFromOutputs(const Vec& input, const Vec& output,
00135                                            const Vec& target, Vec& costs) const
00136 {
00137     costs[0]=target[0]!=output[0];
00138     real old_=int(input[3]);
00139     real old;
00140     if(old_==3) old=2;
00141     else old=old_;
00142     costs[1]=output[0]!=old;
00143 }
00144 
00145 TVec<string> StabilisationLearner::getTestCostNames() const
00146 {
00147     TVec<string> names;
00148     names.append("class_error");
00149     names.append("changed");
00150     return names;
00151 }
00152 
00153 TVec<string> StabilisationLearner::getTrainCostNames() const
00154 {
00155     TVec<string> names;
00156     return names;
00157 }
00158 
00159 TVec<string> StabilisationLearner::getOutputNames() const
00160 {
00161     TVec<string> names(1);
00162     names[0]="SALES_CATEG_STAB";
00163     return names;
00164 }
00165 
00166 } // end of namespace PLearn
00167 
00168 
00169 /*
00170   Local Variables:
00171   mode:c++
00172   c-basic-offset:4
00173   c-file-style:"stroustrup"
00174   c-file-offsets:((innamespace . 0)(inline-open . 0))
00175   indent-tabs-mode:nil
00176   fill-column:79
00177   End:
00178 */
00179 // 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