예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
def generate_init(constraints=None):
    """ Get a random initialization file.
    """
    # Antibugging. This interface is using a sentinel value.
    if constraints is not None:
        assert (isinstance(constraints, dict))

    dict_ = generate_random_dict(constraints)

    print_init_dict(dict_)

    # Finishing.
    return dict_
예제 #4
0
def generate_init(constraints=None):
    """ Get a random initialization file.
    """
    # Antibugging. This interface is using a sentinel value.
    if constraints is not None:
        assert (isinstance(constraints, dict))

    dict_ = generate_random_dict(constraints)

    print_init_dict(dict_)

    # Finishing.
    return dict_
예제 #5
0
def scripts_update(init_file):
    """ Update model parametrization in initialization file.
    """
    # Collect baseline update
    init_dict = read(init_file)

    paras_steps = get_est_info()['paras_step']

    # Get and construct ingredients
    coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky \
        = dist_optim_paras(paras_steps, True)
    shocks_coeffs = cholesky_to_coeffs(shocks_cholesky)

    # Update initialization dictionary
    init_dict['OCCUPATION A']['coeffs'] = coeffs_a
    init_dict['OCCUPATION B']['coeffs'] = coeffs_b
    init_dict['EDUCATION']['coeffs'] = coeffs_edu
    init_dict['SHOCKS']['coeffs'] = shocks_coeffs
    init_dict['HOME']['coeffs'] = coeffs_home

    print_init_dict(init_dict, init_file)
예제 #6
0
    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
예제 #7
0
    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
예제 #8
0
    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
예제 #9
0
def change_status(identifiers, init_file, is_fixed):
    """ Change the status of the a list of parameters.
    """
    # Baseline
    init_dict = read(init_file)
    respy_obj = RespyCls(init_file)

    model_paras = respy_obj.get_attr('model_paras')
    coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky = \
            dist_model_paras(model_paras, True)

    for identifier in identifiers:

        if identifier in list(range(0, 6)):
            j = identifier
            init_dict['OCCUPATION A']['coeffs'][j] = coeffs_a[j]
            init_dict['OCCUPATION A']['fixed'][j] = is_fixed
        elif identifier in list(range(6, 12)):
            j = identifier - 6
            init_dict['OCCUPATION B']['coeffs'][j] = coeffs_b[j]
            init_dict['OCCUPATION B']['fixed'][j] = is_fixed
        elif identifier in list(range(12, 15)):
            j = identifier - 12
            init_dict['EDUCATION']['coeffs'][j] = coeffs_edu[j]
            init_dict['EDUCATION']['fixed'][j] = is_fixed
        elif identifier in list(range(15, 16)):
            j = identifier - 15
            init_dict['HOME']['coeffs'][j] = coeffs_home[j]
            init_dict['HOME']['fixed'][j] = is_fixed
        elif identifier in list(range(16, 26)):
            j = identifier - 16
            shocks_coeffs = cholesky_to_coeffs(shocks_cholesky)
            init_dict['SHOCKS']['coeffs'] = shocks_coeffs
            init_dict['SHOCKS']['fixed'][j] = is_fixed
        else:
            raise NotImplementedError

        # Print dictionary to file
        print_init_dict(init_dict, init_file)
예제 #10
0
    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
예제 #11
0
    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)
예제 #12
0
    def test_5(self):
        """ This test reproduces the results from evaluations of the
        criterion function for previously analyzed scenarios.
        """
        # Prepare setup
        version = str(sys.version_info[0])
        fname = 'test_vault_' + version + '.respy.pkl'

        tests = pkl.load(open(TEST_RESOURCES_DIR + '/' + fname, 'rb'))

        # We want this test to run even when not FORTRAN version is available.
        while True:
            idx = np.random.randint(0, len(tests))
            init_dict, crit_val = tests[idx]

            version = init_dict['PROGRAM']['version']

            if not IS_FORTRAN and version == 'FORTRAN':
                pass
            else:
                break

        # In the case where no parallelism is available, we need to ensure
        # that the request remains valid. This is fine as the disturbances
        # are aligned across parallel and scalar implementation.
        if not IS_PARALLEL:
            init_dict['PARALLELISM']['flag'] = False
        if not IS_FORTRAN:
            init_dict['PROGRAM']['version'] = 'PYTHON'

        print_init_dict(init_dict)

        respy_obj = RespyCls('test.respy.ini')

        simulate(respy_obj)

        _, val = estimate(respy_obj)
        np.testing.assert_almost_equal(val, crit_val)
예제 #13
0
    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)
예제 #14
0
    def write_out(self, fname='model.respy.ini'):
        """ Write out the currently implied initialization file of the class
        instance.
        """
        # We reconstruct the initialization dictionary as otherwise we need
        # to constantly update the original one.
        init_dict = dict()

        # Basics
        init_dict['BASICS'] = dict()
        init_dict['BASICS']['periods'] = self.attr['num_periods']
        init_dict['BASICS']['delta'] = self.attr['delta']

        # Occupation A
        init_dict['OCCUPATION A'] = dict()
        init_dict['OCCUPATION A']['coeffs'] = \
            self.attr['model_paras']['coeffs_a']

        init_dict['OCCUPATION A']['fixed'] = self.attr['paras_fixed'][0:6]

        # Occupation A
        init_dict['OCCUPATION B'] = dict()
        init_dict['OCCUPATION B']['coeffs'] = \
            self.attr['model_paras']['coeffs_b']
        init_dict['OCCUPATION B']['fixed'] = self.attr['paras_fixed'][6:12]

        # Education
        init_dict['EDUCATION'] = dict()
        init_dict['EDUCATION']['coeffs'] = \
            self.attr['model_paras']['coeffs_edu']
        init_dict['EDUCATION']['fixed'] = self.attr['paras_fixed'][12:15]

        init_dict['EDUCATION']['start'] = self.attr['edu_start']
        init_dict['EDUCATION']['max'] = self.attr['edu_max']

        # Home
        init_dict['HOME'] = dict()
        init_dict['HOME']['coeffs'] = \
            self.attr['model_paras']['coeffs_home']
        init_dict['HOME']['fixed'] = self.attr['paras_fixed'][15:16]

        # Shocks
        init_dict['SHOCKS'] = dict()
        shocks_cholesky = self.attr['model_paras']['shocks_cholesky']
        shocks_coeffs = cholesky_to_coeffs(shocks_cholesky)
        init_dict['SHOCKS']['coeffs'] = shocks_coeffs
        init_dict['SHOCKS']['fixed'] = np.array(self.attr['paras_fixed'][16:])

        # Solution
        init_dict['SOLUTION'] = dict()
        init_dict['SOLUTION']['draws'] = self.attr['num_draws_emax']
        init_dict['SOLUTION']['seed'] = self.attr['seed_emax']
        init_dict['SOLUTION']['store'] = self.attr['is_store']

        # Simulation
        init_dict['SIMULATION'] = dict()
        init_dict['SIMULATION']['agents'] = self.attr['num_agents_sim']
        init_dict['SIMULATION']['file'] = self.attr['file_sim']
        init_dict['SIMULATION']['seed'] = self.attr['seed_sim']

        # Estimation
        init_dict['ESTIMATION'] = dict()
        init_dict['ESTIMATION']['optimizer'] = self.attr['optimizer_used']
        init_dict['ESTIMATION']['agents'] = self.attr['num_agents_est']
        init_dict['ESTIMATION']['draws'] = self.attr['num_draws_prob']
        init_dict['ESTIMATION']['seed'] = self.attr['seed_prob']
        init_dict['ESTIMATION']['file'] = self.attr['file_est']
        init_dict['ESTIMATION']['maxfun'] = self.attr['maxfun']
        init_dict['ESTIMATION']['tau'] = self.attr['tau']

        # Derivatives
        init_dict['DERIVATIVES'] = dict()
        init_dict['DERIVATIVES']['version'] = self.attr['derivatives'][0]
        init_dict['DERIVATIVES']['eps'] = self.attr['derivatives'][1]

        # Scaling
        init_dict['SCALING'] = dict()
        init_dict['SCALING']['flag'] = self.attr['scaling'][0]
        init_dict['SCALING']['minimum'] = self.attr['scaling'][1]

        # Program
        init_dict['PROGRAM'] = dict()
        init_dict['PROGRAM']['version'] = self.attr['version']
        init_dict['PROGRAM']['debug'] = self.attr['is_debug']

        # Parallelism
        init_dict['PARALLELISM'] = dict()
        init_dict['PARALLELISM']['flag'] = self.attr['is_parallel']
        init_dict['PARALLELISM']['procs'] = self.attr['num_procs']

        # Interpolation
        init_dict['INTERPOLATION'] = dict()
        init_dict['INTERPOLATION']['points'] = self.attr['num_points_interp']
        init_dict['INTERPOLATION']['flag'] = self.attr['is_interpolated']

        # Optimizers
        for optimizer in self.attr['optimizer_options'].keys():
            init_dict[optimizer] = self.attr['optimizer_options'][optimizer]

        print_init_dict(init_dict, fname)
예제 #15
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])
예제 #16
0
    def write_out(self, fname='model.respy.ini'):
        """ Write out the currently implied initialization file of the class
        instance.
        """
        # We reconstruct the initialization dictionary as otherwise we need
        # to constantly update the original one.
        init_dict = dict()

        # Basics
        init_dict['BASICS'] = dict()
        init_dict['BASICS']['periods'] = self.attr['num_periods']
        init_dict['BASICS']['delta'] = self.attr['delta']

        # Occupation A
        init_dict['OCCUPATION A'] = dict()
        init_dict['OCCUPATION A']['coeffs'] = \
            self.attr['model_paras']['coeffs_a']

        init_dict['OCCUPATION A']['fixed'] = self.attr['paras_fixed'][0:6]

        # Occupation A
        init_dict['OCCUPATION B'] = dict()
        init_dict['OCCUPATION B']['coeffs'] = \
            self.attr['model_paras']['coeffs_b']
        init_dict['OCCUPATION B']['fixed'] = self.attr['paras_fixed'][6:12]

        # Education
        init_dict['EDUCATION'] = dict()
        init_dict['EDUCATION']['coeffs'] = \
            self.attr['model_paras']['coeffs_edu']
        init_dict['EDUCATION']['fixed'] = self.attr['paras_fixed'][12:15]

        init_dict['EDUCATION']['start'] = self.attr['edu_start']
        init_dict['EDUCATION']['max'] = self.attr['edu_max']

        # Home
        init_dict['HOME'] = dict()
        init_dict['HOME']['coeffs'] = \
            self.attr['model_paras']['coeffs_home']
        init_dict['HOME']['fixed'] = self.attr['paras_fixed'][15:16]

        # Shocks
        init_dict['SHOCKS'] = dict()
        shocks_cholesky = self.attr['model_paras']['shocks_cholesky']
        shocks_coeffs = cholesky_to_coeffs(shocks_cholesky)
        init_dict['SHOCKS']['coeffs'] = shocks_coeffs
        init_dict['SHOCKS']['fixed'] = np.array(self.attr['paras_fixed'][16:])

        # Solution
        init_dict['SOLUTION'] = dict()
        init_dict['SOLUTION']['draws'] = self.attr['num_draws_emax']
        init_dict['SOLUTION']['seed'] = self.attr['seed_emax']
        init_dict['SOLUTION']['store'] = self.attr['is_store']

        # Simulation
        init_dict['SIMULATION'] = dict()
        init_dict['SIMULATION']['agents'] = self.attr['num_agents_sim']
        init_dict['SIMULATION']['file'] = self.attr['file_sim']
        init_dict['SIMULATION']['seed'] = self.attr['seed_sim']

        # Estimation
        init_dict['ESTIMATION'] = dict()
        init_dict['ESTIMATION']['optimizer'] = self.attr['optimizer_used']
        init_dict['ESTIMATION']['agents'] = self.attr['num_agents_est']
        init_dict['ESTIMATION']['draws'] = self.attr['num_draws_prob']
        init_dict['ESTIMATION']['seed'] = self.attr['seed_prob']
        init_dict['ESTIMATION']['file'] = self.attr['file_est']
        init_dict['ESTIMATION']['maxfun'] = self.attr['maxfun']
        init_dict['ESTIMATION']['tau'] = self.attr['tau']

        # Derivatives
        init_dict['DERIVATIVES'] = dict()
        init_dict['DERIVATIVES']['version'] = self.attr['derivatives'][0]
        init_dict['DERIVATIVES']['eps'] = self.attr['derivatives'][1]

        # Scaling
        init_dict['SCALING'] = dict()
        init_dict['SCALING']['flag'] = self.attr['scaling'][0]
        init_dict['SCALING']['minimum'] = self.attr['scaling'][1]

        # Program
        init_dict['PROGRAM'] = dict()
        init_dict['PROGRAM']['version'] = self.attr['version']
        init_dict['PROGRAM']['debug'] = self.attr['is_debug']

        # Parallelism
        init_dict['PARALLELISM'] = dict()
        init_dict['PARALLELISM']['flag'] = self.attr['is_parallel']
        init_dict['PARALLELISM']['procs'] = self.attr['num_procs']

        # Interpolation
        init_dict['INTERPOLATION'] = dict()
        init_dict['INTERPOLATION']['points'] = self.attr['num_points_interp']
        init_dict['INTERPOLATION']['flag'] = self.attr['is_interpolated']

        # Optimizers
        for optimizer in self.attr['optimizer_options'].keys():
            init_dict[optimizer] = self.attr['optimizer_options'][optimizer]

        print_init_dict(init_dict, fname)
예제 #17
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])
예제 #18
0
# for the execution of the script.
cwd = os.getcwd()
os.chdir(PACKAGE_DIR + '/respy')
subprocess.check_call(python_exec + ' waf distclean', shell=True)
subprocess.check_call(python_exec + ' waf configure build --debug', shell=True)
os.chdir(cwd)

# Import package. The late import is required as the compilation needs to
# take place first.
from respy.python.shared.shared_constants import TEST_RESOURCES_DIR
from respy.python.shared.shared_auxiliary import print_init_dict

from respy import RespyCls
from respy import simulate
from respy import estimate

############################################################################
# RUN
############################################################################
fname = 'test_vault_' + str(PYTHON_VERSION) + '.respy.pkl'
tests = pkl.load(open(TEST_RESOURCES_DIR + '/' + fname, 'rb'))

for idx, _ in enumerate(tests[:num_tests]):
    print('\n Checking Test ', idx, 'with version ', PYTHON_VERSION)
    init_dict, crit_val = tests[idx]

    print_init_dict(init_dict)
    respy_obj = RespyCls('test.respy.ini')
    simulate(respy_obj)
    np.testing.assert_almost_equal(estimate(respy_obj)[1], crit_val)