Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_tsa_uni_diff (g13aa)

## Purpose

nag_tsa_uni_diff (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

[xd, nxd, ifail] = g13aa(x, nd, nds, ns, 'nx', nx)
[xd, nxd, ifail] = nag_tsa_uni_diff(x, nd, nds, ns, 'nx', nx)

## Description

Let dsDxi${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ be the i$i$th value of a time series xi${x}_{i}$, for i = 1,2,,n$i=1,2,\dots ,n$ after non-seasonal differencing of order d$d$ and seasonal differencing of order D$D$ (with period or seasonality s$s$). In general,
 ∇d∇sDxi${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ = $=$ ∇d − 1∇sDxi + 1 − ∇d − 1∇sDxi${\nabla }^{d-1}{\nabla }_{s}^{D}{x}_{i+1}-{\nabla }^{d-1}{\nabla }_{s}^{D}{x}_{i}$ d > 0$d>0$ ∇d∇sDxi${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ = $=$ ∇d∇sD − 1xi + s − ∇d∇sD − 1xi${\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i+s}-{\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i}$ D > 0$D>0$
Non-seasonal differencing up to the required order d$d$ is obtained using
 ∇1xi${\nabla }^{1}{x}_{i}$ = $=$ xi + 1 − xi${x}_{i+1}-{x}_{i}$ for i = 1,2, … ,(n − 1)$i=1,2,\dots ,\left(n-1\right)$ ∇2xi${\nabla }^{2}{x}_{i}$ = $=$ ∇1xi + 1 − ∇1xi${\nabla }^{1}{x}_{i+1}-{\nabla }^{1}{x}_{i}$ for i = 1,2, … ,(n − 2)$i=1,2,\dots ,\left(n-2\right)$ ⋮ $\text{ }⋮$ ∇dxi${\nabla }^{d}{x}_{i}$ = $=$ ∇d − 1xi + 1 − ∇d − 1xi${\nabla }^{d-1}{x}_{i+1}-{\nabla }^{d-1}{x}_{i}$ for i = 1,2, … ,(n − d)$i=1,2,\dots ,\left(n-d\right)$
Seasonal differencing up to the required order D$D$ is then obtained using
 ∇d∇s1xi${\nabla }^{d}{\nabla }_{s}^{1}{x}_{i}$ = $=$ ∇dxi + s − ∇dxi${\nabla }^{d}{x}_{i+s}-{\nabla }^{d}{x}_{i}$ for i = 1,2, … ,(n − d − s)$i=1,2,\dots ,\left(n-d-s\right)$ ∇d∇s2xi${\nabla }^{d}{\nabla }_{s}^{2}{x}_{i}$ = $=$ ∇d∇s1xi + s − ∇d∇s1xi${\nabla }^{d}{\nabla }_{s}^{1}{x}_{i+s}-{\nabla }^{d}{\nabla }_{s}^{1}{x}_{i}$ for i = 1,2, … ,(n − d − 2s)$i=1,2,\dots ,\left(n-d-2s\right)$ ⋮ $\text{ }⋮$ ∇d∇sDxi${\nabla }^{d}{\nabla }_{s}^{D}{x}_{i}$ = $=$ ∇d∇sD − 1xi + s − ∇d∇sD − 1xi${\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i+s}-{\nabla }^{d}{\nabla }_{s}^{D-1}{x}_{i}$ for i = 1,2, … ,(n − d − D × s)$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 = ndD × s$m=n-d-D×s$ values.

None.

## Parameters

### Compulsory Input Parameters

1:     x(nx) – double array
nx, the dimension of the array, must satisfy the constraint nx > nd + (nds × ns)${\mathbf{nx}}>{\mathbf{nd}}+\left({\mathbf{nds}}×{\mathbf{ns}}\right)$.
The undifferenced time series, xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
2:     nd – int64int32nag_int scalar
d$d$, the order of non-seasonal differencing.
Constraint: nd0${\mathbf{nd}}\ge 0$.
3:     nds – int64int32nag_int scalar
D$D$, the order of seasonal differencing.
Constraint: nds0${\mathbf{nds}}\ge 0$.
4:     ns – int64int32nag_int scalar
s$s$, the seasonality.
Constraints:
• if nds > 0${\mathbf{nds}}>0$, ns > 0${\mathbf{ns}}>0$;
• if nds = 0${\mathbf{nds}}=0$, ns0${\mathbf{ns}}\ge 0$.

### Optional Input Parameters

1:     nx – int64int32nag_int scalar
Default: The dimension of the array x.
n$n$, the number of values in the undifferenced time series.
Constraint: nx > nd + (nds × ns)${\mathbf{nx}}>{\mathbf{nd}}+\left({\mathbf{nds}}×{\mathbf{ns}}\right)$.

None.

### Output Parameters

1:     xd(nx) – double array
The differenced values in elements 1$1$ to nxd${\mathbf{nxd}}$, and reconstitution data in the remainder of the array.
2:     nxd – int64int32nag_int scalar
The number of differenced values in the array xd.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, nd < 0${\mathbf{nd}}<0$, or nds < 0${\mathbf{nds}}<0$, or ns < 0${\mathbf{ns}}<0$, or ns = 0${\mathbf{ns}}=0$ when nds > 0${\mathbf{nds}}>0$.
ifail = 2${\mathbf{ifail}}=2$
 On entry, nx ≤ nd + (nds × ns)${\mathbf{nx}}\le {\mathbf{nd}}+\left({\mathbf{nds}}×{\mathbf{ns}}\right)$.

## Accuracy

The computations are believed to be stable.

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

## Example

```function nag_tsa_uni_diff_example
x = [120;
108;
98;
118;
135;
131;
118;
125;
121;
100;
82;
82;
89;
88;
86;
96;
108;
110;
99;
105];
nd = int64(2);
nds = int64(1);
ns = int64(4);
[xd, nxd, ifail] = nag_tsa_uni_diff(x, nd, nds, ns)
```
```

xd =

-11
-10
-8
4
12
-2
18
9
-4
-6
-5
-2
-12
5
2
-10
-13
17
6
105

nxd =

14

ifail =

0

```
```function g13aa_example
x = [120;
108;
98;
118;
135;
131;
118;
125;
121;
100;
82;
82;
89;
88;
86;
96;
108;
110;
99;
105];
nd = int64(2);
nds = int64(1);
ns = int64(4);
[xd, nxd, ifail] = g13aa(x, nd, nds, ns)
```
```

xd =

-11
-10
-8
4
12
-2
18
9
-4
-6
-5
-2
-12
5
2
-10
-13
17
6
105

nxd =

14

ifail =

0

```