def s(self) -> NDArray: """HAC score covariance estimate""" x, z, eps = self.x, self.z, self.eps nobs, nvar = x.shape pinvz = self._pinvz xhat = z @ (pinvz @ x) xhat_e = xhat * eps kernel = self.config["kernel"] bw = self.config["bandwidth"] if bw is None: self._auto_bandwidth = True from linearmodels.shared.linalg import has_constant const, loc = has_constant(xhat) sel = ones((xhat.shape[1], 1)) if const: sel[loc] = 0 scores = xhat_e @ sel bw = kernel_optimal_bandwidth(scores, kernel) self._bandwidth = bw w = self._kernels[kernel](bw, nobs - 1) s = cov_kernel(xhat_e, w) return self._scale * s
def weight_matrix(self, x: NDArray, z: NDArray, eps: NDArray) -> NDArray: """ Parameters ---------- x : ndarray Model regressors (exog and endog), (nobs by nvar) z : ndarray Model instruments (exog and instruments), (nobs by ninstr) eps : ndarray Model errors (nobs by 1) Returns ------- ndarray Covariance of GMM moment conditions. """ nobs, nvar = x.shape ze = z * eps mu = ze.mean(axis=0) if self._center else 0 ze -= mu if self._orig_bandwidth is None and self._optimal_bw: g = ze / ze.std(0)[None, :] g = g.sum(1) self._bandwidth = kernel_optimal_bandwidth(g, self._kernel) elif self._orig_bandwidth is None: self._bandwidth = nobs - 2 bw = self._bandwidth assert bw is not None w = self._kernels[self._kernel](bw, nobs - 1) s = cov_kernel(ze, w) s *= 1 if not self._debiased else nobs / (nobs - nvar) return s
def test_cov_kernel(): with pytest.raises(ValueError): cov_kernel(np.arange(100), 1 - np.arange(101) / 101)