Class CrossCorrelation
- All Implemented Interfaces:
Serializable,Cloneable
CrossCorrelation estimates the cross-correlation function of
two jointly stationary time series given a sample of n =
x.length observations \(\{X_t\}\)
and \(\{Y_t\}\) for t = 1,2, ..., n.
Let $$\hat \mu _x = \rm{xmean}$$ be the estimate of the mean \(\mu _X\) of the time series \(\{X_t\}\) where $$ \hat \mu _X = \left\{ \begin{array}{ll} \mu _X & {\rm for}\;\mu _X\; {\rm known} \\ \frac{1}{n}\sum\limits_{t=1}^n {X_t } & {\rm for}\;\mu _X\; {\rm unknown} \end{array} \right.$$
The autocovariance function of \(\{X_t\}\),
\(\sigma _X(k)\), is estimated by
$$\hat \sigma _X\left( k \right) = \frac{1}{n}
\sum\limits_{t = 1}^{n - k} {\left( {X_t - \hat \mu _X} \right)} \left(
{X_{t + k} - \hat \mu _X} \right), \mbox{\hspace{20pt}k=0,1,\dots,K}$$
where K = maximum_lag. Note that
\(\hat \sigma _X(0)\) is
equivalent to the sample variance of x returned by method
getVarianceX. The autocorrelation function
\(\rho _X(k)\) is estimated by
$$\hat\rho _X(k) =
\frac{\hat \sigma _X(k)}{\hat \sigma _X(0)},\mbox{\hspace{20pt}}
k=0,1,\dots,K$$
Note that \(\hat \rho _x(0) \equiv 1\) by definition. Let $$\hat \mu _Y = {\rm ymean}, \hat \sigma _Y(k), {\rm and} \hat \rho _Y(k)$$ be similarly defined.
The cross-covariance function \(\sigma _{XY}(k)\) is estimated by $$\hat \sigma _{XY}(k) = \left\{ \begin{array}{ll} \frac{1}{n}\sum\limits_{t=1}^{n-k}(X_t - {\hat \mu _X})(Y_{t+k} - {\hat\mu _Y}) &{k = 0,1, \dots,K} \\ \frac{1}{n}\sum\limits_{t=1-k}^{n}(X_t - {\hat \mu _X})(Y_{t+k} - {\hat\mu _Y}) &{k = -1,-2, \dots,-K} \end {array} \right. $$ The cross-correlation function \(\rho _{XY}(k)\) is estimated by $$ \hat \rho _{XY}(k) = \frac{\hat \sigma _{XY}(k)} {[\hat\sigma _X(0) \hat\sigma _Y(0) ]^{\frac{1}{2}}} \;\;\; {k = 0,\pm1, \dots,\pm K}$$
The standard errors of the sample cross-correlations may be optionally
computed according to the getStandardErrors method argument
stderrMethod. 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 Bartlet (1978, page 352).
The theoretical formula is $$\begin{array}{c}
{\rm var} \left \{ \hat \rho _{XY}(k) \right \} =
\frac{1}{n-k}\sum\limits_{i=-\infty}^{\infty}
\left [\right. {\rho _X(i)}+\rho _{XY}(i-k)\rho _{XY}(i+k) \\
-2\rho _{XY}(k)\{\rho _X(i)\rho _{XY}(i+k)+\rho _{XY}(-i)\rho _Y(i+k)\} \\
+\rho^2_{XY}(k)\{\rho_X(i) + \frac{1}{2}\rho^2_X(i) +
\frac{1}{2}\rho^2_Y(i)\} \left. \right ] \end{array}$$
For computational purposes, the autocorrelations
\(\rho_X(k)\) and \(\rho_Y(k)\)
and the cross-correlations \(\rho _{XY}(k)\)
are replaced by their corresponding estimates for
\(\left|k\right|\le K\),
and the limits of summation are equal to zero for all k such that
\(\left|k\right| > K\).
A second method evaluates Bartlett's formula under the additional assumption that the two series have no cross-correlation. The theoretical formula is $${\rm var}\{\hat \rho_{XY}(k)\} = \frac{1}{n-k}\sum\limits_{i=-\infty}^{\infty}{\rho_X(i)\rho_Y(i)} \;\;\;\;\; {k \ge 0}$$ For additional special cases of Bartlett's formula, see Box and Jenkins (1976, page 377).
An important property of the cross-covariance coefficient is \(\sigma _{XY}(k) = \sigma _{YX}(-k)\) for \(k \ge 0\). This result is used in the computation of the standard error of the sample cross-correlation for lag \(k \lt 0\). In general, the cross-covariance function is not symmetric about zero so both positive and negative lags are of interest.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classThe problem is ill-conditioned. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intIndicates standard error computation using Bartlett's formula.static final intIndicates standard error computation using Bartlett's formula with the assumption of no cross-correlation. -
Constructor Summary
ConstructorsConstructorDescriptionCrossCorrelation(double[] x, double[] y, int maximum_lag) Constructor to compute the sample cross-correlation function of two stationary time series. -
Method Summary
Modifier and TypeMethodDescriptiondouble[]Returns the autocorrelations of the time seriesx.double[]Returns the autocorrelations of the time seriesy.double[]Returns the autocovariances of the time seriesx.double[]Returns the autocovariances of the time seriesy.double[]Returns the cross-correlations between the time seriesxandy.double[]Returns the cross-covariances between the time seriesxandy.doublegetMeanX()Returns the mean of the time seriesx.doublegetMeanY()Returns the mean of the time seriesy.double[]getStandardErrors(int stderrMethod) Returns the standard errors of the cross-correlations between the time seriesxandy.doubleReturns the variance of time seriesx.doubleReturns the variance of time seriesy.voidsetMeanX(double mean) Estimate of the mean of time seriesx.voidsetMeanY(double mean) Estimate of the mean of time seriesy.
-
Field Details
-
BARTLETTS_FORMULA
public static final int BARTLETTS_FORMULAIndicates standard error computation using Bartlett's formula.- See Also:
-
BARTLETTS_FORMULA_NOCC
public static final int BARTLETTS_FORMULA_NOCCIndicates standard error computation using Bartlett's formula with the assumption of no cross-correlation.- See Also:
-
-
Constructor Details
-
CrossCorrelation
public CrossCorrelation(double[] x, double[] y, int maximum_lag) Constructor to compute the sample cross-correlation function of two stationary time series.- Parameters:
x- A one-dimensionaldoublearray containing the first stationary time series.y- A one-dimensionaldoublearray containing the second stationary time series.maximum_lag- Anintcontaining the maximum lag of the cross-covariance and cross-correlations to be computed.maximum_lagmust be greater than or equal to 1 and less than the minimum of the number of observations ofxandy.
-
-
Method Details
-
setMeanX
public void setMeanX(double mean) Estimate of the mean of time seriesx.- Parameters:
mean- Adoublecontaining the estimate mean of the time seriesx.
-
getMeanX
public double getMeanX()Returns the mean of the time seriesx.- Returns:
- A
doublecontaining the mean of the time seriesx.
-
setMeanY
public void setMeanY(double mean) Estimate of the mean of time seriesy.- Parameters:
mean- Adoublecontaining the estimate mean of the time seriesy.
-
getMeanY
public double getMeanY()Returns the mean of the time seriesy.- Returns:
- A
doublecontaining the mean of the time seriesy.
-
getVarianceX
Returns the variance of time seriesx.- Returns:
- A
doublecontaining the variance of the time seriesx. - Throws:
CrossCorrelation.NonPosVariancesException
-
getVarianceY
Returns the variance of time seriesy.- Returns:
- A
doublecontaining the variance of the time seriesy. - Throws:
CrossCorrelation.NonPosVariancesException
-
getCrossCorrelation
Returns the cross-correlations between the time seriesxandy.- Returns:
- A
doublearray of length 2 *maximum_lag+1 containing the cross-correlations between the time seriesxandy. The cross-correlation betweenxandyat lag k, where k = -maximum_lag,..., 0, 1,...,maximum_lag, corresponds to output array indices0, 1,..., (2*maximum_lag). - Throws:
CrossCorrelation.NonPosVariancesException
-
getCrossCovariance
public double[] getCrossCovariance()Returns the cross-covariances between the time seriesxandy.- Returns:
- A
doublearray of length 2 *maximum_lag+1 containing the cross-covariances between the time seriesxandy. The cross-covariance betweenxandyat lag k, where k = -maximum_lag,..., 0, 1,...,maximum_lag, corresponds to output array indices0, 1,..., (2*maximum_lag).
-
getAutoCorrelationX
Returns the autocorrelations of the time seriesx.- Returns:
- A
doublearray of lengthmaximum_lag+1 containing the autocorrelations of the time seriesx. The 0-th element of this array is 1. The k-th element of this array contains the autocorrelation of lag k where k = 1, ...,maximum_lag. - Throws:
CrossCorrelation.NonPosVariancesException
-
getAutoCorrelationY
Returns the autocorrelations of the time seriesy.- Returns:
- A
doublearray of lengthmaximum_lag+1 containing the autocorrelations of the time seriesy. The 0-th element of this array is 1. The k-th element of this array contains the autocorrelation of lag k where k = 1, ...,maximum_lag. - Throws:
CrossCorrelation.NonPosVariancesException
-
getAutoCovarianceX
Returns the autocovariances of the time seriesx.- Returns:
- A
doublearray of lengthmaximum_lag+1 containing the variances and autocovariances of the time seriesx. The 0-th element of the array contains the variance of the time seriesx. The k-th element contains the autocovariance of lag k where k = 1, ...,maximum_lag. - Throws:
CrossCorrelation.NonPosVariancesException
-
getAutoCovarianceY
Returns the autocovariances of the time seriesy.- Returns:
- A
doublearray of lengthmaximum_lag+1 containing the variances and autocovariances of the time seriesy. The 0-th element of the array contains the variance of the time seriesx. The k-th element contains the autocovariance of lag k where k = 1, ...,maximum_lag. - Throws:
CrossCorrelation.NonPosVariancesException
-
getStandardErrors
public double[] getStandardErrors(int stderrMethod) throws CrossCorrelation.NonPosVariancesException Returns the standard errors of the cross-correlations between the time seriesxandy. Method of computation for standard errors of the cross-correlation is determined by thestderrMethodparameter. IfstderrMethodis set to BARTLETTS_FORMULA, Bartlett's formula is used to compute the standard errors of cross-correlations. IfstderrMethodis set to BARTLETTS_FORMULA_NOCC, Bartlett's formula is used to compute the standard errors of cross-correlations, with the assumption of no cross-correlation.- Parameters:
stderrMethod- Anintspecifying the method to compute the standard errors of cross-correlations between the time seriesxandy.- Returns:
- A
doublearray of length 2 *maximum_lag+ 1 containing the standard errors of the cross-correlations between the time seriesxandy. The standard error of cross-correlations betweenxandyat lag k, where k = -maximum_lag,..., 0, 1,...,maximum_lag, corresponds to output array indices 0, 1,...,(2*maximum_lag). - Throws:
CrossCorrelation.NonPosVariancesException
-