Giter Club home page Giter Club logo

Comments (4)

joshday avatar joshday commented on May 29, 2024

This is too much stuff. Do you have a MWE?

from onlinestats.jl.

femtotrader avatar femtotrader commented on May 29, 2024

Thanks @joshday for this nice projet and for your answer.

Here is a MWE

using OnlineStatsBase
using OnlineStats: StatLag

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

y = Float64[10, 11, 12, 13, 14, 15]
println(y)

o = MyMean()
o = StatLag(o, 3)

fit!(o, y)
println(value(o))
println(value(o.lag[end-1]))
println(value(o.lag[end-2]))

and a MNWE

using OnlineStatsBase

"""
    StatLag(stat, b)

Track a moving window (previous `b` copies) of `stat`.

# Example

    fit!(StatLag(Mean(), 10), 1:20)
"""
struct StatLag{T, O<:OnlineStat{T}} <: OnlineStatsBase.StatWrapper{T}
    lag::CircBuff{O}
    stat::O
end
function StatLag(stat::O, b::Integer) where {T, O<:OnlineStat{T}}
    StatLag{T,O}(CircBuff(O,b), stat)
end
function _fit!(o::StatLag, y)
    _fit!(o.stat, y)
    _fit!(o.lag, copy(o.stat))
end
function Base.show(io::IO, o::StatLag)
    print(io, name(o, false, false), ": ")
    print(io, "n=", nobs(o))
    print(io, " | stat_values_old_to_new= ")
    show(IOContext(io, :compact => true), value.(value(o.lag)))
end

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

y = Float64[10, 11, 12, 13, 14, 15]
println(y)

o = MyMean()
o = StatLag(o, 3)

fit!(o, y)
println(value(o))
println(value(o.lag[end-1]))
println(value(o.lag[end-2]))

this one raises

ERROR: LoadError: MethodError: no method matching _fit!(::StatLag{Number, MyMean}, ::Float64)

from onlinestats.jl.

joshday avatar joshday commented on May 29, 2024

Looks like you're missing a OnlineStatsBase. here:

function _fit!(o::StatLag, y)
    _fit!(o.stat, y)
    _fit!(o.lag, copy(o.stat))
end

from onlinestats.jl.

femtotrader avatar femtotrader commented on May 29, 2024

Thanks @joshday

I got it

using OnlineStatsBase

"""
    StatLag(stat, b)

Track a moving window (previous `b` copies) of `stat`.

# Example

    fit!(StatLag(Mean(), 10), 1:20)
"""
struct StatLag{T, O<:OnlineStat{T}} <: OnlineStatsBase.StatWrapper{T}
    lag::CircBuff{O}
    stat::O
end
function StatLag(stat::O, b::Integer) where {T, O<:OnlineStat{T}}
    StatLag{T,O}(CircBuff(O,b), stat)
end
function OnlineStatsBase._fit!(o::StatLag, y)
    OnlineStatsBase._fit!(o.stat, y)
    OnlineStatsBase._fit!(o.lag, copy(o.stat))
end
function Base.show(io::IO, o::StatLag)
    print(io, name(o, false, false), ": ")
    print(io, "n=", nobs(o))
    print(io, " | stat_values_old_to_new= ")
    show(IOContext(io, :compact => true), value.(value(o.lag)))
end

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

y = Float64[10, 11, 12, 13, 14, 15]
println(y)

o = MyMean()
o = StatLag(o, 3)

fit!(o, y)
println(value(o))
println(value(o.lag[end-1]))
println(value(o.lag[end-2]))

from onlinestats.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.