arch.covariance.var.PreWhitenedRecolored¶
-
class arch.covariance.var.PreWhitenedRecolored(x: ndarray | DataFrame | Series, *, lags: int | None =
None, method: str ='aic', diagonal: bool =True, max_lag: int | None =None, sample_autocov: bool =False, kernel: str | None ='bartlett', bandwidth: float | None =None, df_adjust: int =0, center: bool =True, weights: ndarray | DataFrame | Series | None =None, force_int: bool =False)[source]¶ VAR-HAC and Pre-Whitened-Recolored Long-run covariance estimation.
Andrews & Monahan [1] PWRC and DenHaan-Levin’s VAR-HAC [2] covariance estimators.
- Parameters:¶
- x: ndarray | DataFrame | Series¶
The data to use in covariance estimation. Observations are in rows and variables in columns. Integer, Boolean and float32 data are converted to float64. float16 and complex data are not accepted. Must have at least 2 rows and 1 column, and must not contain NaN or infinite values.
- lags: int | None =
None¶ The number of lags to include in the VAR. If None, a specification search is used to select the order. Must be non-negative and small enough that each equation of the VAR has more observations than parameters, see Notes.
- method: str =
'aic'¶ The information criteria to use in the model specification search. Not case sensitive.
- diagonal: bool =
True¶ Flag indicating whether the specification search also considers models where the coefficient matrices on the final lags are diagonal. A diagonal coefficient matrix restricts all off-diagonal coefficients to be zero. Only used when lags is None and x has more than one column.
- max_lag: int | None =
None¶ The maximum lag to use in the model specification search. If None, then int(nobs**(1/3)) is used. Must be non-negative. The value used is limited, see Notes.
- sample_autocov: bool =
False¶ Whether to use the sample autocovariance of x or the autocovariance implied by the estimated VAR when computing the short-run and one-sided covariances. Does not affect the long-run covariance.
- kernel: str | None =
'bartlett'¶ The name of the kernel to use, such as “bartlett” or “quadratic-spectral”. It can be the name of any of the kernels in
arch.covariance.kernel.KERNELS, in lower case with hyphens between the words. The name is not case sensitive and any underscores or hyphens are ignored, so that “quadratic-spectral”, “quadratic_spectral” and “quadraticspectral” are all the same. Use None to compute the VAR-HAC estimator, which recolors the residual covariance without applying a kernel to the residuals.- bandwidth: float | None =
None¶ The kernel’s bandwidth. If None, optimal bandwidth is estimated from the VAR residuals. Must be finite and non-negative, and None or 0 when kernel is None.
- df_adjust: int =
0¶ Degrees of freedom to remove when adjusting the covariance. Uses the number of observations in x minus df_adjust when dividing inner-products, see Notes. Must be at least 0 and smaller than the number of observations in x.
- center: bool =
True¶ A flag indicating whether x should be demeaned before estimating the covariance.
- weights: ndarray | DataFrame | Series | None =
None¶ An array of weights used to combine when estimating optimal bandwidth. If not provided, a vector of 1s is used. Must have nvar elements, be finite and non-negative, and at least one must be strictly positive.
- force_int: bool =
False¶ Force bandwidth to be an integer.
- Raises:¶
TypeError – If x or weights cannot be converted to float64 (x is float16 or complex, or contains strings or objects that are not numbers), if lags, max_lag or df_adjust is not an integer (a float, including 5.0, or a Boolean is not accepted), if bandwidth is not a real number, if diagonal, sample_autocov, center or force_int is not a bool, or if method or kernel is not a str.
ValueError – If x is not 2-d, has fewer than 2 rows or no columns or contains NaN or infinite values; if lags or max_lag is negative or lags is larger than the largest order that can be estimated; if bandwidth is negative, not finite or not None or 0 when kernel is None; if df_adjust is negative or not smaller than the number of observations; if weights does not have nvar finite non-negative elements, or has only zeros; or if method or kernel is not one of the available names.
See also
arch.covariance.kernelKernel-based long-run covariance estimators.
Notes
The estimator is computed in three steps.
Prewhitening. A VAR is estimated by least squares, equation by equation,
\[x_t = c + A_1 x_{t-1} + \ldots + A_P x_{t-P} + \epsilon_t\]where the constant \(c\) is included only when
centeris True. Whenlagsis provided, a VAR(lags) with unrestricted coefficient matrices is used. Otherwise the order is selected by minimizing\[IC = \ln|\hat{\Sigma}| + \lambda \frac{k}{T}\]where \(\hat{\Sigma}=T^{-1}\sum_t \hat{\epsilon}_t\hat{\epsilon}_t^\prime\), \(k\) is the total number of estimated parameters, \(T\) is the number of observations used in the regressions, which is common to all candidate models, and \(\lambda\) is 2 (
"aic"), \(2\ln\ln T\) ("hqc") or \(\ln T\) ("bic"). Models with 0, 1, …,max_laglags are considered. Whendiagonalis True and x has more than one column, the search also considers models with unrestricted coefficient matrices on the first \(p\) lags and diagonal coefficient matrices on lags \(p+1, \ldots, q\) for \(p < q \leq\)max_lag, so that each series only uses its own values at the additional lags.max_lagis limited to(nobs - nvar) // nvarand to the largest order for which each equation of the VAR has more observations than parameters.lagsmust also satisfy this restriction.Kernel estimation. The long-run covariance of the VAR residuals, \(\hat{\Omega}_\epsilon\), is estimated using the selected kernel applied to the residuals without centering. When
kernelis None, a zero-lag kernel is used so that \(\hat{\Omega}_\epsilon=\hat{\Sigma}\), which is the VAR-HAC estimator of den Haan & Levin. This is also the case when the bandwidth is 0. The products of the residuals are divided by \(T-\)df_adjust, where \(T\) is the number of observations in x, rather than by the number of residuals, which is smaller when the order is positive. This matches thesandwichpackage in R: the estimator is identical tosandwich::meatHAC(prewhite=P, adjust=FALSE)whendf_adjustis 0 and tosandwich::meatHAC(prewhite=P, adjust=TRUE)whendf_adjustis the number of columns in x, if the kernel weights are the same, x has been demeaned andcenteris False, since the VAR in R has no intercept.Recoloring. The long-run covariance of x is
\[\hat{\Omega} = \hat{D}\hat{\Omega}_\epsilon\hat{D}^\prime, \quad \hat{D} = \left(I_N - \sum_{i=1}^P \hat{A}_i\right)^{-1}\]where \(N\) is the number of columns in x. When the selected order is 0, no VAR is estimated and all returned values are those of the kernel estimator applied to x (demeaned when
centeris True).When the VAR order is positive, the returned
CovarianceEstimatecontainslong_run: \(\hat{\Omega}\).short_run: the upper-left \(N\) by \(N\) block of \(\Gamma_0\), the covariance of the stacked vector \([x_t^\prime, \ldots, x_{t-P+1}^\prime]^\prime\). By default \(\Gamma_0\) is implied by the estimated VAR and the covariance of its residuals, \(\hat{\Sigma}\), so thatshort_runis the variance of x implied by the VAR. Whensample_autocovis True, \(\Gamma_0\) is computed from the sample autocovariances of x andshort_runis the sample variance of x. It is not the covariance of the VAR residuals, which is available asresid_cov.one_sided_strict: the upper-left \(N\) by \(N\) block of \(F(I-F)^{-1}\Gamma_0\) where \(F\) is the companion-form coefficient matrix of the VAR. This is \(\sum_{j\geq 1}\Gamma_j\) where \(\Gamma_j=E[x_tx_{t-j}^\prime]\).one_sided:short_runplusone_sided_strict.
The one-sided covariances do not use the kernel. The identities in
CovarianceEstimatethat define the one-sided covariances hold, so thatone_sided = short_run + one_sided_strict. The long-run covariance satisfieslong_run = short_run + one_sided_strict + one_sided_strict.Twhen the autocovariances are those of the VAR and the kernel is not used, that is whensample_autocovis False andkernelis None or the bandwidth is 0. Otherwiselong_rundiffers from this sum.Complex data are not supported. The kernel estimators in
arch.covariance.kernelaccept complex data, but this estimator raises aTypeError. Prewhitening fits a model, and there is no single VAR for complex data. A VAR with complex coefficients cannot represent real and imaginary parts that depend on each other in a way that is not a complex multiplication, and the VAR of the stacked real and imaginary parts is a different model with more parameters. The order that the information criteria above select depends on which of them is used. To prewhiten complex data, use this estimator withnp.column_stack([x.real, x.imag]), or use a kernel estimator, which needs no model.Inspecting the VAR. The prewhitening step determines the estimate, so its results are available.
orderis the order of the VAR, which is the only way to learn which order was chosen whenlagsis None.residare the residuals of the VAR, which are the series that the kernel is applied to and can be tested to check that the VAR removed the serial correlation in x.resid_covis their covariance, which is the matrix that is recolored into the long-run covariance of x. None of these require the VAR to be covariance stationary, so they are available even whencovraises an error because it is not.References
Examples
>>> import numpy as np >>> from arch.covariance.var import PreWhitenedRecolored >>> rs = np.random.default_rng(0) >>> e = rs.standard_normal((1001, 2)) >>> x = e[1:] + 0.5 * e[:-1]Prewhiten with a VAR(1) and use a Bartlett kernel with a bandwidth of 5
>>> pwrc = PreWhitenedRecolored(x, lags=1, kernel="bartlett", bandwidth=5.0) >>> lrcov = pwrc.cov.long_runThe order, residuals and residual covariance of the VAR are available
>>> pwrc.order (1, 1) >>> resid = pwrc.resid >>> resid_cov = pwrc.resid_covVAR-HAC with the VAR order selected using BIC
>>> var_hac = PreWhitenedRecolored(x, kernel=None, method="bic") >>> lrcov = var_hac.cov.long_runMethods
Properties
The bandwidth used by the kernel estimator.
The power used in optimal bandwidth calculation.
Flag indicating whether the data are centered (demeaned).
The estimated covariances.
Flag indicating whether the bandwidth is restricted to be an integer.
The constant used in optimal bandwidth calculation.
The weights used in covariance calculation.
The covariance estimator's name.
Estimate optimal bandwidth.
The order of the VAR used to prewhiten x.
The optimal rate used in bandwidth selection.
The residuals of the VAR used to prewhiten x.
The covariance of the residuals of the VAR used to prewhiten x.