Hipparchus-Math / hipparchus

An efficient, general-purpose mathematics components library in the Java programming language
Apache License 2.0
143 stars 42 forks source link

`EmpiricalDistribution#density()` shouldn't return `NaN` if kernel density is `0.0` #316

Closed axkr closed 8 months ago

axkr commented 8 months ago

Discussed in https://github.com/Hipparchus-Math/hipparchus/discussions/315

Originally posted by **axkr** March 5, 2024 Shouldn't the "convention" be, that if `kernel.density(x)`equals `0.0` the `EmpiricalDistribution#density()` method should also return `0.0` and not `NaN` if `kB(binIndex)==0.0` ? https://github.com/Hipparchus-Math/hipparchus/blob/a8fe2cf6f868329820544f74c784aa9de34dc695/hipparchus-stat/src/main/java/org/hipparchus/stat/fitting/EmpiricalDistribution.java#L581 So the patch could be something like this? ```java public double density(double x) { if (x < min || x > max) { return 0d; } final int binIndex = findBin(x); final RealDistribution kernel = getKernel(binStats.get(binIndex)); double kernelDensity = kernel.density(x); if (kernelDensity == 0d) { return 0d; } return kernelDensity * pB(binIndex) / kB(binIndex); } ```