Read the equation
For normalized distributions, the sum p log(p/q) measures relative entropy in nats with the file’s natural logarithm. A term with positive p and zero q should make the divergence infinite. The code skips such terms when q is below its cutoff, so it can return a finite value where the mathematical divergence is infinite and can lose its usual nonnegativity guarantee after truncation.
Terms & definitions
| Term | Meaning in this source |
|---|---|
| p | First probability distribution |
| q | Reference distribution |
| D_KL | Relative entropy |
How it works
Read the definition from its right-hand expression to the quantity on the left. Its surrounding source records the intended input, state, or parameter context. Where the source leaves an operation unspecified, this page preserves that gap.
Assumptions & limits
Source recovery establishes that the expression appears in this record. Its mathematical validity, implementation behavior, and interpretation must be assessed separately. See the linked source and research notes for the checks available for this version.
Original context
Read the surrounding source passage
if p > EPSILON_PHI && p < 1.0-EPSILON_PHI {
h -= p * math.Log2(p)
}
}
return h
}
// KLDivergence computes D_KL(P||Q) = Σ p(x) log(p(x)/q(x))
func KLDivergence(p, q []float64) float64 {
if len(p) != len(q) {
return math.Inf(1)
}
kl := 0.0
for i := range p {
if p[i] > EPSILON_PHI && q[i] > EPSILON_PHI {
kl += p[i] * math.Log(p[i]/q[i])
}Exact original notation
KLDivergence computes D_KL(P||Q) = Σ p(x) log(p(x)/q(x))