sma: Simple Moving Average (SMA)

Description Usage Arguments Details Methods (by class) References See Also Examples

View source: R/sma.R

Description

Calculate a simple moving average (SMA) of a time series by applying a moving average kernel to the sample path.

Usage

1
2
3
4
5
sma(x, ...)

## S3 method for class 'uts'
sma(x, width, interpolation = "last", align = "right",
  interior = FALSE, ...)

Arguments

x

a numeric time series object.

...

further arguments passed to or from methods.

width

a positive, finite duration object, specifying the temporal width of the rolling time window.

interpolation

the sample path interpolation method. Either "last", "next", or "linear". See below for details.

align

either "right", "left", or "center". Specifies the alignment of each output time relative to its corresponding time window. Using "right" gives a causal (i.e. backward-looking) time series operator, while using "left" gives a purely forward-looking time series operator.

interior

logical. Should time windows lie entirely in the interior of the temporal support of x, i.e. inside the time interval [start(x), end(x)]?

Details

Three different time series sample path interpolation schemes are supported for "uts" objects. Each method implicitly puts different weights on the observation values inside the rolling time window:

See the first reference below for precise mathematical definitions.

Which sample path interpolation method to use?

Depending on the application, one sample path interpolation method will often be preferable. For example, to calculate the average FED funds target rate over the past three years, it is desirable to weight each observation value by the amount of time it remained unchanged, which is achieved by using method "last". On the other hand, method "linear" can be used to estimate the rolling average value of a discretely-observed continuous-time stochastic processes (see the second reference below for a precise mathematical statement).

However, these SMAs are usually not ideally suited for analyzing discrete events, such as for calculating the average insurance loss per hurricane over the past twelve months, or for determining the average number of IBM common shares traded on the NYSE per executed order during the past 30 minutes. These quantities are unweighted averages of the observation values inside a rolling time window, and they can be calculated using rolling_apply using argument FUN=mean.

Methods (by class)

References

Eckner, A. (2017) Algorithms for Unevenly Spaced Time Series: Moving Averages and Other Rolling Operators.

Eckner, A. (2017) Some Properties of Operators for Unevenly Spaced Time Series.

See Also

ema for exponential moving averages.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
sma(ex_uts(), ddays(1))
sma(ex_uts(), ddays(1), interpolation="linear")
sma(ex_uts(), ddays(1), interpolation="next")

sma(ex_uts(), ddays(1))
sma(ex_uts(), ddays(1), align="center")
sma(ex_uts(), ddays(1), align="left")

# Plot a monotonically increasing time series 'x' together with
# a backward-looking and forward-looking SMA.
# Note how the forward-looking SMA is leading the increase in 'x', which
# in turn is leading the increase in the backward-looking SMA.
## Not run: 
  x <- uts(0:10, Sys.time() + dhours(0:10))
  par(mfrow=c(1, 3))
  plot(x, ylim=c(0, 10), main="Original time series")
  plot(sma(x, dhours(3), align="right"), ylim=c(0, 10), main="Backward-looking SMA")
  plot(sma(x, dhours(3), align="left"), ylim=c(0, 10), main="Forward-looking SMA")

## End(Not run)

# Plot three different SMAs of a monotonically increasing time series.
# Note that SMA_last(x)_t <= SMA_linear(x)_t <= SMA_next(x)_t for all observation times t
## Not run: 
  x <- uts(0:8, Sys.time() + dhours(0:8))
  par(mfrow=c(1, 3))
  plot(sma(x, dhours(10), interpolation="last"), ylim=c(0, 4), main="Last-point interpolation")
  plot(sma(x, dhours(10), interpolation="linear"), ylim=c(0, 4), main="Linear interpolation")
  plot(sma(x, dhours(10), interpolation="next"), ylim=c(0, 4), main="Next-point interpolation")

## End(Not run)

andreas50/utsOperators documentation built on May 25, 2019, 7:16 a.m.