/
ConditionalEntropy.go
46 lines (40 loc) · 1.12 KB
/
ConditionalEntropy.go
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
package discrete
import (
"math"
)
// ConditionalEntropy calculates the conditional entropy of a probability distribution.
// It takes the log function as an additional parameter, so that the base
// can be chosen
// H(X|Y) = -\sum_x p(x,y) lnFunc(p(x,y)/p(y))
func ConditionalEntropy(pxy [][]float64, log lnFunc) float64 {
var r float64
var py float64
xDim := len(pxy)
yDim := len(pxy[0])
for y := 0; y < yDim; y++ {
py = 0.0
for x := 0; x < xDim; x++ {
py += pxy[x][y]
}
if py > 0.0 {
for x := 0; x < xDim; x++ {
if pxy[x][y] > 0.0 {
r -= pxy[x][y] * (log(pxy[x][y]) - log(py))
}
}
}
}
return r
}
// ConditionalEntropyBaseE calculates the conditional entropy of a
// probability distribution in nats
// H(X|Y) = -\sum_x p(x,y) ln(p(x,y)/p(y))
func ConditionalEntropyBaseE(pxy [][]float64) float64 {
return ConditionalEntropy(pxy, math.Log)
}
// ConditionalEntropyBase2 calculates the conditional entropy of a
// probability distribution in bits
// H(X|Y) = -\sum_x p(x,y) log2(p(x,y)/p(y))
func ConditionalEntropyBase2(pxy [][]float64) float64 {
return ConditionalEntropy(pxy, math.Log2)
}