# NAG CL Interfaceg13bac (multi_​filter_​arima)

Settings help

CL Name Style:

## 1Purpose

g13bac filters a time series by an ARIMA model.

## 2Specification

 #include
 void g13bac (const double y[], Integer ny, Nag_ArimaOrder *arimaf, Nag_ArimaOrder *arimav, const double par[], Integer npar, double cy, double b[], Integer nb, NagError *fail)
The function may be called by the names: g13bac, nag_tsa_multi_filter_arima or nag_tsa_arma_filter.

## 3Description

From a given series ${y}_{1},{y}_{2},\dots ,{y}_{n}$, a new series ${b}_{1},{b}_{2},\dots ,{b}_{n}$ is calculated using a supplied (filtering) ARIMA model. This model will be one which has previously been fitted to a series ${x}_{t}$ with residuals ${a}_{t}$. The equations defining ${b}_{t}$ in terms of ${y}_{t}$ are very similar to those by which ${a}_{t}$ is obtained from ${x}_{t}$. The only dissimilarity is that no constant correction is applied after differencing. This is because the series ${y}_{t}$ is generally distinct from the series ${x}_{t}$ with which the model is associated, though ${y}_{t}$ may be related to ${x}_{t}$. Whilst it is appropriate to apply the ARIMA model to ${y}_{t}$ so as to preserve the same relationship between ${b}_{t}$ and ${a}_{t}$ as exists between ${y}_{t}$ and ${x}_{t}$, the constant term in the ARIMA model is inappropriate for ${y}_{t}$. The consequence is that ${b}_{t}$ will not necessarily have zero mean.
The equations are precisely:
 $wt=∇d∇sDyt,$ (1)
the appropriate differencing of ${y}_{t}$; both the seasonal and non-seasonal inverted autoregressive operations are then applied,
 $ut=wt-Φ1wt-s-⋯-ΦPwt-s×P$ (2)
 $vt=ut-ϕ1ut-1-⋯-ϕput-p$ (3)
followed by the inverted moving average operations
 $zt=vt+Θ1zt-s+⋯+ΘQzt-s×Q$ (4)
 $bt=zt+θ1bt-1+⋯+θqbt-q.$ (5)
Because the filtered series value ${b}_{t}$ depends on present and past values ${y}_{t},{y}_{t-1},\dots \text{}$, there is a problem arising from ignorance of ${y}_{0},{y}_{-1},\dots \text{}$ which particularly affects calculation of the early values ${b}_{1},{b}_{2},\dots \text{}$, causing ‘transient errors’. The function allows two possibilities.
1. (i)The equations (1), (2) and (3) are applied from successively later time points so that all terms on their right-hand sides are known, with ${v}_{t}$ being defined for $t=\left(1+d+s×D+s×P\right),\dots ,n$. Equations (4) and (5) are then applied over the same range, taking any values on the right-hand side associated with previous time points to be zero.
This procedure may still however result in unacceptably large transient errors in early values of ${b}_{t}$.
2. (ii)The unknown values ${y}_{0},{y}_{-1},\dots \text{}$ are estimated by backforecasting. This requires that an ARIMA model distinct from that which has been supplied for filtering, should have been previously fitted to ${y}_{t}$.
For efficiency, you are asked to supply both this ARIMA model for ${y}_{t}$ and a limited number of backforecasts which are prefixed to the known values of ${y}_{t}$. Within the function further backforecasts of ${y}_{t}$, and the series ${w}_{t}$, ${u}_{t}$, ${v}_{t}$ in (1), (2) and (3) are then easily calculated, and a set of linear equations solved for backforecasts of ${z}_{t},{b}_{t}$ for use in (4) and (5) in the case that $q+Q>0$.
Even if the best model for ${y}_{t}$ is not available, a very approximate guess such as
 $yt=c+et$
or
 $∇ yt=et$
can help to reduce the transients substantially.
The backforecasts which need to be prefixed to ${y}_{t}$ are of length ${Q}_{y}^{\prime }={q}_{y}+{s}_{y}×{Q}_{y}$, where ${q}_{y}$ and ${Q}_{y}$ are the non-seasonal and seasonal moving average orders and ${s}_{y}$ the seasonal period for the ARIMA model of ${y}_{t}$. Thus you need not carry out the backforecasting exercise if ${Q}_{y}^{\prime }=0$. Otherwise, the series ${y}_{1},{y}_{2},\dots ,{y}_{n}$ should be reversed to obtain ${y}_{n},{y}_{n-1},\dots ,{y}_{1}$ and g13bjc should be used to forecast ${Q}_{y}^{\prime }$ values, ${\stackrel{^}{y}}_{0},\dots ,{\stackrel{^}{y}}_{1-{Q}_{y}^{\prime }}$. The ARIMA model used is that fitted to ${y}_{t}$ (as a forward series) except that, if ${d}_{y}+{D}_{y}$ is odd, the constant should be changed in sign (to allow, for example, for the fact that a forward upward trend is a reversed downward trend). The ARIMA model for ${y}_{t}$ supplied to the filtering function must however have the appropriate constant for the forward series.
The series ${\stackrel{^}{y}}_{1-{Q}_{y}^{\prime }},\dots ,{\stackrel{^}{y}}_{0},{y}_{1},\dots ,{y}_{n}$ is then supplied to the function, and a corresponding set of values returned for ${b}_{t}$.

## 4References

Box G E P and Jenkins G M (1976) Time Series Analysis: Forecasting and Control (Revised Edition) Holden–Day

## 5Arguments

1: $\mathbf{y}\left[{\mathbf{ny}}\right]$const double Input
On entry: the ${Q}_{y}^{\prime }$ backforecasts, starting with backforecast at time $1-{Q}_{y}^{\prime }$ to backforecast at time $0$, followed by the time series starting at time $1$, where ${Q}_{y}^{\prime }={\mathbf{arimav}}\mathbf{.}\mathbf{q}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigq}×{\mathbf{arimav}}\mathbf{.}\mathbf{s}$. If there are no backforecasts, either because the ARIMA model for the time series is not known, or because it is known but has no moving average terms, then the time series starts at the beginning of y.
2: $\mathbf{ny}$Integer Input
On entry: the total number of backforecasts and time series data points in array y.
Constraint: ${\mathbf{ny}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1+{Q}_{y}^{\prime },{\mathbf{npar}}\right)$.
3: $\mathbf{arimaf}$Nag_ArimaOrder * Input
On entry: the orders for the filtering ARIMA model as a pointer to structure of type Nag_ArimaOrder with the following members:
pInteger
dIntegerInput
qIntegerInput
bigpIntegerInput
bigdIntegerInput
bigqIntegerInput
sIntegerInput
On entry: these seven members of arimaf must specify the orders vector $\left(p,d,q,P,D,Q,s\right)$, respectively, of the ARIMA model for the output noise component.
$p$, $q$, $P$ and $Q$ refer, respectively, to the number of autoregressive ($\varphi$), moving average ($\theta$), seasonal autoregressive ($\Phi$) and seasonal moving average ($\Theta$) parameters.
$d$, $D$ and $s$ refer, respectively, to the order of non-seasonal differencing, the order of seasonal differencing and the seasonal period.
Constraints:
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{p}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{d}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{q}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}\ge 0$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}\ne 1$;
• ${\mathbf{arimaf}}\mathbf{.}\mathbf{p}+{\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}>0$;
• if ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}=0$, ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}=0$;
• if ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}\ne 0$, ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}\ne 0$.
4: $\mathbf{arimav}$Nag_ArimaOrder * Input
On entry: if available, the orders for the ARIMA model for the series as a pointer to structure of type Nag_ArimaOrder with the following members:
pInteger
dIntegerInput
qIntegerInput
bigpIntegerInput
bigdIntegerInput
bigqIntegerInput
sIntegerInput
On entry: these seven members of arimav must specify the orders vector $\left(p,d,q,P,D,Q,s\right)$, respectively, of the ARIMA model for the output noise component.
$p$, $q$, $P$ and $Q$ refer, respectively, to the number of autoregressive ($\varphi$), moving average ($\theta$), seasonal autoregressive ($\Phi$) and seasonal moving average ($\Theta$) parameters.
$d$, $D$ and $s$ refer, respectively, to the order of non-seasonal differencing, the order of seasonal differencing and the seasonal period.
If no ARIMA model for the series is to be supplied arimav should be set to a NULL pointer.
Constraints:
• ${\mathbf{arimav}}\mathbf{.}\mathbf{p}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{d}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{q}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{bigp}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{bigd}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{bigq}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{s}\ge 0$;
• ${\mathbf{arimav}}\mathbf{.}\mathbf{s}\ne 1$;
• if ${\mathbf{arimav}}\mathbf{.}\mathbf{s}=0$, ${\mathbf{arimav}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigq}=0$;
• if ${\mathbf{arimav}}\mathbf{.}\mathbf{s}\ne 0$, ${\mathbf{arimav}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigq}\ne 0$.
5: $\mathbf{par}\left[{\mathbf{npar}}\right]$const double Input
On entry: the parameters of the filtering model, followed by the parameters of the ARIMA model for the time series, if supplied. Within each model the parameters are in the standard order of non-seasonal AR and MA followed by seasonal AR and MA.
6: $\mathbf{npar}$Integer Input
On entry: the total number of parameters held in array par.
Constraints:
• if ${\mathbf{arimav}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{npar}}={\mathbf{arimaf}}\mathbf{.}\mathbf{p}+{\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}$;
• if ${\mathbf{arimav}}\phantom{\rule{0.25em}{0ex}}\text{is}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{npar}}={\mathbf{arimaf}}\mathbf{.}\mathbf{p}+{\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}+\phantom{\rule{0ex}{0ex}}{\mathbf{arimav}}\mathbf{.}\mathbf{p}+{\mathbf{arimav}}\mathbf{.}\mathbf{q}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigq}$.
Note: the first constraint (i.e., ${\mathbf{arimaf}}\mathbf{.}\mathbf{p}+{\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}>0$) on the orders of the filtering model, in argument arimav, ensures that ${\mathbf{npar}}>0$.
7: $\mathbf{cy}$double Input
On entry: if the ARIMA model is known, cy must specify the constant term of the ARIMA model for the time series. If this model is not known, cy is not used.
8: $\mathbf{b}\left[{\mathbf{nb}}\right]$double Output
On exit: the filtered output series. If the ARIMA model for the time series was known, and hence ${Q}_{y}^{\prime }$ backforecasts were supplied in y, then b contains ${Q}_{y}^{\prime }$ ‘filtered’ backforecasts followed by the filtered series. Otherwise, the filtered series begins at the start of b just as the original series began at the start of y. In either case, if the value of the series at time $t$ is held in ${\mathbf{y}}\left[t-1\right]$, then the filtered value at time $t$ is held in ${\mathbf{b}}\left[t-1\right]$.
9: $\mathbf{nb}$Integer Input
On entry: the dimension of the array b. In addition to holding the returned filtered series, b is also used as an intermediate work array if the ARIMA model for the time series was known.
Constraints:
• if ${\mathbf{arimav}}\phantom{\rule{0.25em}{0ex}}\text{is}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{nb}}\ge {\mathbf{ny}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({K}_{3},{K}_{1}+{K}_{2}\right)$;
• if ${\mathbf{arimav}}\phantom{\rule{0.25em}{0ex}}\text{is not}\phantom{\rule{0.25em}{0ex}}\mathbf{NULL}$, ${\mathbf{nb}}\ge {\mathbf{ny}}$.
Where
• ${K}_{1}={\mathbf{arimaf}}\mathbf{.}\mathbf{p}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}×{\mathbf{arimaf}}\mathbf{.}\mathbf{s}$;
• ${K}_{2}={\mathbf{arimaf}}\mathbf{.}\mathbf{d}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}×{\mathbf{arimaf}}\mathbf{.}\mathbf{s}$;
• ${K}_{3}={\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}×{\mathbf{arimaf}}\mathbf{.}\mathbf{s}$.
10: $\mathbf{fail}$NagError * Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).

## 6Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.1.2 in the Introduction to the NAG Library CL Interface for further information.
NE_ARRAY_SIZE
On entry, ${\mathbf{nb}}=⟨\mathit{\text{value}}⟩$ and the minimum size $\text{required}=⟨\mathit{\text{value}}⟩$.
Constraint: if arimav is not NULL then ${\mathbf{nb}}\ge {\mathbf{ny}}+\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left({K}_{3},{K}_{1}+{K}_{2}\right)$, otherwise ${\mathbf{nb}}\ge {\mathbf{ny}}$.
On entry, argument $⟨\mathit{\text{value}}⟩$ had an illegal value.
NE_CONSTRAINT
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}\ge 0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}\ge 0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}\ge 0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{d}\ge 0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{p}+{\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}>0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{p}\ge 0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{q}\ge 0$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}\ne 1$.
Constraint: ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{bigd}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{bigp}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{bigq}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{d}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{p}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{q}\ge 0$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{s}\ne 1$.
Constraint: ${\mathbf{arimav}}\mathbf{.}\mathbf{s}\ge 0$.
Constraint: if ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}=0$, ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}=0$.
Constraint: if ${\mathbf{arimaf}}\mathbf{.}\mathbf{s}\ne 0$, ${\mathbf{arimaf}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}\ne 0$.
Constraint: if ${\mathbf{arimav}}\mathbf{.}\mathbf{s}=0$, ${\mathbf{arimav}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigq}=0$.
Constraint: if ${\mathbf{arimav}}\mathbf{.}\mathbf{s}\ne 0$, ${\mathbf{arimav}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigd}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigq}\ne 0$.
On entry, npar is inconsistent with arimaf and arimav: ${\mathbf{npar}}=⟨\mathit{\text{value}}⟩$.
NE_INIT_FILTER
The initial values of the filtered series are indeterminate for the given models.
NE_INT
On entry, ${\mathbf{ny}}=⟨\mathit{\text{value}}⟩$ and the minimum size $\text{required}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ny}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1+{Q}_{y}^{\prime },{\mathbf{npar}}\right)$.
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 7.5 in the Introduction to the NAG Library CL Interface for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library CL Interface for further information.
NE_ORDERS_ARIMA
On entry, arimaf or arimav is invalid.
The orders vector for the ARIMA model is invalid.
NE_ORDERS_FILTER
The orders vector for the filtering model is invalid.

## 7Accuracy

Accuracy and stability are high except when the MA parameters are close to the invertibility boundary.

## 8Parallelism and Performance

g13bac is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13bac 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.

If an ARIMA model is supplied, local workspace arrays of fixed lengths are allocated internally by g13bac. The total size of these arrays amounts to $K$ Integer elements and $K×\left(K+2\right)$ double elements, where $K={\mathbf{arimaf}}\mathbf{.}\mathbf{q}+{\mathbf{arimaf}}\mathbf{.}\mathbf{bigq}×{\mathbf{arimaf}}\mathbf{.}\mathbf{s}+{\mathbf{arimav}}\mathbf{.}\mathbf{p}+{\mathbf{arimav}}\mathbf{.}\mathbf{d}+\left({\mathbf{arimav}}\mathbf{.}\mathbf{bigp}+{\mathbf{arimav}}\mathbf{.}\mathbf{bigd}\right)×{\mathbf{arimav}}\mathbf{.}\mathbf{s}$.
The time taken by g13bac is approximately proportional to
 $ny×(arimaf.p+arimaf.q+arimaf.bigp+arimaf.bigq),$
with an appreciable fixed increase if an ARIMA model is supplied for the time series.

## 10Example

This example reads a time series of length $296$. It reads the univariate ARIMA $\left(4,0,2,0,0,0,0\right)$ model and the ARIMA filtering $\left(3,0,0,0,0,0,0\right)$ model for the series. Two initial backforecasts are required and these are calculated by a call to g13bjc. The backforecasts are inserted at the start of the series and g13bac is called to perform the calculations.

### 10.1Program Text

Program Text (g13bace.c)

### 10.2Program Data

Program Data (g13bace.d)

### 10.3Program Results

Program Results (g13bace.r)