Class AutoCorrelation
- All Implemented Interfaces:
Serializable,Cloneable
AutoCorrelation estimates the autocorrelation function
of a stationary time series given a sample of n observations
\(\{X_t\}\) for
\({\rm t = 1, 2, \dots, n}\).
Let $$\hat \mu = {\rm {xmean}}$$ be the estimate of the mean \(\mu\) of the time series \(\{X_t\} \) where
$$\hat \mu = \left\{ \begin{array}{ll} \mu & {\rm for}\;\mu\; {\rm known} \\ \frac{1}{n}\sum\limits_{t=1}^n {X_t } & {\rm for}\;\mu\; {\rm unknown} \end{array} \right.$$ The autocovariance function \(\sigma(k)\) is estimated by $$\hat \sigma ( k ) = \frac{1}{n} \sum\limits_{t = 1}^{n - k} {\left( {X_t - \hat \mu } \right)} \left({X_{t + k} - \hat \mu } \right), \mbox{ } k=0,1,\dots, K $$where K = maximum_lag. Note that \(\hat
\sigma(0)\) is an estimate of the sample variance. The
autocorrelation function \(\rho(k)\) is estimated by
$$\hat\rho(k) = \frac{\hat \sigma(k)}{\hat
\sigma(0)},\mbox{ } k=0,1,\dots,K$$
Note that \(\hat \rho(0) \equiv 1\) by definition.
The standard errors of sample autocorrelations may be optionally
computed according to the getStandardErrors method argument
stderrMethod. One method (Bartlett 1946) is based on a
general asymptotic expression for the variance of the sample
autocorrelation coefficient of a stationary time series with
independent, identically distributed normal errors. The theoretical
formula is
$$\mbox{var}\{\hat \rho(k)\} =
\frac{1}{n}\sum\limits_{i=-\infty}^{\infty}
\left[{\rho^2(i)}+\rho(i-k)\rho(i+k)-4\rho(i)
\rho(k)\rho(i-k)+2\rho^2(i)\rho^2(k)\right]$$
where \(\hat \rho(k)\) assumes \(\mu\) is unknown. For computational purposes, the autocorrelations \(\rho(k)\) are replaced by their estimates \(\hat \rho(k)\) for \(\left|k\right|\leq K\), and the limits of summation are bounded because of the assumption that \(\rho(k) = 0\) for all \(k\) such that \(\left|k\right|> K\).
A second method (Moran 1947) utilizes an exact formula for the variance of the sample autocorrelation coefficient of a random process with independent, identically distributed normal errors. The theoretical formula is
$$var\{\hat \rho(k)\} = \frac{n-k}{n(n+2)}$$
where \(\mu\) is assumed to be equal to zero. Note that this formula does not depend on the autocorrelation function.
The method getPartialAutoCorrelations estimates the
partial autocorrelations of the stationary time series given K =
maximum_lag sample autocorrelations \(\hat
\rho(k)\) for k=0,1,...,K. Consider the AR(k)
process defined by $$X_t =
{\phi_{k1}}X_{t-1}+{\phi_{k2}}X_{t-2}+ \dots+{\phi_{kk}}X_{t-k}+A_t
$$ where \(\phi_{kj}\) denotes the j-th
coefficient in the process. The set of estimates \({\{\hat
\phi_{kk}\}}\) for k = 1, ..., K is the sample partial
autocorrelation function. The autoregressive parameters
\(\{\hat \phi_{kj}\}\) for j = 1, ..., k
are approximated by Yule-Walker estimates for successive AR(k)
models where k = 1, ..., K. Based on the sample Yule-Walker
equations
$$\hat\rho(j) = {\hat\phi_{k1}}\hat\rho(j-1) +
{\hat\phi_{k2}}\hat\rho(j-2) + \dots + {\hat\phi_{kk}}\hat\rho(j-k),
\mbox{ } j = 1,2,\dots,k $$
a recursive relationship for k=1, ..., K was developed by Durbin
(1960). The equations are given by
This procedure is sensitive to rounding error and should not be used if the parameters are near the nonstationarity boundary. A possible alternative would be to estimate \( {\{\phi_{kk}\}}\) for successive AR(k) models using least or maximum likelihood. Based on the hypothesis that the true process is AR(p), Box and Jenkins (1976, page 65) note
$$ {\rm var}{\{ \hat\phi_{kk}\}} \simeq \frac {1}{n} \;\;\;\;\; {\rm k}\; \geq \; {\rm p + 1}$$See Box and Jenkins (1976, pages 82-84) for more information concerning the partial autocorrelation function.
- 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 Moran's formula. -
Constructor Summary
ConstructorsConstructorDescriptionAutoCorrelation(double[] x, int maximum_lag) Constructor to compute the sample autocorrelation function of a stationary time series. -
Method Summary
Modifier and TypeMethodDescriptiondouble[]Returns the autocorrelations of the time seriesx.double[]Returns the variance and autocovariances of the time seriesx.doublegetMean()Returns the mean of the time seriesx.intReturns the number ofjava.lang.Threadinstances used for parallel processing.double[]Returns the sample partial autocorrelation function of the stationary time seriesx.double[]getStandardErrors(int stderrMethod) Returns the standard errors of the autocorrelations of the time seriesx.doubleReturns the variance of the time seriesx.voidsetMean(double mean) Estimate mean of the time seriesx.voidsetNumberOfThreads(int numberOfThreads) Sets the number ofjava.lang.Threadinstances to be used for parallel processing.
-
Field Details
-
BARTLETTS_FORMULA
public static final int BARTLETTS_FORMULAIndicates standard error computation using Bartlett's formula.- See Also:
-
MORANS_FORMULA
public static final int MORANS_FORMULAIndicates standard error computation using Moran's formula.- See Also:
-
-
Constructor Details
-
AutoCorrelation
public AutoCorrelation(double[] x, int maximum_lag) Constructor to compute the sample autocorrelation function of a stationary time series.- Parameters:
x- a one-dimensionaldoublearray containing the stationary time seriesmaximum_lag- anintcontaining the maximum lag of autocovariance, autocorrelations, and standard errors of autocorrelations to be computed.maximum_lagmust be greater than or equal to 1 and less than the number of observations inx
-
-
Method Details
-
setNumberOfThreads
public void setNumberOfThreads(int numberOfThreads) Sets the number ofjava.lang.Threadinstances to be used for parallel processing.- Parameters:
numberOfThreads- anintspecifying the number ofjava.lang.Threadinstances to be used for parallel processing.Default:
numberOfThreads= 1.
-
getNumberOfThreads
public int getNumberOfThreads()Returns the number ofjava.lang.Threadinstances used for parallel processing.- Returns:
- an
intcontaining the number ofjava.lang.Threadinstances used for parallel processing.
-
setMean
public void setMean(double mean) Estimate mean of the time seriesx.- Parameters:
mean- adoublecontaining the estimate mean of the time seriesx.
-
getMean
public double getMean()Returns the mean of the time seriesx.- Returns:
- a
doublecontaining the mean
-
getStandardErrors
public double[] getStandardErrors(int stderrMethod) Returns the standard errors of the autocorrelations of the time seriesx. Method of computation for standard errors of the autocorrelation is chosen by thestderrMethodparameter. IfstderrMethodis set toBARTLETTS_FORMULA, Bartlett's formula is used to compute the standard errors of autocorrelations. IfstderrMethodis set toMORANS_FORMULA, Moran's formula is used to compute the standard errors of autocorrelations.- Parameters:
stderrMethod- anintspecifying the method to compute the standard errors of autocorrelations of the time seriesx- Returns:
- a
doublearray of lengthmaximum_lagcontaining the standard errors of the autocorrelations of the time seriesx
-
getAutoCorrelations
public double[] getAutoCorrelations()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 \({\rm k}\) where k = 1, ...,maximum_lag.
-
getAutoCovariances
Returns the variance and 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:
AutoCorrelation.NonPosVariancesException- is thrown if the problem is ill-conditioned
-
getVariance
public double getVariance()Returns the variance of the time seriesx.- Returns:
- a
doublecontaining the variance of the time seriesx
-
getPartialAutoCorrelations
public double[] getPartialAutoCorrelations()Returns the sample partial autocorrelation function of the stationary time seriesx.- Returns:
- a
doublearray of lengthmaximum_lagcontaining the partial autocorrelations of the time seriesx.
-