Skip to content

Commit

Permalink
Several small improvements (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
janosg authored Nov 19, 2022
1 parent 8ce1611 commit 1e1883a
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 46 deletions.
6 changes: 3 additions & 3 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ignore:
- "setup.py"
- "estimagic/tests/**/*"
- "src/estimagic/optimization/subsolvers/bntr_fast.py"
- "src/estimagic/optimization/subsolvers/_trsbox_quadratic_fast.py"
- "src/estimagic/optimization/subsolvers/_conjugate_gradient_quadratic_fast.py"
- "src/estimagic/optimization/subsolvers/_steihaug_toint_quadratic_fast.py"
- "src/estimagic/optimization/subsolvers/_trsbox_fast.py"
- "src/estimagic/optimization/subsolvers/_conjugate_gradient_fast.py"
- "src/estimagic/optimization/subsolvers/_steihaug_toint_fast.py"
- "src/estimagic/optimization/subsolvers/gqtpar_fast.py"
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ flat numpy array are explained in the next section.
Where the product of all but the last parameters is equal to 1.
If you have a function that calculates the derivative of your constraint, you can
add this under the key `"derivative"` to the constraint dictionary. Otherwise,
numerical derivatives are calculated for you if needed.
```

## Imposing multiple constraints at once
Expand Down
4 changes: 3 additions & 1 deletion src/estimagic/inference/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,14 @@ def calculate_estimation_summary(

df = pd.DataFrame(flat_data, index=names)

df.loc[free_names, "stars"] = pd.cut(
stars = pd.cut(
df.loc[free_names, "p_value"],
bins=[-1, 0.01, 0.05, 0.1, 2],
labels=["***", "**", "*", ""],
)

df["stars"] = stars

# ==================================================================================
# Map summary data into params tree structure
# ==================================================================================
Expand Down
19 changes: 19 additions & 0 deletions src/estimagic/optimization/fides_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,29 @@ def _process_fides_res(raw_res, opt):
"solution_hessian": hess,
"success": opt.converged,
"n_iterations": opt.iteration,
"message": _process_exitflag(opt.exitflag),
}
return res


def _process_exitflag(exitflag):
messages = {
"DID_NOT_RUN": "The optimizer did not run",
"MAXITER": "Reached maximum number of allowed iterations",
"MAXTIME": "Expected to reach maximum allowed time in next iteration",
"NOT_FINITE": "Encountered non-finite fval/grad/hess",
"EXCEEDED_BOUNDARY": "Exceeded specified boundaries",
"DELTA_TOO_SMALL": "Trust Region Radius too small to proceed",
"FTOL": "Converged according to fval difference",
"XTOL": "Converged according to x difference",
"GTOL": "Converged according to gradient norm",
}

out = messages.get(exitflag.name)

return out


def _create_hessian_updater_from_user_input(hessian_update_strategy):
hessians_needing_residuals = (
hessian_approximation.FX,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def minimize_trust_cg(
x_candidate, direction, trustregion_radius
)

step_size = (residual @ residual) / square_terms
# avoid divide by zero warning
if square_terms > 0:
step_size = (residual @ residual) / square_terms
else:
step_size = np.inf

if square_terms <= 0 or step_size > distance_to_boundary:
x_candidate = x_candidate + distance_to_boundary * direction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ def minimize_trust_cg_fast(
x_candidate, direction, trustregion_radius
)

step_size = (residual @ residual) / square_terms
# avoid divide by zero warning
if square_terms > 0:
step_size = (residual @ residual) / square_terms
else:
step_size = np.inf

if square_terms <= 0 or step_size > distance_to_boundary:
x_candidate = x_candidate + distance_to_boundary * direction
Expand Down
6 changes: 3 additions & 3 deletions src/estimagic/optimization/subsolvers/bntr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
from typing import Union

import numpy as np
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic import (
from estimagic.optimization.subsolvers._conjugate_gradient import (
minimize_trust_cg,
)
from estimagic.optimization.subsolvers._steihaug_toint_quadratic import (
from estimagic.optimization.subsolvers._steihaug_toint import (
minimize_trust_stcg,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import minimize_trust_trsbox
from estimagic.optimization.subsolvers._trsbox import minimize_trust_trsbox

EPSILON = np.finfo(float).eps ** (2 / 3)

Expand Down
6 changes: 3 additions & 3 deletions src/estimagic/optimization/subsolvers/bntr_fast.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Auxiliary functions for the quadratic BNTR trust-region subsolver."""
import numpy as np
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic_fast import (
from estimagic.optimization.subsolvers._conjugate_gradient_fast import (
minimize_trust_cg_fast,
)
from estimagic.optimization.subsolvers._steihaug_toint_quadratic_fast import (
from estimagic.optimization.subsolvers._steihaug_toint_fast import (
minimize_trust_stcg_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
minimize_trust_trsbox_fast,
)
from numba import njit
Expand Down
6 changes: 6 additions & 0 deletions src/estimagic/optimization/tiktak.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,13 +454,19 @@ def update_convergence_state(
bool: A bool that indicates if the optimizer has converged.
"""
# ==================================================================================
# unpack some variables
# ==================================================================================
xtol = convergence_criteria["xtol"]
max_discoveries = convergence_criteria["max_discoveries"]

best_x = current_state["best_x"]
best_y = current_state["best_y"]
best_res = current_state["best_res"]

# ==================================================================================
# filter out optimizations that raised errors
# ==================================================================================
# get indices of local optimizations that did not fail
valid_indices = [i for i, res in enumerate(results) if not isinstance(res, str)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,93 +1,93 @@
import numpy as np
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic import (
from estimagic.optimization.subsolvers._conjugate_gradient import (
_get_distance_to_trustregion_boundary as gdtb,
)
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic import (
from estimagic.optimization.subsolvers._conjugate_gradient import (
_update_vectors_for_next_iteration as uvnr,
)
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic import (
from estimagic.optimization.subsolvers._conjugate_gradient import (
minimize_trust_cg,
)
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic_fast import (
from estimagic.optimization.subsolvers._conjugate_gradient_fast import (
_get_distance_to_trustregion_boundary as gdtb_fast,
)
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic_fast import (
from estimagic.optimization.subsolvers._conjugate_gradient_fast import (
_update_vectors_for_next_iteration as uvnr_fast,
)
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic_fast import (
from estimagic.optimization.subsolvers._conjugate_gradient_fast import (
minimize_trust_cg_fast,
)
from estimagic.optimization.subsolvers._steihaug_toint_quadratic import (
from estimagic.optimization.subsolvers._steihaug_toint import (
minimize_trust_stcg,
)
from estimagic.optimization.subsolvers._steihaug_toint_quadratic_fast import (
from estimagic.optimization.subsolvers._steihaug_toint_fast import (
minimize_trust_stcg_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_apply_bounds_to_candidate_vector,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_calc_greatest_criterion_reduction as greatest_reduction_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_calc_new_reduction as new_reduction_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_calc_upper_bound_on_tangent as upper_bound_tangent_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_compute_new_search_direction_and_norm as new_dir_and_norm_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_take_constrained_step_up_to_boundary as step_constrained_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_take_unconstrained_step_up_to_boundary as step_unconstrained_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_update_candidate_vectors_and_reduction as update_candidate_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_update_candidate_vectors_and_reduction_alt_step as update_candidate_alt_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import (
from estimagic.optimization.subsolvers._trsbox import (
_update_tangent as update_tanget_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import minimize_trust_trsbox
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox import minimize_trust_trsbox
from estimagic.optimization.subsolvers._trsbox_fast import (
_calc_greatest_criterion_reduction as greatest_reduction_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_calc_new_reduction as new_reduction_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_calc_upper_bound_on_tangent as upper_bound_tangent_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_compute_new_search_direction_and_norm as new_dir_and_norm_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_perform_alternative_trustregion_step as perform_step_alt_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_perform_alternative_trustregion_step as perform_step_alt_orig,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_take_constrained_step_up_to_boundary as step_constrained_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_take_unconstrained_step_up_to_boundary as step_unconstrained_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_update_candidate_vectors_and_reduction as update_candidate_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_update_candidate_vectors_and_reduction_alt_step as update_candidate_alt_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
_update_tangent as update_tanget_fast,
)
from estimagic.optimization.subsolvers._trsbox_quadratic_fast import (
from estimagic.optimization.subsolvers._trsbox_fast import (
minimize_trust_trsbox_fast,
)
from numpy.testing import assert_array_almost_equal as aaae
Expand Down
6 changes: 3 additions & 3 deletions tests/optimization/test_quadratic_subsolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import numpy as np
import pytest
from estimagic.optimization.pounders_auxiliary import MainModel
from estimagic.optimization.subsolvers._conjugate_gradient_quadratic import (
from estimagic.optimization.subsolvers._conjugate_gradient import (
minimize_trust_cg,
)
from estimagic.optimization.subsolvers._steihaug_toint_quadratic import (
from estimagic.optimization.subsolvers._steihaug_toint import (
minimize_trust_stcg,
)
from estimagic.optimization.subsolvers._trsbox_quadratic import minimize_trust_trsbox
from estimagic.optimization.subsolvers._trsbox import minimize_trust_trsbox
from estimagic.optimization.subsolvers.bntr import (
bntr,
)
Expand Down

0 comments on commit 1e1883a

Please sign in to comment.