aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
exactBNdistance.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 algorithm for exact computation KL divergence between BNs
25  *
26  * @author Pierre-Henri WUILLEMIN(@LIP6)
27  *
28  */
29 #ifndef GUM_BRUTE_FORCE_KL_H
30 #define GUM_BRUTE_FORCE_KL_H
31 
32 #include <agrum/BN/algorithms/divergence/BNdistance.h>
33 
34 namespace gum {
35 
36  /**
37  * ExactBNdistance computes exactly the KL divergence betweens 2 BNs.
38  *
39  * ExactBNdistance should be used only if difficulty() gives an estimation (
40  *KL_CORRECT
41  *) of the needed time.
42  * KL.process() computes KL(P||Q) using klPQ() and KL(Q||P) using klQP(). The
43  *computations are made once. The second is for free :)
44  * ExactBNdistance allows as well to compute in the same time the Hellinger
45  *distance
46  *(\f$
47  *\sqrt{\sum_i (\sqrt{p_i}-\sqrt{q_i})^2}\f$) (Kokolakis and Nanopoulos, 2001).
48  *
49  * It may happen that P*ln(P/Q) is not computable (Q=0 and P!=0). In such a
50  *case, KL
51  *keeps working but trace this error (errorPQ() and errorQP())? *
52  *
53  * @warning This ExactBNdistance should be use only if
54  *difficulty()==complexity::CORRECT or at most complexity::DIFFICULT ...
55  * snippets :
56  * @code
57  * gum::KL base_kl(net1,net2);
58  * if (base_kl.difficulty()!=KL::HEAVY) {
59  * gum::ExactBNdistance kl(base_kl);
60  * std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
61  * } else {
62  * gum::GibbsKL kl(base_kl);
63  * std::cout<<"KL net1||net2 :"<<kl.klPQ()<<std::endl;
64  * }
65  * @endcode
66  */
67 
68  template < typename GUM_SCALAR >
70  public:
71  /** constructor must give 2 BNs
72  * @throw gum::OperationNotAllowed if the 2 BNs have not the same domainSize
73  * or
74  * compatible node sets.
75  */
76  ExactBNdistance(const IBayesNet< GUM_SCALAR >& P,
77  const IBayesNet< GUM_SCALAR >& Q);
78 
79  /** copy constructor
80  */
81  explicit ExactBNdistance(const BNdistance< GUM_SCALAR >& kl);
82 
83  /** destructor */
84  virtual ~ExactBNdistance();
85 
86  protected:
87  void computeKL_() final;
88 
89  using BNdistance< GUM_SCALAR >::p_;
90  using BNdistance< GUM_SCALAR >::q_;
93  using BNdistance< GUM_SCALAR >::jsd_;
94 
95  using BNdistance< GUM_SCALAR >::klPQ_;
96  using BNdistance< GUM_SCALAR >::klQP_;
97 
98  using BNdistance< GUM_SCALAR >::errorPQ_;
99  using BNdistance< GUM_SCALAR >::errorQP_;
100  };
101 
102 #ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
103  extern template class ExactBNdistance< double >;
104 #endif
105 
106 } // namespace gum
107 
108 #include <agrum/BN/algorithms/divergence/exactBNdistance_tpl.h>
109 
110 #endif // GUM_BRUTE_FORCE_KL_H
virtual ~ExactBNdistance()
destructor
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:669
ExactBNdistance computes exactly the KL divergence betweens 2 BNs.
ExactBNdistance(const IBayesNet< GUM_SCALAR > &P, const IBayesNet< GUM_SCALAR > &Q)
constructor must give 2 BNs
ExactBNdistance(const BNdistance< GUM_SCALAR > &kl)
copy constructor