g13aa 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.

# Syntax

C#
```public static void g13aa(
double[] x,
int nx,
int nd,
int nds,
int ns,
double[] xd,
out int nxd,
out int ifail
)```
Visual Basic
```Public Shared Sub g13aa ( _
x As Double(), _
nx As Integer, _
nd As Integer, _
nds As Integer, _
ns As Integer, _
xd As Double(), _
<OutAttribute> ByRef nxd As Integer, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void g13aa(
array<double>^ x,
int nx,
int nd,
int nds,
int ns,
array<double>^ xd,
[OutAttribute] int% nxd,
[OutAttribute] int% ifail
)```
F#
```static member g13aa :
x : float[] *
nx : int *
nd : int *
nds : int *
ns : int *
xd : float[] *
nxd : int byref *
ifail : int byref -> unit
```

#### Parameters

x
Type: array<System..::..Double>[]()[][]
An array of size [nx]
On entry: the undifferenced time series, ${x}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n$.
nx
Type: System..::..Int32
On entry: $n$, the number of values in the undifferenced time series.
Constraint: ${\mathbf{nx}}>{\mathbf{nd}}+\left({\mathbf{nds}}×{\mathbf{ns}}\right)$.
nd
Type: System..::..Int32
On entry: $d$, the order of non-seasonal differencing.
Constraint: ${\mathbf{nd}}\ge 0$.
nds
Type: System..::..Int32
On entry: $D$, the order of seasonal differencing.
Constraint: ${\mathbf{nds}}\ge 0$.
ns
Type: System..::..Int32
On entry: $s$, the seasonality.
Constraints:
• if ${\mathbf{nds}}>0$, ${\mathbf{ns}}>0$;
• if ${\mathbf{nds}}=0$, ${\mathbf{ns}}\ge 0$.
xd
Type: array<System..::..Double>[]()[][]
An array of size [nx]
On exit: the differenced values in elements $0$ to ${\mathbf{nxd}}-1$, and reconstitution data in the remainder of the array.
nxd
Type: System..::..Int32%
On exit: the number of differenced values in the array xd.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# 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.

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\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)$.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

The computations are believed to be stable.

# Parallelism and Performance

None.

The time taken by g13aa is approximately proportional to $\left({\mathbf{nd}}+{\mathbf{nds}}\right)×{\mathbf{nx}}$.

# 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.

Example program (C#): g13aae.cs

Example program data: g13aae.d

Example program results: g13aae.r