def test_model_data_ineq(): # note all subdata order is swapped in model_data due to sorting by gini natdata, subdata = data() model = Model(natdata, subdata) # ineq obs = model.model_data['t'] exp = ineq.gini_to_theil(subdata['gini'].values, empirical=False) assert obs == approx(exp[::-1])
def test_gini_to_theil_error(): with pytest.raises(ValueError): iq.gini_to_theil(-1) with pytest.raises(ValueError): iq.gini_to_theil(0.0) with pytest.raises(ValueError): iq.gini_to_theil(1.0) with pytest.raises(ValueError): iq.gini_to_theil(1.1)
def _setup_model_data(self, natdata, subdata): required = (n, i, gini) = 'n', 'i', 'gini' ndf, sdf = self.natdata.copy(), self.subdata.copy() msg = 'Must include all of {} in {} data' if any(x not in ndf for x in required): raise ValueError(msg.format(required, 'national')) if any(x not in sdf for x in required): raise ValueError(msg.format(required, 'subnational')) # correct income by scaling sdf[n] *= ndf[n] / sdf[n].sum() sdf[i] *= (ndf[n] * ndf[i]) / (sdf[n] * sdf[i]).sum() # calculate national within theil T = ineq.gini_to_theil(ndf[gini], empirical=self.empirical) gfrac = (sdf[n] * sdf[i]) / (ndf[n] * ndf[i]) nfrac = sdf[n] / ndf[n] T_b = np.sum(gfrac * np.log(gfrac / nfrac)) T_w = T - T_b # save index of sorted values self.orig_idx = sdf.index sdf = sdf.sort_values(by=gini) self.sorted_idx = sdf.index sdf = sdf.reset_index() self.model_idx = sdf.index # must be ordered # save model data self.model_data = { 'idxs': self.model_idx.values, 'n': sdf[n].values, 'i': sdf[i].values, 'g': (sdf[n] * sdf[i]).values, 't': ineq.gini_to_theil(sdf[gini].values, empirical=self.empirical), 'N': ndf[n], 'I': ndf[i], 'G': ndf[n] * ndf[i], 'T_w': T_w, }
def test_gini_to_theil_close_hi(): # just shouldn't through iq.gini_to_theil(0.99) iq.gini_to_theil(0.99, empirical=True)
def test_gini_to_theil_close_lo(): # just shouldn't through iq.gini_to_theil(0.01) iq.gini_to_theil(0.05, empirical=True) # emp lowers theil
def test_gini_to_theil_empirical(): obs = iq.gini_to_theil(0.5, empirical=True) exp = 0.41796 assert obs == approx(exp, abs=1e-5)
def test_gini_to_theil(): obs = iq.gini_to_theil(0.5) exp = 0.45493 assert obs == approx(exp, abs=1e-5)