(BackBack)
Mathematics//2 min read

KL Divergence

KL Divergence (Kullback-Leibler) also known as Relative Entropy is all about the difference between two distributions.

KL Divergence ( Kullback-Leibler Divergence)

KL Divergence, also known as Relative Entropy, measures the amount of difference between two probability distributions. This metric can give you insights about how similar or dissimilar the two distributions are.

Cross Entropy: Binary Case

In a binary situation, there are only two possible outcomes: 0 or 1. The measure used to determine the "distance" or difference between the true and predicted values in this case is called Binary Cross Entropy (BCE).

Equation

BCE=ylog(y^)(1y)log(1y^)BCE = -y\log(\hat{y}) - (1 - y)\log(1 - \hat{y})

Where:

case 1.

y=0,y^=0y=0, \hat{y}=0

target value matches with the model

BCE=ylog(y^)(1y)log(1y^)=1log(1)0log(0)=0BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=1⋅log(1)−0⋅log(0)=0

case 2

y=0,y^=1y=0, \hat{y}=1

target value doesn't match with the model

BCE=ylog(y^)(1y)log(1y^)=1log(0)0log(1)=BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=−1⋅log(0)−0⋅log(1)=∞

case3

y=1,y^=0y=1, \hat{y}=0

target value doesn't match with the model

BCE=ylog(y^)(1y)log(1y^)=0log(1)(1)log(0)=BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=−0⋅log(1)−(1)⋅log(0)=∞

case4

y=1,y^=1y=1, \hat{y}=1

target value matches with the model

BCE=ylog(y^)(1y)log(1y^)=0log(0)(1)log(1)=0BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=−0⋅log(0)−(1)log(1)=0

Cross Entropy: Multiple Cases

When we're dealing with more than two possible outcomes, the Binary Cross Entropy formula needs to be adjusted.

Equation:

CE=xχP(x)log(Q(x))CE = - \displaystyle\sum_{x \in \chi} P(x) \log(Q(x))

Figure 1. Probability distribution for ideal (or normal) dice and abnormally high number of 1 dice.
Figure 1. Probability distribution for ideal (or normal) dice and abnormally high number of 1 dice.

Where:

Abnormal dice cross entropy

iP(xi)log(Q(xi))=(16log2(12)+56log2(110))=2.9349 bit(8)⇒−\displaystyle∑_iP(x_i)log(Q(x_i))=−(\frac{1}{6}log_2(\frac{1}{2})+\frac{5}{6}log_2(\frac{1}{10}))=2.9349 \ bit(8)

Normal dice cross entropy

iP(xi)log(Q(xi))=(log2(16))=2.5850 bit(8)⇒−\displaystyle∑_iP(x_i)log(Q(x_i))=−(log_2(\frac{1}{6}))= 2.5850 \ bit(8)

KL Divergence: Relative Comparison of Information Entropy

KL Divergence, also called Relative Entropy, shows the difference between two distributions. The key concept here is "divergence", which denotes the level of difference between the two distributions.

Equation:

DKL(PQ)=xχP(x)log(P(x)Q(x))DKL(P||Q) = \displaystyle\sum_{x \in \chi} P(x) \log\left(\frac{P(x)}{Q(x)}\right)

This can be expanded and rewritten as:

DKL(PQ)=xχP(x)log(Q(x)P(x))DKL(P||Q) = - \displaystyle\sum_{x \in \chi} P(x) \log\left(\frac{Q(x)}{P(x)}\right)

And further simplified into:

xχP(x)logb(Q(x)P(x))=xχP(x)logbQ(x)+xχP(x)logbP(x)−\displaystyle∑_{x∈χ}P(x)log_b⁡(\frac{Q(x)}{P(x)})=−\displaystyle∑_{x∈χ}P(x)log_bQ(x)+∑_{x∈χ}P(x)log_bP(x)

EP[logbQ(x)]+EP[logbP(x)]⇒−E_P[log_bQ(x)]+E_P[log_bP(x)]

Hp(Q)HP(P)⇒H_p(Q)-H_P(P)

Conclusion

These formulas and concepts are the backbone of many machine learning algorithms and are essential for understanding how models learn from data and make predictions.