API
UnrollingAverages.unroll
UnrollingAverages.unroll_iterative
UnrollingAverages.unroll_linear_approximation
UnrollingAverages.unroll_recursive
UnrollingAverages.unroll
— Methodunroll(
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 aTuple
ofwindow-1
float or integer values, ornothing
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 reproducesmoving_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.
UnrollingAverages.unroll_iterative
— Methodunroll_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:
- Consider the minimum of
moving_average
, that we will nameminimum_average
; - Produce all possible sets of
n₋ + n₊ + 1
naturals that could haveminimum_average
as mean. This is performed by obtaining all permutations of all the partitions ofminimum_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 iteratorspossibilities
; - For each for each
possibility
and for eachelement
inmoving_average[(minimum_index +1):end]
compute the naturalx
(zero included) to be pushed topossibility
's end so thatsum(possibility[(i+1):(i+1 + n₋ + n₊ - 1)]) == minimum_average*(n₋ + n₊ + 1)
. if there is suchx
, push it to thepossibility
's end and go to the next possibility, else remove thepossibility
. When this loop finishes, perform the same loops backward for eachelement in reverse(moving_average[1:(minimum_index - 1)])
, this time pushing thex
s to thepossibility
's top. This allows for obtaining the set of all possible time series; - Return the remaining possibilities.
UnrollingAverages.unroll_linear_approximation
— Methodunroll_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 .
UnrollingAverages.unroll_recursive
— Methodunroll_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:
- Initialize
deaveraged = collect(initial)
; - for each
i ∈ eachindex(moving_average)
, setdeaveraged[i+window-1] = round(Int64,window*moving_average[i] - sum(@view(deaveraged[i:(i+window-1-1)])))
; - Return
deaveraged
.