Computes the sample cross-correlation function of two stationary time series.
#include <imsls.h>
float *imsls_f_crosscorrelation (int n_observations, float x[], float y[], int lagmax, ..., 0)
The type double function is imsls_d_crosscorrelation.
int n_observations
(Input)
Number of observations in each time series. n_observations must be
greater than or equal to 2.
float x[]
(Input)
Array of length n_observations
containing the first time series.
float y[]
(Input)
Array of length n_observations
containing the second time series.
int lagmax (Input)
Maximum lag of cross-covariances and cross-correlations to be
computed. lagmax must be greater
than or equal to 1 and less than n_observations.
Pointer to an array of length 2*lagmax + 1 containing the cross-correlations between the time series x and y. The kth element of this array contains the cross-correlation between x and y at lag (k-lagmax) where k = 0, 1, …, 2*lagmax. To release this space, use imsls_free. If no solution can be computed, NULL is returned.
#include <imsls.h>
float
*imsls_f_crosscorrelation (int
n_observations,
float x[],
float
y[],
int
lagmax,
IMSLS_PRINT_LEVEL,
int iprint,
IMSLS_INPUT_MEANS, float
x_mean_in,
float y_mean_in,
IMSLS_OUTPUT_MEANS,
float
*x_mean_out,
float *y_mean_out,
IMSLS_VARIANCES,
float *x_variance,
float *y_variance
IMSLS_SE_CCF,
float **standard_errors, int
se_option,
IMSLS_SE_CCF_USER,
float standard_errors[],
int
se_option,
IMSLS_CROSS_COVARIANCES,
float **cross_covariances,
IMSLS_CROSS_COVARIANCES_USER,
float
cross_covariances[],
IMSLS_RETURN_USER,
float crosscorrelations[],
0)
IMSLS_PRINT_LEVEL,
int iprint
(Input)
Printing option. Default = 0.
iprint |
Action |
0 |
No printing is performed. |
1 |
Prints the means and variances. |
2 |
Prints the means, variances, and cross-covariances. |
3 |
Prints the means, variances, cross-covariances, cross-correlations, and standard errors of cross-correlations. |
IMSLS_INPUT_MEANS,
float
x_mean_in,
float
y_mean_in
(Input)
If specified, x_mean_in is the user
input of the estimate of the mean of the time series x and y_mean_in is the user
input of the estimate of the mean of the time series y.
IMSLS_OUTPUT_MEANS,
float
*x_mean_out,
float
*y_mean_out
(Output)
If specified, x_mean_out is the mean
of the time series x and y_mean_out is the
mean of the time series y.
IMSLS_VARIANCES,
float
*x_variance,
float *y_variance
(Output)
If specified, x_variance is variance
of the time series x and y_variance is variance
of the time series y.
IMSLS_SE_CCF,
float **standard_errors,
int se_option
(Output)
Address of a pointer to an array of length 2*lagmax + 1containing
the standard errors of the cross-correlations between the time series x and y. Method of
computation for standard errors of the cross-correlations is chosen by se_option.
se_option |
Action |
1 |
Compute standard errors of cross-correlations using Bartlett’s formula. |
2 |
Compute standard errors of cross-correlations using Bartlett’s formula with the assumption of no cross-correlation. |
IMSLS_SE_CCF_USER,
float standard_errors[],
int se_option
(Output)
If specified, standard_errors is an
array of length 2*lagmax + 1 containing
the standard errors of the cross-correlations between the time series x and y. See IMSLS_SE_CC.
IMSLS_CROSS_COVARIANCES,
float **cross_covariances
(Output)
Address of a pointer to an array of length 2*lagmax + 1 containing
the cross-covariances between the time series x and y. The
kth element of this array contains the cross-covariances between x and y at
lag
(k-lagmax) where
k = 0, 1, …, 2*lagmax.
IMSLS_CROSS_COVARIANCES_USER,
float cross_covariances[]
(Output)
If specified, cross_covariances is
an array of length 2*lagmax + 1 the
cross-covariances between the time series x and y. See IMSLS_CROSS_COVARIANCES.
IMSLS_RETURN_USER,
float crosscorrelations[]
(Output)
If specified, crosscorrelations is an array of length 2*lagmax + 1 containing
the cross-correlations between the time series x and y. The
kth element of this array contains the cross-correlation between x and y at lag
(k-lagmax) where
k = 0, 1, …, 2*lagmax.
Function imsls_f_crosscorrelation
estimates the cross-correlation function of two jointly stationary time
series given a sample of
n = n_observations
observations {Xt} and
{Yt} for t = 1,
2, …, n.
Let
be the estimate of the mean μX of the time series {Xt} where
The autocovariance function of {Xt}, σX(k), is estimated by
where K = lagmax. Note that
is equivalent to the sample variance x_variance. The autocorrelation function ρX(k) is estimated by
Note that
by definition. Let
be similarly defined.
The cross-covariance function σXY(k) is estimated by
The cross-correlation function ρXY(k) is estimated by
The standard errors of the sample cross-correlations may be optionally computed according to argument se_option for the optional argument IMSLS_SE_CCF. One method is based on a general asymptotic expression for the variance of the sample cross-correlation coefficient of two jointly stationary time series with independent, identically distributed normal errors given by Bartlett (1978, page 352). The theoretical formula is
For computational purposes, the autocorrelations ρX(k) and ρY(k) and the cross-correlations ρXY(k) are replaced by their corresponding estimates for |k| ≤ K, and the limits of summation are equal to zero for all k such that |k| > K.
A second method evaluates Bartlett’s formula under the additional assumption that the two series have no cross-correlation. The theoretical formula is
For additional special cases of Bartlett’s formula, see Box and Jenkins (1976, page 377).
An important property of the cross-covariance coefficient is σXY(k) = σYX(−k) for k ≥ 0. This result is used in the computation of the standard error of the sample cross-correlation for lag k < 0. In general, the cross-covariance function is not symmetric about zero so both positive and negative lags are of interest.
Consider the Gas Furnace Data (Box and Jenkins 1976, pages 532–533) where X is the input gas rate in cubic feet/minute and Y is the percent CO2 in the outlet gas. Function imsls_f_crosscorrelation is used to compute the cross-covariances and cross-correlations between time series X and Y with lags from −lagmax = −10 through lag lagmax = 10. In addition, the estimated standard errors of the estimated cross-correlations are computed. The standard errors are based on the additional assumption that all cross-correlations for X and Y are zero.
#include <stdio.h>
#define nobs 296
#define lagmax 10
int main ()
{
int i;
float data[nobs][2], x[nobs], y[nobs];
float *secc = NULL, *ccv = NULL, *cc = NULL;
float xmean, ymean, xvar, yvar;
imsls_f_data_sets (7, IMSLS_X_COL_DIM, 2, IMSLS_RETURN_USER, data, 0);
for (i = 0; i < nobs; i++)
{
x[i] = data[i][0];
y[i] = data[i][1];
}
cc = imsls_f_crosscorrelation (nobs, x, y, lagmax,
IMSLS_OUTPUT_MEANS, &xmean, &ymean,
IMSLS_VARIANCES, &xvar, &yvar,
IMSLS_SE_CCF, &secc, 2,
IMSLS_CROSS_COVARIANCES, &ccv, 0);
printf ("Mean of series X = %g\n", xmean);
printf ("Variance of series X = %g\n\n", xvar);
printf ("Mean of series Y = %g\n", ymean);
printf ("Variance of series Y = %g\n\n", yvar);
printf ("Lag CCV CC SECC\n\n");
for (i = 0; i < 2 * lagmax + 1; i++)
printf ("%-5d%13g%13g%13g\n", i - lagmax, ccv[i], cc[i], secc[i]);
}
Mean of series X = -0.0568344
Variance of series X = 1.14694
Mean of series Y = 53.5091
Variance of series Y = 10.2189
Lag CCV CC SECC
-10 -0.404502 -0.118154 0.162754
-9 -0.508491 -0.148529 0.16247
-8 -0.61437 -0.179456 0.162188
-7 -0.705476 -0.206067 0.161907
-6 -0.776167 -0.226716 0.161627
-5 -0.831474 -0.242871 0.161349
-4 -0.891316 -0.260351 0.161073
-3 -0.980605 -0.286432 0.160798
-2 -1.12477 -0.328542 0.160524
-1 -1.34704 -0.393467 0.160252
0 -1.65853 -0.484451 0.159981
1 -2.04865 -0.598405 0.160252
2 -2.48217 -0.725033 0.160524
3 -2.88541 -0.84282 0.160798
4 -3.16536 -0.924592 0.161073
5 -3.25344 -0.950319 0.161349
6 -3.13113 -0.914593 0.161627
7 -2.83919 -0.82932 0.161907
8 -2.45302 -0.716521 0.162188
9 -2.05269 -0.599584 0.16247
10 -1.69466 -0.495004 0.162754