Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Division by zero if o.W == 0 results in NaN values #42

Open
rlefmann opened this issue Feb 17, 2021 · 1 comment
Open

Division by zero if o.W == 0 results in NaN values #42

rlefmann opened this issue Feb 17, 2021 · 1 comment

Comments

@rlefmann
Copy link

Some of the algorithms, like WeightedMean and WeightedCovMatrix, return NaN values if the first weight is zero. Example:

using WeightedOnlineStats

m = WeightedMean()
x = ones(3)
w = [0.0, 0.5, 1.0]
fit!(m, x, w)
# WeightedMean: ∑wᵢ=1.5 | value=NaN

# but if we reverse the sequences everything works as expected:
m = WeightedMean()
fit!(m, reverse(x), reverse(w))
# WeightedMean: ∑wᵢ=1.5 | value=1.0

If at any point in the computation the sum of the previous weights, o.W, becomes zero, there will be NaN values because of division by zero. For the weighted mean this could be fixed by not changing mu in that case.

@rlefmann rlefmann changed the title Division by zero if o.W != 0 results in NaN values Division by zero if o.W == 0 results in NaN values Feb 17, 2021
@gdkrmr
Copy link
Owner

gdkrmr commented Feb 18, 2021

Good catch. Thanks for reporting. The whole thing was written without zero weights in mind.

This also poses the question if we should we increase the counter of observations when encountering a zero weight or just not do anything at all?

We also don't guard for negative weights so should we guard for zero weights? I personally lean in favor of leaving this in the responsibility of the user.

To spin this even further: Negative weights could also lead to W == 0 later on.

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

No branches or pull requests

2 participants