API

UnrollingAverages.unrollMethod
unroll(
    moving_average::Vector{Float64},
    window::Int64;
    initial_conditions::U=nothing,
    assert_natural::Bool=false,
) where {U<:Union{Tuple{Vararg{Union{Int64,Float64}}},Nothing}}

Retrieve original time series (i.e. unroll) from its moving average moving_average.

Arguments

  • moving_average::Vector{Float64}: the time series representing the moving average to unroll;
  • window:::Int64: the width of the moving average;
  • initial_conditions::U = nothing: the initial values of the original time series to be recovered. It may be a Tuple of window-1 float or integer values, or nothing if initial conditions are unknown;
  • assert_natural::Bool = false default boolean argument. If true, then the pipeline will try to recover a time series of natural numbers only. More then one acceptable time series (where "acceptable" means that it reproduces moving_average) may be found and returned.

NB: If isnothing(initial_conditions) && !assert_natural , then only an approximate method may be used, see this StackExchange post.

source
UnrollingAverages.unroll_iterativeMethod
unroll_iterative(moving_average::Vector{Float64}, window::Int64)

Unroll moving_average (interpreting it as a moving average whose window width is window), returning the original time series assuming it is composed of only natural numbers.

The methodology is as follows:

  1. Consider the minimum of moving_average, that we will name minimum_average;
  2. Produce all possible sets of n₋ + n₊ + 1 naturals that could have minimum_average as mean. This is performed by obtaining all permutations of all the partitions of minimum_average*(n₋ + n₊ + 1) via Combinatorics.jl, and we will refer to each of the resulting array as a "possibility". These are organized in an array of iterators possibilities;
  3. For each for each possibility and for each element in moving_average[(minimum_index +1):end] compute the natural x (zero included) to be pushed to possibility's end so that sum(possibility[(i+1):(i+1 + n₋ + n₊ - 1)]) == minimum_average*(n₋ + n₊ + 1). if there is such x, push it to the possibility's end and go to the next possibility, else remove the possibility. When this loop finishes, perform the same loops backward for each element in reverse(moving_average[1:(minimum_index - 1)]), this time pushing the xs to the possibility's top. This allows for obtaining the set of all possible time series;
  4. Return the remaining possibilities.
source
UnrollingAverages.unroll_linear_approximationMethod
unroll_linear_approximation(moving_average::Vector{Float64}, window::Int64)

Compute the linear approximation of the time series whose moving average of window window is moving_average. For details, please refer to https://stats.stackexchange.com/a/68002 .

source
UnrollingAverages.unroll_recursiveMethod
unroll_recursive(moving_average::Vector{Float64}, window::Int64 , initial_conditions::Tuple{Vararg{Union{Int64,Float64}}} )

Return the raw time series of which moving_average is the moving average, where moving_average[i] is the moving average of the [(i-n₋):(i+n₊)] slice of the raw time series to be returned. Assume the raw time series is composed of only natural numbers.

The methodology is as follow:

  1. Initialize deaveraged = collect(initial) ;
  2. for each i ∈ eachindex(moving_average), set deaveraged[i+window-1] = round(Int64,window*moving_average[i] - sum(@view(deaveraged[i:(i+window-1-1)]))) ;
  3. Return deaveraged .
source