예제 #1
0
def test_robustness_1():
    np.random.seed(5470)
    true_paras = np.random.uniform(size=3)
    start = np.random.uniform(size=3)
    num_agents = 10000

    exog, endog = _simulate_sample(num_agents, true_paras)
    objective = functools.partial(_nonlinear_criterion, endog, exog)
    len_out = len(objective(start))
    minimize_pounders(objective, start, len_out)
예제 #2
0
def test_box_constr():
    np.random.seed(5472)
    true_params = np.random.uniform(0.3, 0.4, size=2)
    start_params = np.random.uniform(0.1, 0.2, size=2)
    bounds = [[0, 0], [0.3, 0.3]]
    num_agents = 10000

    exog, endog = _simulate_ols_sample(num_agents, true_params)
    objective = functools.partial(_ols_criterion, endog, exog)
    len_out = len(objective(start_params))
    calculated = minimize_pounders(objective,
                                   start_params,
                                   len_out,
                                   bounds=bounds)
    assert 0 <= calculated["x"][0] <= 0.3
    assert 0 <= calculated["x"][1] <= 0.3
예제 #3
0
def test_robustness_2():
    np.random.seed(5471)
    true_params = np.random.uniform(size=2)
    start_params = np.random.uniform(size=2)
    num_agents = 10000

    exog, endog = _simulate_ols_sample(num_agents, true_params)
    objective = functools.partial(_ols_criterion, endog, exog)
    len_out = len(objective(start_params))
    calculated = minimize_pounders(objective, start_params, len_out)["x"]

    x = np.column_stack([np.ones_like(exog), exog])
    y = endog.reshape(len(endog), 1)
    expected = np.linalg.lstsq(x, y, rcond=None)[0].flatten()

    np.testing.assert_almost_equal(calculated, expected, decimal=2)
예제 #4
0
def test_max_iters():
    np.random.seed(5473)
    true_params = np.random.uniform(0.3, 0.4, size=2)
    start_params = np.random.uniform(0.1, 0.2, size=2)
    bounds = [[0, 0], [0.3, 0.3]]
    num_agents = 10000

    exog, endog = _simulate_ols_sample(num_agents, true_params)
    objective = functools.partial(_ols_criterion, endog, exog)
    len_out = len(objective(start_params))
    calculated = minimize_pounders(objective,
                                   start_params,
                                   len_out,
                                   bounds=bounds,
                                   max_iterations=25)

    assert (calculated["conv"] == "user defined"
            or calculated["conv"] == "step size small")
    if calculated["conv"] == 8:
        assert calculated["sol"][0] == 25
예제 #5
0
def test_gatol():
    np.random.seed(5475)
    true_params = np.random.uniform(0.3, 0.4, size=2)
    start_params = np.random.uniform(0.1, 0.2, size=2)
    bounds = [[0, 0], [0.3, 0.3]]
    num_agents = 10000

    exog, endog = _simulate_ols_sample(num_agents, true_params)
    objective = functools.partial(_ols_criterion, endog, exog)
    len_out = len(objective(start_params))
    calculated = minimize_pounders(objective,
                                   start_params,
                                   len_out,
                                   bounds=bounds,
                                   grtol=False,
                                   gttol=False)

    assert (calculated["conv"] == "gatol below critical value"
            or calculated["conv"] == "step size small")
    if calculated["conv"] == 3:
        assert calculated["sol"][2] < 1e-4
예제 #6
0
def test_tol():
    np.random.seed(5477)
    true_params = np.random.uniform(0.3, 0.4, size=2)
    start_params = np.random.uniform(0.1, 0.2, size=2)
    bounds = [[0, 0], [0.3, 0.3]]
    num_agents = 10000

    exog, endog = _simulate_ols_sample(num_agents, true_params)
    objective = functools.partial(_ols_criterion, endog, exog)
    len_out = len(objective(start_params))
    calculated = minimize_pounders(
        objective,
        start_params,
        len_out,
        bounds=bounds,
        gatol=1e-7,
        grtol=1e-7,
        gttol=1e-9,
    )

    if calculated["conv"] == 3:
        assert calculated["sol"][2] < 0.00000001
    elif calculated["conv"] == 4:
        assert calculated["sol"][2] / calculated["sol"][1] < 0.00000001
예제 #7
0
def test_exception():
    np.random.seed(5478)
    with pytest.raises(Exception):
        minimize_pounders(_return_exception, 0)
예제 #8
0
def _internal_minimize(
    criterion,
    criterion_kwargs,
    params,
    internal_params,
    constraints,
    algorithm,
    algo_options,
    general_options,
    queue,
    fitness_factor,
):
    """Create the internal criterion function and minimize it.

    Args:
        criterion (function):
            Python function that takes a pandas DataFrame with parameters as the first
            argument and returns a scalar floating point value.

        criterion_kwargs (dict):
            additional keyword arguments for criterion

        params (pd.DataFrame):
            See :ref:`params`.

        internal_params (DataFrame):
            See :ref:`params`.

        constraints (list):
            list with constraint dictionaries. See for details.

        algorithm (str):
            specifies the optimization algorithm. See :ref:`list_of_algorithms`.

        algo_options (dict):
            algorithm specific configurations for the optimization

        general_options (dict):
            additional configurations for the optimization

        queue (Queue):
            queue to which the fitness evaluations and params DataFrames are supplied.

        fitness_factor (float):
            multiplicative factor for the fitness displayed in the dashboard.
            Set to -1 for maximizations to plot the fitness that is being maximized.

    """
    internal_criterion = create_internal_criterion(
        criterion=criterion,
        params=params,
        constraints=constraints,
        criterion_kwargs=criterion_kwargs,
        queue=queue,
        fitness_factor=fitness_factor,
    )

    current_dir_path = Path(__file__).resolve().parent
    with open(current_dir_path / "algo_dict.json") as j:
        algos = json.load(j)
    origin, algo_name = algorithm.split("_", 1)

    try:
        assert algo_name in algos[
            origin], "Invalid algorithm requested: {}".format(algorithm)
    except (AssertionError, KeyError):
        proposals = propose_algorithms(algorithm, algos)
        raise NotImplementedError(
            f"{algorithm} is not a valid choice. Did you mean one of {proposals}?"
        )

    if origin == "pygmo" and algorithm != "pygmo_simulated_annealing":
        assert (
            "popsize" in algo_options
        ), f"For genetic optimizers like {algo_name}, popsize is mandatory."
        assert (
            "gen" in algo_options
        ), f"For genetic optimizers like {algo_name}, gen is mandatory."

    if origin in ["nlopt", "pygmo"]:
        prob = _create_problem(internal_criterion, params)
        algo = _create_algorithm(algo_name, algo_options, origin)
        pop = _create_population(prob, algo_options, internal_params)
        evolved = algo.evolve(pop)
        result = _process_results(evolved, params, internal_params,
                                  constraints, origin)
    elif origin == "scipy":
        bounds = _get_scipy_bounds(params)
        minimized = scipy_minimize(
            internal_criterion,
            internal_params,
            method=algo_name,
            bounds=bounds,
            options=algo_options,
        )
        result = _process_results(minimized, params, internal_params,
                                  constraints, origin)
    elif origin == "tao":
        len_output = algo_options.pop("len_output", None)
        if len_output is None:
            len_output = len(criterion(params))

        bounds = (params.query("_internal_free")[["lower", "upper"
                                                  ]].to_numpy().T.tolist())
        minimized = minimize_pounders(internal_criterion, internal_params,
                                      len_output, bounds, **algo_options)
        result = _process_results(minimized, params, internal_params,
                                  constraints, origin)
    else:
        raise ValueError("Invalid algorithm requested.")

    return result