run_max | R Documentation |
Calculate the trailing maximum values of streaming time series data using an online recursive formula.
run_max(tseries, lambda)
tseries |
A time series or a matrix. |
lambda |
A decay factor which multiplies past estimates. |
The function run_max()
calculates the trailing maximum values of
streaming time series data by recursively weighting present and past
values using the decay factor \lambda
.
It calculates the trailing maximum values p^{max}_t
of the streaming
data p_t
as follows:
p^{max}_t = max(p_t, \lambda p^{max}_{t-1} + (1-\lambda) p_t)
The first term in the sum is the maximum value multiplied by the decay
factor \lambda
, so that the past maximum value is gradually
"forgotten". The second term pulls the maximum value to the current value
p_t
.
The value of the decay factor \lambda
must be in the range between
0
and 1
.
If \lambda
is close to 1
then the past maximum values persist
for longer. This is equivalent to a long look-back interval.
If \lambda
is much less than 1
then the past maximum values
decay quickly, and the trailing maximum depends on the more recent
streaming data. This is equivalent to a short look-back interval.
The above formula can also be expressed as:
p^{max}_t = \lambda max(p_t, p^{max}_{t-1}) + (1-\lambda) p_t
The first term is the maximum value multiplied by the decay factor
\lambda
, so that the past maximum value is gradually "forgotten".
The second term pulls the maximum value to the current value p_t
.
The above recursive formula is convenient for processing live streaming data because it doesn't require maintaining a buffer of past data.
The function run_max()
returns a matrix with the same
dimensions as the input argument tseries
.
A matrix with the same dimensions as the input argument
tseries
.
## Not run:
# Calculate historical prices
closep <- zoo::coredata(quantmod::Cl(rutils::etfenv$VTI))
# Calculate the trailing maximums
lambdaf <- 0.9 # Decay factor
pricmax <- HighFreq::run_max(closep, lambda=lambdaf)
# Plot dygraph of VTI prices and trailing maximums
datav <- cbind(quantmod::Cl(rutils::etfenv$VTI), pricmax)
colnames(datav) <- c("prices", "max")
colnamev <- colnames(datav)
dygraphs::dygraph(datav, main="VTI Prices and Trailing Maximums") %>%
dySeries(label=colnamev[1], strokeWidth=2, col="blue") %>%
dySeries(label=colnamev[2], strokeWidth=2, col="red")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.