Ejemplo n.º 1
0
def respy_interface(respy_obj, request, data=None):
    """Provide the interface to the PYTHON functionality."""
    # Distribute class attributes
    (
        optim_paras,
        num_periods,
        edu_spec,
        is_debug,
        num_draws_prob,
        seed_prob,
        num_draws_emax,
        seed_emax,
        is_interpolated,
        num_points_interp,
        maxfun,
        optimizer_used,
        tau,
        optimizer_options,
        seed_sim,
        num_agents_sim,
        file_sim,
        precond_spec,
        num_types,
        num_paras,
        num_agents_est,
    ) = dist_class_attributes(
        respy_obj,
        "optim_paras",
        "num_periods",
        "edu_spec",
        "is_debug",
        "num_draws_prob",
        "seed_prob",
        "num_draws_emax",
        "seed_emax",
        "is_interpolated",
        "num_points_interp",
        "maxfun",
        "optimizer_used",
        "tau",
        "optimizer_options",
        "seed_sim",
        "num_agents_sim",
        "file_sim",
        "precond_spec",
        "num_types",
        "num_paras",
        "num_agents_est",
    )

    if request == "estimate":

        periods_draws_prob = create_draws(
            num_periods, num_draws_prob, seed_prob, is_debug
        )
        periods_draws_emax = create_draws(
            num_periods, num_draws_emax, seed_emax, is_debug
        )

        # Construct starting values
        x_optim_free_unscaled_start = get_optim_paras(
            optim_paras, num_paras, "free", is_debug
        )
        x_optim_all_unscaled_start = get_optim_paras(
            optim_paras, num_paras, "all", is_debug
        )

        # Construct the state space
        state_space = StateSpace(
            num_periods, num_types, edu_spec["start"], edu_spec["max"]
        )

        # Collect arguments that are required for the criterion function.
        # These must be in the correct order already.
        args = (
            is_interpolated,
            num_points_interp,
            is_debug,
            data,
            tau,
            periods_draws_emax,
            periods_draws_prob,
            state_space,
        )

        # Special case where just one evaluation at the starting values is
        # requested is accounted for. Note, that the relevant value of the
        # criterion function is always the one indicated by the class attribute
        # and not the value returned by the optimization algorithm.
        num_free = optim_paras["paras_fixed"].count(False)

        # Take only bounds from unfixed parameters and insert default bounds.
        mask_paras_fixed = np.array(optim_paras["paras_fixed"])
        paras_bounds_free_unscaled = np.array(optim_paras["paras_bounds"])[
            ~mask_paras_fixed
        ]
        paras_bounds_free_unscaled[:, 0] = np.where(
            paras_bounds_free_unscaled[:, 0] == None,  # noqa: E711
            -HUGE_FLOAT,
            paras_bounds_free_unscaled[:, 0],
        )
        paras_bounds_free_unscaled[:, 1] = np.where(
            paras_bounds_free_unscaled[:, 1] == None,  # noqa: E711
            HUGE_FLOAT,
            paras_bounds_free_unscaled[:, 1],
        )

        record_estimation_scaling(
            x_optim_free_unscaled_start,
            None,
            None,
            None,
            optim_paras["paras_fixed"],
            True,
        )

        precond_matrix = get_precondition_matrix(
            precond_spec,
            optim_paras,
            x_optim_all_unscaled_start,
            args,
            maxfun,
            num_paras,
            num_types,
        )

        x_optim_free_scaled_start = apply_scaling(
            x_optim_free_unscaled_start, precond_matrix, "do"
        )

        paras_bounds_free_scaled = np.full((num_free, 2), np.nan)
        for i in range(2):
            paras_bounds_free_scaled[:, i] = apply_scaling(
                paras_bounds_free_unscaled[:, i], precond_matrix, "do"
            )

        record_estimation_scaling(
            x_optim_free_unscaled_start,
            x_optim_free_scaled_start,
            paras_bounds_free_scaled,
            precond_matrix,
            optim_paras["paras_fixed"],
            False,
        )

        opt_obj = OptimizationClass(
            x_optim_all_unscaled_start,
            optim_paras["paras_fixed"],
            precond_matrix,
            num_types,
        )
        opt_obj.maxfun = maxfun

        if maxfun == 0:

            record_estimation_scalability("Start")
            opt_obj.crit_func(x_optim_free_scaled_start, *args)
            record_estimation_scalability("Finish")

            success = True
            message = "Single evaluation of criterion function at starting values."

        elif optimizer_used == "SCIPY-BFGS":

            bfgs_maxiter = optimizer_options["SCIPY-BFGS"]["maxiter"]
            bfgs_gtol = optimizer_options["SCIPY-BFGS"]["gtol"]
            bfgs_eps = optimizer_options["SCIPY-BFGS"]["eps"]

            try:
                rslt = fmin_bfgs(
                    opt_obj.crit_func,
                    x_optim_free_scaled_start,
                    args=args,
                    gtol=bfgs_gtol,
                    epsilon=bfgs_eps,
                    maxiter=bfgs_maxiter,
                    full_output=True,
                    disp=False,
                )

                success = rslt[6] not in [1, 2]
                message = "Optimization terminated successfully."
                if rslt[6] == 1:
                    message = "Maximum number of iterations exceeded."
                elif rslt[6] == 2:
                    message = "Gradient and/or function calls not changing."

            except MaxfunError:
                success = False
                message = "Maximum number of iterations exceeded."

        elif optimizer_used == "SCIPY-LBFGSB":

            lbfgsb_maxiter = optimizer_options["SCIPY-LBFGSB"]["maxiter"]
            lbfgsb_maxls = optimizer_options["SCIPY-LBFGSB"]["maxls"]
            lbfgsb_factr = optimizer_options["SCIPY-LBFGSB"]["factr"]
            lbfgsb_pgtol = optimizer_options["SCIPY-LBFGSB"]["pgtol"]
            lbfgsb_eps = optimizer_options["SCIPY-LBFGSB"]["eps"]
            lbfgsb_m = optimizer_options["SCIPY-LBFGSB"]["m"]

            try:
                rslt = fmin_l_bfgs_b(
                    opt_obj.crit_func,
                    x_optim_free_scaled_start,
                    args=args,
                    approx_grad=True,
                    bounds=paras_bounds_free_scaled,
                    m=lbfgsb_m,
                    factr=lbfgsb_factr,
                    pgtol=lbfgsb_pgtol,
                    epsilon=lbfgsb_eps,
                    iprint=-1,
                    maxfun=maxfun,
                    maxiter=lbfgsb_maxiter,
                    maxls=lbfgsb_maxls,
                )

                success = rslt[2]["warnflag"] in [0]
                message = rslt[2]["task"]

            except MaxfunError:
                success = False
                message = "Maximum number of iterations exceeded."

        elif optimizer_used == "SCIPY-POWELL":

            powell_maxiter = optimizer_options["SCIPY-POWELL"]["maxiter"]
            powell_maxfun = optimizer_options["SCIPY-POWELL"]["maxfun"]
            powell_xtol = optimizer_options["SCIPY-POWELL"]["xtol"]
            powell_ftol = optimizer_options["SCIPY-POWELL"]["ftol"]

            try:
                rslt = fmin_powell(
                    opt_obj.crit_func,
                    x_optim_free_scaled_start,
                    args,
                    powell_xtol,
                    powell_ftol,
                    powell_maxiter,
                    powell_maxfun,
                    disp=0,
                )

                success = rslt[5] not in [1, 2]
                message = "Optimization terminated successfully."
                if rslt[5] == 1:
                    message = "Maximum number of function evaluations."
                elif rslt[5] == 2:
                    message = "Maximum number of iterations."

            except MaxfunError:
                success = False
                message = "Maximum number of iterations exceeded."

        else:
            raise NotImplementedError

        record_estimation_final(success, message)
        record_estimation_stop()

    elif request == "simulate":

        # Draw draws for the simulation.
        periods_draws_sims = create_draws(
            num_periods, num_agents_sim, seed_sim, is_debug
        )

        # Draw standard normal deviates for the solution and evaluation step.
        periods_draws_emax = create_draws(
            num_periods, num_draws_emax, seed_emax, is_debug
        )

        # Collect arguments for different implementations of the simulation.
        state_space = pyth_solve(
            is_interpolated,
            num_points_interp,
            num_periods,
            is_debug,
            periods_draws_emax,
            edu_spec,
            optim_paras,
            file_sim,
            num_types,
        )

        simulated_data = pyth_simulate(
            state_space,
            num_agents_sim,
            periods_draws_sims,
            seed_sim,
            file_sim,
            edu_spec,
            optim_paras,
            is_debug,
        )

        args = (state_space, simulated_data)

    else:
        raise NotImplementedError("This request is not implemented.")

    return args
Ejemplo n.º 2
0
    def test_6(self):
        """ Further tests for the interpolation routines.
        """
        params_spec, options_spec = generate_random_model()
        respy_obj = RespyCls(params_spec, options_spec)
        respy_obj = simulate_observed(respy_obj)

        # Extract class attributes
        (
            periods_rewards_systematic,
            mapping_state_idx,
            seed_prob,
            periods_emax,
            num_periods,
            states_all,
            num_points_interp,
            edu_spec,
            num_draws_emax,
            is_myopic,
            is_debug,
            is_interpolated,
            optim_paras,
            optimizer_options,
            file_sim,
            num_types,
        ) = dist_class_attributes(
            respy_obj,
            "periods_rewards_systematic",
            "mapping_state_idx",
            "seed_prob",
            "periods_emax",
            "num_periods",
            "states_all",
            "num_points_interp",
            "edu_spec",
            "num_draws_emax",
            "is_myopic",
            "is_debug",
            "is_interpolated",
            "optim_paras",
            "optimizer_options",
            "file_sim",
            "num_types",
        )

        shocks_cholesky = optim_paras["shocks_cholesky"]
        shocks_cov = shocks_cholesky.dot(shocks_cholesky.T)
        coeffs_common = optim_paras["coeffs_common"]
        coeffs_a = optim_paras["coeffs_a"]
        coeffs_b = optim_paras["coeffs_b"]
        delta = optim_paras["delta"]

        # Add some additional objects required for the interfaces to the functions.
        period = np.random.choice(num_periods)

        periods_draws_emax = create_draws(num_periods, num_draws_emax,
                                          seed_prob, is_debug)

        draws_emax_standard = periods_draws_emax[period, :, :]

        draws_emax_risk = transform_disturbances(draws_emax_standard,
                                                 np.zeros(4), shocks_cholesky)

        # Initialize Python version and solve.
        state_space = StateSpace(num_periods, num_types, edu_spec["start"],
                                 edu_spec["max"], optim_paras)

        # Integrate periods_emax in state_space
        state_space.emaxs = np.column_stack((
            np.zeros((state_space.num_states, 4)),
            periods_emax[~np.isnan(periods_emax)
                         & (periods_emax != MISSING_FLOAT)],
        ))

        # Fill emaxs_a - emaxs_home in the requested period
        states_period = state_space.get_attribute_from_period("states", period)

        # Do not get the emaxs from the previous period if we are in the last one.
        if period != state_space.num_periods - 1:
            state_space.emaxs = get_emaxs_of_subsequent_period(
                states_period, state_space.indexer, state_space.emaxs,
                edu_spec["max"])

        num_states = state_space.states_per_period[period]

        shifts = np.random.randn(4)

        # Slight modification of request which assures that the interpolation code is
        # working.
        num_points_interp = min(num_points_interp, num_states)

        # Get the IS_SIMULATED indicator for the subset of points which are used for the
        # predication model.
        is_simulated = get_simulated_indicator(num_points_interp, num_states,
                                               period, is_debug)

        # Unpack necessary attributes
        rewards_period = state_space.get_attribute_from_period(
            "rewards", period)
        emaxs_period = state_space.get_attribute_from_period("emaxs",
                                                             period)[:, :4]
        max_education = (state_space.get_attribute_from_period(
            "states", period)[:, 3] >= edu_spec["max"])

        # Construct the exogenous variables for all points of the state space.
        exogenous, max_emax = get_exogenous_variables(rewards_period,
                                                      emaxs_period, shifts,
                                                      optim_paras["delta"],
                                                      max_education)

        # Align output between Python and Fortran version.
        py = (exogenous, max_emax)

        f90 = fort_debug.wrapper_get_exogenous_variables(
            period,
            num_periods,
            num_states,
            periods_rewards_systematic,
            shifts,
            mapping_state_idx,
            periods_emax,
            states_all,
            edu_spec["start"],
            edu_spec["max"],
            delta,
            coeffs_common,
            coeffs_a,
            coeffs_b,
            num_types,
        )

        assert_almost_equal(py[0], f90[0])
        assert_almost_equal(py[1], f90[1])

        # Construct endogenous variable so that the prediction model can be fitted.
        endogenous = get_endogenous_variable(
            rewards_period,
            emaxs_period,
            max_emax,
            is_simulated,
            draws_emax_risk,
            optim_paras["delta"],
            max_education,
        )

        f90 = fort_debug.wrapper_get_endogenous_variable(
            period,
            num_periods,
            num_states,
            periods_rewards_systematic,
            mapping_state_idx,
            periods_emax,
            states_all,
            is_simulated,
            num_draws_emax,
            max_emax,
            draws_emax_risk,
            edu_spec["start"],
            edu_spec["max"],
            shocks_cov,
            delta,
            coeffs_common,
            coeffs_a,
            coeffs_b,
        )
        assert_almost_equal(endogenous, replace_missing_values(f90))

        py = get_predictions(endogenous, exogenous, max_emax, is_simulated)

        f90 = fort_debug.wrapper_get_predictions(
            endogenous,
            exogenous,
            max_emax,
            is_simulated,
            num_points_interp,
            num_states,
            file_sim,
            False,
        )

        # This assertion fails if a column is all zeros.
        if not exogenous.any(axis=0).any():
            assert_array_almost_equal(py, f90)
Ejemplo n.º 3
0
def respy_interface(respy_obj, request, data_array=None):
    """ This function provides the interface to the PYTHOn functionality.
    """
    # Distribute class attributes
    model_paras, num_periods, num_agents_est, edu_start, is_debug, edu_max, \
        delta, num_draws_prob, seed_prob, num_draws_emax, seed_emax, \
        min_idx, is_myopic, is_interpolated, num_points_interp, maxfun, \
        optimizer_used, tau, paras_fixed, optimizer_options, seed_sim, \
        num_agents_sim, derivatives = dist_class_attributes( respy_obj,
            'model_paras', 'num_periods', 'num_agents_est', 'edu_start',
            'is_debug', 'edu_max', 'delta', 'num_draws_prob', 'seed_prob',
            'num_draws_emax', 'seed_emax', 'min_idx', 'is_myopic',
            'is_interpolated', 'num_points_interp', 'maxfun', 'optimizer_used',
            'tau', 'paras_fixed', 'optimizer_options', 'seed_sim',
            'num_agents_sim', 'derivatives')

    # Auxiliary objects
    dfunc_eps = derivatives[1]

    coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky = \
        dist_model_paras(model_paras, is_debug)

    if request == 'estimate':
        # Check that selected optimizer is in line with version of program.
        if maxfun > 0:
            assert optimizer_used in OPTIMIZERS_PYTH

        periods_draws_prob = create_draws(num_periods, num_draws_prob,
                                          seed_prob, is_debug)

        # Draw standard normal deviates for the solution and evaluation step.
        periods_draws_emax = create_draws(num_periods, num_draws_emax,
                                          seed_emax, is_debug)

        # Construct starting values
        x_free_start = get_optim_paras(coeffs_a, coeffs_b, coeffs_edu,
                                       coeffs_home, shocks_cholesky, 'free',
                                       paras_fixed, is_debug)

        x_all_start = get_optim_paras(coeffs_a, coeffs_b, coeffs_edu,
                                      coeffs_home, shocks_cholesky, 'all',
                                      paras_fixed, is_debug)

        # Collect arguments that are required for the criterion function. These
        # must be in the correct order already.
        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_est, num_draws_prob,
                tau, periods_draws_emax, periods_draws_prob)

        # Special case where just an evaluation at the starting values is
        # requested is accounted for. Note, that the relevant value of the
        # criterion function is always the one indicated by the class
        # attribute and not the value returned by the optimization algorithm.
        opt_obj = OptimizationClass()

        opt_obj.maxfun = maxfun
        opt_obj.paras_fixed = paras_fixed
        opt_obj.x_all_start = x_all_start

        if maxfun == 0:
            opt_obj.crit_func(x_free_start, *args)
            success = True
            message = 'Single evaluation of criterion function at starting ' \
                      'values.'

        elif optimizer_used == 'SCIPY-BFGS':

            bfgs_maxiter = optimizer_options['SCIPY-BFGS']['maxiter']
            bfgs_gtol = optimizer_options['SCIPY-BFGS']['gtol']

            try:
                rslt = fmin_bfgs(opt_obj.crit_func,
                                 x_free_start,
                                 args=args,
                                 gtol=bfgs_gtol,
                                 epsilon=dfunc_eps,
                                 maxiter=bfgs_maxiter,
                                 full_output=True,
                                 disp=False)

                success = (rslt[6] not in [1, 2])
                rslt = 'Optimization terminated successfully.'
                if rslt[5] == 1:
                    message = 'Maximum number of iterations exceeded.'
                elif rslt == 2:
                    message = 'Gradient and/or function calls not changing.'

            except MaxfunError:
                success = False
                message = 'Maximum number of iterations exceeded.'

        elif optimizer_used == 'SCIPY-POWELL':

            powell_maxiter = optimizer_options['SCIPY-POWELL']['maxiter']
            powell_maxfun = optimizer_options['SCIPY-POWELL']['maxfun']
            powell_xtol = optimizer_options['SCIPY-POWELL']['xtol']
            powell_ftol = optimizer_options['SCIPY-POWELL']['ftol']

            try:
                rslt = fmin_powell(opt_obj.crit_func,
                                   x_free_start,
                                   args,
                                   powell_xtol,
                                   powell_ftol,
                                   powell_maxiter,
                                   powell_maxfun,
                                   disp=0)

                success = (rslt[5] not in [1, 2])
                message = 'Optimization terminated successfully.'
                if rslt[5] == 1:
                    message = 'Maximum number of function evaluations.'
                elif rslt[5] == 2:
                    message = 'Maximum number of iterations.'

            except MaxfunError:
                success = False
                message = 'Maximum number of iterations exceeded.'

        record_estimation_final(opt_obj, success, message)
        record_estimation_stop()

    elif request == 'simulate':

        # Draw draws for the simulation.
        periods_draws_sims = create_draws(num_periods, num_agents_sim,
                                          seed_sim, is_debug)

        # Draw standard normal deviates for the solution and evaluation step.
        periods_draws_emax = create_draws(num_periods, num_draws_emax,
                                          seed_emax, is_debug)

        # Collect arguments to pass in different implementations of the
        # simulation.
        periods_payoffs_systematic, states_number_period, mapping_state_idx, \
            periods_emax, states_all = pyth_solve(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, periods_draws_emax)

        solution = (periods_payoffs_systematic, states_number_period,
                    mapping_state_idx, periods_emax, states_all)

        data_array = pyth_simulate(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)

        args = (solution, data_array)
    else:
        raise AssertionError

    return args
Ejemplo n.º 4
0
    def test_4(self):
        """ Testing the core functions of the solution step for the equality of results
        between the PYTHON and FORTRAN implementations.
        """
        params_spec, options_spec = generate_random_model()
        respy_obj = RespyCls(params_spec, options_spec)

        # Ensure that backward induction routines use the same grid for the
        # interpolation.
        write_interpolation_grid(respy_obj)

        # Extract class attributes
        (
            num_periods,
            edu_spec,
            optim_paras,
            num_draws_emax,
            seed_emax,
            is_debug,
            is_interpolated,
            num_points_interp,
            optimizer_options,
            file_sim,
            num_types,
        ) = dist_class_attributes(
            respy_obj,
            "num_periods",
            "edu_spec",
            "optim_paras",
            "num_draws_emax",
            "seed_emax",
            "is_debug",
            "is_interpolated",
            "num_points_interp",
            "optimizer_options",
            "file_sim",
            "num_types",
        )

        shocks_cholesky = optim_paras["shocks_cholesky"]
        coeffs_common = optim_paras["coeffs_common"]
        coeffs_home = optim_paras["coeffs_home"]
        coeffs_edu = optim_paras["coeffs_edu"]
        coeffs_a = optim_paras["coeffs_a"]
        coeffs_b = optim_paras["coeffs_b"]
        delta = optim_paras["delta"]

        type_spec_shifts = optim_paras["type_shifts"]
        type_spec_shares = optim_paras["type_shares"]

        min_idx = edu_spec["max"] + 1

        # Check the state space creation.
        state_space = StateSpace(num_periods, num_types, edu_spec["start"],
                                 edu_spec["max"], optim_paras)

        states_all, mapping_state_idx, _, _ = state_space._get_fortran_counterparts(
        )

        pyth = (
            states_all,
            state_space.states_per_period,
            mapping_state_idx,
            state_space.states_per_period.max(),
        )

        f2py = fort_debug.wrapper_create_state_space(num_periods, num_types,
                                                     edu_spec["start"],
                                                     edu_spec["max"], min_idx)
        for i in range(4):
            # Slice Fortran output to shape of Python output.
            if isinstance(f2py[i], np.ndarray):
                f2py_reduced = f2py[i][tuple(map(slice, pyth[i].shape))]
            else:
                f2py_reduced = f2py[i]

            assert_allclose(pyth[i], f2py_reduced)

        _, _, pyth, _ = state_space._get_fortran_counterparts()

        f2py = fort_debug.wrapper_calculate_rewards_systematic(
            num_periods,
            state_space.states_per_period,
            states_all,
            state_space.states_per_period.max(),
            coeffs_common,
            coeffs_a,
            coeffs_b,
            coeffs_edu,
            coeffs_home,
            type_spec_shares,
            type_spec_shifts,
        )

        assert_allclose(pyth, f2py)

        # Carry some results from the systematic rewards calculation for future use and
        # create the required set of disturbances.
        periods_draws_emax = create_draws(num_periods, num_draws_emax,
                                          seed_emax, is_debug)

        # Save result for next test.
        periods_rewards_systematic = pyth.copy()

        # Fix for hardcoded myopic agents.
        optim_paras["delta"] = 0.00000000000000001

        # Check backward induction procedure.
        state_space = pyth_backward_induction(
            periods_draws_emax,
            state_space,
            is_debug,
            is_interpolated,
            num_points_interp,
            optim_paras,
            file_sim,
            False,
        )
        _, _, _, pyth = state_space._get_fortran_counterparts()

        f2py = fort_debug.wrapper_backward_induction(
            num_periods,
            False,
            state_space.states_per_period.max(),
            periods_draws_emax,
            num_draws_emax,
            state_space.states_per_period,
            periods_rewards_systematic,
            mapping_state_idx,
            states_all,
            is_debug,
            is_interpolated,
            num_points_interp,
            edu_spec["start"],
            edu_spec["max"],
            shocks_cholesky,
            delta,
            coeffs_common,
            coeffs_a,
            coeffs_b,
            file_sim,
            False,
        )

        assert_allclose(pyth, f2py)
Ejemplo n.º 5
0
    def test_6(self):
        """ Further tests for the interpolation routines.
        """
        # Generate random initialization file
        generate_init()

        # Perform toolbox actions
        respy_obj = RespyCls('test.respy.ini')
        respy_obj = simulate(respy_obj)

        # Extract class attributes
        periods_payoffs_systematic, states_number_period, mapping_state_idx, seed_prob, periods_emax, num_periods, states_all, num_points_interp, edu_start, num_draws_emax, is_debug, edu_max, delta = dist_class_attributes(
            respy_obj, 'periods_payoffs_systematic', 'states_number_period',
            'mapping_state_idx', 'seed_prob', 'periods_emax',
            'num_periods', 'states_all', 'num_points_interp', 'edu_start',
            'num_draws_emax', 'is_debug', 'edu_max', 'delta')

        # Add some additional objects required for the interfaces to the
        # functions.
        period = np.random.choice(range(num_periods))

        periods_draws_emax = create_draws(num_periods, num_draws_emax, seed_prob,
            is_debug)

        draws_emax = periods_draws_emax[period, :, :]

        num_states = states_number_period[period]

        shifts = np.random.randn(4)

        # Slight modification of request which assures that the
        # interpolation code is working.
        num_points_interp = min(num_points_interp, num_states)

        # Get the IS_SIMULATED indicator for the subset of points which are
        # used for the predication model.
        args = (num_points_interp, num_states, period, is_debug)
        is_simulated = get_simulated_indicator(*args)

        # Construct the exogenous variables for all points of the state
        # space.
        args = (
        period, num_periods, num_states, delta, periods_payoffs_systematic, shifts,
        edu_max, edu_start, mapping_state_idx, periods_emax, states_all)

        py = get_exogenous_variables(*args)
        f90 = fort_debug.wrapper_get_exogenous_variables(*args)

        np.testing.assert_equal(py, f90)

        # Distribute validated results for further functions.
        exogenous, maxe = py

        # Construct endogenous variable so that the prediction model can be
        # fitted.
        args = (period, num_periods, num_states, delta,
            periods_payoffs_systematic, edu_max, edu_start,
            mapping_state_idx, periods_emax, states_all, is_simulated,
            num_draws_emax, maxe, draws_emax)

        py = get_endogenous_variable(*args)
        f90 = fort_debug.wrapper_get_endogenous_variable(*args)

        np.testing.assert_equal(py, replace_missing_values(f90))

        # Distribute validated results for further functions.
        endogenous = py

        args = (endogenous, exogenous, maxe, is_simulated, num_points_interp,
            num_states, is_debug)

        py = get_predictions(*args)
        f90 = fort_debug.wrapper_get_predictions(*args[:-1])

        np.testing.assert_array_almost_equal(py, f90)
Ejemplo n.º 6
0
    def test_4(self):
        """ Testing the core functions of the solution step for the equality
        of results between the PYTHON and FORTRAN 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.
        write_interpolation_grid('test.respy.ini')

        # Extract class attributes
        num_periods, edu_start, edu_max, min_idx, model_paras, num_draws_emax, \
            seed_emax, is_debug, delta, is_interpolated, num_points_interp, = \
                dist_class_attributes(respy_obj,
                    'num_periods', 'edu_start', 'edu_max', 'min_idx',
                    'model_paras', 'num_draws_emax', 'seed_emax', 'is_debug',
                    'delta', 'is_interpolated', 'num_points_interp')

        # Auxiliary objects
        coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky = \
            dist_model_paras(model_paras, is_debug)

        # Check the state space creation.
        args = (num_periods, edu_start, edu_max, min_idx)
        pyth = pyth_create_state_space(*args)
        f2py = fort_debug.f2py_create_state_space(*args)
        for i in range(4):
            np.testing.assert_allclose(pyth[i], f2py[i])

        # Carry some results from the state space creation for future use.
        states_all, states_number_period = pyth[:2]
        mapping_state_idx, max_states_period = pyth[2:]

        # Cutting to size
        states_all = states_all[:, :max(states_number_period), :]

        # Check calculation of systematic components of payoffs.
        args = (num_periods, states_number_period, states_all, edu_start,
            coeffs_a, coeffs_b, coeffs_edu, coeffs_home, max_states_period)
        pyth = pyth_calculate_payoffs_systematic(*args)
        f2py = fort_debug.f2py_calculate_payoffs_systematic(*args)
        np.testing.assert_allclose(pyth, f2py)

        # Carry some results from the systematic payoff calculation for
        # future use and create the required set of disturbances.
        periods_draws_emax = create_draws(num_periods, num_draws_emax,
            seed_emax, is_debug)

        periods_payoffs_systematic = pyth

        # Check backward induction procedure.
        args = (num_periods, max_states_period, periods_draws_emax,
            num_draws_emax, states_number_period, periods_payoffs_systematic,
            edu_max, edu_start, mapping_state_idx, states_all, delta,
            is_debug, is_interpolated, num_points_interp, shocks_cholesky)

        pyth = pyth_backward_induction(*args)

        f2py = fort_debug.f2py_backward_induction(*args)
        np.testing.assert_allclose(pyth, f2py)
Ejemplo n.º 7
0
    def test_6(self):
        """ Further tests for the interpolation routines.
        """
        # Generate random initialization file
        generate_init()

        # Perform toolbox actions
        respy_obj = RespyCls('test.respy.ini')
        respy_obj = simulate(respy_obj)

        # Extract class attributes
        periods_payoffs_systematic, states_number_period, mapping_state_idx, seed_prob, periods_emax, num_periods, states_all, num_points_interp, edu_start, num_draws_emax, is_debug, edu_max, delta = dist_class_attributes(
            respy_obj, 'periods_payoffs_systematic', 'states_number_period',
            'mapping_state_idx', 'seed_prob', 'periods_emax', 'num_periods',
            'states_all', 'num_points_interp', 'edu_start', 'num_draws_emax',
            'is_debug', 'edu_max', 'delta')

        # Add some additional objects required for the interfaces to the
        # functions.
        period = np.random.choice(range(num_periods))

        periods_draws_emax = create_draws(num_periods, num_draws_emax,
                                          seed_prob, is_debug)

        draws_emax = periods_draws_emax[period, :, :]

        num_states = states_number_period[period]

        shifts = np.random.randn(4)

        # Slight modification of request which assures that the
        # interpolation code is working.
        num_points_interp = min(num_points_interp, num_states)

        # Get the IS_SIMULATED indicator for the subset of points which are
        # used for the predication model.
        args = (num_points_interp, num_states, period, is_debug)
        is_simulated = get_simulated_indicator(*args)

        # Construct the exogenous variables for all points of the state
        # space.
        args = (period, num_periods, num_states, delta,
                periods_payoffs_systematic, shifts, edu_max, edu_start,
                mapping_state_idx, periods_emax, states_all)

        py = get_exogenous_variables(*args)
        f90 = fort_debug.wrapper_get_exogenous_variables(*args)

        np.testing.assert_equal(py, f90)

        # Distribute validated results for further functions.
        exogenous, maxe = py

        # Construct endogenous variable so that the prediction model can be
        # fitted.
        args = (period, num_periods, num_states, delta,
                periods_payoffs_systematic, edu_max, edu_start,
                mapping_state_idx, periods_emax, states_all, is_simulated,
                num_draws_emax, maxe, draws_emax)

        py = get_endogenous_variable(*args)
        f90 = fort_debug.wrapper_get_endogenous_variable(*args)

        np.testing.assert_equal(py, replace_missing_values(f90))

        # Distribute validated results for further functions.
        endogenous = py

        args = (endogenous, exogenous, maxe, is_simulated, num_points_interp,
                num_states, is_debug)

        py = get_predictions(*args)
        f90 = fort_debug.wrapper_get_predictions(*args[:-1])

        np.testing.assert_array_almost_equal(py, f90)
Ejemplo n.º 8
0
    def test_4(self):
        """ Testing the core functions of the solution step for the equality
        of results between the PYTHON and FORTRAN 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.
        write_interpolation_grid('test.respy.ini')

        # Extract class attributes
        num_periods, edu_start, edu_max, min_idx, model_paras, num_draws_emax, \
            seed_emax, is_debug, delta, is_interpolated, num_points_interp, = \
                dist_class_attributes(respy_obj,
                    'num_periods', 'edu_start', 'edu_max', 'min_idx',
                    'model_paras', 'num_draws_emax', 'seed_emax', 'is_debug',
                    'delta', 'is_interpolated', 'num_points_interp')

        # Auxiliary objects
        coeffs_a, coeffs_b, coeffs_edu, coeffs_home, shocks_cholesky = \
            dist_model_paras(model_paras, is_debug)

        # Check the state space creation.
        args = (num_periods, edu_start, edu_max, min_idx)
        pyth = pyth_create_state_space(*args)
        f2py = fort_debug.f2py_create_state_space(*args)
        for i in range(4):
            np.testing.assert_allclose(pyth[i], f2py[i])

        # Carry some results from the state space creation for future use.
        states_all, states_number_period = pyth[:2]
        mapping_state_idx, max_states_period = pyth[2:]

        # Cutting to size
        states_all = states_all[:, :max(states_number_period), :]

        # Check calculation of systematic components of payoffs.
        args = (num_periods, states_number_period, states_all, edu_start,
                coeffs_a, coeffs_b, coeffs_edu, coeffs_home, max_states_period)
        pyth = pyth_calculate_payoffs_systematic(*args)
        f2py = fort_debug.f2py_calculate_payoffs_systematic(*args)
        np.testing.assert_allclose(pyth, f2py)

        # Carry some results from the systematic payoff calculation for
        # future use and create the required set of disturbances.
        periods_draws_emax = create_draws(num_periods, num_draws_emax,
                                          seed_emax, is_debug)

        periods_payoffs_systematic = pyth

        # Check backward induction procedure.
        args = (num_periods, max_states_period, periods_draws_emax,
                num_draws_emax, states_number_period,
                periods_payoffs_systematic, edu_max, edu_start,
                mapping_state_idx, states_all, delta, is_debug,
                is_interpolated, num_points_interp, shocks_cholesky)

        pyth = pyth_backward_induction(*args)

        f2py = fort_debug.f2py_backward_induction(*args)
        np.testing.assert_allclose(pyth, f2py)