G13 Chapter Contents
G13 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentG13AAF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

G13AAF carries out non-seasonal and seasonal differencing on a time series. Information which allows the original series to be reconstituted from the differenced series is also produced. This information is required in time series forecasting.

## 2  Specification

 SUBROUTINE G13AAF ( X, NX, ND, NDS, NS, XD, NXD, IFAIL)
 INTEGER NX, ND, NDS, NS, NXD, IFAIL REAL (KIND=nag_wp) X(NX), XD(NX)

## 3  Description

Let ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ be the $i$th value of a time series ${x}_{i}$, for $i=1,2,\dots ,n$ after non-seasonal differencing of order $d$ and seasonal differencing of order $D$ (with period or seasonality $s$). In general,
 ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ $=$ ${\nabla }^{d-1}{\nabla }_{s}^{D}{x}_{i+1}-{\nabla }^{d-1}{\nabla }_{s}^{D}{x}_{i}$ $d>0$ ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ $=$ ${\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i+s}-{\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i}$ $D>0$
Non-seasonal differencing up to the required order $d$ is obtained using
 ${\nabla }^{1}{x}_{i}$ $=$ ${x}_{i+1}-{x}_{i}$ for $i=1,2,\dots ,\left(n-1\right)$ ${\nabla }^{2}{x}_{i}$ $=$ ${\nabla }^{1}{x}_{i+1}-{\nabla }^{1}{x}_{i}$ for $i=1,2,\dots ,\left(n-2\right)$ $\text{ }⋮$ ${\nabla }^{d}{x}_{i}$ $=$ ${\nabla }^{d-1}{x}_{i+1}-{\nabla }^{d-1}{x}_{i}$ for $i=1,2,\dots ,\left(n-d\right)$
Seasonal differencing up to the required order $D$ is then obtained using
 ${\nabla }^{d}{\nabla }_{s}^{1}{x}_{i}$ $=$ ${\nabla }^{d}{x}_{i+s}-{\nabla }^{d}{x}_{i}$ for $i=1,2,\dots ,\left(n-d-s\right)$ ${\nabla }^{d}{\nabla }_{s}^{2}{x}_{i}$ $=$ ${\nabla }^{d}{\nabla }_{s}^{1}{x}_{i+s}-{\nabla }^{d}{\nabla }_{s}^{1}{x}_{i}$ for $i=1,2,\dots ,\left(n-d-2s\right)$ $\text{ }⋮$ ${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ $=$ ${\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i+s}-{\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i}$ for $i=1,2,\dots ,\left(n-d-D×s\right)$
Mathematically, the sequence in which the differencing operations are performed does not affect the final resulting series of $m=n-d-D×s$ values.

None.

## 5  Parameters

1:     X(NX) – REAL (KIND=nag_wp) arrayInput
On entry: the undifferenced time series, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
2:     NX – INTEGERInput
On entry: $n$, the number of values in the undifferenced time series.
Constraint: ${\mathbf{NX}}>{\mathbf{ND}}+\left({\mathbf{NDS}}×{\mathbf{NS}}\right)$.
3:     ND – INTEGERInput
On entry: $d$, the order of non-seasonal differencing.
Constraint: ${\mathbf{ND}}\ge 0$.
4:     NDS – INTEGERInput
On entry: $D$, the order of seasonal differencing.
Constraint: ${\mathbf{NDS}}\ge 0$.
5:     NS – INTEGERInput
On entry: $s$, the seasonality.
Constraints:
• if ${\mathbf{NDS}}>0$, ${\mathbf{NS}}>0$;
• if ${\mathbf{NDS}}=0$, ${\mathbf{NS}}\ge 0$.
6:     XD(NX) – REAL (KIND=nag_wp) arrayOutput
On exit: the differenced values in elements $1$ to ${\mathbf{NXD}}$, and reconstitution data in the remainder of the array.
7:     NXD – INTEGEROutput
On exit: the number of differenced values in the array XD.
8:     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, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{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).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{ND}}<0$, or ${\mathbf{NDS}}<0$, or ${\mathbf{NS}}<0$, or ${\mathbf{NS}}=0$ when ${\mathbf{NDS}}>0$.
${\mathbf{IFAIL}}=2$
 On entry, ${\mathbf{NX}}\le {\mathbf{ND}}+\left({\mathbf{NDS}}×{\mathbf{NS}}\right)$.

## 7  Accuracy

The computations are believed to be stable.

The time taken by G13AAF is approximately proportional to $\left({\mathbf{ND}}+{\mathbf{NDS}}\right)×{\mathbf{NX}}$.

## 9  Example

This example reads in a set of data consisting of $20$ observations from a time series. Non-seasonal differencing of order $2$ and seasonal differencing of order $1$ (with seasonality of $4$) are applied to the input data, giving an output array holding $14$ differenced values and $6$ values which can be used to reconstitute the output array.

### 9.1  Program Text

Program Text (g13aafe.f90)

### 9.2  Program Data

Program Data (g13aafe.d)

### 9.3  Program Results

Program Results (g13aafe.r)