running_sum {fromo} | R Documentation |
Compute sums or means over a sliding window.
Description
Compute the mean or sum over an infinite or finite sliding window, returning a vector the same size as the input.
Usage
running_sum(v, window = NULL, wts = NULL, na_rm = FALSE,
restart_period = 10000L, check_wts = FALSE)
running_mean(v, window = NULL, wts = NULL, na_rm = FALSE, min_df = 0L,
restart_period = 10000L, check_wts = FALSE)
Arguments
v |
a vector. |
window |
the window size. if given as finite integer or double, passed through.
If |
wts |
an optional vector of weights. Weights are ‘replication’
weights, meaning a value of 2 is shorthand for having two observations
with the corresponding |
na_rm |
whether to remove NA, false by default. |
restart_period |
the recompute period. because subtraction of elements can cause loss of precision, the computation of moments is restarted periodically based on this parameter. Larger values mean fewer restarts and faster, though potentially less accurate results. Unlike in the computation of even order moments, loss of precision is unlikely to be disastrous, so the default value is rather large. |
check_wts |
a boolean for whether the code shall check for negative weights, and throw an error when they are found. Default false for speed. |
min_df |
the minimum df to return a value, otherwise |
Details
Computes the mean or sum of the elements, using a Kahan's Compensated Summation Algorithm, a numerically robust one-pass method.
Given the length n
vector x
, we output matrix M
where
M_{i,1}
is the sum or mean
of x_{i-window+1},x_{i-window+2},...,x_{i}
.
Barring NA
or NaN
, this is over a window of size window
.
During the 'burn-in' phase, we take fewer elements. If fewer than min_df
for
running_mean
, returns NA
.
Value
A vector the same size as the input.
Note
The moment computations provided by fromo are numerically robust, but will often not provide the same results as the 'standard' implementations, due to differences in roundoff. We make every attempt to balance speed and robustness. User assumes all risk from using the fromo package.
Author(s)
Steven E. Pav shabbychef@gmail.com
References
Terriberry, T. "Computing Higher-Order Moments Online." http://people.xiph.org/~tterribe/notes/homs.html
J. Bennett, et. al., "Numerically Stable, Single-Pass, Parallel Statistics Algorithms," Proceedings of IEEE International Conference on Cluster Computing, 2009. https://www.semanticscholar.org/paper/Numerically-stable-single-pass-parallel-statistics-Bennett-Grout/a83ed72a5ba86622d5eb6395299b46d51c901265
Cook, J. D. "Accurately computing running variance." http://www.johndcook.com/standard_deviation.html
Cook, J. D. "Comparing three methods of computing standard deviation." http://www.johndcook.com/blog/2008/09/26/comparing-three-methods-of-computing-standard-deviation
Kahan, W. "Further remarks on reducing truncation errors," Communications of the ACM, 8 (1), 1965. https://doi.org/10.1145/363707.363723
Wikipedia contributors "Kahan summation algorithm," Wikipedia, The Free Encyclopedia, https://en.wikipedia.org/w/index.php?title=Kahan_summation_algorithm&oldid=777164752 (accessed May 31, 2017).
Examples
x <- rnorm(1e5)
xs <- running_sum(x,10)
xm <- running_mean(x,100)