aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
median_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright 2005-2020 Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief median aggregator
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
27  */
28 
29 #include <sstream>
30 
31 #include <vector>
32 
33 // to ease parser in IDEs
34 #include <agrum/tools/multidim/aggregators/median.h>
35 #include <agrum/tools/multidim/aggregators/multiDimAggregator.h>
36 
37 namespace gum {
38 
39  namespace aggregator {
40  template < typename GUM_SCALAR >
41  INLINE Median< GUM_SCALAR >::Median() : MultiDimAggregator< GUM_SCALAR >() {
42  GUM_CONSTRUCTOR(Median);
43  }
44 
45  template < typename GUM_SCALAR >
46  INLINE Median< GUM_SCALAR >::Median(const Median< GUM_SCALAR >& from) :
47  MultiDimAggregator< GUM_SCALAR >(from) {
48  GUM_CONS_CPY(Median);
49  }
50 
51  template < typename GUM_SCALAR >
52  INLINE Median< GUM_SCALAR >::~Median() {
53  GUM_DESTRUCTOR(Median);
54  }
55 
56  template < typename GUM_SCALAR >
57  INLINE std::string Median< GUM_SCALAR >::aggregatorName() const {
58  std::stringstream ss;
59  ss << "median";
60  return ss.str();
61  }
62 
63  template < typename GUM_SCALAR >
64  INLINE MultiDimContainer< GUM_SCALAR >*
65  Median< GUM_SCALAR >::newFactory() const {
66  return new Median< GUM_SCALAR >();
67  }
68 
69  template < typename GUM_SCALAR >
70  Idx Median< GUM_SCALAR >::buildValue_(const gum::Instantiation& i) const {
71  if (i.nbrDim() < 2)
72  return i.nbrDim() / 2; // arbitrary. Guess = (max-min)/2 .
73 
74  // we assume that every (parent) variable has the same domainSize
75  Idx maxVal = i.variable(1).domainSize();
76 
77  std::vector< Idx > cum(maxVal, 0);
78 
79  for (Idx j = 1; j < this->nbrDim(); j++)
80  cum[i.val(j)]++;
81 
82  Idx half = (this->nbrDim() + 1) / 2; // 50% of the population
83 
84  Idx max = maxVal;
85  for (Idx j = 0, sumcum = 0; j < maxVal; j++)
86  if ((sumcum += cum[j]) >= half) {
87  max = j;
88  break;
89  }
90  Idx maxR = maxVal;
91  for (Idx j = maxVal - 1, jj = 0, sumcum = 0; jj < maxVal; jj++, j--)
92  if ((sumcum += cum[j]) >= half) {
93  maxR = j;
94  break;
95  }
96 
97  // multiDimAggregator::get will truncate if needed.
98  return (maxR + max) / 2;
99  }
100 
101  template < typename GUM_SCALAR >
102  INLINE Idx Median< GUM_SCALAR >::fold_(const DiscreteVariable& v,
103  Idx i1,
104  Idx i2,
105  bool& stop_iteration) const {
106  return 0;
107  }
108 
109  } // namespace aggregator
110 } // namespace gum