aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
GibbsBNdistance.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 algorithm for approximated computation KL divergence between BNs using
25  *GIBBS
26  *sampling
27  *
28  * @author Paul ALAM & Pierre-Henri WUILLEMIN(@LIP6)
29  *
30  */
31 
32 
33 #ifndef GUM_GIBBS_KL2_H
34 #define GUM_GIBBS_KL2_H
35 
36 #include <agrum/BN/algorithms/divergence/BNdistance.h>
37 #include <agrum/BN/inference/tools/gibbsOperator.h>
38 #include <agrum/tools/core/approximations/approximationScheme.h>
39 
40 #include <agrum/tools/core/signal/signaler.h>
41 
42 namespace gum {
43 
44  /**
45  * GibbsKL computes the KL divergence betweens 2 BNs using an approximation
46  *pattern: GIBBS sampling.
47  *
48  * KL.process() computes KL(P||Q) using klPQ() and KL(Q||P) using klQP(). The
49  *computations are made once. The second is for free :)
50  * GibbsKL allows as well to compute in the same time the Hellinger distance
51  *(\f$
52  *\sqrt{\sum_i (\sqrt{p_i}-\sqrt{q_i})^2}\f$) (Kokolakis and Nanopoulos, 2001)
53  * and Bhattacharya distance (Kaylath,T. 1967)
54  *
55  * It may happen that P*ln(P/Q) is not computable (Q=0 and P!=0). In such a
56  *case, KL
57  *keeps working but trace this error (errorPQ() and errorQP()). In those cases,
58  *Hellinger distance approximation is under-evaluated.
59  *
60  * @warning : convergence and stop criteria are designed w.r.t the main
61  *computation
62  *: KL(P||Q). The 3 others have no guarantee.
63  *
64  * snippets :
65  * @code
66  * gum::KL base_kl(net1,net2);
67  * if (base_kl.difficulty()!=KL::HEAVY) {
68  * gum::ExactBNdistance kl(base_kl);
69  * std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
70  * } else {
71  * gum::GibbsBNdistance kl(base_kl);
72  * std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
73  * }
74  * @endcode
75  */
76 
77  template < typename GUM_SCALAR >
79  public BNdistance< GUM_SCALAR >,
80  public ApproximationScheme,
81  public GibbsOperator< GUM_SCALAR > {
82  public:
83  /* no default constructor */
84 
85  /** constructor must give 2 BNs
86  * @throw gum::OperationNotAllowed if the 2 BNs have not the same domainSize
87  * or
88  * compatible node sets.
89  */
90 
91 
92  GibbsBNdistance(const IBayesNet< GUM_SCALAR >& P, const IBayesNet< GUM_SCALAR >& Q);
93 
94  /** copy constructor
95  */
96  explicit GibbsBNdistance(const BNdistance< GUM_SCALAR >& kl);
97 
98  /** destructor */
99  ~GibbsBNdistance();
100 
101  /**
102  * @brief Number of burn in for one iteration.
103  * @param b The number of burn in.
104  * @throw OutOfLowerBound Raised if b < 1.
105  */
106  void setBurnIn(Size b);
107 
108  /**
109  * @brief Returns the number of burn in.
110  * @return Returns the number of burn in.
111  */
112  Size burnIn() const;
113 
114  protected:
115  void computeKL_() final;
116 
117  using BNdistance< GUM_SCALAR >::p_;
118  using BNdistance< GUM_SCALAR >::q_;
119  using BNdistance< GUM_SCALAR >::hellinger_;
121  using BNdistance< GUM_SCALAR >::jsd_;
122 
123  using BNdistance< GUM_SCALAR >::klPQ_;
124  using BNdistance< GUM_SCALAR >::klQP_;
125 
126  using BNdistance< GUM_SCALAR >::errorPQ_;
127  using BNdistance< GUM_SCALAR >::errorQP_;
128  };
129 
130 
131 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
132  extern template class GibbsBNdistance< double >;
133 #endif
134 
135 } // namespace gum
136 
137 #include <agrum/BN/algorithms/divergence/GibbsKL_tpl.h>
138 
139 #endif
void computeKL_() final
Definition: GibbsKL_tpl.h:96
GibbsBNdistance(const BNdistance< GUM_SCALAR > &kl)
copy constructor
Definition: GibbsKL_tpl.h:71
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
~GibbsBNdistance()
destructor
Definition: GibbsKL_tpl.h:91
GibbsKL computes the KL divergence betweens 2 BNs using an approximation pattern: GIBBS sampling...
Size burnIn() const
Returns the number of burn in.
Definition: GibbsKL_tpl.h:184
void setBurnIn(Size b)
Number of burn in for one iteration.
Definition: GibbsKL_tpl.h:179
GibbsBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
Definition: GibbsKL_tpl.h:51