
KL Divergence Between Population and Sample Distributions
Source:R/kl_optimization.R
ss_kl_divergence.RdComputes the mean Kullback-Leibler divergence across all numeric variables shared by a population data set and a sample drawn from it.
Details
For each numeric variable, population and sample distributions are estimated as histograms over the same bins (defined by the population range). KL divergence is then \(\sum O_i \log(O_i / E_i)\), where \(O_i\) is the sample density and \(E_i\) the population density in bin \(i\). The result is averaged across variables.
Examples
pop <- data.frame(x = rnorm(200), y = runif(200))
samp <- pop[sample(nrow(pop), 30), ]
ss_kl_divergence(pop, samp)
#> [1] 0.3444531