def test_1(self): """ Compare results from the RESTUD program and the RESPY package. """ # Impose some constraints on the initialization file which ensures that # the problem can be solved by the RESTUD code. The code is adjusted to # run with zero draws. constraints = dict() constraints['edu'] = (10, 20) constraints['is_deterministic'] = True # Generate random initialization file. The RESTUD code uses the same # random draws for the solution and simulation of the model. Thus, # the number of draws is required to be less or equal to the number # of agents. init_dict = generate_random_dict(constraints) num_agents_sim = init_dict['SIMULATION']['agents'] num_draws_emax = init_dict['SOLUTION']['draws'] if num_draws_emax < num_agents_sim: init_dict['SOLUTION']['draws'] = num_agents_sim print_init_dict(init_dict) # Indicate RESTUD code the special case of zero disturbance. open('.restud.testing.scratch', 'a').close() # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # This flag aligns the random components between the RESTUD program and # RESPY package. The existence of the file leads to the RESTUD program # to write out the random components. model_paras, edu_start, edu_max, num_agents_sim, num_periods, \ num_draws_emax, delta = \ dist_class_attributes(respy_obj, 'model_paras', 'edu_start', 'edu_max', 'num_agents_sim', 'num_periods', 'num_draws_emax', 'delta') transform_respy_to_restud(model_paras, edu_start, edu_max, num_agents_sim, num_periods, num_draws_emax, delta) # Solve model using RESTUD code. cmd = TEST_RESOURCES_DIR + '/kw_dp3asim' subprocess.check_call(cmd, shell=True) # Solve model using RESPY package. simulate(respy_obj) # Compare the simulated datasets generated by the programs. py = pd.DataFrame( np.array(np.genfromtxt('data.respy.dat', missing_values='.'), ndmin=2)[:, -4:]) fort = pd.DataFrame( np.array(np.genfromtxt('ftest.txt', missing_values='.'), ndmin=2)[:, -4:]) assert_frame_equal(py, fort)
def test_1(self): """ Compare results from the RESTUD program and the RESPY package. """ # Impose some constraints on the initialization file which ensures that # the problem can be solved by the RESTUD code. The code is adjusted to # run with zero draws. constraints = dict() constraints['edu'] = (10, 20) constraints['is_deterministic'] = True # Generate random initialization file. The RESTUD code uses the same # random draws for the solution and simulation of the model. Thus, # the number of draws is required to be less or equal to the number # of agents. init_dict = generate_random_dict(constraints) num_agents_sim = init_dict['SIMULATION']['agents'] num_draws_emax = init_dict['SOLUTION']['draws'] if num_draws_emax < num_agents_sim: init_dict['SOLUTION']['draws'] = num_agents_sim print_init_dict(init_dict) # Indicate RESTUD code the special case of zero disturbance. open('.restud.testing.scratch', 'a').close() # Perform toolbox actions respy_obj = RespyCls('test.respy.ini') # This flag aligns the random components between the RESTUD program and # RESPY package. The existence of the file leads to the RESTUD program # to write out the random components. model_paras, edu_start, edu_max, num_agents_sim, num_periods, \ num_draws_emax, delta = \ dist_class_attributes(respy_obj, 'model_paras', 'edu_start', 'edu_max', 'num_agents_sim', 'num_periods', 'num_draws_emax', 'delta') transform_respy_to_restud(model_paras, edu_start, edu_max, num_agents_sim, num_periods, num_draws_emax, delta) # Solve model using RESTUD code. cmd = TEST_RESOURCES_DIR + '/kw_dp3asim' subprocess.check_call(cmd, shell=True) # Solve model using RESPY package. simulate(respy_obj) # Compare the simulated datasets generated by the programs. py = pd.DataFrame(np.array(np.genfromtxt('data.respy.dat', missing_values='.'), ndmin=2)[:, -4:]) fort = pd.DataFrame(np.array(np.genfromtxt('ftest.txt', missing_values='.'), ndmin=2)[:, -4:]) assert_frame_equal(py, fort)
def test_2(self): """ This test compares the results from a solution using the interpolation code for the special case where the number of interpolation points is exactly the number of states in the final period. In this case the interpolation code is run and then all predicted values replaced with their actual values. """ # Set initial constraints constraints = dict() constraints['flag_interpolation'] = False constraints['periods'] = np.random.randint(3, 6) # Initialize request init_dict = generate_random_dict(constraints) baseline = None # Solve with and without interpolation code for _ in range(2): # Write out request print_init_dict(init_dict) # Process and solve respy_obj = RespyCls('test.respy.ini') respy_obj = simulate(respy_obj) # Extract class attributes states_number_period, periods_emax = \ dist_class_attributes(respy_obj, 'states_number_period', 'periods_emax') # Store and check results if baseline is None: baseline = periods_emax else: np.testing.assert_array_almost_equal(baseline, periods_emax) # Updates for second iteration init_dict['INTERPOLATION']['points'] = max(states_number_period) init_dict['INTERPOLATION']['flag'] = True
def test_1(self): """ This is the special case where the EMAX better be equal to the MAXE. """ # Set initial constraints constraints = dict() constraints['flag_interpolation'] = False constraints['periods'] = np.random.randint(3, 6) constraints['is_deterministic'] = True # Initialize request init_dict = generate_random_dict(constraints) baseline = None # Solve with and without interpolation code for _ in range(2): # Write out request print_init_dict(init_dict) # Process and solve respy_obj = RespyCls('test.respy.ini') respy_obj = simulate(respy_obj) # Extract class attributes states_number_period, periods_emax = \ dist_class_attributes(respy_obj, 'states_number_period', 'periods_emax') # Store and check results if baseline is None: baseline = periods_emax else: np.testing.assert_array_almost_equal(baseline, periods_emax) # Updates for second iteration. This ensures that there is at least # one interpolation taking place. init_dict['INTERPOLATION']['points'] = max( states_number_period) - 1 init_dict['INTERPOLATION']['flag'] = True
def test_2(self): """ This test ensures that the record files are identical. """ # Generate random initialization file. The number of periods is # higher than usual as only FORTRAN implementations are used to # solve the random request. This ensures that also some cases of # interpolation are explored. constr = dict() constr['version'] = 'FORTRAN' constr['periods'] = np.random.randint(3, 10) constr['maxfun'] = 0 init_dict = generate_random_dict(constr) base_sol_log, base_est_info_log, base_est_log = None, None, None for is_parallel in [False, True]: init_dict['PARALLELISM']['flag'] = is_parallel print_init_dict(init_dict) respy_obj = RespyCls('test.respy.ini') simulate(respy_obj) estimate(respy_obj) # Check for identical records 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 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_1(self): """ This is the special case where the EMAX better be equal to the MAXE. """ # Set initial constraints constraints = dict() constraints['flag_interpolation'] = False constraints['periods'] = np.random.randint(3, 6) constraints['is_deterministic'] = True # Initialize request init_dict = generate_random_dict(constraints) baseline = None # Solve with and without interpolation code for _ in range(2): # Write out request print_init_dict(init_dict) # Process and solve respy_obj = RespyCls('test.respy.ini') respy_obj = simulate(respy_obj) # Extract class attributes states_number_period, periods_emax = \ dist_class_attributes(respy_obj, 'states_number_period', 'periods_emax') # Store and check results if baseline is None: baseline = periods_emax else: np.testing.assert_array_almost_equal(baseline, periods_emax) # Updates for second iteration. This ensures that there is at least # one interpolation taking place. init_dict['INTERPOLATION']['points'] = max(states_number_period) - 1 init_dict['INTERPOLATION']['flag'] = True
def test_1(self): """ This test ensures that it makes no difference whether the criterion function is evaluated in parallel or not. """ # Generate random initialization file constr = dict() constr['version'] = 'FORTRAN' constr['maxfun'] = np.random.randint(0, 50) init_dict = generate_random_dict(constr) base = None for is_parallel in [True, False]: init_dict['PARALLELISM']['flag'] = is_parallel print_init_dict(init_dict) respy_obj = RespyCls('test.respy.ini') respy_obj = simulate(respy_obj) _, crit_val = estimate(respy_obj) if base is None: base = crit_val np.testing.assert_equal(base, crit_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_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])