joshday / OnlineStats.jl

⚡ Single-pass algorithms for statistics
https://joshday.github.io/OnlineStats.jl/latest/
MIT License
831 stars 62 forks source link

Folds OnlineStats Unitful combination #263

Closed jariji closed 1 year ago

jariji commented 1 year ago
using Folds, OnlineStats, Unitful, StatsBase

julia> StatsBase.mean([1u"m",2u"m"])
1.5 m

julia> Folds.reduce(OnlineStats.Mean(), [3,4,5])
Mean: n=3 | value=4.0

julia> Folds.reduce(OnlineStats.Mean(), [3u"m", 4u"m", 5u"m"])
ERROR: DimensionError: 3.0 m and 0.0 are not dimensionally compatible.

Is this intended to be supported?

joshday commented 1 year ago

Mean is parameterized by the <:Number that stores the estimate. In this case, you'd want the storage type to be typeof(1.0u"m") to accommodate the input type from Unitful.

julia> Folds.reduce(OnlineStats.Mean(typeof(1.0u"m")), [3u"m", 4u"m", 5u"m"])
Mean: n=3 | value=4.0 m