Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KL divergence of continuous pdfs

Say I have two pdfs, e.g.:

from scipy import stats
pdf_y = stats.beta(5, 9).pdf
pdf_x = stats.beta(9, 5).pdf

I would like to compute their KL divergence. Before I reinvent the wheel, are there any builtins in the PyData eco-system for doing this?

like image 697
Amelio Vazquez-Reina Avatar asked Mar 20 '23 09:03

Amelio Vazquez-Reina


1 Answers

KL divergence is available in scipy.stats.entropy. From the docstring

stats.entropy(pk, qk=None, base=None) 

Calculate the entropy of a distribution for given probability values.           

If only probabilities `pk` are given, the entropy is calculated as              
``S = -sum(pk * log(pk), axis=0)``.                                             

If `qk` is not None, then compute a relative entropy (also known as             
Kullback-Leibler divergence or Kullback-Leibler distance)                       
``S = sum(pk * log(pk / qk), axis=0)``.  
like image 70
jseabold Avatar answered Apr 02 '23 01:04

jseabold