Esempio n. 1
0
def test_unit_init_print():
    """This test ensures that the init file printing process work as intended. For this
    purpose we generate random init file specifications import the resulting files,
    write the specifications to another init file, import it again and comparing both
     initialization dicts
    """
    order = [
        "GENERAL",
        "CONSTANTS",
        "EDUC",
        "SIMULATION",
        "SOLUTION",
        "EXOG_PROC",
    ]

    for _ in range(5):
        random_init()
        model_params_df, _ = read_model_params_init("test.soepy.pkl")
        model_spec = read_model_spec_init("test.soepy.yml", model_params_df)
        init_dict_flat = namedtuple_to_dict(model_spec)
        init_dict = init_dict_flat_to_init_dict(init_dict_flat)
        init_dict2 = read_init_file2("test.soepy.yml")

        for key in order:
            for subkey in init_dict[key].keys():
                if not init_dict[key][subkey] == init_dict2[key][subkey]:
                    raise AssertionError()
Esempio n. 2
0
def partiable_simulate(
    states,
    indexer,
    covariates,
    child_age_update_rule,
    child_state_indexes,
    prob_educ_level,
    prob_child_age,
    prob_partner_present,
    prob_exp_ft,
    prob_exp_pt,
    prob_child,
    prob_partner,
    model_params_init_file_name,
    model_spec_init_file_name,
    is_expected=True,
):
    # Read in model specification from yaml file
    model_params_df, model_params = read_model_params_init(
        model_params_init_file_name)

    model_spec = read_model_spec_init(model_spec_init_file_name,
                                      model_params_df)

    # Obtain model solution
    non_employment_consumption_resources, emaxs = pyth_solve(
        states,
        covariates,
        child_state_indexes,
        model_params,
        model_spec,
        prob_child,
        prob_partner,
        is_expected,
    )

    # Simulate agents experiences according to parameters in the model specification
    df = pyth_simulate(
        model_params,
        model_spec,
        states,
        indexer,
        emaxs,
        covariates,
        non_employment_consumption_resources,
        child_age_update_rule,
        prob_educ_level,
        prob_child_age,
        prob_partner_present,
        prob_exp_ft,
        prob_exp_pt,
        prob_child,
        prob_partner,
        is_expected=False,
    )

    return df
Esempio n. 3
0
def get_simulate_func(model_params_init_file_name, model_spec_init_file_name):
    """Create the simulation function, such that the state space creation is already
    done ."""

    # Read in model specification from yaml file
    model_params_df, model_params = read_model_params_init(
        model_params_init_file_name)

    model_spec = read_model_spec_init(model_spec_init_file_name,
                                      model_params_df)

    # Get information concerning exogenous processes
    prob_educ_level = gen_prob_educ_level_vector(model_spec)
    prob_child_age = gen_prob_child_init_age_vector(model_spec)
    prob_partner_present = gen_prob_partner_present_vector(model_spec)
    prob_exp_ft = gen_prob_init_exp_vector(model_spec,
                                           model_spec.ft_exp_shares_file_name)
    prob_exp_pt = gen_prob_init_exp_vector(model_spec,
                                           model_spec.pt_exp_shares_file_name)
    prob_child = gen_prob_child_vector(model_spec)
    prob_partner = gen_prob_partner(model_spec)

    # Create state space
    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    partial_simulate = partial(
        partiable_simulate,
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
        prob_educ_level,
        prob_child_age,
        prob_partner_present,
        prob_exp_ft,
        prob_exp_pt,
        prob_child,
        prob_partner,
    )
    return partial_simulate
Esempio n. 4
0
def simulate(model_params_init_file_name, model_spec_init_file_name):
    """Create a data frame of individuals' simulated experiences."""

    # Read in model specification from yaml file
    model_params_df, model_params = read_model_params_init(
        model_params_init_file_name)
    model_spec = read_model_spec_init(model_spec_init_file_name,
                                      model_params_df)

    # Obtain model solution
    states, indexer, covariates, emaxs = pyth_solve(model_params, model_spec)

    # Simulate agents experiences according to parameters in the model specification
    df = pyth_simulate(model_params, model_spec, states, indexer, emaxs,
                       covariates)

    return df
Esempio n. 5
0
def test_pyth_simulate(input_vault, test_id):
    """This test runs a random selection of test regression tests from
    our regression test battery.
    """

    (
        model_spec_init_dict,
        random_model_params_df,
        exog_educ_shares,
        exog_child_age_shares,
        exog_partner_shares,
        exog_exper_shares_pt,
        exog_exper_shares_ft,
        exog_child_info,
        exog_partner_arrival_info,
        exog_partner_separation_info,
        expected_df_sim_func,
        expected_df_sim_sol,
    ) = input_vault[test_id]

    exog_educ_shares.to_pickle("test.soepy.educ.shares.pkl")
    exog_child_age_shares.to_pickle("test.soepy.child.age.shares.pkl")
    exog_child_info.to_pickle("test.soepy.child.pkl")
    exog_partner_shares.to_pickle("test.soepy.partner.shares.pkl")
    exog_exper_shares_pt.to_pickle("test.soepy.pt.exp.shares.pkl")
    exog_exper_shares_ft.to_pickle("test.soepy.ft.exp.shares.pkl")
    exog_partner_arrival_info.to_pickle("test.soepy.partner.arrival.pkl")
    exog_partner_separation_info.to_pickle("test.soepy.partner.separation.pkl")

    model_params_df, model_params = read_model_params_init(random_model_params_df)
    model_spec = read_model_spec_init(model_spec_init_dict, model_params_df)

    prob_educ_level = gen_prob_educ_level_vector(model_spec)
    prob_child_age = gen_prob_child_init_age_vector(model_spec)
    prob_partner_present = gen_prob_partner_present_vector(model_spec)
    prob_exp_ft = gen_prob_init_exp_vector(
        model_spec, model_spec.ft_exp_shares_file_name
    )
    prob_exp_pt = gen_prob_init_exp_vector(
        model_spec, model_spec.pt_exp_shares_file_name
    )
    prob_child = gen_prob_child_vector(model_spec)
    prob_partner = gen_prob_partner(model_spec)

    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    # Obtain model solution
    non_employment_consumption_resources, emaxs = pyth_solve(
        states,
        covariates,
        child_state_indexes,
        model_params,
        model_spec,
        prob_child,
        prob_partner,
        False,
    )

    # Simulate
    calculated_df = pyth_simulate(
        model_params,
        model_spec,
        states,
        indexer,
        emaxs,
        covariates,
        non_employment_consumption_resources,
        child_age_update_rule,
        prob_educ_level,
        prob_child_age,
        prob_partner_present,
        prob_exp_ft,
        prob_exp_pt,
        prob_child,
        prob_partner,
        is_expected=False,
    )

    pd.testing.assert_series_equal(
        calculated_df.sum(axis=0),
        expected_df_sim_sol,
    )
    cleanup()
Esempio n. 6
0
def test_unit_data_frame_shape():
    """This test ensures that the shape of the simulated data frame corresponds
    to the random specifications of our initialization file.
    """
    for _ in range(5):
        constr = dict()
        constr["AGENTS"] = np.random.randint(10, 100)
        constr["PERIODS"] = np.random.randint(7, 10)
        constr["EDUC_YEARS"] = [
            0, np.random.randint(1, 2),
            np.random.randint(3, 5)
        ]

        random_init(constr)

        model_params_df, model_params = read_model_params_init(
            "test.soepy.pkl")
        model_spec = read_model_spec_init("test.soepy.yml", model_params_df)

        prob_educ_level = gen_prob_educ_level_vector(model_spec)
        prob_child_age = gen_prob_child_init_age_vector(model_spec)
        prob_partner_present = gen_prob_partner_present_vector(model_spec)
        prob_exp_ft = gen_prob_init_exp_vector(
            model_spec, model_spec.ft_exp_shares_file_name)
        prob_exp_pt = gen_prob_init_exp_vector(
            model_spec, model_spec.pt_exp_shares_file_name)
        prob_child = gen_prob_child_vector(model_spec)
        prob_partner = gen_prob_partner(model_spec)

        (
            states,
            indexer,
            covariates,
            child_age_update_rule,
            child_state_indexes,
        ) = create_state_space_objects(model_spec)

        # Obtain model solution
        non_employment_consumption_resources, emaxs = pyth_solve(
            states,
            covariates,
            child_state_indexes,
            model_params,
            model_spec,
            prob_child,
            prob_partner,
            False,
        )

        # Simulate
        df = pyth_simulate(
            model_params,
            model_spec,
            states,
            indexer,
            emaxs,
            covariates,
            non_employment_consumption_resources,
            child_age_update_rule,
            prob_educ_level,
            prob_child_age,
            prob_partner_present,
            prob_exp_ft,
            prob_exp_pt,
            prob_child,
            prob_partner,
            is_expected=False,
        )

        # Count individuals with each educ level
        counts = []
        for i in [0, 1, 2]:
            counts.append(
                df[df["Education_Level"] == i]["Identifier"].nunique())

        shape = (constr["AGENTS"] * constr["PERIODS"] -
                 counts[1] * constr["EDUC_YEARS"][1] -
                 counts[2] * constr["EDUC_YEARS"][2])

        np.testing.assert_array_equal(df.shape[0], shape)
Esempio n. 7
0
def test_coef_educ_level_specificity():
    """This test ensures that when parameters for a specific
    education group are changed, the simulated data for the remaining education
    groups does not change."""

    constr = dict()
    constr["AGENTS"] = 100000
    constr["PERIODS"] = 10

    random_init(constr)

    model_params_base = pd.read_pickle("test.soepy.pkl")

    # Draw random education level to change
    educ_levels = ["low", "middle", "high"]
    random_educ_level = random.choice([0, 1, 2])
    param_to_change = f"gamma_f_{educ_levels[random_educ_level]}"

    model_params_changed = model_params_base
    model_params_changed.loc[("exp_returns_f", param_to_change), "value"] = (
        model_params_changed.loc[("exp_returns_f", param_to_change), "value"] *
        2)

    data = []

    for num_sim, i in enumerate([model_params_base, model_params_changed]):

        model_params_df, model_params = read_model_params_init(i)
        model_spec = read_model_spec_init("test.soepy.yml", model_params_df)

        prob_educ_level = gen_prob_educ_level_vector(model_spec)
        prob_child_age = gen_prob_child_init_age_vector(model_spec)
        prob_partner_present = gen_prob_partner_present_vector(model_spec)
        prob_exp_ft = gen_prob_init_exp_vector(
            model_spec, model_spec.ft_exp_shares_file_name)
        prob_exp_pt = gen_prob_init_exp_vector(
            model_spec, model_spec.pt_exp_shares_file_name)
        prob_child = gen_prob_child_vector(model_spec)
        prob_partner = gen_prob_partner(model_spec)

        (
            states,
            indexer,
            covariates,
            child_age_update_rule,
            child_state_indexes,
        ) = create_state_space_objects(model_spec)

        # Obtain model solution
        non_employment_consumption_resources, emaxs = pyth_solve(
            states,
            covariates,
            child_state_indexes,
            model_params,
            model_spec,
            prob_child,
            prob_partner,
            False,
        )
        # Simulate
        df = pyth_simulate(
            model_params,
            model_spec,
            states,
            indexer,
            emaxs,
            covariates,
            non_employment_consumption_resources,
            child_age_update_rule,
            prob_educ_level,
            prob_child_age,
            prob_partner_present,
            prob_exp_ft,
            prob_exp_pt,
            prob_child,
            prob_partner,
            is_expected=False,
        )

        data.append(df)

    data_base = data[0]
    data_changed = data[1]

    for level in (0, 1, 2):
        if level == random_educ_level:
            continue
        data_base_educ_level = data_base[data_base["Education_Level"] == level]
        data_changed_educ_level = data_changed[data_changed["Education_Level"]
                                               == level]
        true_list = [
            col for col in data_changed_educ_level.columns.values
            if not pd.Series.equals(data_base_educ_level[col],
                                    data_changed_educ_level[col])
        ]

        pd.testing.assert_frame_equal(data_base_educ_level,
                                      data_changed_educ_level)
Esempio n. 8
0
def test_shares_according_to_initial_conditions():
    """This test ensures that the shares of individuals with particular characteristics
    in the simulated data frame as determined by initial conditions correspond to the probabilities
    specified in the init file.
    """

    constr = dict()
    constr["AGENTS"] = 500000
    constr["EDUC_YEARS"] = [0, 0, 0]
    constr["PERIODS"] = 2
    constr["CHILD_AGE_INIT_MAX"] = 1
    constr["INIT_EXP_MAX"] = 2

    random_init(constr)

    model_params_df, model_params = read_model_params_init("test.soepy.pkl")
    model_spec = read_model_spec_init("test.soepy.yml", model_params_df)

    prob_educ_level = gen_prob_educ_level_vector(model_spec)
    prob_child_age = gen_prob_child_init_age_vector(model_spec)
    prob_partner_present = gen_prob_partner_present_vector(model_spec)
    prob_exp_ft = gen_prob_init_exp_vector(model_spec,
                                           model_spec.ft_exp_shares_file_name)
    prob_exp_pt = gen_prob_init_exp_vector(model_spec,
                                           model_spec.pt_exp_shares_file_name)
    prob_child = gen_prob_child_vector(model_spec)
    prob_partner = gen_prob_partner(model_spec)

    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    # Obtain model solution
    non_employment_consumption_resources, emaxs = pyth_solve(
        states,
        covariates,
        child_state_indexes,
        model_params,
        model_spec,
        prob_child,
        prob_partner,
        False,
    )

    # Simulate
    df = pyth_simulate(
        model_params,
        model_spec,
        states,
        indexer,
        emaxs,
        covariates,
        non_employment_consumption_resources,
        child_age_update_rule,
        prob_educ_level,
        prob_child_age,
        prob_partner_present,
        prob_exp_ft,
        prob_exp_pt,
        prob_child,
        prob_partner,
        is_expected=False,
    )

    # Education level shares
    simulated = (
        df.groupby(["Education_Level"])["Identifier"].nunique().to_numpy() /
        constr["AGENTS"])
    np.testing.assert_almost_equal(simulated,
                                   prob_educ_level,
                                   decimal=2,
                                   err_msg="Education level shares mismatch")

    # Partner status in initial period
    simulated = (df[df["Period"] == 0].groupby(
        ["Education_Level"])["Partner_Indicator"].mean().to_numpy())
    np.testing.assert_almost_equal(simulated,
                                   prob_partner_present,
                                   decimal=2,
                                   err_msg="Partner shares mismatch")

    # Child ages in initial period
    simulated = (df[df["Period"] == 0].groupby(
        ["Education_Level"])["Age_Youngest_Child"].value_counts(
            normalize=True).sort_index(ascending=True).to_numpy())
    prob_child_age_flat = [
        item for sublist in prob_child_age for item in sublist
    ]
    np.testing.assert_almost_equal(simulated,
                                   prob_child_age_flat,
                                   decimal=2,
                                   err_msg="Child age shares mismatch")

    # Experience in initial period
    # Part-time
    simulated = (df[df["Period"] == 0].groupby(
        ["Education_Level"])["Experience_Part_Time"].value_counts(
            normalize=True).sort_index(ascending=True).to_numpy())
    prob_exp_pt_flat = [item for sublist in prob_exp_pt for item in sublist]
    np.testing.assert_almost_equal(
        simulated,
        prob_exp_pt_flat,
        decimal=2,
        err_msg="Part-time experience shares mismatch",
    )

    # Full-time
    simulated = (df[df["Period"] == 0].groupby(
        ["Education_Level"])["Experience_Full_Time"].value_counts(
            normalize=True).sort_index(ascending=True).to_numpy())
    prob_exp_ft_flat = [item for sublist in prob_exp_ft for item in sublist]
    np.testing.assert_almost_equal(
        simulated,
        prob_exp_ft_flat,
        decimal=2,
        err_msg="Full-time experience shares mismatch",
    )
Esempio n. 9
0
def test_no_children_no_exp():
    """This test ensures that
    i) child age equals -1 in the entire simulates sample,
    equivalent to no kid is ever born, if the probability to get a child is zero
    for all periods
    ii) initial experience is zero if so specified in constraint"""

    expected = 0

    is_expected = False

    constr = {
        "AGENTS": 200,
        "PERIODS": 10,
        "CHILD_AGE_INIT_MAX": -1,
        "INIT_EXP_MAX": 0
    }
    random_init(constr)

    model_params_df, model_params = read_model_params_init("test.soepy.pkl")
    model_spec = read_model_spec_init("test.soepy.yml", model_params_df)

    # Set probability of having children to zero for all periods
    prob_child = np.full((model_spec.num_periods, 3), 0.00)

    prob_educ_level = gen_prob_educ_level_vector(model_spec)
    prob_child_age = gen_prob_child_init_age_vector(model_spec)
    prob_partner_present = gen_prob_partner_present_vector(model_spec)
    prob_exp_ft = gen_prob_init_exp_vector(model_spec,
                                           model_spec.ft_exp_shares_file_name)
    prob_exp_pt = gen_prob_init_exp_vector(model_spec,
                                           model_spec.pt_exp_shares_file_name)
    prob_partner = gen_prob_partner(model_spec)

    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    # Obtain model solution
    non_employment_consumption_resources, emaxs = pyth_solve(
        states,
        covariates,
        child_state_indexes,
        model_params,
        model_spec,
        prob_child,
        prob_partner,
        False,
    )

    # Simulate
    df = pyth_simulate(
        model_params,
        model_spec,
        states,
        indexer,
        emaxs,
        covariates,
        non_employment_consumption_resources,
        child_age_update_rule,
        prob_educ_level,
        prob_child_age,
        prob_partner_present,
        prob_exp_ft,
        prob_exp_pt,
        prob_child,
        prob_partner,
        is_expected=False,
    )

    np.testing.assert_equal(sum(df.dropna()["Age_Youngest_Child"] != -1),
                            expected)
    np.testing.assert_equal(
        sum(df[df["Period"] == 0].dropna()["Experience_Part_Time"] != 0),
        expected)
    np.testing.assert_equal(
        sum(df[df["Period"] == 0].dropna()["Experience_Full_Time"] != 0),
        expected)
def test_pyth_simulate(input_vault, test_id, is_expected):
    """This test runs a random selection of test regression tests from
    our regression test battery.
    """

    (
        model_spec_init_dict,
        random_model_params_df,
        exog_educ_shares,
        exog_child_age_shares,
        exog_partner_shares,
        exog_exper_shares_pt,
        exog_exper_shares_ft,
        exog_child_info,
        exog_partner_arrival_info,
        exog_partner_separation_info,
        expected_df_sim_func,
        expected_df_sim_sol,
    ) = input_vault[test_id]

    model_params_df, model_params = read_model_params_init(random_model_params_df)
    model_spec = read_model_spec_init(model_spec_init_dict, model_params_df)

    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    # Calculate utility components
    log_wage_systematic, non_consumption_utilities = calculate_utility_components(
        model_params, model_spec, states, covariates, is_expected
    )

    for edu_ind, edu_type in enumerate(["low", "middle", "high"]):
        # Now test the full and part time level, 1 and 2 for the wages
        relevant_states_ind = (
            (states[:, 3] == 1) & (states[:, 4] == 2) & (states[:, 1] == edu_ind)
        )

        gamma_0 = random_model_params_df.loc[
            ("const_wage_eq", f"gamma_0_{edu_type}"), "value"
        ]
        gamma_f = random_model_params_df.loc[
            ("exp_returns_f", f"gamma_f_{edu_type}"), "value"
        ]
        if is_expected:
            gamma_p = (
                random_model_params_df.loc[
                    ("exp_returns_p_bias", f"gamma_p_bias_{edu_type}"), "value"
                ]
                * gamma_f
            )
        else:
            gamma_p = random_model_params_df.loc[
                ("exp_returns_p", f"gamma_p_{edu_type}"), "value"
            ]

        wage_calc = gamma_0 + gamma_f * np.log(3) + gamma_p * np.log(2)

        np.testing.assert_array_equal(
            log_wage_systematic[relevant_states_ind],
            np.full(log_wage_systematic[relevant_states_ind].shape, wage_calc),
        )
Esempio n. 11
0
def update_solve_objectes():
    vault_file = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl"

    vault = {}
    with open(vault_file, "rb") as file:
        tests_sim_func = pickle.load(file)

    for i in range(0, 100):
        print(i)

        (
            model_spec_init_dict,
            random_model_params_df,
            exog_educ_shares,
            exog_child_age_shares,
            exog_partner_shares,
            exog_exper_shares_pt,
            exog_exper_shares_ft,
            exog_child_info,
            exog_partner_arrival_info,
            exog_partner_separation_info,
            expected_df_sim_func,
            expected_df_sim_sol,
        ) = tests_sim_func[i]

        exog_educ_shares.to_pickle("test.soepy.educ.shares.pkl")
        exog_child_age_shares.to_pickle("test.soepy.child.age.shares.pkl")
        exog_child_info.to_pickle("test.soepy.child.pkl")
        exog_partner_shares.to_pickle("test.soepy.partner.shares.pkl")
        exog_exper_shares_pt.to_pickle("test.soepy.pt.exp.shares.pkl")
        exog_exper_shares_ft.to_pickle("test.soepy.ft.exp.shares.pkl")
        exog_partner_arrival_info.to_pickle("test.soepy.partner.arrival.pkl")
        exog_partner_separation_info.to_pickle("test.soepy.partner.separation.pkl")

        model_params_df, model_params = read_model_params_init(random_model_params_df)
        model_spec = read_model_spec_init(model_spec_init_dict, model_params_df)

        prob_educ_level = gen_prob_educ_level_vector(model_spec)
        prob_child_age = gen_prob_child_init_age_vector(model_spec)
        prob_partner_present = gen_prob_partner_present_vector(model_spec)
        prob_exp_ft = gen_prob_init_exp_vector(
            model_spec, model_spec.ft_exp_shares_file_name
        )
        prob_exp_pt = gen_prob_init_exp_vector(
            model_spec, model_spec.pt_exp_shares_file_name
        )
        prob_child = gen_prob_child_vector(model_spec)
        prob_partner = gen_prob_partner(model_spec)

        # Create state space
        (
            states,
            indexer,
            covariates,
            child_age_update_rule,
            child_state_indexes,
        ) = create_state_space_objects(model_spec)

        # Obtain model solution
        non_employment_consumption_resources, emaxs = pyth_solve(
            states,
            covariates,
            child_state_indexes,
            model_params,
            model_spec,
            prob_child,
            prob_partner,
            False,
        )
        # Simulate
        calculated_df_sim_sol = pyth_simulate(
            model_params,
            model_spec,
            states,
            indexer,
            emaxs,
            covariates,
            non_employment_consumption_resources,
            child_age_update_rule,
            prob_educ_level,
            prob_child_age,
            prob_partner_present,
            prob_exp_ft,
            prob_exp_pt,
            prob_child,
            prob_partner,
            is_expected=False,
        )

        vault[i] = (
            model_spec_init_dict,
            random_model_params_df,
            exog_educ_shares,
            exog_child_age_shares,
            exog_partner_shares,
            exog_exper_shares_pt,
            exog_exper_shares_ft,
            exog_child_info,
            exog_partner_arrival_info,
            exog_partner_separation_info,
            expected_df_sim_func,
            calculated_df_sim_sol.sum(axis=0),
        )

    with open(vault_file, "wb") as file:
        pickle.dump(vault, file)

    cleanup(options="regression")
def input_data():
    out = {}

    vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl"

    with open(vault, "rb") as file:
        tests = pickle.load(file)
    (
        model_spec_init_dict,
        random_model_params_df,
        exog_educ_shares,
        exog_child_age_shares,
        exog_partner_shares,
        exog_exper_shares_pt,
        exog_exper_shares_ft,
        exog_child_info,
        exog_partner_arrival_info,
        exog_partner_separation_info,
        expected_df_sim_sol,
        expected_df_sim,
    ) = tests[0]

    exog_educ_shares.to_pickle("test.soepy.educ.shares.pkl")
    exog_child_age_shares.to_pickle("test.soepy.child.age.shares.pkl")
    exog_child_info.to_pickle("test.soepy.child.pkl")
    exog_partner_shares.to_pickle("test.soepy.partner.shares.pkl")
    exog_exper_shares_pt.to_pickle("test.soepy.pt.exp.shares.pkl")
    exog_exper_shares_ft.to_pickle("test.soepy.ft.exp.shares.pkl")
    exog_partner_arrival_info.to_pickle("test.soepy.partner.arrival.pkl")
    exog_partner_separation_info.to_pickle("test.soepy.partner.separation.pkl")

    model_params_df, model_params = read_model_params_init(
        random_model_params_df)

    for name, tax in [("splitted", True), ("individual", False)]:
        # Standard tax_spltting is true
        if tax:
            pass
        else:
            model_spec_init_dict["TAXES_TRANSFERS"]["tax_splitting"] = tax

        model_spec = read_model_spec_init(model_spec_init_dict,
                                          model_params_df)

        prob_educ_level = gen_prob_educ_level_vector(model_spec)
        prob_child_age = gen_prob_child_init_age_vector(model_spec)
        prob_partner_present = gen_prob_partner_present_vector(model_spec)
        prob_exp_ft = gen_prob_init_exp_vector(
            model_spec, model_spec.ft_exp_shares_file_name)
        prob_exp_pt = gen_prob_init_exp_vector(
            model_spec, model_spec.pt_exp_shares_file_name)
        prob_child = gen_prob_child_vector(model_spec)
        prob_partner = gen_prob_partner(model_spec)
        prob_partner[:, :, 0, 1] = 0
        prob_partner[:, :, 0, 0] = 1
        prob_partner_present[:] = 0

        (
            states,
            indexer,
            covariates,
            child_age_update_rule,
            child_state_indexes,
        ) = create_state_space_objects(model_spec)

        # Obtain model solution
        non_employment_consumption_resources, emaxs = pyth_solve(
            states,
            covariates,
            child_state_indexes,
            model_params,
            model_spec,
            prob_child,
            prob_partner,
            False,
        )

        # Simulate
        calculated_df = pyth_simulate(
            model_params,
            model_spec,
            states,
            indexer,
            emaxs,
            covariates,
            non_employment_consumption_resources,
            child_age_update_rule,
            prob_educ_level,
            prob_child_age,
            prob_partner_present,
            prob_exp_ft,
            prob_exp_pt,
            prob_child,
            prob_partner,
            is_expected=False,
        )

        out[name] = create_disc_sum_av_utility(
            calculated_df, model_spec_init_dict["CONSTANTS"]["delta"])

        # Check if really all are single at any time
        assert (calculated_df["Male_Wages"] == 0).all()

    out["regression_disc_sum"] = -0.10754078488166594
    return out
Esempio n. 13
0
def simulate(model_params_init_file_name,
             model_spec_init_file_name,
             is_expected=True):
    """Create a data frame of individuals' simulated experiences."""

    # Read in model specification from yaml file
    model_params_df, model_params = read_model_params_init(
        model_params_init_file_name)

    model_spec = read_model_spec_init(model_spec_init_file_name,
                                      model_params_df)

    # Get information concerning exogenous processes
    prob_educ_level = gen_prob_educ_level_vector(model_spec)
    prob_child_age = gen_prob_child_init_age_vector(model_spec)
    prob_partner_present = gen_prob_partner_present_vector(model_spec)
    prob_exp_ft = gen_prob_init_exp_vector(model_spec,
                                           model_spec.ft_exp_shares_file_name)
    prob_exp_pt = gen_prob_init_exp_vector(model_spec,
                                           model_spec.pt_exp_shares_file_name)
    prob_child = gen_prob_child_vector(model_spec)
    prob_partner = gen_prob_partner(model_spec)

    # Create state space
    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    # Obtain model solution
    non_employment_consumption_resources, emaxs = pyth_solve(
        states,
        covariates,
        child_state_indexes,
        model_params,
        model_spec,
        prob_child,
        prob_partner,
        is_expected,
    )

    # Simulate agents experiences according to parameters in the model specification
    df = pyth_simulate(
        model_params,
        model_spec,
        states,
        indexer,
        emaxs,
        covariates,
        non_employment_consumption_resources,
        child_age_update_rule,
        prob_educ_level,
        prob_child_age,
        prob_partner_present,
        prob_exp_ft,
        prob_exp_pt,
        prob_child,
        prob_partner,
        is_expected=False,
    )

    return df
Esempio n. 14
0
def input_data():

    vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl"

    with open(vault, "rb") as file:
        tests = pickle.load(file)

    (
        model_spec_init_dict,
        random_model_params_df,
        exog_educ_shares,
        exog_child_age_shares,
        exog_partner_shares,
        exog_exper_shares_pt,
        exog_exper_shares_ft,
        exog_child_info,
        exog_partner_arrival_info,
        exog_partner_separation_info,
        expected_df_sim_func,
        expected_df_sim_sol,
    ) = tests[0]

    exog_educ_shares.to_pickle("test.soepy.educ.shares.pkl")
    exog_child_age_shares.to_pickle("test.soepy.child.age.shares.pkl")
    exog_child_info.to_pickle("test.soepy.child.pkl")
    exog_partner_shares.to_pickle("test.soepy.partner.shares.pkl")
    exog_exper_shares_pt.to_pickle("test.soepy.pt.exp.shares.pkl")
    exog_exper_shares_ft.to_pickle("test.soepy.ft.exp.shares.pkl")
    exog_partner_arrival_info.to_pickle("test.soepy.partner.arrival.pkl")
    exog_partner_separation_info.to_pickle("test.soepy.partner.separation.pkl")

    model_params_df, model_params = read_model_params_init(
        random_model_params_df)
    model_spec = read_model_spec_init(model_spec_init_dict, model_params_df)

    prob_child = gen_prob_child_vector(model_spec)
    prob_partner = gen_prob_partner(model_spec)

    states, indexer = pyth_create_state_space(model_spec)

    # Create objects that depend only on the state space
    covariates = construct_covariates(states, model_spec)

    child_age_update_rule = define_child_age_update_rule(model_spec, states)

    child_state_indexes = create_child_indexes(states, indexer, model_spec,
                                               child_age_update_rule)

    attrs_spec = ["seed_emax", "num_periods", "num_draws_emax"]
    draws_emax = draw_disturbances(
        *[getattr(model_spec, attr) for attr in attrs_spec], model_params)

    log_wage_systematic, non_consumption_utilities = calculate_utility_components(
        model_params, model_spec, states, covariates, True)

    non_employment_benefits = calculate_non_employment_benefits(
        model_spec, states, log_wage_systematic)

    deductions_spec = np.array(model_spec.ssc_deductions)
    tax_splitting = model_spec.tax_splitting

    non_employment_consumption_resources = (
        calculate_non_employment_consumption_resources(
            deductions_spec,
            model_spec.tax_params,
            covariates[:, 1],
            non_employment_benefits,
            tax_splitting,
        ))

    # Solve the model in a backward induction procedure
    # Error term for continuation values is integrated out
    # numerically in a Monte Carlo procedure
    emaxs = pyth_backward_induction(
        model_spec,
        states,
        child_state_indexes,
        log_wage_systematic,
        non_consumption_utilities,
        draws_emax,
        covariates,
        prob_child,
        prob_partner,
        non_employment_consumption_resources,
        deductions_spec,
    )

    return states, emaxs, child_state_indexes, prob_child, prob_partner
Esempio n. 15
0
def input_data():
    vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl"

    with open(vault, "rb") as file:
        tests = pickle.load(file)

    (
        model_spec_init_dict,
        random_model_params_df,
        exog_educ_shares,
        exog_child_age_shares,
        exog_partner_shares,
        exog_exper_shares_pt,
        exog_exper_shares_ft,
        exog_child_info,
        exog_partner_arrival_info,
        exog_partner_separation_info,
        expected_df_sim_func,
        expected_df_sim_sol,
    ) = tests[0]

    exog_educ_shares.to_pickle("test.soepy.educ.shares.pkl")
    exog_child_age_shares.to_pickle("test.soepy.child.age.shares.pkl")
    exog_child_info.to_pickle("test.soepy.child.pkl")
    exog_partner_shares.to_pickle("test.soepy.partner.shares.pkl")
    exog_exper_shares_pt.to_pickle("test.soepy.pt.exp.shares.pkl")
    exog_exper_shares_ft.to_pickle("test.soepy.ft.exp.shares.pkl")
    exog_partner_arrival_info.to_pickle("test.soepy.partner.arrival.pkl")
    exog_partner_separation_info.to_pickle("test.soepy.partner.separation.pkl")

    model_params_df, model_params = read_model_params_init(
        random_model_params_df)
    model_spec = read_model_spec_init(model_spec_init_dict, model_params_df)

    prob_child = gen_prob_child_vector(model_spec)
    prob_partner = gen_prob_partner(model_spec)

    (
        states,
        indexer,
        covariates,
        child_age_update_rule,
        child_state_indexes,
    ) = create_state_space_objects(model_spec)

    # Obtain model solution
    non_employment_consumption_resources, emaxs = pyth_solve(
        states,
        covariates,
        child_state_indexes,
        model_params,
        model_spec,
        prob_child,
        prob_partner,
        False,
    )
    return (
        covariates,
        states,
        non_employment_consumption_resources,
        model_spec,
        child_age_update_rule,
    )
Esempio n. 16
0
def input_data():
    out = {}

    vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl"

    with open(vault, "rb") as file:
        tests = pickle.load(file)
    (
        model_spec_init_dict,
        random_model_params_df,
        exog_educ_shares,
        exog_child_age_shares,
        exog_partner_shares,
        exog_exper_shares_pt,
        exog_exper_shares_ft,
        exog_child_info,
        exog_partner_arrival_info,
        exog_partner_separation_info,
        expected_df_sim_sol,
        expected_df_sim,
    ) = tests[6]

    exog_educ_shares.to_pickle("test.soepy.educ.shares.pkl")
    exog_child_age_shares.to_pickle("test.soepy.child.age.shares.pkl")
    exog_child_info.to_pickle("test.soepy.child.pkl")
    exog_partner_shares.to_pickle("test.soepy.partner.shares.pkl")
    exog_exper_shares_pt.to_pickle("test.soepy.pt.exp.shares.pkl")
    exog_exper_shares_ft.to_pickle("test.soepy.ft.exp.shares.pkl")
    exog_partner_arrival_info.to_pickle("test.soepy.partner.arrival.pkl")
    exog_partner_separation_info.to_pickle("test.soepy.partner.separation.pkl")

    model_params_df, model_params = read_model_params_init(random_model_params_df)
    ccc_under_3 = np.array(
        model_spec_init_dict["TAXES_TRANSFERS"]["child_care_costs"]["under_3"]
    )
    ccc_3_to_6 = np.array(
        model_spec_init_dict["TAXES_TRANSFERS"]["child_care_costs"]["3_to_6"]
    )

    for name, factor in [("original", 1), ("multiplied", 5)]:

        model_spec_init_dict["TAXES_TRANSFERS"]["child_care_costs"]["under_3"] = (
            ccc_under_3 * factor
        )
        model_spec_init_dict["TAXES_TRANSFERS"]["child_care_costs"]["3_to_6"] = (
            ccc_3_to_6 * factor
        )

        params_ind_list = [
            ("disutil_work", "yes_kids_f_educ_middle"),
            ("disutil_work", "yes_kids_p_educ_middle"),
            ("disutil_work", "yes_kids_p_educ_low"),
            ("disutil_work", "yes_kids_p_educ_high"),
            ("disutil_work", "child_0_2_f"),
            ("disutil_work", "child_3_5_f"),
        ]

        for param_ind in params_ind_list:
            model_params_df.loc[param_ind, "value"] *= factor

        model_spec = read_model_spec_init(model_spec_init_dict, model_params_df)

        prob_educ_level = gen_prob_educ_level_vector(model_spec)
        prob_child_age = []
        prob_child_age_old = gen_prob_child_init_age_vector(model_spec)
        for educ_level_list in prob_child_age_old:
            educ_level_array = np.array(educ_level_list)
            educ_level_array[0] = 1
            educ_level_array[1:] = 0
            prob_child_age += [educ_level_array]
        prob_partner_present = gen_prob_partner_present_vector(model_spec)
        prob_exp_ft = gen_prob_init_exp_vector(
            model_spec, model_spec.ft_exp_shares_file_name
        )
        prob_exp_pt = gen_prob_init_exp_vector(
            model_spec, model_spec.pt_exp_shares_file_name
        )
        prob_child = gen_prob_child_vector(model_spec)
        prob_child[:, :] = 0
        prob_partner = gen_prob_partner(model_spec)
        prob_partner[:, 0, 1] = 0
        prob_partner[:, 0, 0] = 1
        prob_partner_present[:] = 0

        # Create state space
        (
            states,
            indexer,
            covariates,
            child_age_update_rule,
            child_state_indexes,
        ) = create_state_space_objects(model_spec)

        # Obtain model solution
        non_employment_consumption_resources, emaxs = pyth_solve(
            states,
            covariates,
            child_state_indexes,
            model_params,
            model_spec,
            prob_child,
            prob_partner,
            False,
        )

        # Simulate
        calculated_df = pyth_simulate(
            model_params,
            model_spec,
            states,
            indexer,
            emaxs,
            covariates,
            non_employment_consumption_resources,
            child_age_update_rule,
            prob_educ_level,
            prob_child_age,
            prob_partner_present,
            prob_exp_ft,
            prob_exp_pt,
            prob_child,
            prob_partner,
            is_expected=False,
        )

        out[name] = calculated_df
    return out