Title: | Forecasting for Stationary and Non-Stationary Time Series |
---|---|
Description: | Methods to compute linear h-step ahead prediction coefficients based on localised and iterated Yule-Walker estimates and empirical mean squared and absolute prediction errors for the resulting predictors. Also, functions to compute autocovariances for AR(p) processes, to simulate tvARMA(p,q) time series, and to verify an assumption from Kley et al. (2017), Preprint <http://personal.lse.ac.uk/kley/forecastSNSTS.pdf>. |
Authors: | Tobias Kley [aut, cre], Philip Preuss [aut], Piotr Fryzlewicz [aut] |
Maintainer: | Tobias Kley <[email protected]> |
License: | GPL (>= 2) |
Version: | 1.2-0.9000 |
Built: | 2025-03-04 03:16:24 UTC |
Source: | https://github.com/tobiaskley/forecastsnsts |
Methods to compute linear -step ahead prediction coefficients based
on localised and iterated Yule-Walker estimates and empirical mean squared
and absolute prediction errors for the resulting predictors. Also, functions
to compute autocovariances for AR(p) processes, to simulate tvARMA(p,q) time
series, and to verify an assumption from Kley et al. (2017).
Package: | forecastSNSTS |
Type: | Package |
Version: | 1.2-0 |
Date: | 2017-06-18 |
License: | GPL (>= 2) |
The core functionality of this R package is accessable via the function
predCoef
, which is used to compute the linear prediction
coefficients, and the functions MSPE
and MAPE
,
which are used to compute the empirical mean squared or absolute prediction
errors. Further, the function f
can be used to verify
condition (10) of Theorem 3.1 in Kley et al. (2017) for any given tvAR(p) model.
The function tvARMA
can be used to simulate time-varying
ARMA(p,q) time series.
The function acfARp
computes the autocovariances of a AR(p)
process from the coefficients and innovations standard deviation.
Tobias Kley
Kley, T., Preuss, P. & Fryzlewicz, P. (2017). Predictive, finite-sample model choice for time series under stationarity and non-stationarity. [cf. http://personal.lse.ac.uk/kley/forecastSNSTS.pdf]
This functions returns the autocovariances of a
stationary time series
that fulfills the following equation:
where ,
is white noise and
are real numbers satisfying that the roots
of the polynomial
lie strictly outside the unit circle.
acfARp(a = NULL, sigma, k)
acfARp(a = NULL, sigma, k)
a |
vector |
sigma |
standard deviation of |
k |
lag for which to compute the autocovariances. |
Returns autocovariance at lag k of the AR(p) process.
## Taken from Section 6 in Dahlhaus (1997, AoS) a1 <- function(u) {1.8 * cos(1.5 - cos(4*pi*u))} a2 <- function(u) {-0.81} # local autocovariance for u === 1/2: lag 1 acfARp(a = c(a1(1/2), a2(1/2)), sigma = 1, k = 1) # local autocovariance for u === 1/2: lag -2 acfARp(a = c(a1(1/2), a2(1/2)), sigma = 1, k = -1) # local autocovariance for u === 1/2: the variance acfARp(a = c(a1(1/2), a2(1/2)), sigma = 1, k = 0)
## Taken from Section 6 in Dahlhaus (1997, AoS) a1 <- function(u) {1.8 * cos(1.5 - cos(4*pi*u))} a2 <- function(u) {-0.81} # local autocovariance for u === 1/2: lag 1 acfARp(a = c(a1(1/2), a2(1/2)), sigma = 1, k = 1) # local autocovariance for u === 1/2: lag -2 acfARp(a = c(a1(1/2), a2(1/2)), sigma = 1, k = -1) # local autocovariance for u === 1/2: the variance acfARp(a = c(a1(1/2), a2(1/2)), sigma = 1, k = 0)
This function computes the estimated mean squared prediction errors from a given time series and prediction coefficients
X |
the data |
coef |
the array of coefficients. |
h |
which lead time to compute the MSPE for |
t |
a vector of times from which backward the forecasts are computed |
type |
indicating what type of measure of accuracy is to be computed; 1: mspe, 2: msae |
trimLo |
percentage of lower observations to be trimmed away |
trimUp |
percentage of upper observations to be trimmed away |
The array of prediction coefficients coef
is expected to be of
dimension P x P x H x length(N) x length(t)
and in the format as
it is returned by the function predCoef
. More precisely, for
and the
j.N
th element of N
element of
N
the coefficient of the
h
-step ahead predictor for which is computed from
the observations
has to be available via
coef[p, 1:p, h, j.N, t==i]
.
Note that t
have to be the indices corresponding to the coefficients.
The resulting mean squared prediction error
is then stored in the resulting matrix at position (p, j.N)
.
Returns a P x length(N)
matrix with the results.
for a tvAR(p) processThis functions computes the quantity defined in (10) of
Kley et al. (2017) when the underlying process follows an tvAR(p) process.
Recall that, to apply Theorem 3.1 in Kley et al. (2017), the function
is required to be positive, which can be verified with the
numbers returned from this function.
The function returns a vector with elements
for each
in
which.deltas
, with
defined as
where are positive integers,
, and
.
f(which.deltas, p_max, h, T, Ns, m, a, sigma)
f(which.deltas, p_max, h, T, Ns, m, a, sigma)
which.deltas |
vector containing the |
p_max |
parameter |
h |
parameter |
T |
parameter |
Ns |
a vector containing the elements of the set
|
m |
parameter |
a |
a list of real-valued functions, specifying the coefficients of the tvAR(p) process, |
sigma |
a positive-valued function, specifying the variance of the innovations of the tvAR(p) process, |
The function is defined, for real-valued
and
, in terms of the second order properties of the process:
with and, for
,
where
with and
defined in the documentation of
predCoef
and,
for every real-valued and
,
where
The local autocovariances are defined as the lag-
autocovariances of an AR(p) process which has coefficients
and innovations with variance
,
because the underlying model is assumed to be tvAR(p)
where are real valued functions (defined on
) and
is a
positive function (defined on
).
Returns a vector with the values , as defined in
(10) of Kley et al. (2017), for each
in
which.delta
.
## Not run: ## because computation is quite time-consuming. n <- 100 a <- list( function(u) {return(0.8+0.19*sin(4*pi*u))} ) sigma <- function (u) {return(1)} Ns <- seq( floor((n/2)^(4/5)), floor(n^(4/5)), ceiling((floor(n^(4/5)) - floor((n/2)^(4/5)))/25) ) which.deltas <- c(0, 0.01, 0.05, 0.1, 0.15, 0.2, 0.4, 0.6) P_max <- 7 H <- 1 m <- floor(n^(.85)/4) # now replicate some results from Table 4 in Kley et al. (2017) f( which.deltas, P_max, h = 1, n - m, Ns, m, a, sigma ) f( which.deltas, P_max, h = 5, n - m, Ns, m, a, sigma ) ## End(Not run)
## Not run: ## because computation is quite time-consuming. n <- 100 a <- list( function(u) {return(0.8+0.19*sin(4*pi*u))} ) sigma <- function (u) {return(1)} Ns <- seq( floor((n/2)^(4/5)), floor(n^(4/5)), ceiling((floor(n^(4/5)) - floor((n/2)^(4/5)))/25) ) which.deltas <- c(0, 0.01, 0.05, 0.1, 0.15, 0.2, 0.4, 0.6) P_max <- 7 H <- 1 m <- floor(n^(.85)/4) # now replicate some results from Table 4 in Kley et al. (2017) f( which.deltas, P_max, h = 1, n - m, Ns, m, a, sigma ) f( which.deltas, P_max, h = 5, n - m, Ns, m, a, sigma ) ## End(Not run)
-step ahead prediction errorsThe function MSPE
computes the empirical mean squared prediction
errors for a collection of -step ahead, linear predictors
(
) of observations
, where
, for two indices
and
.
The resulting array provides
with being the prediction errors
ordered by magnitude; i.e., they are such that .
The lower and upper limits of the indices are
and
.
The function
MAPE
computes the empirical mean absolute prediction
errors
with ,
and
defined as before.
MSPE(X, predcoef, m1 = length(X)/10, m2 = length(X), P = 1, H = 1, N = c(0, seq(P + 1, m1 - H + 1)), trimLo = 0, trimUp = 0) MAPE(X, predcoef, m1 = length(X)/10, m2 = length(X), P = 1, H = 1, N = c(0, seq(P + 1, m1 - H + 1)), trimLo = 0, trimUp = 0)
MSPE(X, predcoef, m1 = length(X)/10, m2 = length(X), P = 1, H = 1, N = c(0, seq(P + 1, m1 - H + 1)), trimLo = 0, trimUp = 0) MAPE(X, predcoef, m1 = length(X)/10, m2 = length(X), P = 1, H = 1, N = c(0, seq(P + 1, m1 - H + 1)), trimLo = 0, trimUp = 0)
X |
the data |
predcoef |
the prediction coefficients in form of a list of an array
|
m1 |
first index from the set in which the indices |
m2 |
last index from the set in which the indices |
P |
maximum order of prediction coefficients to be used;
must not be larger than |
H |
maximum lead time to be used;
must not be larger than |
N |
vector with the segment sizes to be used, 0 corresponds to using 1, ..., t; has to be a subset of predcoef$N. |
trimLo |
percentage |
trimUp |
percentage |
MSPE
returns an object of type MSPE
that has mspe
,
an array of size H
P
length(N)
,
as an attribute, as well as the parameters N
, m1
,
m2
, P
, and H
.
MAPE
analogously returns an object of type MAPE
that
has mape
and the same parameters as attributes.
T <- 1000 X <- rnorm(T) P <- 5 H <- 1 m <- 20 Nmin <- 20 pcoef <- predCoef(X, P, H, (T - m - H + 1):T, c(0, seq(Nmin, T - m - H, 1))) mspe <- MSPE(X, pcoef, 991, 1000, 3, 1, c(0, Nmin:(T-m-H))) plot(mspe, vr = 1, Nmin = Nmin)
T <- 1000 X <- rnorm(T) P <- 5 H <- 1 m <- 20 Nmin <- 20 pcoef <- predCoef(X, P, H, (T - m - H + 1):T, c(0, seq(Nmin, T - m - H, 1))) mspe <- MSPE(X, pcoef, 991, 1000, 3, 1, c(0, Nmin:(T-m-H))) plot(mspe, vr = 1, Nmin = Nmin)
MSPE
or MAPE
objectThe function plot.MSPE
plots a MSPE
object that is returned by
the MSPE
function.
The function plot.MAPE
plots a MAPE
object that is returned by
the MAPE
function.
## S3 method for class 'MSPE' plot(x, vr = NULL, h = 1, N_min = 1, legend = TRUE, display.mins = TRUE, add.for.legend = 0, ...) ## S3 method for class 'MAPE' plot(x, vr = NULL, h = 1, N_min = 1, legend = TRUE, display.mins = TRUE, add.for.legend = 0, ...)
## S3 method for class 'MSPE' plot(x, vr = NULL, h = 1, N_min = 1, legend = TRUE, display.mins = TRUE, add.for.legend = 0, ...) ## S3 method for class 'MAPE' plot(x, vr = NULL, h = 1, N_min = 1, legend = TRUE, display.mins = TRUE, add.for.legend = 0, ...)
x |
The |
vr |
parameter to plot a line at level |
h |
Defines for which |
N_min |
If specified, the mean squared prediction errors with
|
legend |
Flag to specify if a legend, indicating which colour of the
lines corresponds to which |
display.mins |
Flag to specify if the minima for each |
add.for.legend |
add this much extra space for the legend, right of the lines. |
... |
Arguments to be passed to the underlying plot method |
Returns the plot, as specified.
-step Prediction coefficientsThis function computes the localised and iterated Yule-Walker coefficients
for h-step ahead forecasting of from
,
where
H
and
P
.
X |
the data |
P |
the maximum order of coefficients to be computed; has to be a positive integer |
H |
the maximum lead time; has to be a positive integer |
t |
a vector of values |
N |
a vector of values |
For every
t
and every
N
the (iterated) Yule-Walker
estimates are computed. They are defined as
and
with
and
where
and
is the usual lag- autocovariance estimator (without mean adjustment),
computed from the observations
.
The Durbin-Levinson Algorithm is used to successively compute the solutions to the
Yule-Walker equations (cf. Brockwell/Davis (1991), Proposition 5.2.1).
To compute the -step ahead coefficients we use the recursive relationship
(cf. the proof of Lemma E.3 in Kley et al. (2017)).
Returns a named list with elements coef
, t
, and N
,
where coef
is an array of dimension
P
P
H
length(t)
length(N)
, and
t
, and N
are the parameters provided on the call of the
function. See the example on how to access the vector
.
Brockwell, P. J. & Davis, R. A. (1991). Time Series: Theory and Methods. Springer, New York.
T <- 100 X <- rnorm(T) P <- 5 H <- 1 m <- 20 Nmin <- 25 pcoef <- predCoef(X, P, H, (T - m - H + 1):T, c(0, seq(Nmin, T - m - H, 1))) ## Access the prediction vector for p = 2, h = 1, t = 95, N = 25 p <- 2 h <- 1 t <- 95 N <- 35 res <- pcoef$coef[p, 1:p, h, pcoef$t == t, pcoef$N == N]
T <- 100 X <- rnorm(T) P <- 5 H <- 1 m <- 20 Nmin <- 25 pcoef <- predCoef(X, P, H, (T - m - H + 1):T, c(0, seq(Nmin, T - m - H, 1))) ## Access the prediction vector for p = 2, h = 1, t = 95, N = 25 p <- 2 h <- 1 t <- 95 N <- 35 res <- pcoef$coef[p, 1:p, h, pcoef$t == t, pcoef$N == N]
Returns a simulated time series that fulfills
the following equation:
where are real-valued functions on
,
is a positive function on
and
is white noise.
tvARMA(T = 128, a = list(), b = list(), sigma = function(u) { return(1) }, innov = function(n) { rnorm(n, 0, 1) })
tvARMA(T = 128, a = list(), b = list(), sigma = function(u) { return(1) }, innov = function(n) { rnorm(n, 0, 1) })
T |
length of the time series to be returned |
a |
list of p real-valued functions defined on |
b |
list of q real-valued functions defined on |
sigma |
function |
innov |
a function with one argument |
Returns a tvARMA(p,q) time series with specified parameters.
## Taken from Section 6 in Dahlhaus (1997, AoS) a1 <- function(u) {1.8 * cos(1.5 - cos(4 * pi * u))} a2 <- function(u) {-0.81} plot(tvARMA(128, a = list(a1, a2), b = list()), type = "l")
## Taken from Section 6 in Dahlhaus (1997, AoS) a1 <- function(u) {1.8 * cos(1.5 - cos(4 * pi * u))} a2 <- function(u) {-0.81} plot(tvARMA(128, a = list(a1, a2), b = list()), type = "l")
More explanation!
z |
a ... |
x_int |
a ... |
A |
... |
B |
a ... |
Sigma |
a ... |
Returns a ...