def test_simulation_func(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") calculated_df = simulate(random_model_params_df, model_spec_init_dict) pd.testing.assert_series_equal( expected_df_sim_func, calculated_df.sum(axis=0), ) cleanup()
def update_sim_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") calculated_df_sim = simulate(random_model_params_df, model_spec_init_dict) 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, calculated_df_sim.sum(axis=0), expected_df_sim_sol, ) with open(vault_file, "wb") as file: pickle.dump(vault, file) cleanup(options="regression")
def test_simulation_func_exp(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") # calculated_df_false = simulate(random_model_params_df, model_spec_init_dict, is_expected=False) for edu_type in ["low", "middle", "high"]: random_model_params_df.loc[( "exp_returns_p_bias", f"gamma_p_bias_{edu_type}"), "value"] = ( random_model_params_df.loc[("exp_returns_p", f"gamma_p_{edu_type}"), "value"] / random_model_params_df.loc[("exp_returns_f", f"gamma_f_{edu_type}"), "value"]) calculated_df_true = simulate(random_model_params_df, model_spec_init_dict, is_expected=True) pd.testing.assert_series_equal( calculated_df_false.sum(axis=0), calculated_df_true.sum(axis=0), ) cleanup()
def create_vault(num_test=1000, seed=123456): """This function creates our regression vault.""" np.random.seed(seed) seeds = np.random.randint(0, 1000, size=num_test) vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl" tests = [] for counter, seed in enumerate(seeds): np.random.seed(seed) ( 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, ) = random_init() df = simulate("test.soepy.pkl", "test.soepy.yml") tests += [ ( 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, df, ) ] cleanup("regression") with open(vault, "wb") as file: pickle.dump(tests, file)
def check_vault(num_test): """This function runs another simulation for each init file in our regression vault. """ vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl" with open(vault, "rb") as file: tests = pickle.load(file) for test in tests[:num_test]: init_dict, expected_df = test calculated_df = simulate(init_dict) for col in expected_df.columns.tolist(): expected_df[col].equals(calculated_df[col]) cleanup("regression")
def create_vault(num_test=1000, seed=123456): """This function creates our regression vault.""" np.random.seed(seed) seeds = np.random.randint(0, 1000, size=num_test) vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl" tests = [] for counter, seed in enumerate(seeds): np.random.seed(seed) init_dict = random_init() df = simulate("test.soepy.yml") tests += [(init_dict, df)] cleanup("regression") with open(vault, "wb") as file: pickle.dump(tests, file)
def check_vault(num_test): """This function runs another simulation for each init file in our regression vault.""" vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl" with open(vault, "rb") as file: tests = pickle.load(file) for test in tests[:num_test]: ( 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, ) = test 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") calculated_df = simulate(random_model_params_df, model_spec_init_dict) for col in expected_df.columns.tolist(): expected_df[col].equals(calculated_df[col]) cleanup("regression")
import soepy from development.tests.auxiliary.auxiliary import cleanup # Generate simulated dataset data_frame = soepy.simulate("toy_model_init_file_03_3types.pkl", "model_spec_init.yml") data_frame = soepy.simulate("toy_model_init_file_03_1type.pkl", "model_spec_init.yml") cleanup()
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()
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")