JeffreySarnoff / RollingFunctions.jl

Roll a window over data; apply a function over the window.
MIT License
114 stars 6 forks source link

Rolling statistics between two+ vectors #10

Closed tnederlof closed 5 years ago

tnederlof commented 5 years ago

You have done a really nice job creating a focused and very useful package, I have learned a lot by reading through the source code as I am fairly new to Julia.

Often times I find the need to do a rolling correlation, rolling least squares regression, etc, which involve calculations between two or more columns of a matrix instead of a single vector. Have you thought about this sort of rolling application before? I would be happy to help and contribute but wasn't sure if it's beyond the spirit of what your package is focused on or if it would fit.

JeffreySarnoff commented 5 years ago

I am open to this. What are your thoughts?

xgdgsc commented 5 years ago

I also find it useful. Keep a similar syntax for it like:

running((x,y)->some_function(x, y), [A, B], window)

would be nice.

JeffreySarnoff commented 5 years ago

Thanks for being specific -- that helps. Give me some arity-2 functions you would like to use this way.

xgdgsc commented 5 years ago
    function running2(fun::Function, A, B, window)
        result = Array{Union{Missing,Float64}}(undef, length(A))
        offset = window - 1
        @inbounds for idx in eachindex(A)
            result[idx] = fun(view(A, idx:idx + offset), view(B, idx:idx + offset))
        end
        result
    end

I just defined this and used cov corr. Maybe you could consider how to make it work for arbitrary number of arrays or specified columns inside a single Dataframe to make it more general.

JeffreySarnoff commented 5 years ago

cool -- looking into it . do not want to be fixed to DataFrames though

JeffreySarnoff commented 5 years ago

done for 2 data vectors -- see README