-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntropy.H
144 lines (119 loc) · 3.56 KB
/
Entropy.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/***********************************************************************
Entropy.H
BOOM : Bioinformatics Object Oriented Modules
Copyright (C)2012 William H. Majoros ([email protected]).
This is OPEN SOURCE SOFTWARE governed by the Gnu General Public
License (GPL) version 3, as described at www.opensource.org.
***********************************************************************/
#ifndef INCL_BOOM_Entropy_H
#define INCL_BOOM_Entropy_H
#include "Vector.H"
#include <math.h>
using namespace std;
namespace BOOM {
template<class FltDbl>
class Entropy
{
public:
static FltDbl entropy(BOOM::Vector<FltDbl> &);
static FltDbl informationContent(BOOM::Vector<FltDbl> &);
static FltDbl relativeEntropy(BOOM::Vector<FltDbl> &,
BOOM::Vector<FltDbl> &);
static FltDbl crossEntropy(BOOM::Vector<FltDbl> &,
BOOM::Vector<FltDbl> &);
static FltDbl JensenShannonDivergence(BOOM::Vector<FltDbl> &,
BOOM::Vector<FltDbl> &);
};
/*
class Entropy
{
public:
static double entropy(BOOM::Vector<double> &);
static double informationContent(BOOM::Vector<double> &);
static double relativeEntropy(BOOM::Vector<double> &,
BOOM::Vector<double> &);
static double crossEntropy(BOOM::Vector<double> &,
BOOM::Vector<double> &);
};
*/
template<class FltDbl>
FltDbl BOOM::Entropy<FltDbl>::crossEntropy(BOOM::Vector<FltDbl> &P,
BOOM::Vector<FltDbl> &Q)
{
/*
cross entropy = amount of uncertainty about next symbol from data source Q
given that we are actually expecting the symbol to come
from data source P (i.e., uncertainty of Q while accounting
for our imperfect knowledge of Q's distribution).
*/
FltDbl H=0.0;
int n=P.size();
for(int i=0 ; i<n ; ++i)
{
FltDbl p=P[i], q=Q[i];
if(p>0 && q>0) H-=p*log(q);
}
return H;
}
template<class FltDbl>
FltDbl BOOM::Entropy<FltDbl>::entropy(BOOM::Vector<FltDbl> &P)
{
/*
entropy = expected number of bits needed to code data source P
(expressed as an average per symbol)
= amount of uncertainty about next symbol from data source
*/
FltDbl H=0.0;
int n=P.size();
for(int i=0 ; i<n ; ++i)
{
FltDbl p=P[i];
if(p>0) H-=p*log(p);
}
return H;
}
template<class FltDbl>
FltDbl BOOM::Entropy<FltDbl>::informationContent(BOOM::Vector<FltDbl> &V)
{
int n=V.size();
FltDbl Hmax=log(n)/log(2);
FltDbl H=entropy(V);
return (Hmax-H)/Hmax;
}
template<class FltDbl>
FltDbl BOOM::Entropy<FltDbl>::relativeEntropy(BOOM::Vector<FltDbl> &P,
BOOM::Vector<FltDbl> &Q)
{
/*
Rel. entropy = -sum(p*log p/q) = -sum(p log p) + sum(p log q)
= entropy(P) - crossEntropy(P,Q)
= uncertainty about P after discounting for cross entropy
between P and Q
*/
FltDbl H=0.0;
int n=P.size();
for(int i=0 ; i<n ; ++i)
{
FltDbl p=P[i], q=Q[i];
if(p>0 && q>0) H+=p*log(p/q);
}
return H;
}
template<class FltDbl>
FltDbl Entropy<FltDbl>::JensenShannonDivergence(BOOM::Vector<FltDbl> &P,
BOOM::Vector<FltDbl> &Q)
{
/* A true metric (symmetric, nonnegative, zero only when identical, and
obeys the triangle equality), unlike KL Divergence, which is asymmetric
and violates the triangle inequality (even in the symmetric version of
KL Divergence). See:
http://en.wikipedia.org/wiki/Kullback-Leibler_divergence
*/
int n=P.size();
BOOM::Vector<FltDbl> M(n);
for(int i=0 ; i<n ; ++i) M[i]=(P[i]+Q[i])/2.0;
FltDbl JSD=(relativeEntropy(P,M)+relativeEntropy(Q,M))/2.0;
return JSD;
}
}
#endif