def _validate_data(self) -> None: p = self.portfolios.ndarray f = self.factors.ndarray if p.shape[0] != f.shape[0]: raise ValueError("The number of observations in portfolios and " "factors is not the same.") self._drop_missing() p = self.portfolios.ndarray f = self.factors.ndarray if has_constant(p)[0]: raise ValueError("portfolios must not contains a constant or " "equivalent and must not have rank\n" "less than the dimension of the smaller shape.") if has_constant(f)[0]: raise ValueError( "factors must not contain a constant or equivalent.") if np.linalg.matrix_rank(f) < f.shape[1]: raise ValueError( "Model cannot be estimated. factors do not have full column rank." ) if p.shape[0] < (f.shape[1] + 1): raise ValueError( "Model cannot be estimated. portfolios must have factors + 1 or " "more returns to\nestimate the model parameters.")
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 test_hasconstant(): x = np.random.randn(100, 3) hc, loc = has_constant(x) assert bool(hc) is False assert loc is None x[:, 0] = 1 hc, loc = has_constant(x) assert hc is True assert loc == 0 x[:, 0] = 2 hc, loc = has_constant(x) assert hc is True assert loc == 0 x[::2, 0] = 0 x[:, 1] = 1 x[1::2, 1] = 0 hc, loc = has_constant(x) assert hc is True