aGrUM  0.14.2
median_tpl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Pierre-Henri WUILLEMIN et Christophe GONZALES *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program 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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
27 #include <sstream>
28 
29 #include <vector>
30 
31 // to ease parser in IDEs
34 
35 namespace gum {
36 
37  namespace aggregator {
38  template < typename GUM_SCALAR >
39  INLINE Median< GUM_SCALAR >::Median() : MultiDimAggregator< GUM_SCALAR >() {
40  GUM_CONSTRUCTOR(Median);
41  }
42 
43  template < typename GUM_SCALAR >
45  MultiDimAggregator< GUM_SCALAR >(from) {
46  GUM_CONS_CPY(Median);
47  }
48 
49  template < typename GUM_SCALAR >
51  GUM_DESTRUCTOR(Median);
52  }
53 
54  template < typename GUM_SCALAR >
55  INLINE std::string Median< GUM_SCALAR >::aggregatorName() const {
56  std::stringstream ss;
57  ss << "median";
58  return ss.str();
59  }
60 
61  template < typename GUM_SCALAR >
64  return new Median< GUM_SCALAR >();
65  }
66 
67  template < typename GUM_SCALAR >
69  if (i.nbrDim() < 2)
70  return i.nbrDim() / 2; // arbitrary. Guess = (max-min)/2 .
71 
72  // we assume that every (parent) variable has the same domainSize
73  Idx maxVal = i.variable(1).domainSize();
74 
75  std::vector< Idx > cum(maxVal, 0);
76 
77  for (Idx j = 1; j < this->nbrDim(); j++)
78  cum[i.val(j)]++;
79 
80  Idx half = (this->nbrDim() + 1) / 2; // 50% of the population
81 
82  Idx max = maxVal;
83  for (Idx j = 0, sumcum = 0; j < maxVal; j++)
84  if ((sumcum += cum[j]) >= half) {
85  max = j;
86  break;
87  }
88  Idx maxR = maxVal;
89  for (Idx j = maxVal - 1, jj = 0, sumcum = 0; jj < maxVal; jj++, j--)
90  if ((sumcum += cum[j]) >= half) {
91  maxR = j;
92  break;
93  }
94 
95  // multiDimAggregator::get will truncate if needed.
96  return (maxR + max) / 2;
97  }
98 
99  template < typename GUM_SCALAR >
101  Idx i1,
102  Idx i2,
103  bool& stop_iteration) const {
104  return 0;
105  }
106 
107  } // namespace aggregator
108 } // namespace gum
virtual MultiDimContainer< GUM_SCALAR > * newFactory() const
This method creates a clone of this object, withouth its content (including variable), you must use this method if you want to ensure that the generated object has the same type than the object containing the called newFactory()
Definition: median_tpl.h:63
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Base class for discrete random variable.
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
Abstract base class for all multi dimensionnal containers.
<agrum/multidim/aggregators/multiDimAggregator.h>
Idx val(Idx i) const
Returns the current value of the variable at position i.
virtual std::string aggregatorName() const
Definition: median_tpl.h:55
virtual Size domainSize() const =0
virtual Idx _fold(const DiscreteVariable &v, Idx i1, Idx i2, bool &stop_iteration) const
_fold is applied on value i1 for variable v. the actual result for precedent applications is i2...
Definition: median_tpl.h:100
MultiDimAggregator.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
Definition: instantiation.h:80
virtual Idx _buildValue(const gum::Instantiation &i) const
by default, _buildValue uses a "fold" scheme and the user has to implement _neutralElt and _fold but ...
Definition: median_tpl.h:68
median aggregator
Size Idx
Type for indexes.
Definition: types.h:50
median aggregator
Definition: median.h:57
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.