X-Boost  2.3.8
BinaryClassifierOptimize.h
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _BINARY_CLASSIFIER_OPTIMIZER_H
22 #define _BINARY_CLASSIFIER_OPTIMIZER_H
23 
31 template<class DataType, class ScalarType>
32 ScalarType Optimize(std::pair<DataType, int> & ret, const std::pair<DataType, ScalarType> *data, int n, ScalarType wp, ScalarType wn, bool strictly_growin)
33 {
34  ScalarType w, curW; // current best w
35  ScalarType sumW = wp + wn;
36 
37  // initial "bestW" using external border (all NEGs)
38  ret.first = data[0].first; // th is the global minimum value
39  ret.second = 1; // v < th ? 1 : -1
40  curW = wn;
41 
42  if(strictly_growin)
43  {
44  w = wn;
45  for(int i=0; i<n-1; i++)
46  {
47  w += data[i].second; // se prendo un POS alzo w, se prendo un NEG abbasso w
48  if(w>curW) // parity +1
49  {
50  curW = w;
51  ret.first = data[i+1].first;
52  ret.second = 1;
53  }
54  if((sumW-w)>curW) // parity -1
55  {
56  curW = sumW-w;
57  ret.first = data[i].first;
58  ret.second = -1;
59  }
60  }
61  }
62  else
63  {
64  w = wn;
65  for(int i=1; i<n; i++)
66  {
67  w += data[i-1].second; // se prendo un POS alzo w, se prendo un NEG abbasso w
68  // (boundary check) se e' cresicuto value, solo ora faccio il check
69  if(data[i].first>data[i-1].first)
70  {
71  // 1) search the maximum of W in the case v < th
72  if(w>curW)
73  {
74  curW = w;
75  ret.first = data[i].first;
76  ret.second = 1;
77  }
78  // 2) opposite
79  if((sumW-w)>curW)
80  {
81  curW = sumW-w;
82  ret.first = data[i-1].first;
83  ret.second = -1;
84  }
85  }
86  }
87  }
88 
89  return curW; // best w found
90 }
91 
94 template<class ScalarType>
95 ScalarType SimpleOptimize(std::pair<int, int> & ret, const ScalarType *data, int n, ScalarType wp, ScalarType wn)
96 {
97  ScalarType w, curW; // current best w
98  ScalarType sumW = wp + wn;
99 
100  // initial "bestW" using external border (all NEGs)
101  ret.first = 0; // th is the global minimum value
102  ret.second = 1; // v < th ? 1 : -1
103  curW = wn;
104 
105  w = wn;
106  for(int i=0; i<n-1; i++)
107  {
108  w += data[i]; // se prendo un POS alzo w, se prendo un NEG abbasso w
109  if(w>curW) // parity +1
110  {
111  curW = w;
112  ret.first = i+1;
113  ret.second = 1;
114  }
115  if((sumW-w)>curW) // parity -1
116  {
117  curW = sumW-w;
118  ret.first = i;
119  ret.second = -1;
120  }
121  }
122  return curW; // best w found
123 }
124 
125 #endif