• You have a data sequence ๐ท๐‘Ž๐‘ก๐‘Ž, the Vector[1, 2, 3, 4, 5].
  • The window span ๐‘†๐‘๐‘Ž๐‘› of each subsequence is 3.
  • The function ๐น๐‘ข๐‘›๐‘ to be applied over subsequences of ๐ท๐‘Ž๐‘ก๐‘Ž is sum.
using RollingFunctions

๐ท๐‘Ž๐‘ก๐‘Ž = [1, 2, 3, 4, 5]
๐น๐‘ข๐‘›๐‘ = sum
๐‘†๐‘๐‘Ž๐‘› = 3

rolled = rolling(๐น๐‘ข๐‘›๐‘,๐ท๐‘Ž๐‘ก๐‘Ž, ๐‘†๐‘๐‘Ž๐‘›)


julia> rolled
3-element Vector{Int64}:
  6
  9
 12

#=
The first  windowed value is the ๐น๐‘ข๐‘›๐‘ (sum) of the first  ๐‘†๐‘๐‘Ž๐‘› (3) values in ๐ท๐‘Ž๐‘ก๐‘Ž.
The second windowed value is the ๐น๐‘ข๐‘›๐‘ (sum) of the second ๐‘†๐‘๐‘Ž๐‘› (3) values in ๐ท๐‘Ž๐‘ก๐‘Ž.
The third  windowed value is the ๐น๐‘ข๐‘›๐‘ (sum) of the third  ๐‘†๐‘๐‘Ž๐‘› (3) values in ๐ท๐‘Ž๐‘ก๐‘Ž.

There can be no fourth value as the third value used the fins entries in๐ท๐‘Ž๐‘ก๐‘Ž.
=#

julia> sum(๐ท๐‘Ž๐‘ก๐‘Ž[1:3]), sum(๐ท๐‘Ž๐‘ก๐‘Ž[2:4]), sum(๐ท๐‘Ž๐‘ก๐‘Ž[3:5])
(6, 9, 12)


If the span of each subsequence increases to 4..

๐‘†๐‘๐‘Ž๐‘› = 4
rolled = rolling(๐ท๐‘Ž๐‘ก๐‘Ž, ๐‘†๐‘๐‘Ž๐‘›, ๐’ฎ);

rolled
2-element Vector{Int64}:
 10
 14

Generally, with data that has r rows using a window_span of s results in r - s + 1 rows of values.

To get back a result with the same number of rows as your data

Welcome to the wonderful world of padding

You may pad the result with the padding value of your choice

  • padding is a keyword argument
  • missing, 0.0 are commonly used
    • almost all values are usable
    • using nothing as the padding is allowed
    • using the type Nothing is disallowed
using RollingFunctions

๐ท๐‘Ž๐‘ก๐‘Ž = [1, 2, 3, 4, 5]
๐น๐‘ข๐‘›๐‘ = sum
๐‘†๐‘๐‘Ž๐‘› = 3

rolled = rolling(๐น๐‘ข๐‘›๐‘,๐ท๐‘Ž๐‘ก๐‘Ž, ๐‘†๐‘๐‘Ž๐‘›; padding = missing);

julia> rolled
5-element Vector{Union{Missing, Int64}}:
   missing
   missing
   missing
 10
 14
 
rolled = rolling(๐ท๐‘Ž๐‘ก๐‘Ž, ๐‘†๐‘๐‘Ž๐‘›, ๐’ฎ; padding = zero(eltype(๐ท๐‘Ž๐‘ก๐‘Ž));
julia> rolled
5-element Vector{Int64}:
  0
  0
  0
 10
 14

Give me the real values first, pad to the end.

rolled = rolling(๐น๐‘ข๐‘›๐‘,๐ท๐‘Ž๐‘ก๐‘Ž, ๐‘†๐‘๐‘Ž๐‘›; padding = zero(eltype(๐ท๐‘Ž๐‘ก๐‘Ž), padlast=true);
julia> rolled
5-element Vector{Int64}:
 10
 14
  0
  0
  0

technical note: this is not the same asreverse(rolling(๐ท๐‘Ž๐‘ก๐‘Ž, ๐‘†๐‘๐‘Ž๐‘›, ๐’ฎ; padding = zero(eltype(๐ท๐‘Ž๐‘ก๐‘Ž)).