def test_center(self, data): wm = OneWayClusteredWeightMatrix(data.clusters, True) weight = wm.weight_matrix(data.x, data.z, data.e) ze = data.z * data.e ze -= ze.mean(0) uc = np.unique(data.clusters) s = np.zeros((ze.shape[1], ze.shape[1])) for val in uc: obs = ze[data.clusters == val] sum = obs.sum(0)[:, None] s += sum @ sum.T assert_allclose(weight, s / data.nobs)
def test_debiased(self, data): wm = OneWayClusteredWeightMatrix(data.clusters, debiased=True) weight = wm.weight_matrix(data.x, data.z, data.e) ze = data.z * data.e uc = np.unique(data.clusters) s = np.zeros((ze.shape[1], ze.shape[1])) for val in uc: obs = ze[data.clusters == val] sum = obs.sum(0)[:, None] s += sum @ sum.T nobs, nvar = data.nobs, data.nvar groups = len(uc) scale = (nobs - 1) / (nobs - nvar) * groups / (groups - 1) assert_allclose(weight, scale * s / data.nobs)
def test_clustered(self, data): c = IVGMMCovariance(data.x, data.y, data.z, data.params, data.i, 'clustered', clusters=data.clusters) s = OneWayClusteredWeightMatrix(clusters=data.clusters).weight_matrix(data.x, data.z, data.e) x, z = data.x, data.z 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['debiased'] is False assert_equal(c.config['clusters'], data.clusters) c = IVGMMCovariance(data.x, data.y, data.z, data.params, data.i, 'clustered') assert 'Clustered' in str(c)
def test_errors(self, data): wm = OneWayClusteredWeightMatrix(data.clusters[:10]) with pytest.raises(ValueError): wm.weight_matrix(data.x, data.z, data.e)
def test_config(self, data): wm = OneWayClusteredWeightMatrix(data.clusters) assert wm.config['center'] is False assert wm.config['debiased'] is False assert_equal(wm.config['clusters'], data.clusters)
def test_config(self, data): wm = OneWayClusteredWeightMatrix(data.clusters) assert wm.config["center"] is False assert wm.config["debiased"] is False assert_equal(wm.config["clusters"], data.clusters)