NAG Library Routine Document

g13baf (multi_filter_arima)


    1  Purpose
    7  Accuracy


g13baf filters a time series by an ARIMA model.


Fortran Interface
Subroutine g13baf ( y, ny, mr, nmr, par, npar, cy, wa, nwa, b, nb, ifail)
Integer, Intent (In):: ny, mr(nmr), nmr, npar, nwa, nb
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: y(ny), par(npar), cy
Real (Kind=nag_wp), Intent (Out):: wa(nwa), b(nb)
C Header Interface
#include nagmk26.h
void  g13baf_ (const double y[], const Integer *ny, const Integer mr[], const Integer *nmr, const double par[], const Integer *npar, const double *cy, double wa[], const Integer *nwa, double b[], const Integer *nb, Integer *ifail)


From a given series y1,y2,,yn, a new series b1,b2,,bn is calculated using a supplied (filtering) ARIMA model. This model will be one which has previously been fitted to a series xt with residuals at. The equations defining bt in terms of yt are very similar to those by which at is obtained from xt. The only dissimilarity is that no constant correction is applied after differencing. This is because the series yt is generally distinct from the series xt with which the model is associated, though yt may be related to xt. Whilst it is appropriate to apply the ARIMA model to yt so as to preserve the same relationship between bt and at as exists between yt and xt, the constant term in the ARIMA model is inappropriate for yt. The consequence is that bt will not necessarily have zero mean.
The equations are precisely:
wt=dsDyt, (1)
the appropriate differencing of yt; 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 bt depends on present and past values yt,yt-1,, there is a problem arising from ignorance of y0,y-1, which particularly affects calculation of the early values b1,b2,, causing ‘transient errors’. The routine allows two possibilities.
(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 vt being defined for t=1+d+s×D+s×P,,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 bt.
(ii) The unknown values y0,y-1, 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 yt.
For efficiency, you are asked to supply both this ARIMA model for yt and a limited number of backforecasts which are prefixed to the known values of yt. Within the routine further backforecasts of yt, and the series wt, ut, vt in (1), (2) and (3) are then easily calculated, and a set of linear equations solved for backforecasts of zt,bt for use in (4) and (5) in the case that q+Q>0.
Even if the best model for yt is not available, a very approximate guess such as
can help to reduce the transients substantially.
The backforecasts which need to be prefixed to yt are of length Qy=qy+sy×Qy, where qy and Qy are the non-seasonal and seasonal moving average orders and sy the seasonal period for the ARIMA model of yt. Thus you need not carry out the backforecasting exercise if Qy=0. Otherwise, the series y1,y2,,yn should be reversed to obtain yn,yn-1,,y1 and g13ajf should be used to forecast Qy values, y^0,,y^1-Qy. The ARIMA model used is that fitted to yt (as a forward series) except that, if dy+Dy 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 yt supplied to the filtering routine must however have the appropriate constant for the forward series.
The series y^1-Qy,,y^0,y1,,yn is then supplied to the routine, and a corresponding set of values returned for bt.


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


1:     yny – Real (Kind=nag_wp) arrayInput
On entry: the Qy backforecasts, starting with backforecast at time 1-Qy to backforecast at time 0, followed by the time series starting at time 1, where Qy=mr10+mr13×mr14. 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:     ny – IntegerInput
On entry: the total number of backforecasts and time series data points in array y.
Constraint: nymax1+Qy,npar.
3:     mrnmr – Integer arrayInput
On entry: the orders vector for the filtering model, followed by the orders vector for the ARIMA model for the time series if the latter is known. The orders appear in the standard sequence p,d,q,P,D,Q,s as given in the G13 Chapter Introduction. If the ARIMA model for the time series is supplied, the routine will assume that the first Qy values of the array y are backforecasts.
the filtering model is restricted in the following ways:
  • mr1+mr3+mr4+mr6>0, i.e., filtering by a model which contains only differencing terms is not permitted;
  • mrk0, for k=1,2,,7;
  • if mr7=0, mr4+mr5+mr6=0;
  • if mr70, mr4+mr5+mr60;
  • mr71.
the ARIMA model for the time series is restricted in the following ways:
  • mrk0, for k=8,9,,14;
  • if mr14=0, mr11+mr12+mr13=0;
  • if mr140, mr11+mr12+mr130;
  • mr141.
4:     nmr – IntegerInput
On entry: the number of values specified in the array mr. It takes the value 7 if no ARIMA model for the time series is supplied but otherwise it takes the value 14. Thus nmr acts as an indicator as to whether backforecasting can be carried out.
Constraint: nmr=7 or 14.
5:     parnpar – Real (Kind=nag_wp) arrayInput
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:     npar – IntegerInput
On entry: the total number of parameters held in array par.
  • if nmr=7, npar=mr1+mr3+mr4+mr6;
  • if nmr=14, npar=mr1+mr3+mr4+mr6+ mr8+mr10+mr11+mr13.
Note: the first constraint (i.e., mr1+mr3+mr4+mr6>0) on the orders of the filtering model, in argument mr, ensures that npar>0.
7:     cy – Real (Kind=nag_wp)Input
On entry: if the ARIMA model is known (i.e., nmr=14), cy must specify the constant term of the ARIMA model for the time series. If this model is not known (i.e., nmr=7), cy is not used.
8:     wanwa – Real (Kind=nag_wp) arrayWorkspace
9:     nwa – IntegerInput
On entry: the dimension of the array wa as declared in the (sub)program from which g13baf is called. Workspace is only required if the ARIMA model for the time series is known.
let K=mr3+mr6×mr7+mr8+mr9+mr11+mr12×mr14,
  • if nmr=14, nwaK×K+2;
  • if nmr=7, nwa1.
10:   bnb – Real (Kind=nag_wp) arrayOutput
On exit: the filtered output series. If the ARIMA model for the time series was known, and hence Qy backforecasts were supplied in y, then b contains Qy ‘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 yt, then the filtered value at time t is held in bt.
11:   nb – IntegerInput
On entry: the dimension of the array b as declared in the (sub)program from which g13baf is called. 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.
  • if nmr=14, nbny+maxK3,K1+K2;
  • if nmr=7, nbny.
  • K1=mr1+mr4×mr7;
  • K2=mr2+mr5×mr7;
  • K3=mr3+mr6×mr7.
12:   ifail – IntegerInput/Output
On entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ 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 argument, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

Error Indicators and Warnings

If on entry ifail=0 or -1, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
On entry,nmr7 and nmr14.
On entry,the orders vector mr does not satisfy the constraints given in Section 5.
On entry,npar is inconsistent with the contents of mr (see Section 5).
On entry,ny is too small to successfully carry out the requested filtering, (see Section 5).
On entry,the work array wa is too small.
On entry,the array b is too small.
The orders vector for the filtering model is invalid.
The orders vector for the ARIMA model is invalid. (Only occurs if nmr=14.)
The initial values of the filtered series are indeterminate for the given models.
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.


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

Parallelism and Performance

g13baf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
g13baf 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 routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Further Comments

If an ARIMA model is supplied, a local workspace array of fixed length is allocated internally by g13baf. The total size of this array amounts to K integer elements, where K is the expression defined in the description of the argument wa.
The time taken by g13baf is approximately proportional to
with an appreciable fixed increase if an ARIMA model is supplied for the time series.


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

Program Text

Program Text (g13bafe.f90)

Program Data

Program Data (g13bafe.d)

Program Results

Program Results (g13bafe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017