Skip to contents

Analytical KL divergence for two discrete distributions

Usage

kld_discrete(P, Q)

Arguments

P, Q

Numerical arrays with the same dimensions, representing discrete probability distributions

Value

A scalar (the Kullback-Leibler divergence)

Examples

# 1-D example
P <- 1:4/10
Q <- rep(0.25,4)
kld_discrete(P,Q)
#> [1] 0.1064401

# The above example in 2-D
P <- matrix(1:4/10,nrow=2)
Q <- matrix(0.25,nrow=2,ncol=2)
kld_discrete(P,Q)
#> [1] 0.1064401