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.
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).
B C E = − y log ( y ^ ) − ( 1 − y ) log ( 1 − y ^ ) BCE = -y\log(\hat{y}) - (1 - y)\log(1 - \hat{y}) B C E = − y log ( y ^ ) − ( 1 − y ) log ( 1 − y ^ )
Where:
y = 0 , y ^ = 0 y=0, \hat{y}=0 y = 0 , y ^ = 0
target value matches with the model
B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = 1 ⋅ l o g ( 1 ) − 0 ⋅ l o g ( 0 ) = 0 BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=1⋅log(1)−0⋅log(0)=0 B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = 1 ⋅ l o g ( 1 ) − 0 ⋅ l o g ( 0 ) = 0
y = 0 , y ^ = 1 y=0, \hat{y}=1 y = 0 , y ^ = 1
target value doesn't match with the model
B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = − 1 ⋅ l o g ( 0 ) − 0 ⋅ l o g ( 1 ) = ∞ BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=−1⋅log(0)−0⋅log(1)=∞ B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = − 1 ⋅ l o g ( 0 ) − 0 ⋅ l o g ( 1 ) = ∞
y = 1 , y ^ = 0 y=1, \hat{y}=0 y = 1 , y ^ = 0
target value doesn't match with the model
B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = − 0 ⋅ l o g ( 1 ) − ( 1 ) ⋅ l o g ( 0 ) = ∞ BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=−0⋅log(1)−(1)⋅log(0)=∞ B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = − 0 ⋅ l o g ( 1 ) − ( 1 ) ⋅ l o g ( 0 ) = ∞
y = 1 , y ^ = 1 y=1, \hat{y}=1 y = 1 , y ^ = 1
target value matches with the model
B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = − 0 ⋅ l o g ( 0 ) − ( 1 ) l o g ( 1 ) = 0 BCE=−ylog(\hat{y})−(1−y)log(1−\hat{y}) \\=−0⋅log(0)−(1)log(1)=0 B C E = − y l o g ( y ^ ) − ( 1 − y ) l o g ( 1 − y ^ ) = − 0 ⋅ l o g ( 0 ) − ( 1 ) l o g ( 1 ) = 0
When we're dealing with more than two possible outcomes, the Binary Cross Entropy formula needs to be adjusted.
Equation:
C E = − ∑ x ∈ χ P ( x ) log ( Q ( x ) ) CE = - \displaystyle\sum_{x \in \chi} P(x) \log(Q(x)) C E = − x ∈ χ ∑ P ( x ) log ( Q ( x ))
Figure 1. Probability distribution for ideal (or normal) dice and abnormally high number of 1 dice.
Where:
Abnormal dice cross entropy
⇒ − ∑ i P ( x i ) l o g ( Q ( x i ) ) = − ( 1 6 l o g 2 ( 1 2 ) + 5 6 l o g 2 ( 1 10 ) ) = 2.9349 b i t ( 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) ⇒ − i ∑ P ( x i ) l o g ( Q ( x i )) = − ( 6 1 l o g 2 ( 2 1 ) + 6 5 l o g 2 ( 10 1 )) = 2.9349 bi t ( 8 )
Normal dice cross entropy
⇒ − ∑ i P ( x i ) l o g ( Q ( x i ) ) = − ( l o g 2 ( 1 6 ) ) = 2.5850 b i t ( 8 ) ⇒−\displaystyle∑_iP(x_i)log(Q(x_i))=−(log_2(\frac{1}{6}))= 2.5850 \ bit(8) ⇒ − i ∑ P ( x i ) l o g ( Q ( x i )) = − ( l o g 2 ( 6 1 )) = 2.5850 bi t ( 8 )
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:
D K L ( P ∣ ∣ Q ) = ∑ 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) D K L ( P ∣∣ Q ) = x ∈ χ ∑ P ( x ) log ( Q ( x ) P ( x ) )
This can be expanded and rewritten as:
D K L ( P ∣ ∣ Q ) = − ∑ 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) D K L ( P ∣∣ Q ) = − x ∈ χ ∑ P ( x ) log ( P ( x ) Q ( x ) )
And further simplified into:
− ∑ x ∈ χ P ( x ) l o g b ( Q ( x ) P ( x ) ) = − ∑ x ∈ χ P ( x ) l o g b Q ( x ) + ∑ x ∈ χ P ( x ) l o g b P ( 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) − x ∈ χ ∑ P ( x ) l o g b ( P ( x ) Q ( x ) ) = − x ∈ χ ∑ P ( x ) l o g b Q ( x ) + x ∈ χ ∑ P ( x ) l o g b P ( x )
⇒ − E P [ l o g b Q ( x ) ] + E P [ l o g b P ( x ) ] ⇒−E_P[log_bQ(x)]+E_P[log_bP(x)] ⇒ − E P [ l o g b Q ( x )] + E P [ l o g b P ( x )]
⇒ H p ( Q ) − H P ( P ) ⇒H_p(Q)-H_P(P) ⇒ H p ( Q ) − H P ( P )
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.