# NAG Library Routine Document

## 1Purpose

g13ahf produces forecasts of a time series, given a time series model which has already been fitted to the time series using g13aef or g13aff. The original observations are not required, since g13ahf uses as input either the original state set produced by g13aef or g13aff or the state set updated by a series of new observations using g13agf. Standard errors of the forecasts are also provided.

## 2Specification

Fortran Interface
 Subroutine g13ahf ( st, nst, mr, par, npar, c, rms, nfv, fva, fsd, wa, nwa,
 Integer, Intent (In) :: nst, mr(7), npar, nfv, nwa Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: st(nst), par(npar), c, rms Real (Kind=nag_wp), Intent (Out) :: fva(nfv), fsd(nfv), wa(nwa)
#include <nagmk26.h>
 void g13ahf_ (const double st[], const Integer *nst, const Integer mr[], const double par[], const Integer *npar, const double *c, const double *rms, const Integer *nfv, double fva[], double fsd[], double wa[], const Integer *nwa, Integer *ifail)

## 3Description

The original time series is ${x}_{\mathit{t}}$, for $\mathit{t}=1,2,\dots ,n$ and parameters have been fitted to the model of this time series using g13aef or g13aff.
Forecasts of ${x}_{\mathit{t}}$, for $\mathit{t}=n+1,\dots ,n+L$, are calculated in five stages, as follows:
 (i) set ${a}_{t}=0$ for $t=N+1,N+2,\dots ,N+L$, where $N=n-d-\left(D×s\right)$ is the number of differenced values in the series; (ii) calculate the values of ${e}_{\mathit{t}}$, for $\mathit{t}=N+1,\dots ,N+L$, and ${e}_{\mathit{t}}={\varphi }_{1}×{e}_{t-1}+\cdots +\phantom{\rule{0ex}{0ex}}{\varphi }_{p}×{e}_{t-p}+{a}_{t}-{\theta }_{1}×{a}_{t-1}-\cdots -{\theta }_{q}×{a}_{t-q}$; (iii) calculate the values of ${w}_{\mathit{t}}$, for $\mathit{t}=N+1,\dots ,N+L$, where ${w}_{t}={\Phi }_{1}×{w}_{t-s}+\cdots +\phantom{\rule{0ex}{0ex}}{\Phi }_{P}×{w}_{t-s×P}+{e}_{t}-{\Theta }_{1}×{e}_{t-s}-\cdots -{\Theta }_{Q}×{e}_{t-s×Q}$ and ${w}_{t}$ for $t\le N$ are the first $s×P$ values in the state set, corrected for the constant; (iv) add the constant term $c$ to give the differenced series ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{\mathit{t}}={w}_{\mathit{t}}+c$, for $\mathit{t}=N+1,\dots ,N+L$; (v) the differencing operations are reversed to reconstitute ${x}_{\mathit{t}}$, for $\mathit{t}=n+1,\dots ,n+L$.
The standard errors of these forecasts are given by ${s}_{\mathit{t}}={\left[V×\left({\psi }_{0}^{2}+{\psi }_{1}^{2}+\dots +{\psi }_{\mathit{t}-n-1}^{2}\right)\right]}^{1/2}$, for $\mathit{t}=n+1,\dots ,n+L$, where ${\psi }_{0}=1$, $V$ is the residual variance of ${a}_{t}$, and ${\psi }_{j}$ is the coefficient expressing the dependence of ${x}_{t}$ on ${a}_{t-j}$.
To calculate ${\psi }_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,\left(L-1\right)$, the following device is used.
A copy of the state set is initialized to zero throughout and the calculations outlined above for the construction of forecasts are carried out with the settings ${a}_{N+1}=1$, and ${a}_{\mathit{t}}=0$, for $\mathit{t}=N+2,\dots ,N+L$.
The resulting quantities corresponding to the sequence ${x}_{N+1},{x}_{N+2},\dots ,{x}_{N+L}$ are precisely $1$, ${\psi }_{1},{\psi }_{2},\dots ,{\psi }_{L-1}$.
The supplied time series model is used throughout these calculations, with the exception that the constant term $c$ is taken to be zero.

None.

## 5Arguments

1:     $\mathbf{st}\left({\mathbf{nst}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the state set derived from g13aef or g13aff originally, or as modified using earlier calls of g13agf.
2:     $\mathbf{nst}$ – IntegerInput
On entry: the number of values in the state set array st.
Constraint: ${\mathbf{nst}}=P×s+D×s+d+q+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,Q×s\right)$. (As returned by g13aef or g13aff).
3:     $\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$.
4:     $\mathbf{par}\left({\mathbf{npar}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the estimates of 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 which specify the model and which were output originally by g13aef or g13aff.
5:     $\mathbf{npar}$ – IntegerInput
On entry: the number of $\varphi$, $\theta$, $\Phi$ and $\Theta$ parameters in the model.
Constraint: ${\mathbf{npar}}=p+q+P+Q$.
6:     $\mathbf{c}$ – Real (Kind=nag_wp)Input
On entry: $c$, the value of the model constant. This will have been output by g13aef or g13aff.
7:     $\mathbf{rms}$ – Real (Kind=nag_wp)Input
On entry: $V$, the residual variance associated with the model.
If g13aff was used to estimate the model, rms should be set to ${\mathbf{s}}/{\mathbf{ndf}}$, where s and ndf were output by g13aff.
If g13aef was used to estimate the model, rms should be set to ${\mathbf{s}}/{\mathbf{icount}}\left(5\right)$, where s and ${\mathbf{icount}}\left(5\right)$ were output by g13aef.
Constraint: ${\mathbf{rms}}\ge 0.0$.
8:     $\mathbf{nfv}$ – IntegerInput
On entry: $L$, the required number of forecasts.
Constraint: ${\mathbf{nfv}}>0$.
9:     $\mathbf{fva}\left({\mathbf{nfv}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: nfv forecast values relating to the original undifferenced series.
10:   $\mathbf{fsd}\left({\mathbf{nfv}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the standard errors associated with each of the nfv forecast values in fva.
11:   $\mathbf{wa}\left({\mathbf{nwa}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
12:   $\mathbf{nwa}$ – IntegerInput
On entry: the dimension of the array wa as declared in the (sub)program from which g13ahf is called.
Constraint: ${\mathbf{nwa}}\ge \left(4×{\mathbf{npar}}+3×{\mathbf{nst}}\right)$.
13:   $\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{npar}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{npar}}=p+q+P+Q$.
The orders vector mr is invalid.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{nst}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nst}}=P×s+D×s+d+q+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(Q×s,p\right)$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{nfv}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nfv}}>0$.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{nwa}}=〈\mathit{\text{value}}〉$ and the minimum size $\text{required}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{nwa}}\ge 4×{\mathbf{npar}}+3×{\mathbf{nst}}$.
${\mathbf{ifail}}=5$
On entry, ${\mathbf{rms}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{rms}}\ge 0.0$.
${\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

g13ahf is not threaded in any implementation.

The time taken by g13ahf is approximately proportional to ${\mathbf{nfv}}×{\mathbf{npar}}$.

## 10Example

The following program is based on the data derived in the example used to illustrate g13agf.
These consist of a set of orders indicating that there are two moving average parameters (one non-seasonal, and one seasonal with periodicity $12$).
The model constant is zero.
The state set contains $26$ values.
In addition the residual mean-square derived when the model was originally fitted is given.
Twelve forecasts and their associated errors are obtained.

### 10.1Program Text

Program Text (g13ahfe.f90)

### 10.2Program Data

Program Data (g13ahfe.d)

### 10.3Program Results

Program Results (g13ahfe.r)