def test1(): """This test ensures that the columns of the output data frame correspond to the function output values. """ for _ in range(100): constr = {"EDUC_MAX": 10, "AGENTS": 1, "PERIODS": 1} random_init(constr) model_params = read_init_file("test.soepy.yml") df = simulate("test.soepy.yml") educ_level = np.array([1.0, 0.0, 0.0]) exp_p, exp_f = 0.0, 0.0 wage_systematic = calculate_wage_systematic( model_params, educ_level, exp_p, exp_f ) np.testing.assert_array_equal(wage_systematic, df["Systematic Wage"]) draw_sim = draw_disturbances( model_params.seed_sim, model_params.shocks_cov, 1, 1 ) period_wages = calculate_period_wages( model_params, wage_systematic, draw_sim[0, 0, :] ) np.testing.assert_array_equal( period_wages, np.squeeze( df[["Period Wage N", "Period Wage P", "Period Wage F"]].values.T ), ) consumption_utilities = calculate_consumption_utilities( model_params, period_wages ) np.testing.assert_array_equal( consumption_utilities, np.squeeze( df[ [ "Consumption Utility N", "Consumption Utility P", "Consumption Utility F", ] ].values.T ), ) total_utilities = calculate_total_utilities(model_params, consumption_utilities) np.testing.assert_array_equal( total_utilities, np.squeeze( df[["Flow Utility N", "Flow Utility P", "Flow Utility F"]].values ), )
def test4(): """This test ensures that the shape of the simulated data frame corresponds to the 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(1, 6) constr["EDUC_MAX"] = np.random.randint(10, min(10 + constr["PERIODS"], 12)) random_init(constr) df = simulate("test.soepy.yml") np.testing.assert_array_equal(df.shape[0], constr["AGENTS"] * constr["PERIODS"])
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 check_vault(): """This function runs another simulation for each init file in our regression vault. """ file_dir = os.path.join(TEST_RESOURCES_DIR, "regression_vault.soepy.json") tests = json.load(open(file_dir, "r")) for test in tests: stat, init_dict = test print_dict(init_dict) df = simulate("test.soepy.yml") stat_new = np.sum(df.sum()) np.testing.assert_array_almost_equal(stat, stat_new) cleanup("regression")
def create_vault(num_test=100, seed=123456): """This function creates our regression vault.""" np.random.seed(seed) seeds = np.random.randint(0, 1000, size=num_test) file_dir = os.path.join(TEST_RESOURCES_DIR, "regression_vault.soepy.json") tests = [] for counter, seed in enumerate(seeds): np.random.seed(seed) init_dict = random_init() df = simulate("test.soepy.yml") stat = np.sum(df.sum()) tests += [(stat, init_dict)] cleanup("regression") json.dump(tests, open(file_dir, "w"))
def test1(): """This test runs a random selection of five regression tests from our regression test battery. """ fname = TEST_RESOURCES_DIR / "regression_vault.soepy.json" tests = json.load(open(fname)) random_choice = np.random.choice(range(len(tests)), 5) tests = [tests[i] for i in random_choice] for test in tests: stat, init_dict = test print_dict(init_dict) df = simulate("test.soepy.yml") stat_new = np.sum(df.sum()) np.testing.assert_array_equal(stat_new, stat)
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 test1(idx): """This test runs a random selection of five regression tests from our regression test battery. """ vault = TEST_RESOURCES_DIR / "regression_vault.soepy.pkl" with open(vault, "rb") as file: tests = pickle.load(file) test = tests[idx] init_dict, expected_df = test calculated_df = simulate(init_dict) for col in expected_df.columns.tolist(): print(col) np.testing.assert_array_almost_equal( expected_df[col][expected_df[col].notna()], calculated_df[col][calculated_df[col].notna()], )
def test2(): """This test ensures that the data frame contain only nan values if individuals are still a in education. """ constr = {"AGENTS": 200} random_init(constr) df = simulate("test.soepy.yml") for year in [11, 12]: df2 = df[(df["Years of Education"] == year) & (df["Period"] < year - 10)] df2 = df2[ [ col for col in df2.columns.values if col not in ["Identifier", "Period", "Years of Education"] ] ] a = np.empty(df2.shape) a[:] = np.nan np.testing.assert_array_equal(df2.values, a)
from soepy.python.simulate.simulate_python import simulate from soepy.test.auxiliary import cleanup # Generate simulated dataset data_frame = simulate("toy_model_init_file_1000.yml") # Save data frame to csv file data_frame.to_pickle("test.soepy.pkl") cleanup()
def func(maxrt): stop = datetime.datetime.now() + maxrt while datetime.datetime.now() < stop: random_init() simulate("test.soepy.yml")
def test_1(): """This test makes sure the full package works for random initialization files.""" random_init() simulate("test.soepy.yml")