NAG Library Routine Document
G13AFF
1 Purpose
G13AFF is an easytouse version of
G13AEF. It fits a seasonal autoregressive integrated moving average (ARIMA) model to an observed time series, using a nonlinear least squares procedure incorporating backforecasting. Parameter estimates are obtained, together with appropriate standard errors. The residual series is returned, and information for use in forecasting the time series is produced for use in
G13AGF and
G13AHF.
The estimation procedure is iterative, starting with initial parameter values such as may be obtained using
G13ADF. It continues until a specified convergence criterion is satisfied or until a specified number of iterations have been carried out. The progress of the iteration can be monitored by means of an optional printing facility.
2 Specification
SUBROUTINE G13AFF ( 
MR, PAR, NPAR, C, KFC, X, NX, S, NDF, SD, NPPC, CM, LDCM, ST, NST, KPIV, NIT, ITC, ISF, RES, IRES, NRES, IFAIL) 
INTEGER 
MR(7), NPAR, KFC, NX, NDF, NPPC, LDCM, NST, KPIV, NIT, ITC, ISF(4), IRES, NRES, IFAIL 
REAL (KIND=nag_wp) 
PAR(NPAR), C, X(NX), S, SD(NPPC), CM(LDCM,NPPC), ST(NX), RES(IRES) 

3 Description
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 defined as follows:
where
${\nabla}^{d}{\nabla}_{s}^{D}{x}_{t}$ is the result of applying nonseasonal differencing of order
$d$ and seasonal differencing of seasonality
$s$ and order
$D$ to the series
${x}_{t}$, as outlined in the description of
G13AAF. The differenced series is then of length
$N=n{d}^{\prime}$, where
${d}^{\prime}=d+\left(D\times s\right)$ is the generalized order of differencing. The scalar
$c$ is the expected value of the differenced series, and the series
${w}_{1},{w}_{2},\dots ,{w}_{N}$ follows a zeromean stationary autoregressive moving average (ARMA) model defined by a pair of recurrence equations. These express
${w}_{t}$ in terms of an uncorrelated series
${a}_{t}$, via an intermediate series
${e}_{t}$. The first equation describes the seasonal structure:
The second equation describes the nonseasonal structure. If the model is purely nonseasonal the first equation is redundant and
${e}_{t}$ above is equated with
${w}_{t}$:
Estimates of the model parameters defined by
and (optionally)
$c$ are obtained by minimizing a quadratic form in the vector
$w={\left({w}_{1},{w}_{2},\dots ,{w}_{N}\right)}^{\prime}$.
The minimization process is iterative, iterations being performed until convergence is achieved (see
Section 3 in G13AEF for full details), or until the userspecified maximum number of iterations are completed.
The final values of the residual sum of squares and the parameter estimates are used to obtain asymptotic approximations to the standard deviations of the parameters, and the correlation matrix for the parameters. The ‘state set’ array of information required by forecasting is also returned.
Note: if the maximum number of iterations are performed without convergence, these quantities may not be reliable. In this case, the sequence of iterates should be checked, using the optional monitoring routine, to verify that convergence is adequate for practical purposes.
4 References
Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day
Marquardt D W (1963) An algorithm for leastsquares estimation of nonlinear parameters J. Soc. Indust. Appl. Math. 11 431
5 Parameters
 1: MR($7$) – INTEGER arrayInput
On entry: the orders vector $\left(p,d,q,P,D,Q,s\right)$ of the ARIMA model whose parameters are to be estimated. $p$, $q$, $P$ and $Q$ refer respectively to the number of autoregressive $\left(\varphi \right)$, moving average $\left(\theta \right)$, seasonal autoregressive $\left(\Phi \right)$ and seasonal moving average $\left(\Theta \right)$ parameters. $d$, $D$ and $s$ refer respectively to the order of nonseasonal differencing, the order of seasonal differencing and the seasonal period.
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\times \left(P+D\right)\le n$;
 $p+dq+s\times \left(P+DQ\right)\le n$.
 2: PAR(NPAR) – REAL (KIND=nag_wp) arrayInput/Output
On entry: the initial 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, in that order.
On exit: contains the latest values of the estimates of these parameters.
 3: NPAR – INTEGERInput
On entry: the total number of $\varphi $, $\theta $, $\Phi $ and $\Theta $ parameters to be estimated.
Constraint:
${\mathbf{NPAR}}=p+q+P+Q$.
 4: C – REAL (KIND=nag_wp)Input/Output
On entry: if
${\mathbf{KFC}}=0$,
C must contain the expected value,
$c$, of the differenced series.
If
${\mathbf{KFC}}=1$,
C must contain an initial estimate of
$c$.
Therefore, if
C and
KFC are both zero on entry, there is no constant correction.
On exit: if
${\mathbf{KFC}}=0$,
C is unchanged.
If
${\mathbf{KFC}}=1$,
C contains the latest estimate of
$c$.
 5: KFC – INTEGERInput
On entry: must be set to $1$ if the constant, $c$, is to be estimated and $0$ if it is to be held fixed at its initial value.
Constraint:
${\mathbf{KFC}}=0$ or $1$.
 6: X(NX) – REAL (KIND=nag_wp) arrayInput
On entry: the $n$ values of the original undifferenced time series.
 7: NX – INTEGERInput
On entry: $n$, the length of the original undifferenced time series.
 8: S – REAL (KIND=nag_wp)Output
On exit: the residual sum of squares after the latest series of parameter estimates has been incorporated into the model. If the routine exits with a faulty input parameter,
S contains zero.
 9: NDF – INTEGEROutput
On exit: the number of degrees of freedom associated with
S,
${\mathbf{NDF}}=ndD\times spqPQ{\mathbf{KFC}}$.
 10: SD(NPPC) – REAL (KIND=nag_wp) arrayOutput
On exit: the standard deviations corresponding to the parameters in the model (
$p$ autoregressive,
$q$ moving average,
$P$ seasonal autoregressive,
$Q$ seasonal moving average and
$c$, if estimated, in that order). If the routine exits with
IFAIL containing a value other than
$0$ or
$9$, or if the required number of iterations is zero, the contents of
SD will be indeterminate.
 11: NPPC – INTEGERInput
On entry: the number of $\varphi $, $\theta $, $\Phi $, $\Theta $ and $c$ parameters to be estimated. ${\mathbf{NPPC}}=p+q+P+Q+1$ if the constant is being estimated and ${\mathbf{NPPC}}=p+q+P+Q$ if not.
Constraint:
${\mathbf{NPPC}}={\mathbf{NPAR}}+{\mathbf{KFC}}$.
 12: CM(LDCM,NPPC) – REAL (KIND=nag_wp) arrayOutput
On exit: the correlation coefficients associated with each pair of the
NPPC parameters. These are held in the first
NPPC rows and the first
NPPC columns of
CM. These correlation coefficients are indeterminate if
IFAIL contains on exit a value other than
$0$ or
$9$, or if the required number of iterations is zero.
 13: LDCM – INTEGERInput
On entry: the first dimension of the array
CM as declared in the (sub)program from which G13AFF is called.
Constraint:
${\mathbf{LDCM}}\ge {\mathbf{NPPC}}$.
 14: ST(NX) – REAL (KIND=nag_wp) arrayOutput
On exit: the value of the state set in its first
NST elements. If the routine exits with
IFAIL containing a value other than
$0$ or
$9$, the contents of
ST will be indeterminate.
 15: NST – INTEGEROutput
On exit: the size of the state set.
${\mathbf{NST}}=P\times s+D\times s+d+q+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(p,Q\times s\right)$.
NST should be used subsequently in
G13AGF and
G13AHF as the dimension of
ST.
 16: KPIV – INTEGERInput
On entry: must be nonzero if the progress of the optimization is to be monitored using the builtin printing facility. Otherwise
KPIV must contain zero. If selected, monitoring output will be sent to the current advisory message unit defined by
X04ABF. For each iteration, the heading
G13AFZ MONITORING OUTPUT  ITERATION n
followed by the parameter values, and residual sum of squares, are printed.
 17: NIT – INTEGERInput
On entry: the maximum number of iterations to be performed.
Constraint:
${\mathbf{NIT}}\ge 0$.
 18: ITC – INTEGEROutput
On exit: the number of iterations performed.
 19: ISF($4$) – INTEGER arrayOutput
On exit: the first four elements of
ISF contain success/failure indicators, one for each of the four types of parameter in the model (autoregressive, moving average, seasonal autoregressive, seasonal moving average), in that order.
Each indicator has the interpretation:
$2$ 
On entry parameters of this type have initial estimates which do not satisfy the stationarity or invertibility test conditions. 
$1$ 
The search procedure has failed to converge because the latest set of parameter estimates of this type is invalid. 
$\phantom{}0$ 
No parameter of this type is in the model. 
$\phantom{}1$ 
Valid final estimates for parameters of this type have been obtained. 
 20: RES(IRES) – REAL (KIND=nag_wp) arrayOutput
On exit: the first
NRES elements of
RES contain the model residuals derived from the differenced series. If the routine exits with
IFAIL holding a value other than
$0$ or
$9$, these elements of
RES will be indeterminate. The rest of the array
RES is used as workspace.
 21: IRES – INTEGERInput
On entry: the dimension of the array
RES as declared in the (sub)program from which G13AFF is called.
Constraint:
${\mathbf{IRES}}\ge 15\times {Q}^{\prime}+11n+13\times {\mathbf{NPPC}}+8\times {P}^{\prime}+12+2\times {\left({Q}^{\prime}+{\mathbf{NPPC}}\right)}^{2}$, where ${P}^{\prime}=p+\left(P\times s\right)$ and ${Q}^{\prime}=q+\left(Q\times s\right)$.
 22: NRES – INTEGEROutput
On exit: the number of model residuals returned in
RES.
 23: IFAIL – INTEGERInput/Output

On entry:
IFAIL must be set to
$0$,
$1\text{ or}1$. If you are unfamiliar with this parameter you should refer to
Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
$1\text{ or}1$ is recommended. If the output of error messages is undesirable, then the value
$1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if
${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is
$1$.
When the value $\mathbf{1}\text{ or}1$ 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).
6 Error Indicators and Warnings
If on entry
${\mathbf{IFAIL}}={\mathbf{0}}$ or
${{\mathbf{1}}}$, explanatory error messages are output on the current error message unit (as defined by
X04AAF).
Note: G13AFF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
 ${\mathbf{IFAIL}}=1$
On entry,  ${\mathbf{NPAR}}\ne p+q+P+Q$, 
or  the orders vector MR is invalid (check the constraints in Section 5), 
or  ${\mathbf{KFC}}\ne 0$ or $1$, 
or  ${\mathbf{NPPC}}\ne {\mathbf{NPAR}}+{\mathbf{KFC}}$. 
 ${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{NX}}dD\times s\le {\mathbf{NPAR}}+{\mathbf{KFC}}$, i.e., the number of terms in the differenced series is not greater than the number of parameters in the model. The model is overparameterised.
 ${\mathbf{IFAIL}}=3$
On entry,  ${\mathbf{NIT}}<0$. 
 ${\mathbf{IFAIL}}=4$
On entry, the required size of the state set array
ST is greater than
NX. This occurs only for very unusual models with long seasonal periods or large numbers of parameters. First check that the orders vector
MR has been set up as intended. If it has, change to
G13AEF with
ST dimensioned at least (
NST), where
NST is the value returned by G13AFF, or computed using the formula in
Section 5 of this document.
 ${\mathbf{IFAIL}}=5$
On entry, the workspace array
RES is too small. Check the value of
IRES against the constraints in
Section 5.
 ${\mathbf{IFAIL}}=6$
On entry,  ${\mathbf{LDCM}}<{\mathbf{NPPC}}$. 
 ${\mathbf{IFAIL}}=7$
The search procedure in the algorithm has failed. This may be due to a badly conditioned sum of squares function, or the default convergence criterion may be too strict. Use
G13AEF with a less strict convergence criterion.
Some output parameters may contain meaningful values; see
Section 5 for details.
 ${\mathbf{IFAIL}}=8$
The inversion of the Hessian matrix in the calculation of the covariance matrix of the parameter estimates has failed.
Some output parameters may contain meaningful values; see
Section 5 for details.
 ${\mathbf{IFAIL}}=9$
This indicates a failure when solving the equations giving the latest estimates of the backforecasts.
Some output parameters may contain meaningful values; see
Section 5 for details.
 ${\mathbf{IFAIL}}=10$

Satisfactory parameter estimates could not be obtained for all parameter types in the model. Inspect array
ISF for further information on the parameter type(s) in error.
 ${\mathbf{IFAIL}}=11$
An internal error has arisen in partitioning
RES for use by
G13AEF. This error should not occur; report it to NAG via your site representative.
7 Accuracy
The computations are believed to be stable.
The time taken by G13AFF is approximately proportional to ${\mathbf{NX}}\times {\mathbf{ITC}}\times {\left(q+Q\times s+{\mathbf{NPPC}}\right)}^{2}$.
9 Example
This example reads $30$ observations from a time series relating to the rate of the earth's rotation about its polar axis. Differencing of order $1$ is applied, and the number of nonseasonal parameters is $3$, one autoregressive ($\varphi $) and two moving average ($\theta $). No seasonal effects are taken into account.
The constant is estimated. Up to $50$ iterations are allowed.
The initial estimates of ${\varphi}_{1}$, ${\theta}_{1}$, ${\theta}_{2}$ and $c$ are zero.
Some intermediate monitoring output from G13AFZ has been omitted.
9.1 Program Text
Program Text (g13affe.f90)
9.2 Program Data
Program Data (g13affe.d)
9.3 Program Results
Program Results (g13affe.r)