Skip to content

Conversation

@Monasri29-hub
Copy link

Description

Fixes #12233

This PR fixes a critical bug in the kullback_leibler_divergence function where entries with y_true = 0 caused the function to return nan.

Problem

The current implementation computes y_true * np.log(y_true / y_pred) for all entries, including when y_true = 0. This results in 0 * log(0) = 0 * (-inf) = nan, which breaks the function.

Solution

Filter out zero entries from y_true before computing the logarithm. This is mathematically correct because:

  • In information theory, the convention is that 0 * log(0) = 0
  • Mathematical limit: lim(p→0+) p·log(p) = 0
  • This is how SciPy's rel_entr and other standard implementations handle this case

Changes Made

  • Added boolean masking to filter zero entries: mask = y_true != 0
  • Applied mask to both arrays before logarithm computation
  • Added two new doctests demonstrating correct behavior with zeros
  • Added inline comment explaining the mathematical justification

Testing

New Test Cases Added:

  1. Single zero in y_true: [0.0, 0.3, 0.7] → Returns 0.0237...
  2. Multiple zeros in y_true: [0.0, 0.0, 1.0] → Returns 0.6931...

All Existing Tests Pass:

  • Original non-zero case still works correctly
  • Error handling for mismatched array lengths unchanged

Verification

Monasri29-hub and others added 2 commits October 20, 2025 20:13
Fixes TheAlgorithms#12233

- Filter out zero entries from y_true before computing logarithm
- Add doctests demonstrating correct behavior with zeros
- Mathematically correct per information theory conventions
@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid log(0) in KL divergence

2 participants