# ANN: MaxMinFilters.jl - fast streaming maximum / minimum within moving window

**URL:** <https://discourse.julialang.org/t/ann-maxminfilters-jl-fast-streaming-maximum-minimum-within-moving-window/30151>\
**Category:** Package Announcements\
**Tags:** announcement\
**Created:** [October 22, 2019, 12:34am UTC](https://discourse.julialang.org/t/ann-maxminfilters-jl-fast-streaming-maximum-minimum-within-moving-window/30151 "2019-10-22T00:34:24Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [October 24, 2019, 8:01pm UTC](https://discourse.julialang.org/t/ann-maxminfilters-jl-fast-streaming-maximum-minimum-within-moving-window/30151/7 "2019-10-24T20:01:31Z")

</div>

Here is a minimal example of running mean, the same applies to higher order stats:

```julia
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:

[https://github.com/dysonance/Indicators.jl/pull/20/files](https://github.com/dysonance/Indicators.jl/pull/20/files)

---

_[View the full topic](https://discourse.julialang.org/t/ann-maxminfilters-jl-fast-streaming-maximum-minimum-within-moving-window/30151)._
