Example #1
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
Example #2
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()
Example #3
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)
Example #4
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)
Example #5
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",
    )
Example #6
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),
        )
Example #8
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
Example #10
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
Example #11
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,
    )
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