KullbackLeiberDivergence.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief Provides a function for estimating the kullback-leibler-divergence.
5  *
6  *
7  *
8  * \author O. Krause
9  * \date 2010-01-01
10  *
11  *
12  * \par Copyright 1995-2017 Shark Development Team
13  *
14  * <BR><HR>
15  * This file is part of Shark.
16  * <http://shark-ml.org/>
17  *
18  * Shark is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU Lesser General Public License as published
20  * by the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * Shark is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU Lesser General Public License for more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License
29  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
30  *
31  */
32 #ifndef SHARK_RNG_KULLBACK_LEIBER_DIVERGENCE_H
33 #define SHARK_RNG_KULLBACK_LEIBER_DIVERGENCE_H
34 
35 #include <shark/Core/Exception.h>
36 
37 namespace shark {
38 
39  /**
40  * \brief Estimates the kullback-leibler-divergence between two distributions. The more trials the better is the estimate, but good estimates are slow.
41  * \tparam DistributionP The type of distribution, needs to provide operator()() for sampling purposes.
42  * \tparam DistributionQ The type of distribution, needs to provide operator()() for sampling purposes.
43  * \param [in,out] p Distribution instance to sample from.
44  * \param [in,out] q Distribution instance to sample from.
45  * \param [in] trials The number of samples drawn from the distribution. Needs to be larger than 0.
46  * \throws shark::Exception if trials == 0.
47  */
48  template<typename DistributionP, typename DistributionQ>
49  double kullback_leiber_divergence( DistributionP & p, DistributionQ & q, std::size_t trials = 10000 ) {
50  if( trials == 0 )
51  throw( shark::Exception( "kullback_leiber_divergence: Trial count needs to be larger than 0.", __FILE__, __LINE__ ) );
52 
53  double t( 0 );
54  double x;
55  for ( unsigned int i = 0; i < trials; i++ ) {
56  x = p();
57  t += ::log( p.p( x ) / q.p( x ) );
58  }
59  return( t / trials );
60  }
61 }
62 
63 #endif