示例#1
0
def test_ipw_numpy_data():
    w, t, y = generate_wty_linear_multi_w_data(n=N,
                                               wdim=5,
                                               binary_treatment=True,
                                               delta=ATE,
                                               data_format='numpy')
    ipw = IPWEstimator()
    ipw.fit(w, t, y)
    assert ipw.estimate_ate() == approx(ATE, rel=.2)
示例#2
0
def test_ipw_matches_causallib(linear_data_pandas):
    w, t, y = linear_data_pandas
    causallib_ipw = IPW(learner=LogisticRegression())
    causallib_ipw.fit(w, t)
    potential_outcomes = causallib_ipw.estimate_population_outcome(
        w, t, y, treatment_values=[0, 1])
    causallib_effect = causallib_ipw.estimate_effect(potential_outcomes[1],
                                                     potential_outcomes[0])[0]

    ipw = IPWEstimator()
    ipw.fit(w, t, y)
    our_effect = ipw.estimate_ate()
    assert our_effect == causallib_effect
示例#3
0
ATE = 5
N = 50


@pytest.fixture('module')
def linear_gen_model():
    w, t, y = generate_wty_linear_multi_w_data(n=N,
                                               wdim=5,
                                               binary_treatment=True,
                                               delta=ATE,
                                               data_format='numpy')
    return LinearGenModel(w, t, y, binary_treatment=True)


@pytest.fixture('module',
                params=[IPWEstimator(),
                        StandardizationEstimator()],
                ids=class_name)
def estimator(request):
    return request.param


def test_ate_metrics(linear_gen_model, estimator):
    metrics = calculate_metrics(gen_model=linear_gen_model,
                                estimator=estimator,
                                n_seeds=10,
                                conf_ints=False)
    assert metrics['ate_squared_bias'] + metrics['ate_variance'] == approx(
        metrics['ate_mse'])
    assert metrics['ate_bias'] == approx(0, abs=1)
示例#4
0
def ipw_estimator(request, linear_data):
    w, t, y = linear_data
    prop_score_model = request.param
    ipw = IPWEstimator(prop_score_model=prop_score_model)
    ipw.fit(w, t, y)
    return ipw
示例#5
0
def test_ipw_weight_trimming_and_stabilized_weights(linear_data):
    w, t, y = linear_data
    ipw = IPWEstimator(trim_weights=True, stabilized=True)
    ipw.fit(w, t, y)
    assert ipw.estimate_ate() == approx(ATE, rel=.2)
示例#6
0
def test_ipw_trim_eps(linear_data):
    w, t, y = linear_data
    ipw = IPWEstimator(trim_eps=.05)
    ipw.fit(w, t, y)
    assert ipw.estimate_ate() == approx(ATE, rel=.2)
示例#7
0
    model_type='outcome',
    gen_models=GEN_MODELS)

print('STRATIFIED STANDARDIZATION')
strat_df = run_experiments_for_estimator(
    lambda model: StratifiedStandardizationEstimator(outcome_models=model),
    model_grid=OUTCOME_MODEL_GRID,
    exclude=[('lalonde_cps', 'KernelRidge')],
    save_location=RESULTS_DIR / 'psid_cps_twins_strat_standard.csv',
    meta_est_name='stratified_standardization',
    model_type='outcome',
    gen_models=GEN_MODELS)

print('IPW')
ps_df = run_experiments_for_estimator(
    lambda model: IPWEstimator(prop_score_model=model),
    model_grid=PROP_SCORE_MODEL_GRID,
    # exclude=[('lalonde_psid', 'SVM_rbf')],
    exclude=['SVM_rbf'],
    save_location=RESULTS_DIR / 'psid_cps_twins_ipw.csv',
    meta_est_name='ipw',
    model_type='prop_score',
    gen_models=GEN_MODELS)

print('IPW TRIM EPS 0.01')
ps_trim_df = run_experiments_for_estimator(
    lambda model: IPWEstimator(prop_score_model=model, trim_eps=0.01),
    model_grid=PROP_SCORE_MODEL_GRID,
    # exclude=[('lalonde_psid', 'SVM_rbf')],
    exclude=['SVM_rbf'],
    save_location=RESULTS_DIR / 'psid_cps_twins_ipw_trim_01.csv',