Comparison.score.kullback_leibler_divergence
Comparison.score.kullback_leibler_divergence ¶
Calculates the Kullback-Leibler divergence (KLD) between the reference and comparison corpora.
Example
Warning
Using this measure forces the comparison to be evaluated eagerly rather than lazily, because validating it requires computing over the full vocabulary.
The KLD is an asymmetric measure of how much more information is needed to encode the comparison corpus \(C\) when using the reference corpus \(R\) as the "codebook":
where \(p_\tau^{(R)}\) and \(p_\tau^{(C)}\) are the normalized frequencies of a token \(\tau\) in
\(R\) and \(C\) respectively, and \(b\) is the logarithm base.
Reference
For more details, see the following paper:
Gallagher, R. J., Frank, M. R., Mitchell, L., Schwartz, A. J., Reagan, A. J., Danforth, C. M., & Dodds, P. S. (2021). Generalized word shift graphs: a method for visualizing and explaining pairwise comparisons between texts. EPJ Data Science, 10(1), 4.
See Also
| Score | Description |
|---|---|
Comparison.score.jensen_shannon_divergence |
Scores corpora using a bounded, symmetric, entropy-based divergence |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
float
|
Base of the logarithms used to calculate the KLD |
2
|
Returns:
| Type | Description |
|---|---|
Comparison
|
A new |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the comparison corpus contains any token absent from the reference corpus, which
causes the KLD to be infinite. Use a symmetric measure like the
Jensen-Shannon divergence,
or drop those tokens with
|