linearmodels.iv.covariance.kernel_optimal_bandwidth

linearmodels.iv.covariance.kernel_optimal_bandwidth(x: ndarray[tuple[int, ...], dtype[float64]], kernel: str = 'bartlett') → int[source]

Select the bandwidth of a kernel covariance estimator from the data.

Parameters:
x: ndarray[tuple[int, ...], dtype[float64]]

The series to use when computing the bandwidth. It has t observations, and is a 1-d array of length t or an array with t rows and 1 column.

kernel: str = 'bartlett'

Name of the kernel to use. The default is “bartlett”. The names are not case sensitive. Supported kernels are:

  • ”bartlett”, “newey-west” - Bartlett’s kernel

  • ”parzen”, “gallant” - Parzen’s kernel

  • ”qs”, “quadratic-spectral”, “andrews” - Quadratic spectral kernel

Returns:

Optimal bandwidth. Set to nobs - 1 if computed bandwidth is larger.

Return type:

int

Raises:
  • ValueError – If x has fewer observations than the kernel needs: 4 for the Bartlett kernel, 5 for the Parzen kernel and 6 for the quadratic spectral kernel. It is also raised if the bandwidth is not finite, which happens if x has values that are not finite or if the estimate of the long-run variance that the bandwidth divides by is exactly 0, as it is for a series of zeros.

  • UnknownOptionError – If kernel is not one of the supported names. It is both a KeyError and a ValueError.

See also

kernel_weight_bartlett

Kernel weights from a Bartlett kernel.

kernel_weight_parzen

Kernel weights from a Parzen kernel.

kernel_weight_quadratic_spectral

Kernel weights from a quadratic-spectral kernel.

Notes

The bandwidth is that of the nonparametric rule of Newey and West [1]. With the autocovariances of the series \(x_i\),

\[\hat{\sigma}_j = t^{-1} \sum_{i=j+1}^{t} x_i x_{i-j},\]

the rule computes two weighted sums of the first \(n\) of them,

\[\begin{split}\hat{s}_0 & = \hat{\sigma}_0 + 2 \sum_{j=1}^{n} \hat{\sigma}_j \\ \hat{s}_q & = 2 \sum_{j=1}^{n} j^q \hat{\sigma}_j\end{split}\]

and the bandwidth is the smaller of \(\lceil \hat{m} \rceil\) and \(t - 1\), where

\[\hat{m} = c \left\{ \left(\hat{s}_q / \hat{s}_0 \right)^2 \right\}^{1 / (2q + 1)} t^{1 / (2q + 1)}.\]

The exponent \(q\) and the constant \(c\) depend on the kernel. The number of lags in the pilot estimates, \(\hat{s}_0\) and \(\hat{s}_q\), is \(n = \lceil 4 (t / 100)^a \rceil\), and the exponent \(a\) depends on the kernel too:

Kernel

q

c

a

Fewest obs.

“bartlett”, “newey-west”

1

1.1447

2/9

4

“parzen”, “gallant”

2

2.6614

4/25

5

“qs”, “quadratic-spectral”, “andrews”

2

1.3221

2/25

6

The autocovariances of a series with \(t\) observations are defined up to lag \(t - 1\). If \(n \geq t - 1\), \(\hat{s}_0\) is the sum of all of them, which is \((\sum_i x_i)^2 / t\). It is 0, up to rounding error, for a series with a mean of 0, such as the scores of a least squares regression or the deviations of estimates from their average, so that the bandwidth divides by 0 or by rounding error. For any other series it depends on the mean alone, and not on the autocorrelation. The rule needs \(n < t - 1\). That is the case with at least 4 observations for the Bartlett kernel (\(n = 2\)), 5 for the Parzen kernel (\(n = 3\)) and 6 for the quadratic spectral kernel (\(n = 4\)), and a ValueError is raised for fewer. Pass the bandwidth to the estimator that uses the kernel to use it with fewer observations.

Changed in version 7.1: A ValueError is raised if the series has fewer observations than the kernel needs, and if the bandwidth is not finite. Earlier versions raised “cannot convert float infinity to integer” (an OverflowError) or “cannot convert float NaN to integer” for some series, and returned \(t - 1\), or another number, for the others.

References