Here is a minimal example of running mean, the same applies to higher order stats:
using Statistics
function runmean1(x::Vector{T}, n::Int64=10)::Vector{T} where {T<:Real}
len = size(x,1)
@assert n<len && n>1 "Argument n is out of bounds."
out = zeros(len - n + 1)
@inbounds for i = n:len
out[i-n+1] = mean(view(x, i-n+1:i))
end
return out
end
function runmean2(x::Vector{T}, n::Int64=10)::Vector{T} where {T<:Real}
len = size(x,1)
@assert n<len && n>1 "Argument n is out of bounds."
out = zeros(len - n + 1)
sum::T = 0
@inbounds for i = 1:n
sum += x[i]
end
out[1] = sum / n
@inbounds for i = n+1:size(x,1)
sum += x[i] - x[i-n] # sum = previous sum - old point + new point
out[i-n+1] = sum / n
end
return out
end
x = randn(1000000)
w = 100
@time m1 = runmean1(x, w)
@time m2 = runmean2(x, w)
m1 ≈ m2
For nan-stable version you can see my last PR to Indicators.jl: