def test_debiased(self, kernel, data, bandwidth): wm = KernelWeightMatrix(debiased=True, kernel=kernel.kernel, bandwidth=bandwidth) weight = wm.weight_matrix(data.x, data.z, data.e) z, e, nobs, nvar = data.z, data.e, data.nobs, data.nvar bw = bandwidth or nobs - 2 w = kernel.weight(bw, nobs - 1) ze = z * e s = ze.T @ ze for i in range(1, len(w)): op = ze[i:].T @ ze[:-i] s += w[i] * (op + op.T) assert_allclose(weight, s / (nobs - nvar)) assert wm.config['bandwidth'] == bw assert wm.config['kernel'] == kernel.kernel
def test_kernel(self, data, kernel, bandwidth): c = IVGMMCovariance( data.x, data.y, data.z, data.params, data.i, "kernel", kernel=kernel.kernel, bandwidth=bandwidth, ) s = KernelWeightMatrix(kernel=kernel.kernel, bandwidth=bandwidth).weight_matrix( data.x, data.z, data.e) x, z, nobs = data.x, data.z, data.nobs xzwswzx = x.T @ z @ s @ z.T @ x / data.nobs cov = data.xzizx_inv @ xzwswzx @ data.xzizx_inv cov = (cov + cov.T) / 2 assert_allclose(c.cov, cov) assert c.config["kernel"] == kernel.kernel assert c.config["debiased"] is False assert c.config["bandwidth"] == bandwidth or nobs - 2 c = IVGMMCovariance(data.x, data.y, data.z, data.params, data.i, "kernel") assert "Kernel" in str(c)
def test_center(self, data, kernel, bandwidth): wm = KernelWeightMatrix(kernel.kernel, bandwidth, center=True, optimal_bw=True) weight = wm.weight_matrix(data.x, data.z, data.e) z, e, nobs = data.z, data.e, data.nobs bw = bandwidth or wm.bandwidth w = kernel.weight(bw, nobs - 1) ze = z * e ze = ze - ze.mean(0) s = ze.T @ ze for i in range(1, len(w)): op = ze[i:].T @ ze[:-i] s += w[i] * (op + op.T) assert_allclose(weight, s / nobs) assert wm.config['bandwidth'] == bw assert wm.config['kernel'] == kernel.kernel for name in kernel.alt_names: wm = KernelWeightMatrix(name, bandwidth, center=True, optimal_bw=True) weight2 = wm.weight_matrix(data.x, data.z, data.e) assert_equal(weight, weight2)
def test_config(self, data, kernel, bandwidth): wm = KernelWeightMatrix(kernel=kernel.kernel, bandwidth=bandwidth) weight = wm.weight_matrix(data.x, data.z, data.e) z, e, nobs = data.z, data.e, data.nobs bw = bandwidth or wm.bandwidth w = kernel.weight(bw, nobs - 1) ze = z * e s = ze.T @ ze for i in range(1, len(w)): op = ze[i:].T @ ze[:-i] s += w[i] * (op + op.T) assert_allclose(weight, s / nobs) assert wm.config['center'] is False assert wm.config['debiased'] is False assert wm.config['bandwidth'] == bw assert wm.config['kernel'] == kernel.kernel for name in kernel.alt_names: wm = KernelWeightMatrix(kernel=name, bandwidth=bandwidth) weight2 = wm.weight_matrix(data.x, data.z, data.e) assert_equal(weight, weight2)