def test_kernel_covariance_asymptotic(data, kernel): c = KernelCovariance(data.x, data.y, data.z, data.params, kernel=kernel.kernel) cs = str(c) assert "\nBandwidth" not in cs for name in kernel.alt_names: c2 = KernelCovariance(data.x, data.y, data.z, data.params, kernel=name) assert_equal(c.cov, c2.cov) assert c.debiased is False assert c.config["debiased"] is False assert_equal(c.config["kernel"], kernel.kernel) assert_allclose(c.s2, data.s2) bw = c.config["bandwidth"] xe = data.xhat * data.e s = xe.T @ xe w = kernel.weight(bw, xe.shape[0] - 1) for i in range(1, len(w)): s += w[i] * (xe[i:].T @ xe[:-i] + xe[:-i].T @ xe[i:]) s /= data.nobs assert_allclose(c.s, s) assert_allclose(c.cov, data.vinv @ s @ data.vinv / data.nobs) cs = str(c) assert "Kappa" not in cs assert "Kernel: {0}".format(kernel.kernel) in cs assert "Bandwidth: {0}".format(bw) in cs
def test_debiased(self, data, kernel): c = KernelCovariance( data.x, data.y, data.z, data.params, kernel=kernel.kernel, debiased=True ) for name in kernel.alt_names: c2 = KernelCovariance( data.x, data.y, data.z, data.params, kernel=name, debiased=True ) assert_equal(c.cov, c2.cov) assert c._kappa == 1 assert c.debiased is True assert c.config["debiased"] is True assert_equal(c.config["kernel"], kernel.kernel) assert_allclose(c.s2, data.s2_debiased) c2 = KernelCovariance( data.x, data.y, data.z, data.params, kernel=kernel.kernel, debiased=False ) scale = data.nobs / (data.nobs - data.nvar) assert_allclose(c.s, scale * c2.s) assert_allclose(c.cov, scale * c2.cov) cs = str(c) assert "Debiased: True" in cs assert "Kernel: {0}".format(kernel.kernel) in cs assert "Bandwidth: {0}".format(c.config["bandwidth"]) in cs assert "id" in c.__repr__()
def test_kernel_covariance_unknown_kernel(data, kernel): with pytest.raises(ValueError): KernelCovariance(data.x, data.y, data.z, data.params, kernel=kernel.kernel + "_unknown")
def test_unknown_kernel(self, data, kernel): with pytest.raises(ValueError): KernelCovariance(data.x, data.y, data.z, data.params, kernel=kernel.kernel + '_unknown')