Closed karlwessel closed 4 years ago
Hmm, Unitful
is an interesting case. I hadn't considered that a type could change from multiplication.
Variance
internally stores the mean and variance, both initialized with zero(T)
, so this was a relatively easy fix of initializing the variance with zero(T) ^ 2
.
This is fixed on OnlineStatsBase master, along with a few other things like making value
type stable.
julia> a = 4u"m"
4 m
julia> v = Variance(typeof(a))
Variance: n=0 | value=1 mΒ²
julia> fit!(v, a)
Variance: n=1 | value=1 mΒ²
That was quick! Thank you!
You're welcome!
Big caveat: you need to use 4.0u"m"
instead of 4u"m"
Just kidding, I made it smarter. 4u"m"
is fine.
Is it possible to use
OnlineStats
to calculate the Variance of values with units usingUnitful
?I tried to define the type for the
Variance
, but neither the type of the unitful value nor the square of it works:It works for the
Mean
:I would guess that the reason is that Unitful defines the neutral element for multiplication (
one
) of a unitful number as1
without any unit. Which is correct. HoweverVariance
seems to initialize itself using this value and therefore does not get the right units? It seems to "forget" the units after creation: