Beispiel #1
0
def get_solve_func(params, options):
    """Get the solve function.

    This function takes a model specification and returns the state space of the model
    along with components of the solution such as covariates, non-pecuniary rewards,
    wages, continuation values and expected value functions as attributes of the class.

    Parameters
    ----------
    params : pandas.DataFrame
        DataFrame containing parameter series.
    options : dict
        Dictionary containing model attributes which are not optimized.

    Returns
    -------
    solve : :func:`~respy.solve.solve`
        Function with partialed arguments.

    Examples
    --------
    >>> import respy as rp
    >>> params, options = rp.get_example_model("robinson_crusoe_basic", with_data=False)
    >>> solve = rp.get_solve_func(params, options)
    >>> state_space = solve(params)

    """
    optim_paras, options = process_params_and_options(params, options)

    state_space = create_state_space_class(optim_paras, options)
    solve_function = functools.partial(solve,
                                       options=options,
                                       state_space=state_space)

    return solve_function
def test_child_indices():
    """Testing existence of properties for calculation of child indices!"""
    point_constr = {"n_periods": 2, "n_lagged_choices": 1}

    params, options = generate_random_model(point_constr=point_constr)

    # Add some inadmissible states
    optim_paras, options = process_params_and_options(params, options)

    state_space = create_state_space_class(optim_paras, options)

    # Create all relevant columns
    core_columns = ["period"] + create_core_state_space_columns(optim_paras)

    # compose child indices of first choice
    initial_state = state_space.core.iloc[0][core_columns].to_numpy()

    # Get all the future states
    states = []
    for i in range(len(optim_paras["choices"])):
        child = initial_state.copy()
        child[0] += 1
        child[i + 1] += 1
        child[-1] = i
        ix = state_space.indexer[tuple(child)]
        states.append(np.array(ix).reshape(1, 2))

    manual = np.concatenate(states, axis=0)
    np.testing.assert_array_equal(state_space.child_indices[0][0], manual)
Beispiel #3
0
def test_choice_restrictions():
    """Basic first test."""
    # Load model.
    params, options = process_model_or_seed("robinson_crusoe_extended")

    # Extend with observable characteristic.
    params.loc[("observable_health_well", "probability"), "value"] = 0.9
    params.loc[("observable_health_sick", "probability"), "value"] = 0.1

    # Sick people can never work.
    options["negative_choice_set"] = {
        "fishing":
        ["health == 'sick' & period < 2", "health == 'sick' & period >= 2"],
        "friday": ["period < 2", "exp_fishing == 0"],
    }
    # Create internal specification objects.
    optim_paras, options = process_params_and_options(params, options)

    state_space = create_state_space_class(optim_paras, options)

    for x in state_space.dense_key_to_complex.values():
        if (x[0] < 2) & (x[2] == (0, )):
            assert x[1] == (False, False, True)
        elif x[2] == (0, ):
            assert x[1] in [(False, False, True), (False, True, True)]
        elif (x[0] < 2) & (x[2] == (1, )):
            assert x[1] == (True, False, True)
        elif x[2] == (1, ):
            assert x[1] in [(True, False, True), (True, True, True)]
Beispiel #4
0
def test_dense_period_choice():
    params, options = rp.get_example_model("kw_94_one", with_data=False)
    options["negative_choice_set"] = {}
    options["negative_choice_set"]["b"] = ["period < 5"]

    optim_paras, options = process_params_and_options(params, options)
    state_space = create_state_space_class(optim_paras, options)

    check = _create_dense_period_choice(state_space.core, state_space.dense,
                                        state_space.core_key_to_core_indices,
                                        state_space.core_key_to_complex,
                                        optim_paras, options)

    for key in check:
        if key[0] < 5:
            assert ~key[1][1]