def test_formula_equivalence_weights(data): weights = AttrDict() eqn_copy = AttrDict() for key in data.eqns: eqn = {k: v for k, v in data.eqns[key].items()} nobs = eqn["dependent"].shape[0] w = np.random.chisquare(2, (nobs, 1)) / 2 weights[key] = w eqn["weights"] = w eqn_copy[key] = eqn mod = IVSystemGMM(eqn_copy, weight_type="unadjusted") df = [] formulas = {} for i, key in enumerate(data.eqns): eqn = data.eqns[key] dep = eqn.dependent ex = eqn.exog en = eqn.endog instr = eqn.instruments dep = DataFrame(dep, columns=["dep_{0}".format(i)]) has_const = False if np.any(np.all(ex == 1, 0)): ex = ex[:, 1:] has_const = True ex = DataFrame( ex, columns=["ex_{0}_{1}".format(i, j) for j in range(ex.shape[1])]) en = DataFrame( en, columns=["en_{0}_{1}".format(i, j) for j in range(en.shape[1])]) instr = DataFrame( instr, columns=["instr_{0}_{1}".format(i, j) for j in range(ex.shape[1])]) fmla = "".join(dep.columns) + " ~ " if has_const: fmla += " 1 + " fmla += " + ".join(ex.columns) + " + [" fmla += " + ".join(en.columns) + " ~ " fmla += " + ".join(instr.columns) + " ] " formulas[key] = fmla df.extend([dep, ex, en, instr]) df = concat(df, 1) formula_mod = IVSystemGMM.from_formula(formulas, df, weights=weights, weight_type="unadjusted") res = mod.fit(cov_type="unadjusted") formula_res = formula_mod.fit(cov_type="unadjusted") assert_allclose(res.params, formula_res.params)
def test_formula_equivalence(data): mod = IVSystemGMM(data.eqns, weight_type="unadjusted") formula = [] df = [] for i, key in enumerate(data.eqns): eqn = data.eqns[key] dep = eqn.dependent ex = eqn.exog en = eqn.endog instr = eqn.instruments dep = DataFrame(dep, columns=["dep_{0}".format(i)]) has_const = False if np.any(np.all(ex == 1, 0)): ex = ex[:, 1:] has_const = True ex = DataFrame( ex, columns=["ex_{0}_{1}".format(i, j) for j in range(ex.shape[1])]) en = DataFrame( en, columns=["en_{0}_{1}".format(i, j) for j in range(en.shape[1])]) instr = DataFrame( instr, columns=["instr_{0}_{1}".format(i, j) for j in range(ex.shape[1])]) fmla = "".join(dep.columns) + " ~ " if has_const: fmla += " 1 + " fmla += " + ".join(ex.columns) + " + [" fmla += " + ".join(en.columns) + " ~ " fmla += " + ".join(instr.columns) + " ] " formula.append(fmla) df.extend([dep, ex, en, instr]) formulas = {} for i, f in enumerate(formula): formulas["eq{0}".format(i)] = f df = concat(df, 1) formula_mod = IVSystemGMM.from_formula(formulas, df, weight_type="unadjusted") res = mod.fit(cov_type="unadjusted") formula_res = formula_mod.fit(cov_type="unadjusted") assert_allclose(res.params, formula_res.params)
def test_formula_equivalence_weights(data): weights = AttrDict() eqn_copy = AttrDict() for key in data.eqns: eqn = {k: v for k, v in data.eqns[key].items()} nobs = eqn['dependent'].shape[0] w = np.random.chisquare(2, (nobs, 1)) / 2 weights[key] = w eqn['weights'] = w eqn_copy[key] = eqn mod = IVSystemGMM(eqn_copy, weight_type='unadjusted') df = [] formulas = OrderedDict() for i, key in enumerate(data.eqns): eqn = data.eqns[key] dep = eqn.dependent ex = eqn.exog en = eqn.endog instr = eqn.instruments dep = pd.DataFrame(dep, columns=['dep_{0}'.format(i)]) has_const = False if np.any(np.all(ex == 1, 0)): ex = ex[:, 1:] has_const = True ex = pd.DataFrame(ex, columns=['ex_{0}_{1}'.format(i, j) for j in range(ex.shape[1])]) en = pd.DataFrame(en, columns=['en_{0}_{1}'.format(i, j) for j in range(en.shape[1])]) instr = pd.DataFrame(instr, columns=['instr_{0}_{1}'.format(i, j) for j in range(ex.shape[1])]) fmla = ''.join(dep.columns) + ' ~ ' if has_const: fmla += ' 1 + ' fmla += ' + '.join(ex.columns) + ' + [' fmla += ' + '.join(en.columns) + ' ~ ' fmla += ' + '.join(instr.columns) + ' ] ' formulas[key] = fmla df.extend([dep, ex, en, instr]) df = pd.concat(df, 1) formula_mod = IVSystemGMM.from_formula(formulas, df, weights=weights, weight_type='unadjusted') res = mod.fit(cov_type='unadjusted') formula_res = formula_mod.fit(cov_type='unadjusted') assert_allclose(res.params, formula_res.params)
def test_formula_equivalence(data): mod = IVSystemGMM(data.eqns, weight_type='unadjusted') formula = [] df = [] for i, key in enumerate(data.eqns): eqn = data.eqns[key] dep = eqn.dependent ex = eqn.exog en = eqn.endog instr = eqn.instruments dep = pd.DataFrame(dep, columns=['dep_{0}'.format(i)]) has_const = False if np.any(np.all(ex == 1, 0)): ex = ex[:, 1:] has_const = True ex = pd.DataFrame(ex, columns=['ex_{0}_{1}'.format(i, j) for j in range(ex.shape[1])]) en = pd.DataFrame(en, columns=['en_{0}_{1}'.format(i, j) for j in range(en.shape[1])]) instr = pd.DataFrame(instr, columns=['instr_{0}_{1}'.format(i, j) for j in range(ex.shape[1])]) fmla = ''.join(dep.columns) + ' ~ ' if has_const: fmla += ' 1 + ' fmla += ' + '.join(ex.columns) + ' + [' fmla += ' + '.join(en.columns) + ' ~ ' fmla += ' + '.join(instr.columns) + ' ] ' formula.append(fmla) df.extend([dep, ex, en, instr]) from collections import OrderedDict formulas = OrderedDict() for i, f in enumerate(formula): formulas['eq{0}'.format(i)] = f df = pd.concat(df, 1) formula_mod = IVSystemGMM.from_formula(formulas, df, weight_type='unadjusted') res = mod.fit(cov_type='unadjusted') formula_res = formula_mod.fit(cov_type='unadjusted') assert_allclose(res.params, formula_res.params)