def test_summary():

    Y = np.array([1, 2, 3, 4, 6, 5])
    D = np.array([0, 0, 1, 1, 0, 1])
    X = np.array([[1, 3], [5, 7], [8, 6], [4, 2], [9, 11], [12, 10]])
    data = d.Data(Y, D, X)
    summary = s.Summary(data)

    N = 6
    K = 2
    N_c = 3
    N_t = 3
    Y_c_mean = 3
    Y_t_mean = 4
    Y_c_sd = np.sqrt(7)
    Y_t_sd = 1
    rdiff = 1
    X_c_mean = np.array([5, 7])
    X_t_mean = np.array([8, 6])
    X_c_sd = np.array([4, 4])
    X_t_sd = np.array([4, 4])
    ndiff = np.array([0.75, -0.25])
    keys1 = {
        'N', 'K', 'N_c', 'N_t', 'Y_c_mean', 'Y_t_mean', 'Y_c_sd', 'Y_t_sd',
        'X_c_mean', 'X_t_mean', 'X_c_sd', 'X_t_sd', 'rdiff', 'ndiff',
        'sum_of_abs_ndiffs', 'num_large_ndiffs', 'num_outputs'
    }

    assert_equal(summary['N'], N)
    assert_equal(summary['N_c'], N_c)
    assert_equal(summary['N_t'], N_t)
    assert_equal(summary['Y_c_mean'], Y_c_mean)
    assert_equal(summary['Y_t_mean'], Y_t_mean)
    assert_equal(summary['Y_c_sd'], Y_c_sd)
    assert_equal(summary['Y_t_sd'], Y_t_sd)
    assert_equal(summary['rdiff'], rdiff)
    assert np.array_equal(summary['X_c_mean'], X_c_mean)
    assert np.array_equal(summary['X_t_mean'], X_t_mean)
    assert np.array_equal(summary['X_c_sd'], X_c_sd)
    assert np.array_equal(summary['X_t_sd'], X_t_sd)
    assert np.array_equal(summary['ndiff'], ndiff)
    assert_equal(set(summary.keys()), keys1)

    p_c = np.array([0.3, 0.5, 0.7])
    p_t = np.array([0.1, 0.5, 0.9])
    summary._summarize_pscore(p_c, p_t)
    keys2 = {
        'N', 'K', 'N_c', 'N_t', 'Y_c_mean', 'Y_t_mean', 'Y_c_sd', 'Y_t_sd',
        'X_c_mean', 'X_t_mean', 'X_c_sd', 'X_t_sd', 'rdiff', 'ndiff', 'p_min',
        'p_max', 'p_c_mean', 'p_t_mean', 'sum_of_abs_ndiffs',
        'num_large_ndiffs', 'num_outputs'
    }

    assert_equal(summary['p_min'], 0.1)
    assert_equal(summary['p_max'], 0.9)
    assert_equal(summary['p_c_mean'], 0.5)
    assert_equal(summary['p_t_mean'], 0.5)
    assert_equal(set(summary.keys()), keys2)
Beispiel #2
0
def test_propensity():
    import causalinference.core.data as d
    import causalinference.core.propensity as p
    from utils import random_data

    D = np.array([0, 0, 0, 1, 1, 1])
    X = np.array([[7, 8], [3, 10], [7, 10], [4, 7], [5, 10], [9, 8]])
    Y = random_data(D_cur=D, X_cur=X)
    print Y

    data = d.Data(Y, D, X)
    propensity = p.Propensity(data, [0, 1], [])
    print propensity
def test_weighting():

    Y = np.array([1, -2, 3, -5, 7])
    D = np.array([0, 1, 0, 1, 0])
    X = np.array([3, 2, 3, 5, 5])
    pscore = np.array([0.1, 0.25, 0.5, 0.75, 0.9])
    data = d.Data(Y, D, X)
    data._dict['pscore'] = pscore

    weighting = w.Weighting(data)
    ate = -6.7963178
    ate_se = 2.8125913
    keys = {'ate', 'ate_se'}
    assert np.allclose(weighting['ate'], ate)
    assert np.allclose(weighting['ate_se'], ate_se)
    assert_equal(set(weighting.keys()), keys)
Beispiel #4
0
def test_ols():

	Y = np.array([52, 30, 5, 29, 12, 10, 44, 87])
	D = np.array([0, 0, 0, 0, 1, 1, 1, 1])
	X = np.array([[1, 42], [3, 32], [9, 7], [12, 86],
	              [5, 94], [4, 36], [2, 13], [6, 61]])
	data = d.Data(Y, D, X)

	adj1 = 0
	ols1 = o.OLS(data, adj1, [])
	ate1 = 9.25
	ate_se1 = 17.68253
	keys = {'ate', 'ate_se',
		'atc',
		'att',
		'atc_se',
		'name_to_coef',
		'att_se',
		'r2'
	}
	assert np.allclose(ols1['ate'], ate1)
	assert np.allclose(ols1['ate_se'], ate_se1)
	assert_equal(set(ols1.keys()), keys)

	adj2 = 1
	ols2 = o.OLS(data, adj2, [])
	ate2 = 3.654552
	ate_se2 = 17.749993
	assert np.allclose(ols2['ate'], ate2)
	assert np.allclose(ols2['ate_se'], ate_se2)
	assert_equal(set(ols2.keys()), keys)

	adj3 = 2
	ols3 = o.OLS(data, adj3, feature_names=[])
	ate3 = 30.59444
	atc3 = 63.2095
	att3 = -2.020611
	ate_se3 = 19.91887865
	atc_se3 = 29.92152
	att_se3 = 11.8586
	assert np.allclose(ols3['ate'], ate3)
	assert np.allclose(ols3['atc'], atc3)
	assert np.allclose(ols3['att'], att3)
	assert np.allclose(ols3['ate_se'], ate_se3)
	assert np.allclose(ols3['atc_se'], atc_se3)
	assert np.allclose(ols3['att_se'], att_se3)
	assert_equal(set(ols3.keys()), keys)
def test_propensityselect():

    D = np.array([0, 0, 0, 1, 1, 1])
    X = np.array([[7, 8], [3, 10], [7, 10], [4, 7], [5, 10], [9, 8]])
    Y = random_data(D_cur=D, X_cur=X)
    data = d.Data(Y, D, X)

    propensity1 = p.PropensitySelect(data, [], 1, 2.71)
    lin1 = [1]
    qua1 = []
    coef1 = np.array([6.5424027, -0.7392041])
    loglike1 = -3.627939
    fitted1 = np.array(
        [0.6522105, 0.2995088, 0.2995088, 0.7970526, 0.2995088, 0.6522105])
    se1 = np.array([6.8455179, 0.7641445])
    keys = {'lin', 'qua', 'coef', 'loglike', 'fitted', 'se'}

    assert_equal(propensity1['lin'], lin1)
    assert_equal(propensity1['qua'], qua1)
    assert np.allclose(propensity1['coef'], coef1)
    assert np.allclose(propensity1['loglike'], loglike1)
    assert np.allclose(propensity1['fitted'], fitted1)
    assert np.allclose(propensity1['se'], se1)
    assert_equal(set(propensity1.keys()), keys)

    propensity2 = p.PropensitySelect(data, [0, 1], 1, 2.71)
    lin2 = [0, 1]
    qua2 = []
    coef2 = np.array([6.8066090, -0.0244874, -0.7524939])
    loglike2 = -3.626517
    fitted2 = np.array(
        [0.6491366, 0.3117840, 0.2911631, 0.8086407, 0.3013733, 0.6379023])
    se2 = np.array([8.5373779, 0.4595191, 0.8106499])

    assert_equal(propensity2['lin'], lin2)
    assert_equal(propensity2['qua'], qua2)
    assert np.allclose(propensity2['coef'], coef2)
    assert np.allclose(propensity2['loglike'], loglike2)
    assert np.allclose(propensity2['fitted'], fitted2)
    assert np.allclose(propensity2['se'], se2)
Beispiel #6
0
def test_propensity():

	D = np.array([0, 0, 0, 1, 1, 1])
	X = np.array([[7, 8], [3, 10], [7, 10], [4, 7], [5, 10], [9, 8]])
	Y = random_data(D_cur=D, X_cur=X)

	data = d.Data(Y, D, X)
	propensity = p.Propensity(data, [0, 1], [])
	lin = [0, 1]
	qua = []
	coef = np.array([6.8066090, -0.0244874, -0.7524939])
	loglike = -3.626517
	fitted = np.array([0.6491366, 0.3117840, 0.2911631,
	                   0.8086407, 0.3013733, 0.6379023])
	se = np.array([8.5373779, 0.4595191, 0.8106499])
	keys = {'lin', 'qua', 'coef', 'loglike', 'fitted', 'se'}
	
	assert_equal(propensity['lin'], lin)
	assert_equal(propensity['qua'], qua)
	assert np.allclose(propensity['coef'], coef)
	assert np.allclose(propensity['loglike'], loglike)
	assert np.allclose(propensity['fitted'], fitted)
	assert np.allclose(propensity['se'], se)
	assert_equal(set(propensity.keys()), keys)
Beispiel #7
0
def test_data():

    Y1 = np.array([1.2, 3.45, -6, 78.90, -9, 8.7654])
    D1 = np.array([0, 1, 0, 1.0, 0.0, 1])
    X1 = np.array([[-1, 2], [3, -4], [-5.6, -7], [8.9, 0.0], [99, 877],
                   [-666, 54321]])
    data = d.Data(Y1, D1, X1)

    ans1 = np.array([[1.2, 3.45, -6, 78.9, -9, 8.7654]]).transpose()
    print(ans1)
    print(data['Y'])
    assert np.array_equal(data['Y'], ans1)

    ans2 = np.array([0, 1, 0, 1, 0, 1])
    assert np.array_equal(data['D'], ans2)

    ans3 = np.array([[-1, 2], [3, -4], [-5.6, -7], [8.9, 0], [99, 877],
                     [-666, 54321]])
    assert np.array_equal(data['X'], ans3)

    ans4 = 6
    assert_equal(data['N'], ans4)

    ans5 = 2
    assert_equal(data['K'], ans5)

    ans6 = np.array([True, False, True, False, True, False])
    assert np.array_equal(data['controls'], ans6)

    ans7 = np.array([False, True, False, True, False, True])
    assert np.array_equal(data['treated'], ans7)

    ans8 = np.array([[1.2, -6, -9]]).transpose()
    assert np.array_equal(data['Y_c'], ans8)

    ans9 = np.array([[3.45, 78.9, 8.7654]]).transpose()
    assert np.array_equal(data['Y_t'], ans9)

    ans10 = np.array([[-1, 2], [-5.6, -7], [99, 877]])
    assert np.array_equal(data['X_c'], ans10)

    ans11 = np.array([[3, -4], [8.9, 0], [-666, 54321]])
    assert np.array_equal(data['X_t'], ans11)

    ans12 = 3
    assert_equal(data['N_t'], ans12)

    ans13 = 3
    assert_equal(data['N_c'], ans13)

    ans14 = 'int'
    assert_equal(data['D'].dtype, ans14)

    ans15 = {
        'Y', 'D', 'X', 'N', 'K', 'controls', 'treated', 'Y_c', 'Y_t', 'X_c',
        'X_t', 'N_c', 'N_t', 'pairs', 'ids', 'num_outputs'
    }
    assert_equal(set(data.keys()), ans15)

    Y2 = np.array([[1.2], [3.45], [-6], [78.90]])
    D2 = np.array([[0], [1], [0.0], [1]])
    X2 = np.array([[-1, 2], [3, -4], [-5.6, -7], [8.9, 0.0]])
    assert_raises(ValueError, d.Data, Y2, D2, X2)