def test_hhgq_va(data_answers_name, cons_name):
    ## Get the constraint, data with answers and schema
    c, data_answers, schema = ctu.getConstraintDataAnswersSchema(
        schemaname, data_answers_name,
        ctu.getAllImplementedInvariantNames(schemaname), cons_name)

    # Check sign and right hand side
    assert c.sign == "le" if cons_name == 'hhgq_va_ub' else "ge"
    assert np.array_equal(c.rhs, data_answers[cons_name])

    if c.sign == "le":
        assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) <= c.rhs)
    else:
        assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) >= c.rhs)

    # Check Kronecker factors
    # Each column of the HHGQ matrix selects that GQ type (so only one True value -- on the diagonal)
    ctu.assertKronFactorDiagonal(c, CC.ATTR_HHGQ, schema)
    # For non-voting, select first AGECAT_VA_CUT categories, for voting select the other categories
    ctu.assertKronFactor(
        c, CC.ATTR_AGECAT,
        np.array([[True] * AGECAT_VA_CUT + [False] *
                  (ctu.dimSch(CC.ATTR_AGECAT, schema) - AGECAT_VA_CUT),
                  [False] * AGECAT_VA_CUT + [True] *
                  (ctu.dimSch(CC.ATTR_AGECAT, schema) - AGECAT_VA_CUT)]),
        schema)

    # Selecting everyone on all other axes
    for dimname in (CC.ATTR_SEX, CC.ATTR_HISP, CC.ATTR_CENRACE):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
Exemple #2
0
def test_householder_ub(data_answers_name):
    cons_name = "householder_ub"
    inv_names = list(ctu.getAllImplementedInvariantNames(schemaname))
    c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, inv_names, cons_name)

    # Check right hand side
    assert np.all(c.rhs == data_answers[cons_name])

    # Check left hand side
    assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) == data_answers[cons_name + "_lhs"])

    # Check sign
    assert c.sign == "le"
    assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) <= c.rhs)

    dim_hhgq = ctu.dimSch(CC.ATTR_RELGQ, schema)
    kf_hhgq = c.query.queries[0].kronFactors()[ctu.indSch(CC.ATTR_RELGQ, schema)]
    assert kf_hhgq.shape == (1, dim_hhgq)

    # True for 0,1 -- householder living alone and householder not living alone
    assert np.array_equal(kf_hhgq[0, :], np.array([True] * 2 + [False] * (dim_hhgq - 2)))

    # Selecting everyone on all other axes
    for dimname in ctu.excludeAx((CC.ATTR_RELGQ,), schema):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_no_refhh_under_15(data_answers_name):
    cons_name = 'no_refhh_under_15'

    # Get the constraint, data with answers and schema
    c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, ctu.getAllImplementedInvariantNames(schemaname), cons_name)

    assert c.sign == "="
    assert c.rhs[0] == 0
    assert ctu.lhsFromMatrix(c, data_answers["hist"][0])[0] == data_answers[cons_name]

    # Selecting relatives
    # 'Householder (0)', 'Husband/Wife (1)', 'Father/Mother (6)', 'Parent-in-law (8)', 'Son/Daughter-in-law (9)', 'Housemate, Roommate (12)',
    # 'Unmarried Partner (13)'
    age_cut = 15
    ans = np.array([[False] * ctu.dimSch(CC.ATTR_REL, schema)])
    ans[0, [0, 1, 6, 8, 9, 12, 13]] = True
    ctu.assertKronFactor(c, CC.ATTR_REL, ans, schema)
    # Selecting people under age_cut (First age_cut True, the rest are False)
    ctu.assertKronFactor(c, CC.ATTR_AGE, np.array([[True] * age_cut + [False] * (ctu.dimSch(CC.ATTR_AGE, schema) - age_cut)]), schema)

    # Everyone on all other axes
    for dimname in ctu.excludeAx((CC.ATTR_REL, CC.ATTR_AGE), schema):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_hhgq_total(data_answers_name, tot_inv, cons_name):

    # Create invariant names, remove 'tot' if checking without total
    inv_names = list(ctu.getAllImplementedInvariantNames(schemaname))
    if not tot_inv:
        inv_names.remove('tot')

    # Get the constraint, data with answers and schema
    c, data_answers, schema = ctu.getConstraintDataAnswersSchema(schemaname, data_answers_name, inv_names, cons_name)

    # Check sign, <= for upper bound, >= for lower
    assert c.sign == "le" if cons_name == 'hhgq_total_ub' else "ge"

    # Check right hand side
    if tot_inv:
        assert np.array_equal(c.rhs, data_answers[cons_name])
    else:
        assert np.array_equal(c.rhs, data_answers[cons_name + "_no_tot"])

    # Check right hand side from matrix application to the data
    if c.sign == "le":
        assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) <= c.rhs)
    else:
        assert np.all(ctu.lhsFromMatrix(c, data_answers["hist"][0]) >= c.rhs)

    # Should get 1 + (schDim(HHGQ) - NUM_HH_CATS) rows x schDim(HHGQ) columns
    dim_hhgq = ctu.dimSch(CC.ATTR_REL, schema)
    dim_gq = dim_hhgq - NUM_HH_CATS

    kf_hhgq = c.query.queries[0].kronFactors()[ctu.indSch(CC.ATTR_REL, schema)]
    assert kf_hhgq.shape == (dim_gq + 1, dim_hhgq)
    # First row is selecting first NUM_HH_CATS, and dropping all the GQs, so first NUM_HH_CATS values True, then False
    assert np.array_equal(kf_hhgq[0, :], np.array([True] * NUM_HH_CATS + [False] * dim_gq))
    # The rest rows drop first NUM_HH_CATS values
    assert np.array_equal(kf_hhgq[1:, :NUM_HH_CATS],  np.broadcast_to(np.array([False]), (dim_gq, NUM_HH_CATS)))
    # and select only the corresponding GQ type, leaving a diagonal of True in lower right corner of the matrix
    assert np.array_equal(kf_hhgq[1:, NUM_HH_CATS:], np.diag([True]*dim_gq))

    # Selecting everyone on all other axes
    for dimname in ctu.excludeAx((CC.ATTR_REL, ), schema):
        ctu.assertKronFactorSelectAll(c, dimname, schema)
def test_voting_age(data_answers_name):
    cons_name = 'voting_age'

    ## Get the constraint, data with answers and schema
    c, data_answers, schema = ctu.getConstraintDataAnswersSchema(
        schemaname, data_answers_name,
        ctu.getAllImplementedInvariantNames(schemaname), cons_name)

    # Check sign and right hand side, in constraint and from query matrix applied to the data
    assert c.sign == "="
    assert c.rhs[0] == data_answers[cons_name]
    assert ctu.lhsFromMatrix(
        c, data_answers["hist"][0])[0] == data_answers[cons_name]

    # Check Kronecker factors
    # The values below AGECAT_VA_CUT are non-voting, selecting the other ones
    ctu.assertKronFactorMiltiple(
        c, CC.ATTR_AGECAT,
        range(AGECAT_VA_CUT, ctu.dimSch(CC.ATTR_AGECAT, schema)), schema)

    # Selecting everyone on other axes
    for dimname in (CC.ATTR_HHGQ, CC.ATTR_SEX, CC.ATTR_HISP, CC.ATTR_CENRACE):
        ctu.assertKronFactorSelectAll(c, dimname, schema)