This package defines the basic types and interface for OnlineStats.
_fit!(stat, y): Update the "sufficient statistics" of the estimator from a single observationy.
value(stat, args...; kw...) = <first field of struct>: Calculate the value of the estimator from the "sufficient statistics".nobs(stat) = stat.n: Return the number of observations.
_merge!(stat1, stat2): Mergestat2intostat1(an error by default in OnlineStatsBase versions >= 1.5).Base.empty!(stat): Return the stat to its initial state (an error by default).
- Make a subtype of OnlineStat and give it a
_fit!(::OnlineStat{T}, y::T)method. Tis the type of a single observation. Make sure it's adequately wide.
using OnlineStatsBase
mutable struct MyMean <: OnlineStat{Number}
value::Float64
n::Int
MyMean() = new(0.0, 0)
end
function OnlineStatsBase._fit!(o::MyMean, y)
o.n += 1
o.value += (1 / o.n) * (y - o.value)
endy = randn(1000)
o = fit!(MyMean(), y)
# MyMean: n=1_000 | value=0.0530535