KLDivergence.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  QM DSP Library
5 
6  Centre for Digital Music, Queen Mary, University of London.
7  This file copyright 2008 QMUL
8 
9  This program is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 2 of the
12  License, or (at your option) any later version. See the file
13  COPYING included with this distribution for more information.
14 */
15 
16 #include "KLDivergence.h"
17 
18 #include <cmath>
19 
20 using std::vector;
21 
22 double KLDivergence::distanceGaussian(const vector<double> &m1,
23  const vector<double> &v1,
24  const vector<double> &m2,
25  const vector<double> &v2)
26 {
27  int sz = m1.size();
28 
29  double d = -2.0 * sz;
30  double small = 1e-20;
31 
32  for (int k = 0; k < sz; ++k) {
33 
34  double kv1 = v1[k] + small;
35  double kv2 = v2[k] + small;
36  double km = (m1[k] - m2[k]) + small;
37 
38  d += kv1 / kv2 + kv2 / kv1;
39  d += km * (1.0 / kv1 + 1.0 / kv2) * km;
40  }
41 
42  d /= 2.0;
43 
44  return d;
45 }
46 
47 double KLDivergence::distanceDistribution(const vector<double> &d1,
48  const vector<double> &d2,
49  bool symmetrised)
50 {
51  int sz = d1.size();
52 
53  double d = 0;
54  double small = 1e-20;
55 
56  for (int i = 0; i < sz; ++i) {
57  d += d1[i] * log10((d1[i] + small) / (d2[i] + small));
58  }
59 
60  if (symmetrised) {
61  d += distanceDistribution(d2, d1, false);
62  }
63 
64  return d;
65 }
66 
double distanceDistribution(const std::vector< double > &d1, const std::vector< double > &d2, bool symmetrised)
Calculate a Kullback-Leibler divergence of two probability distributions.
double distanceGaussian(const std::vector< double > &means1, const std::vector< double > &variances1, const std::vector< double > &means2, const std::vector< double > &variances2)
Calculate a symmetrised Kullback-Leibler divergence of Gaussian models based on mean and variance vec...