OnlineStatsBase.jl

Base types for OnlineStats.
Author joshday
Popularity
27 Stars
Updated Last
1 Year Ago
Started In
June 2017

Build status codecov


OnlineStatsBase

This package defines the basic types and interface for OnlineStats.



Interface

Required

  • _fit!(stat, y): Update the "sufficient statistics" of the estimator from a single observation y.

Required (with Defaults)

  • 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.

Optional

  • _merge!(stat1, stat2): Merge stat2 into stat1 (an error by default in OnlineStatsBase versions >= 1.5).
  • Base.empty!(stat): Return the stat to its initial state (an error by default).



Example

  • Make a subtype of OnlineStat and give it a _fit!(::OnlineStat{T}, y::T) method.
  • T is 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)
end



That's all there is to it!

y = randn(1000)

o = fit!(MyMean(), y)
# MyMean: n=1_000 | value=0.0530535