Skip to content

Commit bc6a727

Browse files
variance is NaN for empty collection
1 parent 1a1a3e3 commit bc6a727

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/aggregation.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,29 @@ end
2727
return VarianceWelfordAggregation{Ti,T}(count, mean, M2)
2828
end
2929

30-
function result(ag::VarianceWelfordAggregation)
31-
sample_variance = ag.M2 / (ag.count - 1)
30+
Base.count(ag::VarianceWelfordAggregation) = ag.count
31+
32+
function Statistics.mean(ag::VarianceWelfordAggregation{Ti,T}) where {Ti,T}
33+
if ag.count > 0
34+
return ag.mean
35+
else
36+
# NaN of the right kind
37+
return zero(T) / 0
38+
end
39+
end
40+
41+
function Statistics.var(ag::VarianceWelfordAggregation{Ti,T}) where {Ti,T}
42+
if ag.count > 1
43+
sample_variance = ag.M2 / (ag.count - 1)
44+
else
45+
# ag.count is 0 or 1
46+
# NaN of the right kind
47+
sample_variance = zero(T) / 0
48+
end
3249
return sample_variance
3350
end
3451

52+
result(ag::VarianceWelfordAggregation) = var(ag)
3553

3654

3755
for (funAggregation,fun) in ((:MaxAggregation,max),(:MinAggregation,min))

0 commit comments

Comments
 (0)