Skip to content

Fix: Prevent geometric mean underflow for small values#362

Open
sunlight7777777 wants to merge 1 commit intoAffineFoundation:mainfrom
sunlight7777777:fix/geometric-mean-underflow
Open

Fix: Prevent geometric mean underflow for small values#362
sunlight7777777 wants to merge 1 commit intoAffineFoundation:mainfrom
sunlight7777777:fix/geometric-mean-underflow

Conversation

@sunlight7777777
Copy link
Copy Markdown

Problem

The current implementation of geometric_mean computes the product of all values before taking the nth root. For many small positive values (e.g. [1e-200] * 1000), this causes floating-point underflow to 0.0, producing incorrect results.

Root Cause

Iterative multiplication underflows in IEEE 754 double precision:

product *= v
Solution

Replaced the product-based computation with a numerically stable log-space formulation:

exp(sum(log(v)) / n)

This avoids underflow while preserving mathematical correctness.

Impact
Fixes incorrect outputs for small-value inputs
Improves numerical stability of scoring functions
No breaking changes
Example

Before:

geometric_mean([1e-200] * 1000) → 0.0

After:

→ ~1e-200

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant