aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
median_tpl.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by 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 >* Median< GUM_SCALAR >::newFactory() const {
65  return new Median< GUM_SCALAR >();
66  }
67 
68  template < typename GUM_SCALAR >
69  Idx Median< GUM_SCALAR >::buildValue_(const gum::Instantiation& i) const {
70  if (i.nbrDim() < 2) 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 >
100  INLINE Idx Median< GUM_SCALAR >::fold_(const DiscreteVariable& v,
101  Idx i1,
102  Idx i2,
103  bool& stop_iteration) const {
104  return 0;
105  }
106 
107  } // namespace aggregator
108 } // namespace gum