ema: Exponential Moving Average (EMA)

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

View source: R/ema.R

Description

Calculate an exponential moving average (EMA) of a time series by applying an exponential kernel to the time series sample path.

Usage

1
2
3
4
ema(x, ...)

## S3 method for class 'uts'
ema(x, tau, interpolation = "last", ...)

Arguments

x

a numeric time series object.

...

further arguments passed to or from methods.

tau

a finite duration object, specifying the effective temporal length of the EMA. Use positive values for backward-looking (i.e. normal, causal) EMAs, and negative values for forward-looking EMAs.

interpolation

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

Details

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

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. See the corresponding discussion for simple moving averages.

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

sma for simple 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
ema(ex_uts(), ddays(1))
ema(ex_uts(), ddays(1), interpolation="linear")
ema(ex_uts(), ddays(1), interpolation="next")

# Plot a monotonically increasing time series 'x', together with
# a backward-looking and forward-looking EMA.
# 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(ema(x, dhours(3)), ylim=c(0, 10), main="Backward-looking EMA")
  plot(ema(x, dhours(-3)), ylim=c(0, 10), main="Forward-looking EMA")

## End(Not run)

# Plot three different EMAs of a monotonically increasing time series
# Note that EMA_last(x)_t <= EMA_linear(x)_t <= EMA_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(ema(x, dhours(10), interpolation="last"), ylim=c(0, 3), main="Last-point interpolation")
  plot(ema(x, dhours(10), interpolation="linear"), ylim=c(0, 3), main="Linear interpolation")
  plot(ema(x, dhours(10), interpolation="next"), ylim=c(0, 3), main="Next-point interpolation")

## End(Not run)

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