# NAG Library Routine Document

## 1Purpose

g13ajf applies a fully specified seasonal ARIMA model to an observed time series, generates the state set for forecasting and (optionally) derives a specified number of forecasts together with their standard deviations.

## 2Specification

Fortran Interface
 Subroutine g13ajf ( mr, par, npar, c, kfc, x, nx, rms, st, ist, nst, nfv, fva, fsd, ifv, isf, w, iw,
 Integer, Intent (In) :: mr(7), npar, kfc, nx, ist, nfv, ifv, iw Integer, Intent (Inout) :: ifail Integer, Intent (Out) :: nst, isf(4) Real (Kind=nag_wp), Intent (In) :: par(npar), c, x(nx) Real (Kind=nag_wp), Intent (Out) :: rms, st(ist), fva(ifv), fsd(ifv), w(iw)
#include <nagmk26.h>
 void g13ajf_ (const Integer mr[], const double par[], const Integer *npar, const double *c, const Integer *kfc, const double x[], const Integer *nx, double *rms, double st[], const Integer *ist, Integer *nst, const Integer *nfv, double fva[], double fsd[], const Integer *ifv, Integer isf[], double w[], const Integer *iw, Integer *ifail)

## 3Description

The time series ${x}_{1},{x}_{2},\dots ,{x}_{n}$ supplied to the routine is assumed to follow a seasonal autoregressive integrated moving average (ARIMA) model with known parameters.
The model is defined by the following relations.
 (a) ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{t}-c={w}_{t}$ where ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{t}$ is the result of applying non-seasonal differencing of order $d$ and seasonal differencing of seasonality $s$ and order $D$ to the series ${x}_{t}$, and $c$ is a constant. (b) ${w}_{t}={\Phi }_{1}{w}_{t-s}+{\Phi }_{2}{w}_{t-2×s}+\cdots +{\Phi }_{P}{w}_{t-P×s}+{e}_{t}-{\Theta }_{1}{e}_{t-s}-{\Theta }_{2}{e}_{t-2×s}-\cdots -{\Theta }_{Q}{e}_{t-Q×s}\text{.}$ This equation describes the seasonal structure with seasonal period $s$; in the absence of seasonality it reduces to ${w}_{t}={e}_{t}$. (c) ${e}_{t}={\varphi }_{1}{e}_{t-1}+{\varphi }_{2}{e}_{t-2}+\cdots +{\varphi }_{p}{e}_{t-p}+{a}_{t}-{\theta }_{1}{a}_{t-1}-{\theta }_{2}{a}_{t-2}-\cdots -{\theta }_{q}{a}_{t-q}\text{.}$ This equation describes the non-seasonal structure.
Given the series, the constant $c$, and the model parameters $\Phi$, $\Theta$, $\varphi$, $\theta$, the routine computes the following.
(a) The state set required for forecasting. This contains the minimum amount of information required for forecasting and comprises:
 (i) the differenced series ${w}_{t}$, for $\left(N-s×P\right)\le t\le N$; (ii) the $\left(d+D×s\right)$ values required to reconstitute the original series ${x}_{t}$ from the differenced series ${w}_{t}$; (iii) the intermediate series ${e}_{t}$, for $N-\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,Q×s\right); (iv) the residual series ${a}_{t}$, for $\left(N-q\right), where $N=n-\left(d+D×s\right)$.
(b) A set of $L$ forecasts of ${x}_{t}$ and their estimated standard errors, ${s}_{t}$, for $\mathit{t}=n+1,\dots ,n+L$ ($L$ may be zero).
The forecasts and estimated standard errors are generated from the state set, and are identical to those that would be produced from the same state set by g13ahf.
Use of g13ajf should be confined to situations in which the state set for forecasting is unknown. Forecasting from the series requires recalculation of the state set and this is relatively expensive.
Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day

## 5Arguments

1:     $\mathbf{mr}\left(7\right)$ – Integer arrayInput
On entry: the orders vector $\left(p,d,q,P,D,Q,s\right)$ of the ARIMA model, in the usual notation.
Constraints:
• $p,d,q,P,D,Q,s\ge 0$;
• $p+q+P+Q>0$;
• $s\ne 1$;
• if $s=0$, $P+D+Q=0$;
• if $s>1$, $P+D+Q>0$;
• $d+s×\left(P+D\right)\le n$;
• $p+d-q+s×\left(P+D-Q\right)\le n$.
2:     $\mathbf{par}\left({\mathbf{npar}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the $p$ values of the $\varphi$ parameters, the $q$ values of the $\theta$ parameters, the $P$ values of the $\Phi$ parameters, and the $Q$ values of the $\Theta$ parameters, in that order.
3:     $\mathbf{npar}$ – IntegerInput
On entry: the exact number of $\varphi$, $\theta$, $\Phi$ and $\Theta$ parameters.
Constraint: ${\mathbf{npar}}=p+q+P+Q$.
4:     $\mathbf{c}$ – Real (Kind=nag_wp)Input
On entry: $c$, the expected value of the differenced series (i.e., $c$ is the constant correction). Where there is no constant term, c must be set to $0.0$.
5:     $\mathbf{kfc}$ – IntegerInput
On entry: must be set to $0$ if c was not estimated, and $1$ if c was estimated. This is irrespective of whether or not ${\mathbf{c}}=0.0$. The only effect is that the residual degrees of freedom are one greater when ${\mathbf{kfc}}=0$. Assuming the supplied time series to be the same as that to which the model was originally fitted, this ensures an unbiased estimate of the residual mean-square.
Constraint: ${\mathbf{kfc}}=0$ or $1$.
6:     $\mathbf{x}\left({\mathbf{nx}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the $n$ values of the original undifferenced time series.
7:     $\mathbf{nx}$ – IntegerInput
On entry: $n$, the length of the original undifferenced time series.
8:     $\mathbf{rms}$ – Real (Kind=nag_wp)Output
On exit: the residual variance (mean square) associated with the model.
9:     $\mathbf{st}\left({\mathbf{ist}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the nst values of the state set.
10:   $\mathbf{ist}$ – IntegerInput
On entry: the dimension of the array st as declared in the (sub)program from which g13ajf is called.
Constraint: ${\mathbf{ist}}\ge \left(P×s\right)+d+\left(D×s\right)+q+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,Q×s\right)$. The expression on the right-hand side of the inequality is returned in nst.
11:   $\mathbf{nst}$ – IntegerOutput
On exit: the number of values in the state set array st.
12:   $\mathbf{nfv}$ – IntegerInput
On entry: the required number of forecasts. If ${\mathbf{nfv}}\le 0$, no forecasts will be computed.
13:   $\mathbf{fva}\left({\mathbf{ifv}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: if ${\mathbf{nfv}}>0$, fva contains the nfv forecast values relating to the original undifferenced time series.
14:   $\mathbf{fsd}\left({\mathbf{ifv}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: if ${\mathbf{nfv}}>0$, fsd contains the estimated standard errors of the nfv forecast values.
15:   $\mathbf{ifv}$ – IntegerInput
On entry: the dimension of the arrays fva and fsd as declared in the (sub)program from which g13ajf is called.
Constraint: ${\mathbf{ifv}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nfv}}\right)$.
16:   $\mathbf{isf}\left(4\right)$ – Integer arrayOutput
On exit: contains validity indicators, one for each of the four possible parameter types in the model (autoregressive, moving average, seasonal autoregressive, seasonal moving average), in that order.
Each indicator has the interpretation:
 $-1$ On entry the set of parameter values of this type does not satisfy the stationarity or invertibility test conditions. $\phantom{-}0$ No parameter of this type is in the model. $\phantom{-}1$ Valid parameter values of this type have been supplied.
17:   $\mathbf{w}\left({\mathbf{iw}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
18:   $\mathbf{iw}$ – IntegerInput
On entry: the dimension of the array w as declared in the (sub)program from which g13ajf is called.
Constraint: ${\mathbf{iw}}\ge 6×n+5×\left(p+q+P+Q\right)+{{Q}^{\prime }}^{2}+11×{Q}^{\prime }+3×{P}^{\prime }+7$,
where ${Q}^{\prime }=Q×s+q$ and ${P}^{\prime }=P×s+p$.
19:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value  is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value  is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{kfc}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{kfc}}=0$ or $1$.
On entry, ${\mathbf{npar}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{npar}}=p+q+P+Q$.
The orders vector mr is invalid.
${\mathbf{ifail}}=2$
The model is over-parameterised. The number of parameters in the model is greater than the number of terms in the differenced series, i.e., ${\mathbf{npar}}+{\mathbf{kfc}}\ge {\mathbf{nx}}-d-D×s$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{iw}}=〈\mathit{\text{value}}〉$ and the minimum size $\text{required}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{iw}}\ge 6×n+5×\left(p+q+P+Q\right)+{{Q}^{\prime }}^{2}+11×{Q}^{\prime }+3×{P}^{\prime }+7$, where ${Q}^{\prime }=Q×s+q$ and ${P}^{\prime }=P×s+p$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{ist}}=〈\mathit{\text{value}}〉$ and the minimum size $\text{required}=〈\mathit{\text{value}}〉$ (the minimum size required is returned in nst).
Constraint: ${\mathbf{ist}}\ge \left(P×s\right)+d+\left(D×s\right)+q+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,Q×s\right)$.
${\mathbf{ifail}}=5$
Unable to calculate the latest estimates of the backforecasts.
${\mathbf{ifail}}=6$
Satisfactory parameter estimates could not be obtained for all parameter types in the model. Inspect array isf for futher information on the parameter type(s) in error.
${\mathbf{ifail}}=7$
On entry, ${\mathbf{ifv}}=〈\mathit{\text{value}}〉$ and ${\mathbf{nfv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{ifv}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{nfv}}\right)$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

g13ajf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13ajf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The time taken by g13ajf is approximately proportional to $n$ and the square of the number of backforecasts derived.

## 10Example

The data is that used in the example program for g13aff. Five forecast values and their standard errors, together with the state set, are computed and printed.

### 10.1Program Text

Program Text (g13ajfe.f90)

### 10.2Program Data

Program Data (g13ajfe.d)

### 10.3Program Results

Program Results (g13ajfe.r)