Skip to content

Commit

Permalink
prelminary rolling acf function
Browse files Browse the repository at this point in the history
still needs much further testing
  • Loading branch information
dysonance committed Jul 5, 2018
1 parent 07904af commit 48e42ed
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Indicators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ VERSION >= v"0.4.0" && __precompile__(true)
module Indicators

export
runmean, runsum, runvar, runsd, runcov, runcor, runmax, runmin, runmad, runquantile,
runmean, runsum, runvar, runsd, runcov, runcor, runmax, runmin, runmad, runquantile, runacf,
wilder_sum, mode, diffn, #lag,
sma, trima, wma, ema, mma, kama, mama, hma, swma, dema, tema, alma, zlema,
mlr_beta, mlr_slope, mlr_intercept, mlr, mlr_se, mlr_ub, mlr_lb, mlr_bands, mlr_rsq,
Expand Down
33 changes: 28 additions & 5 deletions src/run.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Temporal.acf # used for running autocorrelation function

@doc doc"""
Lagged differencing
Expand Down Expand Up @@ -96,7 +98,7 @@ function runsum(x::Array{Float64}; n::Int64=10, cumulative::Bool=true)::Array{Fl
out[i] = sum(x[i-n+1:i])
end
end
return out
return out
end

@doc doc"""
Expand Down Expand Up @@ -216,7 +218,7 @@ function runcor(x::Array{Float64}, y::Array{Float64}; n::Int64=10, cumulative::B
end

@doc doc"""
Compute the running or rolling maximum of an array.
Compute the running or rolling maximum of an array
`runmax(x::Array{Float64}; n::Int64=10, cumulative::Bool=true, inclusive::Bool=true)::Array{Float64}`
""" ->
Expand Down Expand Up @@ -253,7 +255,7 @@ function runmax(x::Array{Float64}; n::Int64=10, cumulative::Bool=true, inclusive
end

@doc doc"""
Compute the running or rolling minimum of an array.
Compute the running or rolling minimum of an array
`runmin(x::Array{Float64}; n::Int64=10, cumulative::Bool=true, inclusive::Bool=true)::Array{Float64}`
""" ->
Expand Down Expand Up @@ -290,10 +292,10 @@ function runmin(x::Array{Float64}; n::Int64=10, cumulative::Bool=true, inclusive
end

@doc doc"""
Compute the running/rolling quantile of an array.
Compute the running/rolling quantile of an array
`runquantile(x::Vector{Float64}; p::Float64=0.05, n::Int=10, cumulative::Bool=true)::Vector{Float64}`
"""
""" ->
function runquantile(x::Array{Float64}; p::Float64=0.05, n::Int=10, cumulative::Bool=true)::Array{Float64}
@assert n<size(x,1) && n>1 "Argument n is out of bounds."
out = zeros(Float64, (size(x,1), size(x,2)))
Expand All @@ -310,3 +312,24 @@ function runquantile(x::Array{Float64}; p::Float64=0.05, n::Int=10, cumulative::
end
return out
end


@doc doc"""
Compute the running/rolling autocorrelation of a vector.
""" ->
function runacf(x::Array{Float64}; lookback::Int=10, maxlag::Int=7, lags::AbstractArray{Int,1}=0:maxlag)::Matrix{Float64}
@assert size(x, 2) == 1 "Autocorrelation input array must be one-dimensional"
N = size(x, 1)
if length(lags) == 1 && lags[1] == 0
return ones((N, 1))
end
# @assert all(lags .>= 0) "All autocorrelation lags must be positive integers"
# @assert all(lags .< N+3) "All autocorrelation lags must be less than the number of observations in the input vector"
# @assert all(lags .< lookback+2)
@assert lookback < N && lookback > 0
out = zeros((N, length(lags))) * NaN
@inbounds for i in lookback:N
out[i,:] = acf(x[i-lookback+1:i], lags=lags)
end
return out
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,8 @@ end
@test tmp.values[10,1] == quantile(x.values[1:10,1], 0.05)
tmp = runquantile(x, cumulative=false)
@test tmp.values[10,1] == quantile(x.values[1:10,1], 0.05)
tmp = runacf(randn(100))
@test all(tmp[10:end,1] .== 1.0)
# moving average functions
tmp = sma(x)
@test size(tmp, 1) == N
Expand Down

0 comments on commit 48e42ed

Please sign in to comment.