NAG Library Routine Document

g13dmf (multi_corrmat_cross)


g13dmf calculates the sample cross-correlation (or cross-covariance) matrices of a multivariate time series.


Fortran Interface
Subroutine g13dmf ( matrix, k, n, m, w, kmax, wmean, r0, r, ifail)
Integer, Intent (In):: k, n, m, kmax
Integer, Intent (Inout):: ifail
Real (Kind=nag_wp), Intent (In):: w(kmax,n)
Real (Kind=nag_wp), Intent (Inout):: r0(kmax,k), r(kmax,kmax,m)
Real (Kind=nag_wp), Intent (Out):: wmean(k)
Character (1), Intent (In):: matrix
C Header Interface
#include <nagmk26.h>
void  g13dmf_ (const char *matrix, const Integer *k, const Integer *n, const Integer *m, const double w[], const Integer *kmax, double wmean[], double r0[], double r[], Integer *ifail, const Charlen length_matrix)


Let Wt = w1t,w2t,,wktT , for t=1,2,,n, denote n observations of a vector of k time series. The sample cross-covariance matrix at lag l is defined to be the k by k matrix C^l, whose (i,j)th element is given by
C^ijl=1nt=l+1nwit-l-w-iwjt-w-j,  l=0,1,2,,m, ​i=1,2,,k​ and ​j=1,2,,k,  
where w-i and w-j denote the sample means for the ith and jth series respectively. The sample cross-correlation matrix at lag l is defined to be the k by k matrix R^l, whose i,jth element is given by
R^ ij l = C^ ij l C^ ii 0 C^ jj 0 ,   l=0,1,2,,m , ​ i=1,2,,k ​ and ​ j=1,2,,k .  
The number of lags, m, is usually taken to be at most n/4.
If Wt follows a vector moving average model of order q, then it can be shown that the theoretical cross-correlation matrices Rl are zero beyond lag q. In order to help spot a possible cut-off point, the elements of R^l are usually compared to their approximate standard error of 1/n. For further details see, for example, Wei (1990).
The routine uses a single pass through the data to compute the means and the cross-covariance matrix at lag zero. The cross-covariance matrices at further lags are then computed on a second pass through the data.


Wei W W S (1990) Time Series Analysis: Univariate and Multivariate Methods Addison–Wesley
West D H D (1979) Updating mean and variance estimates: An improved method Comm. ACM 22 532–555


1:     matrix – Character(1)Input
On entry: indicates whether the cross-covariance or cross-correlation matrices are to be computed.
The cross-covariance matrices are computed.
The cross-correlation matrices are computed.
Constraint: matrix='V' or 'R'.
2:     k – IntegerInput
On entry: k, the dimension of the multivariate time series.
Constraint: k1.
3:     n – IntegerInput
On entry: n, the number of observations in the series.
Constraint: n2.
4:     m – IntegerInput
On entry: m, the number of cross-correlation (or cross-covariance) matrices to be computed. If in doubt set m=10. However it should be noted that m is usually taken to be at most n/4.
Constraint: 1m<n.
5:     wkmaxn – Real (Kind=nag_wp) arrayInput
On entry: wit must contain the observation wit, for i=1,2,,k and t=1,2,,n.
6:     kmax – IntegerInput
On entry: the first dimension of the arrays w, r0 and r and the second dimension of the array r as declared in the (sub)program from which g13dmf is called.
Constraint: kmaxk.
7:     wmeank – Real (Kind=nag_wp) arrayOutput
On exit: the means, w-i, for i=1,2,,k.
8:     r0kmaxk – Real (Kind=nag_wp) arrayOutput
On exit: if ij, then r0ij contains an estimate of the i,jth element of the cross-correlation (or cross-covariance) matrix at lag zero, R^ij0; if i=j, then if matrix='V', r0ii contains the variance of the ith series, C^ii0, and if matrix='R', r0ii contains the standard deviation of the ith series, C^ii0.
If ifail=2 and matrix='R', then on exit all the elements in r0 whose computation involves the zero variance are set to zero.
9:     rkmaxkmaxm – Real (Kind=nag_wp) arrayOutput
On exit: rijl  contains an estimate of the (i,j)th element of the cross-correlation (or cross-covariance) at lag l, R^ijl, for l=1,2,,m, i=1,2,,k and j=1,2,,k.
If ifail=2 and matrix='R', then on exit all the elements in r whose computation involves the zero variance are set to zero.
10:   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, k=value.
Constraint: k1.
On entry, kmax=value and k=value.
Constraint: kmaxk.
On entry, m=value and n=value.
Constraint: m1 and m<n.
On entry, matrix=value.
Constraint: matrix='V' or 'R'.
On entry, n=value.
Constraint: n2.
On entry, at least one of the series is such that all its elements are practically identical giving zero (or near zero) variance. In this case if matrix='R' all the correlations in r0 and r involving this variance are set to zero.
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.


For a discussion of the accuracy of the one-pass algorithm used to compute the sample cross-covariances at lag zero see West (1979). For the other lags a two-pass algorithm is used to compute the cross-covariances; the accuracy of this algorithm is also discussed in West (1979). The accuracy of the cross-correlations will depend on the accuracy of the computed cross-covariances.

Parallelism and Performance

g13dmf 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

The time taken is roughly proportional to mnk2.


This program computes the sample cross-correlation matrices of two time series of length 48, up to lag 10. It also prints the cross-correlation matrices together with plots of symbols indicating which elements of the correlation matrices are significant. Three * represent significance at the 0.5% level, two * represent significance at the 1% level and a single * represents significance at the 5% level. The * are plotted above or below the line depending on whether the elements are significant in the positive or negative direction.

Program Text

Program Text (g13dmfe.f90)

Program Data

Program Data (g13dmfe.d)

Program Results

Program Results (g13dmfe.r)