JuliaMath / NaNMath.jl

Julia math built-ins which return NaN and accumulator functions which ignore NaN
Other
52 stars 26 forks source link

StatsBase.mean ~= NaNMath.mean #60

Closed CMGreenspon closed 1 year ago

CMGreenspon commented 1 year ago

NaNMath.mean and StatsBase.mean do not produce the same result

using NaNMath
using StatsBase
temp = rand(100)
NaNMath.mean(temp) == StatsBase.mean(temp)

This will evaluate to false. This seems to be true after digit 15: round(mean(temp),digits=16) == round(NaNMath.mean(temp),digits=16) Any idea why?

jd-foster commented 1 year ago

The difference between the two is only at most the floating point round-off error, and is an inevitable part of computation in finite precision arithmetic. You can see the variation by something like

A = Float64[]
for i in 1:1000
    temp = rand(100);
    push!(A, NaNMath.mean(temp) - StatsBase.mean(temp))
 end
[minimum(A), maximum(A)]

and compare this to eps().

mlubin commented 1 year ago

Agreed with @jd-foster. It's unreasonable to expect exact equality in this case.