TargetInfo.cpp

00001 /*          
00002  *             Copyright (C) 2005 Maarten Keijzer
00003  *
00004  *          This program is free software; you can redistribute it and/or modify
00005  *          it under the terms of version 2 of the GNU General Public License as 
00006  *          published by the Free Software Foundation. 
00007  *
00008  *          This program is distributed in the hope that it will be useful,
00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *          GNU General Public License for more details.
00012  *
00013  *          You should have received a copy of the GNU General Public License
00014  *          along with this program; if not, write to the Free Software
00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00016  */
00017 
00018 #include "TargetInfo.h"
00019 
00020 using namespace std;
00021 
00022 TargetInfo::TargetInfo(const TargetInfo& org) { operator=(org); }
00023 
00024 TargetInfo& TargetInfo::operator=(const TargetInfo& org) {
00025     _targets.resize(org._targets.size());
00026     _weights.resize(org._weights.size());
00027     _tcov_part.resize(org._tcov_part.size());
00028     
00029     _targets = org._targets;
00030     _weights = org._weights;
00031     _tcov_part = org._tcov_part;
00032 
00033     _tmean = org._tmean;
00034     _tvar  = org._tvar;
00035     _tstd  = org._tstd;
00036     _tmed = org._tmed;
00037     return *this;
00038 }
00039       
00040 
00041 TargetInfo::TargetInfo(const std::valarray<double>& t) {
00042     _weights.resize(0);
00043     _targets.resize(t.size());
00044     _targets = t;
00045     
00046     _tmean = _targets.sum()/_targets.size();
00047     
00048     _tcov_part.resize(_targets.size());
00049     _tcov_part = _targets;
00050     _tcov_part -= _tmean;
00051         
00052     std::valarray<double> tmp = _tcov_part;
00053     tmp = _tcov_part;
00054     tmp *= tmp;
00055         
00056     _tvar = tmp.sum() / (tmp.size()-1);
00057     _tstd = sqrt(_tvar);
00058     _tmed = 0;
00059 }
00060 
00061 TargetInfo::TargetInfo(const std::valarray<double>& t, const std::valarray<double>& w) {
00062 
00063     _targets.resize(t.size());
00064     _weights.resize(w.size());
00065 
00066     _targets = t;
00067     _weights = w;
00068     
00069     double sumw = _weights.sum();
00070         // scale weights so that they'll add up to 1
00071     _weights /= sumw;
00072         
00073     _tmean = (_targets * _weights).sum();
00074     _tcov_part.resize(_targets.size());
00075     _tcov_part = _targets;
00076     _tcov_part -= _tmean;
00077 
00078     _tvar = (pow(_targets - _tmean, 2.0) * _weights).sum();
00079     _tstd = sqrt(_tvar);
00080     _tmed = 0.;
00081 }
00082 
00083 // calculate the members, now in the context of a mask
00084 void TargetInfo::set_training_mask(const std::valarray<bool>& tmask) {
00085     
00086     TargetInfo tmp;
00087     
00088     if (has_weights() ) {
00089         tmp = TargetInfo( _targets[tmask], _weights[tmask]);
00090     } else {
00091         tmp = TargetInfo( _targets[tmask] );
00092     }
00093     
00094     _tcov_part.resize(tmp._tcov_part.size());
00095     _tcov_part = tmp._tcov_part;
00096 
00097     _tmean = tmp._tmean;
00098     _tvar  = tmp._tvar;
00099     _tstd  = tmp._tstd;
00100     _tmed =  tmp._tmed;
00101 
00102     _training_mask.resize(tmask.size());
00103     _training_mask = tmask;
00104 }
00105 
00106 struct SortOnTargets
00107 {
00108     const valarray<double>& t;
00109     SortOnTargets(const valarray<double>& v) : t(v) {}
00110 
00111     bool operator()(int i, int j) const {
00112         return fabs(t[i]) < fabs(t[j]);
00113     }
00114 };
00115     
00116 vector<int> TargetInfo::sort() {
00117     
00118     vector<int> ind(_targets.size());
00119     for (unsigned i = 0; i < ind.size(); ++i) { ind[i] = i; }
00120 
00121     std::sort(ind.begin(), ind.end(), SortOnTargets(_targets));
00122 
00123     valarray<double> tmptargets = _targets;
00124     valarray<double> tmpweights = _weights;
00125     valarray<double> tmpcov     = _tcov_part;
00126     
00127     for (unsigned i = 0; i < ind.size(); ++i) 
00128     {
00129         _targets[i] = tmptargets[ ind[i] ];
00130         _tcov_part[i] = tmpcov[ ind[i] ];       
00131         if (_weights.size()) _weights[i] = tmpweights[ ind[i] ];
00132     }
00133 
00134     return ind;
00135 }
00136 
00137 
00138 

Generated on Thu Oct 19 05:06:44 2006 for EO by  doxygen 1.3.9.1