# NAG C Library Function Document

## 1Purpose

nag_tsa_multi_diff (g13dlc) differences and/or transforms a multivariate time series.

## 2Specification

 #include #include
 void nag_tsa_multi_diff (Integer k, Integer n, const double z[], const Integer tr[], const Integer id[], const double delta[], double w[], Integer *nd, NagError *fail)

## 3Description

For certain time series it may first be necessary to difference the original data to obtain a stationary series before calculating autocorrelations, etc. This function also allows you to apply either a square root or a log transformation to the original time series to stabilize the variance if required.
If the order of differencing required for the $i$th series is ${\mathit{d}}_{i}$, then the differencing operator is defined by ${\delta }_{i}\left(B\right)=1-{\delta }_{i1}B-{\delta }_{i2}{B}^{2}-\cdots -{\delta }_{i{\mathit{d}}_{i}}{B}^{{\mathit{d}}_{i}}$, where $B$ is the backward shift operator; that is, $B{Z}_{t}={Z}_{t-1}$. Let $\mathit{d}$ denote the maximum of the orders of differencing, ${\mathit{d}}_{i}$, over the $k$ series. The function computes values of the differenced/transformed series ${W}_{\mathit{t}}={\left({w}_{1\mathit{t}},{w}_{2\mathit{t}},\dots ,{w}_{\mathit{k}\mathit{t}}\right)}^{\mathrm{T}}$, for $\mathit{t}=\mathit{d}+1,\dots ,n$, as follows:
 $wit=δiBzit*, i=1,2,…,k$
where ${z}_{it}^{*}$ are the transformed values of the original $k$-dimensional time series ${Z}_{t}={\left({z}_{1t},{z}_{2t},\dots ,{z}_{kt}\right)}^{\mathrm{T}}$.
The differencing parameters ${\delta }_{ij}$, for $i=1,2,\dots ,k$ and $j=1,2,\dots ,{\mathit{d}}_{i}$, must be supplied by you. If the $i$th series does not require differencing, then ${\mathit{d}}_{i}=0$.

## 4References

Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day
Wei W W S (1990) Time Series Analysis: Univariate and Multivariate Methods Addison–Wesley

## 5Arguments

1:    $\mathbf{k}$IntegerInput
On entry: $k$, the dimension of the multivariate time series.
Constraint: ${\mathbf{k}}\ge 1$.
2:    $\mathbf{n}$IntegerInput
On entry: $n$, the number of observations in the series, prior to differencing.
Constraint: ${\mathbf{n}}\ge 1$.
3:    $\mathbf{z}\left[{\mathbf{k}}×{\mathbf{n}}\right]$const doubleInput
On entry: ${\mathbf{z}}\left[\left(\mathit{t}-1\right)k+\mathit{i}-1\right]$ must contain the $\mathit{i}$th series at time $\mathit{t}$, for $\mathit{t}=1,2,\dots ,n$ and $\mathit{i}=1,2,\dots ,k$.
4:    $\mathbf{tr}\left[{\mathbf{k}}\right]$const IntegerInput
On entry: ${\mathbf{tr}}\left[\mathit{i}-1\right]$ indicates whether the $\mathit{i}$th series is to be transformed, for $\mathit{i}=1,2,\dots ,k$.
${\mathbf{tr}}\left[i-1\right]=-1$
A square root transformation is used.
${\mathbf{tr}}\left[i-1\right]=0$
No transformation is used.
${\mathbf{tr}}\left[i-1\right]=1$
A log transformation is used.
Constraint: ${\mathbf{tr}}\left[\mathit{i}-1\right]=-1$, $0$ or $1$, for $\mathit{i}=1,2,\dots ,k$.
5:    $\mathbf{id}\left[{\mathbf{k}}\right]$const IntegerInput
On entry: the order of differencing for each series, ${\mathit{d}}_{1},{\mathit{d}}_{2},\dots ,{\mathit{d}}_{k}$.
Constraint: $0\le {\mathbf{id}}\left[\mathit{i}\right]<{\mathbf{n}}$, for $\mathit{i}=0,1,\dots ,{\mathbf{k}}-1$.
6:    $\mathbf{delta}\left[\mathit{dim}\right]$const doubleInput
Note: the dimension, dim, of the array delta must be at least ${\mathbf{k}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,\mathit{d}\right)$, where $\mathit{d}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{id}}\left[i-1\right]\right)$.
On entry: if ${\mathbf{id}}\left[\mathit{i}-1\right]>0$ then ${\mathbf{delta}}\left[\left(\mathit{j}-1\right)k+\mathit{i}-1\right]$ must be set to ${\delta }_{\mathit{i}\mathit{j}}$, for $\mathit{j}=1,2,\dots ,{\mathit{d}}_{l}$ and $\mathit{i}=1,2,\dots ,k$.
7:    $\mathbf{w}\left[\mathit{dim}\right]$doubleOutput
Note: the dimension, dim, of the array w must be at least ${\mathbf{k}}×\left({\mathbf{n}}-\mathit{d}\right)$, where $\mathit{d}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{id}}\left[i-1\right]\right)$.
On exit: ${\mathbf{w}}\left[\left(\mathit{t}-1\right)k+\mathit{i}-1\right]$ contains the value of ${w}_{\mathit{i},\mathit{t}+\mathit{d}}$, for $\mathit{i}=1,2,\dots ,k$ and $\mathit{t}=1,2,\dots ,n-\mathit{d}$.
8:    $\mathbf{nd}$Integer *Output
On exit: the number of differenced values, $n-\mathit{d}$, in the series, where $\mathit{d}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathbf{id}}\left[i-1\right]\right)$.
9:    $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.7 in How to Use the NAG Library and its Documentation).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 2.3.1.2 in How to Use the NAG Library and its Documentation for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{k}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{k}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_INT_ARRAY
On entry, $i=〈\mathit{\text{value}}〉$ and ${\mathbf{id}}\left[i-1\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{id}}\left[i-1\right]\ge 0$.
On entry, $i=〈\mathit{\text{value}}〉$, ${\mathbf{id}}\left[i-1\right]=〈\mathit{\text{value}}〉$ and ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{id}}<$ or ${\mathbf{n}}$.
On entry, ${\mathbf{tr}}\left[〈\mathit{\text{value}}〉\right]=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{tr}}\left[\mathit{i}\right]=-1$, $0$ or $1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 2.7.6 in How to Use the NAG Library and its Documentation for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 2.7.5 in How to Use the NAG Library and its Documentation for further information.
NE_TRANSFORMATION
On entry, one (or more) of the transformations requested is invalid. Check that you are not trying to log or square-root a series, some of whose values are negative.

## 7Accuracy

The computations are believed to be stable.

## 8Parallelism and Performance

nag_tsa_multi_diff (g13dlc) makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the x06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this function. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

The same differencing operator does not have to be applied to all the series. For example, suppose we have $k=2$, and wish to apply the second-order differencing operator ${\nabla }^{2}$ to the first series and the first-order differencing operator $\nabla$ to the second series:
 $w1t =∇2z1t= 1-B 2z1t=1-2B+B2z1t, and w2t =∇z2t=1-Bz2t.$
Then ${\mathit{d}}_{1}=2,{\mathit{d}}_{2}=1$, $\mathit{d}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({\mathit{d}}_{1},{\mathit{d}}_{2}\right)=2$, and
 $delta = δ11 δ12 δ21 = 2 -1 1 .$

## 10Example

A program to difference (non-seasonally) each of two time series of length $48$. No transformation is to be applied to either of the series.

### 10.1Program Text

Program Text (g13dlce.c)

### 10.2Program Data

Program Data (g13dlce.d)

### 10.3Program Results

Program Results (g13dlce.r)