def test_5(self): """ This test ensures that the logging looks exactly the same for the different versions. """ max_draws = np.random.randint(10, 300) # Generate random initialization file constr = dict() constr['flag_parallelism'] = False constr['max_draws'] = max_draws constr['flag_interpolation'] = False constr['maxfun'] = 0 # Generate random initialization file init_dict = generate_init(constr) # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # Iterate over alternative implementations base_sol_log, base_est_info_log, base_est_log = None, None, None base_sim_log = None num_periods = init_dict['BASICS']['periods'] write_draws(num_periods, max_draws) for version in ['FORTRAN', 'PYTHON']: respy_obj.unlock() respy_obj.set_attr('version', version) respy_obj.lock() simulate(respy_obj) estimate(respy_obj) # Check for identical logging if base_sol_log is None: base_sol_log = open('sol.respy.log', 'r').read() assert open('sol.respy.log', 'r').read() == base_sol_log # Check for identical logging if base_sim_log is None: base_sim_log = open('sim.respy.log', 'r').read() assert open('sim.respy.log', 'r').read() == base_sim_log if base_est_info_log is None: base_est_info_log = open('est.respy.info', 'r').read() assert open('est.respy.info', 'r').read() == base_est_info_log if base_est_log is None: base_est_log = open('est.respy.log', 'r').readlines() compare_est_log(base_est_log)
def test_6(self): """ This test ensures that the logging looks exactly the same for the different versions. """ max_draws = np.random.randint(10, 300) # Generate random initialization file constr = dict() constr['flag_parallelism'] = False constr['max_draws'] = max_draws constr['flag_interpolation'] = False constr['maxfun'] = 0 # Generate random initialization file init_dict = generate_init(constr) # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # Iterate over alternative implementations base_sol_log, base_est_info_log, base_est_log = None, None, None base_sim_log = None num_periods = init_dict['BASICS']['periods'] write_draws(num_periods, max_draws) for version in ['FORTRAN', 'PYTHON']: respy_obj.unlock() respy_obj.set_attr('version', version) respy_obj.lock() simulate(respy_obj) estimate(respy_obj) # Check for identical logging if base_sol_log is None: base_sol_log = open('sol.respy.log', 'r').read() assert open('sol.respy.log', 'r').read() == base_sol_log # Check for identical logging if base_sim_log is None: base_sim_log = open('sim.respy.log', 'r').read() assert open('sim.respy.log', 'r').read() == base_sim_log if base_est_info_log is None: base_est_info_log = open('est.respy.info', 'r').read() assert open('est.respy.info', 'r').read() == base_est_info_log if base_est_log is None: base_est_log = open('est.respy.log', 'r').readlines() compare_est_log(base_est_log)
def test_2(self): """ This test ensures that the evaluation of the criterion function at the starting value is identical between the different versions. """ max_draws = np.random.randint(10, 100) # Generate random initialization file constr = dict() constr['flag_parallelism'] = False constr['max_draws'] = max_draws constr['flag_interpolation'] = False constr['maxfun'] = 0 # Generate random initialization file init_dict = generate_init(constr) # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # Simulate a dataset simulate(respy_obj) # Iterate over alternative implementations base_x, base_val = None, None num_periods = init_dict['BASICS']['periods'] write_draws(num_periods, max_draws) for version in ['FORTRAN', 'PYTHON']: respy_obj.unlock() respy_obj.set_attr('version', version) respy_obj.lock() x, val = estimate(respy_obj) # Check for the returned parameters. if base_x is None: base_x = x np.testing.assert_allclose(base_x, x) # Check for the value of the criterion function. if base_val is None: base_val = val np.testing.assert_allclose(base_val, val)
def test_1(self): """ Testing the equality of an evaluation of the criterion function for a random request. """ # Run evaluation for multiple random requests. is_deterministic = np.random.choice([True, False], p=[0.10, 0.9]) is_interpolated = np.random.choice([True, False], p=[0.10, 0.9]) is_myopic = np.random.choice([True, False], p=[0.10, 0.9]) max_draws = np.random.randint(10, 100) # Generate random initialization file constr = dict() constr['is_deterministic'] = is_deterministic constr['flag_parallelism'] = False constr['is_myopic'] = is_myopic constr['max_draws'] = max_draws constr['maxfun'] = 0 init_dict = generate_random_dict(constr) # The use of the interpolation routines is a another special case. # Constructing a request that actually involves the use of the # interpolation routine is a little involved as the number of # interpolation points needs to be lower than the actual number of # states. And to know the number of states each period, I need to # construct the whole state space. if is_interpolated: # Extract from future initialization file the information # required to construct the state space. The number of periods # needs to be at least three in order to provide enough state # points. num_periods = np.random.randint(3, 6) edu_start = init_dict['EDUCATION']['start'] edu_max = init_dict['EDUCATION']['max'] min_idx = min(num_periods, (edu_max - edu_start + 1)) max_states_period = pyth_create_state_space( num_periods, edu_start, edu_max, min_idx)[3] # Updates to initialization dictionary that trigger a use of the # interpolation code. init_dict['BASICS']['periods'] = num_periods init_dict['INTERPOLATION']['flag'] = True init_dict['INTERPOLATION']['points'] = \ np.random.randint(10, max_states_period) # Print out the relevant initialization file. print_init_dict(init_dict) # Write out random components and interpolation grid to align the # three implementations. num_periods = init_dict['BASICS']['periods'] write_draws(num_periods, max_draws) write_interpolation_grid('test.respy.ini') # Clean evaluations based on interpolation grid, base_val, base_data = None, None for version in ['PYTHON', 'FORTRAN']: respy_obj = RespyCls('test.respy.ini') # Modify the version of the program for the different requests. respy_obj.unlock() respy_obj.set_attr('version', version) respy_obj.lock() # Solve the model respy_obj = simulate(respy_obj) # This parts checks the equality of simulated dataset for the # different versions of the code. data_frame = pd.read_csv('data.respy.dat', delim_whitespace=True) if base_data is None: base_data = data_frame.copy() assert_frame_equal(base_data, data_frame) # This part checks the equality of an evaluation of the # criterion function. _, crit_val = estimate(respy_obj) if base_val is None: base_val = crit_val np.testing.assert_allclose(base_val, crit_val, rtol=1e-05, atol=1e-06) # We know even more for the deterministic case. if constr['is_deterministic']: assert (crit_val in [-1.0, 0.0])
def test_5(self): """ This methods ensures that the core functions yield the same results across implementations. """ # Generate random initialization file generate_init() # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # Ensure that backward induction routines use the same grid for the # interpolation. max_states_period = write_interpolation_grid('test.respy.ini') # Extract class attributes num_periods, edu_start, edu_max, min_idx, model_paras, num_draws_emax, \ is_debug, delta, is_interpolated, num_points_interp, is_myopic, num_agents_sim, \ num_draws_prob, tau, paras_fixed, seed_sim = \ dist_class_attributes( respy_obj, 'num_periods', 'edu_start', 'edu_max', 'min_idx', 'model_paras', 'num_draws_emax', 'is_debug', 'delta', 'is_interpolated', 'num_points_interp', 'is_myopic', 'num_agents_sim', 'num_draws_prob', 'tau', 'paras_fixed', 'seed_sim') # Write out random components and interpolation grid to align the # three implementations. max_draws = max(num_agents_sim, num_draws_emax, num_draws_prob) write_draws(num_periods, max_draws) periods_draws_emax = read_draws(num_periods, num_draws_emax) periods_draws_prob = read_draws(num_periods, num_draws_prob) periods_draws_sims = read_draws(num_periods, num_agents_sim) # Extract coefficients coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky = dist_model_paras( model_paras, True) # Check the full solution procedure base_args = (coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky, is_interpolated, num_draws_emax, num_periods, num_points_interp, is_myopic, edu_start, is_debug, edu_max, min_idx, delta) fort, _ = resfort_interface(respy_obj, 'simulate') pyth = pyth_solve(*base_args + (periods_draws_emax,)) f2py = fort_debug.f2py_solve(*base_args + (periods_draws_emax, max_states_period)) for alt in [f2py, fort]: for i in range(5): np.testing.assert_allclose(pyth[i], alt[i]) # Distribute solution arguments for further use in simulation test. periods_payoffs_systematic, _, mapping_state_idx, periods_emax, states_all = pyth args = (periods_payoffs_systematic, mapping_state_idx, \ periods_emax, states_all, shocks_cholesky, num_periods, edu_start, edu_max, delta, num_agents_sim, periods_draws_sims, seed_sim) pyth = pyth_simulate(*args) f2py = fort_debug.f2py_simulate(*args) np.testing.assert_allclose(pyth, f2py) data_array = pyth base_args = (coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky, is_interpolated, num_draws_emax, num_periods, num_points_interp, is_myopic, edu_start, is_debug, edu_max, min_idx, delta, data_array, num_agents_sim, num_draws_prob, tau) args = base_args + (periods_draws_emax, periods_draws_prob) pyth = pyth_evaluate(*args) args = base_args + (periods_draws_emax, periods_draws_prob) f2py = fort_debug.f2py_evaluate(*args) np.testing.assert_allclose(pyth, f2py) # Evaluation of criterion function x0 = get_optim_paras(coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky, 'all', paras_fixed, is_debug) args = ( is_interpolated, num_draws_emax, num_periods, num_points_interp, is_myopic, edu_start, is_debug, edu_max, min_idx, delta, data_array, num_agents_sim, num_draws_prob, tau, periods_draws_emax, periods_draws_prob) pyth = pyth_criterion(x0, *args) f2py = fort_debug.f2py_criterion(x0, *args) np.testing.assert_allclose(pyth, f2py)
def test_5(self): """ This methods ensures that the core functions yield the same results across implementations. """ # Generate random initialization file generate_init() # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # Ensure that backward induction routines use the same grid for the # interpolation. max_states_period = write_interpolation_grid('test.respy.ini') # Extract class attributes num_periods, edu_start, edu_max, min_idx, model_paras, num_draws_emax, \ is_debug, delta, is_interpolated, num_points_interp, is_myopic, num_agents_sim, \ num_draws_prob, tau, paras_fixed, seed_sim = \ dist_class_attributes( respy_obj, 'num_periods', 'edu_start', 'edu_max', 'min_idx', 'model_paras', 'num_draws_emax', 'is_debug', 'delta', 'is_interpolated', 'num_points_interp', 'is_myopic', 'num_agents_sim', 'num_draws_prob', 'tau', 'paras_fixed', 'seed_sim') # Write out random components and interpolation grid to align the # three implementations. max_draws = max(num_agents_sim, num_draws_emax, num_draws_prob) write_draws(num_periods, max_draws) periods_draws_emax = read_draws(num_periods, num_draws_emax) periods_draws_prob = read_draws(num_periods, num_draws_prob) periods_draws_sims = read_draws(num_periods, num_agents_sim) # Extract coefficients coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky = dist_model_paras( model_paras, True) # Check the full solution procedure base_args = (coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky, is_interpolated, num_draws_emax, num_periods, num_points_interp, is_myopic, edu_start, is_debug, edu_max, min_idx, delta) fort, _ = resfort_interface(respy_obj, 'simulate') pyth = pyth_solve(*base_args + (periods_draws_emax, )) f2py = fort_debug.f2py_solve(*base_args + (periods_draws_emax, max_states_period)) for alt in [f2py, fort]: for i in range(5): np.testing.assert_allclose(pyth[i], alt[i]) # Distribute solution arguments for further use in simulation test. periods_payoffs_systematic, _, mapping_state_idx, periods_emax, states_all = pyth args = (periods_payoffs_systematic, mapping_state_idx, \ periods_emax, states_all, shocks_cholesky, num_periods, edu_start, edu_max, delta, num_agents_sim, periods_draws_sims, seed_sim) pyth = pyth_simulate(*args) f2py = fort_debug.f2py_simulate(*args) np.testing.assert_allclose(pyth, f2py) data_array = pyth base_args = (coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky, is_interpolated, num_draws_emax, num_periods, num_points_interp, is_myopic, edu_start, is_debug, edu_max, min_idx, delta, data_array, num_agents_sim, num_draws_prob, tau) args = base_args + (periods_draws_emax, periods_draws_prob) pyth = pyth_evaluate(*args) args = base_args + (periods_draws_emax, periods_draws_prob) f2py = fort_debug.f2py_evaluate(*args) np.testing.assert_allclose(pyth, f2py) # Evaluation of criterion function x0 = get_optim_paras(coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky, 'all', paras_fixed, is_debug) args = (is_interpolated, num_draws_emax, num_periods, num_points_interp, is_myopic, edu_start, is_debug, edu_max, min_idx, delta, data_array, num_agents_sim, num_draws_prob, tau, periods_draws_emax, periods_draws_prob) pyth = pyth_criterion(x0, *args) f2py = fort_debug.f2py_criterion(x0, *args) np.testing.assert_allclose(pyth, f2py)
def test_1(self): """ Testing the equality of an evaluation of the criterion function for a random request. """ # Run evaluation for multiple random requests. is_deterministic = np.random.choice([True, False], p=[0.10, 0.9]) is_interpolated = np.random.choice([True, False], p=[0.10, 0.9]) is_myopic = np.random.choice([True, False], p=[0.10, 0.9]) max_draws = np.random.randint(10, 100) # Generate random initialization file constr = dict() constr['is_deterministic'] = is_deterministic constr['flag_parallelism'] = False constr['is_myopic'] = is_myopic constr['max_draws'] = max_draws constr['maxfun'] = 0 init_dict = generate_random_dict(constr) # The use of the interpolation routines is a another special case. # Constructing a request that actually involves the use of the # interpolation routine is a little involved as the number of # interpolation points needs to be lower than the actual number of # states. And to know the number of states each period, I need to # construct the whole state space. if is_interpolated: # Extract from future initialization file the information # required to construct the state space. The number of periods # needs to be at least three in order to provide enough state # points. num_periods = np.random.randint(3, 6) edu_start = init_dict['EDUCATION']['start'] edu_max = init_dict['EDUCATION']['max'] min_idx = min(num_periods, (edu_max - edu_start + 1)) max_states_period = pyth_create_state_space(num_periods, edu_start, edu_max, min_idx)[3] # Updates to initialization dictionary that trigger a use of the # interpolation code. init_dict['BASICS']['periods'] = num_periods init_dict['INTERPOLATION']['flag'] = True init_dict['INTERPOLATION']['points'] = \ np.random.randint(10, max_states_period) # Print out the relevant initialization file. print_init_dict(init_dict) # Write out random components and interpolation grid to align the # three implementations. num_periods = init_dict['BASICS']['periods'] write_draws(num_periods, max_draws) write_interpolation_grid('test.respy.ini') # Clean evaluations based on interpolation grid, base_val, base_data = None, None for version in ['PYTHON', 'FORTRAN']: respy_obj = RespyCls('test.respy.ini') # Modify the version of the program for the different requests. respy_obj.unlock() respy_obj.set_attr('version', version) respy_obj.lock() # Solve the model respy_obj = simulate(respy_obj) # This parts checks the equality of simulated dataset for the # different versions of the code. data_frame = pd.read_csv('data.respy.dat', delim_whitespace=True) if base_data is None: base_data = data_frame.copy() assert_frame_equal(base_data, data_frame) # This part checks the equality of an evaluation of the # criterion function. _, crit_val = estimate(respy_obj) if base_val is None: base_val = crit_val np.testing.assert_allclose(base_val, crit_val, rtol=1e-05, atol=1e-06) # We know even more for the deterministic case. if constr['is_deterministic']: assert (crit_val in [-1.0, 0.0])