src/WeakLearners/BaseLearner.cpp

00001 /*
00002 * This file is part of MultiBoost, a multi-class 
00003 * AdaBoost learner/classifier
00004 *
00005 * Copyright (C) 2005 Norman Casagrande
00006 * For informations write to nova77@gmail.com
00007 *
00008 * This library is free software; you can redistribute it and/or
00009 * modify it under the terms of the GNU Lesser General Public
00010 * License as published by the Free Software Foundation; either
00011 * version 2.1 of the License, or (at your option) any later version.
00012 *
00013 * This library is distributed in the hope that it will be useful,
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 * Lesser General Public License for more details.
00017 *
00018 * You should have received a copy of the GNU Lesser General Public
00019 * License along with this library; if not, write to the Free Software
00020 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 *
00022 */
00023 
00024 #include <cmath> // for log
00025 
00026 #include "WeakLearners/BaseLearner.h"
00027 #include "Utils/Utils.h" // for is_zero
00028 #include "IO/Serialization.h" // for the helper function "standardTag"
00029 
00030 namespace MultiBoost {
00031 
00032 // -----------------------------------------------------------------------
00033 
00034 InputData* BaseLearner::createInputData()
00035 {
00036    return new InputData();
00037 }
00038 
00039 // -----------------------------------------------------------------------
00040 
00041 double BaseLearner::getAlpha(const double error)
00042 {
00043    return 0.5 * log( (1-error)/error );
00044 }
00045 
00046 // -----------------------------------------------------------------------
00047 
00048 double BaseLearner::getAlpha(const double eps_min, const double eps_pls)
00049 {
00050    return 0.5 * log( (eps_pls + _smoothingVal) / (eps_min + _smoothingVal) );
00051 }
00052 
00053 // -----------------------------------------------------------------------
00054 
00055 double BaseLearner::getAlpha(const double eps_min, const double eps_pls, 
00056                              const double theta)
00057 {
00058    // if theta == 0
00059    if ( nor_utils::is_zero(theta) )
00060       return getAlpha( eps_min, eps_pls );
00061 
00062    const double eps_zero = 1 - eps_min - eps_pls;
00063 
00064    if (eps_min < _smallVal)
00065    {
00066       // if eps_min == 0
00067       return log( ( (1-theta)* eps_pls ) / (theta * eps_zero) );
00068    }
00069    else
00070    {
00071       // ln( -b + sqrt( b^2 + c) );
00072       const double denom = (1+theta) * eps_min;
00073       const double b = ((theta) * eps_zero) / (2*denom);
00074       const double c = ((1-theta) * eps_pls) / denom;
00075 
00076       return log( -b + sqrt( b * b + c ) );
00077    }
00078 
00079 }
00080 
00081 // -----------------------------------------------------------------------
00082 
00083 void BaseLearner::save(ofstream& outputStream, const int numTabs)
00084 {
00085    // save alpha
00086    outputStream << Serialization::standardTag("alpha", _alpha, numTabs) << endl;
00087 }
00088 
00089 // -----------------------------------------------------------------------
00090 
00091 void BaseLearner::load(nor_utils::StreamTokenizer& st)
00092 {
00093    _alpha = UnSerialization::seekAndParseEnclosedValue<double>(st, "alpha");
00094 }
00095 
00096 // -----------------------------------------------------------------------
00097 
00098 } // end of namespace MultiBoost

Generated on Mon Nov 28 21:43:46 2005 for MultiBoost by  doxygen 1.4.5