aGrUM  0.16.0
median_tpl.h
Go to the documentation of this file.
1 
30 #include <sstream>
31 
32 #include <vector>
33 
34 // to ease parser in IDEs
37 
38 namespace gum {
39 
40  namespace aggregator {
41  template < typename GUM_SCALAR >
42  INLINE Median< GUM_SCALAR >::Median() : MultiDimAggregator< GUM_SCALAR >() {
43  GUM_CONSTRUCTOR(Median);
44  }
45 
46  template < typename GUM_SCALAR >
48  MultiDimAggregator< GUM_SCALAR >(from) {
49  GUM_CONS_CPY(Median);
50  }
51 
52  template < typename GUM_SCALAR >
54  GUM_DESTRUCTOR(Median);
55  }
56 
57  template < typename GUM_SCALAR >
58  INLINE std::string Median< GUM_SCALAR >::aggregatorName() const {
59  std::stringstream ss;
60  ss << "median";
61  return ss.str();
62  }
63 
64  template < typename GUM_SCALAR >
67  return new Median< GUM_SCALAR >();
68  }
69 
70  template < typename GUM_SCALAR >
72  if (i.nbrDim() < 2)
73  return i.nbrDim() / 2; // arbitrary. Guess = (max-min)/2 .
74 
75  // we assume that every (parent) variable has the same domainSize
76  Idx maxVal = i.variable(1).domainSize();
77 
78  std::vector< Idx > cum(maxVal, 0);
79 
80  for (Idx j = 1; j < this->nbrDim(); j++)
81  cum[i.val(j)]++;
82 
83  Idx half = (this->nbrDim() + 1) / 2; // 50% of the population
84 
85  Idx max = maxVal;
86  for (Idx j = 0, sumcum = 0; j < maxVal; j++)
87  if ((sumcum += cum[j]) >= half) {
88  max = j;
89  break;
90  }
91  Idx maxR = maxVal;
92  for (Idx j = maxVal - 1, jj = 0, sumcum = 0; jj < maxVal; jj++, j--)
93  if ((sumcum += cum[j]) >= half) {
94  maxR = j;
95  break;
96  }
97 
98  // multiDimAggregator::get will truncate if needed.
99  return (maxR + max) / 2;
100  }
101 
102  template < typename GUM_SCALAR >
104  Idx i1,
105  Idx i2,
106  bool& stop_iteration) const {
107  return 0;
108  }
109 
110  } // namespace aggregator
111 } // 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:66
Idx nbrDim() const final
Returns the number of variables in the Instantiation.
Base class for discrete random variable.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:58
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:103
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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:83
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:71
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Size Idx
Type for indexes.
Definition: types.h:53
median aggregator
Definition: median.h:60
const DiscreteVariable & variable(Idx i) const final
Returns the variable at position i in the tuple.