From 7b6ec37e0cb3a5688efafa8f42441ef207681cce Mon Sep 17 00:00:00 2001 From: Tim Mensinger Date: Wed, 27 Mar 2024 16:44:07 +0100 Subject: [PATCH] Implement a few fixes (#490) --- pyproject.toml | 1 - src/estimagic/estimation/estimate_msm.py | 7 ++++ .../visualization/estimation_table.py | 36 ++++++++++++++++++- tests/estimation/test_estimate_msm.py | 4 +++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index ad7f7a157..c224f2557 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,7 +77,6 @@ infer_latex_dependencies = true filterwarnings = [ "ignore:Using or importing the ABCs from 'collections'", "ignore:the imp module is deprecated", - "ignore:indexing past lexsort depth may impact performance.", "ignore:Method .ptp is deprecated and will be removed in a future version. Use numpy.ptp instead.", "ignore:In a future version of pandas all arguments of concat except for the argument 'objs' will be keyword-only", "ignore:Please use `MemoizeJac` from the `scipy.optimize` namespace", diff --git a/src/estimagic/estimation/estimate_msm.py b/src/estimagic/estimation/estimate_msm.py index 3acfec748..26886594c 100644 --- a/src/estimagic/estimation/estimate_msm.py +++ b/src/estimagic/estimation/estimate_msm.py @@ -27,6 +27,7 @@ transform_free_cov_to_cov, transform_free_values_to_params_tree, ) +from estimagic.optimization.optimize_result import OptimizeResult from estimagic.optimization.optimize import minimize from estimagic.parameters.block_trees import block_tree_to_matrix, matrix_to_block_tree from estimagic.parameters.conversion import Converter, get_converter @@ -328,6 +329,7 @@ def func(x): _params=estimates, _weights=weights, _converter=converter, + _optimize_result=opt_res, _internal_weights=internal_weights, _internal_moments_cov=internal_moments_cov, _internal_jacobian=int_jac, @@ -463,6 +465,7 @@ class MomentsResult: _internal_jacobian: np.ndarray _empirical_moments: Any _has_constraints: bool + _optimize_result: Union[OptimizeResult, None] = None _jacobian: Any = None _no_jacobian_reason: Union[str, None] = None _cache: Dict = field(default_factory=dict) @@ -504,6 +507,10 @@ def _get_free_cov(self, method, n_samples, bounds_handling, seed): def params(self): return self._params + @property + def optimize_result(self): + return self._optimize_result + @property def weights(self): return self._weights diff --git a/src/estimagic/visualization/estimation_table.py b/src/estimagic/visualization/estimation_table.py index 39dcdd6a3..e749f717a 100644 --- a/src/estimagic/visualization/estimation_table.py +++ b/src/estimagic/visualization/estimation_table.py @@ -182,7 +182,7 @@ def estimation_table( if return_type == "render_inputs": out = render_inputs elif str(return_type).endswith("tex"): - out = render_latex( + out = _render_latex( **render_inputs, show_footer=show_footer, append_notes=append_notes, @@ -229,6 +229,7 @@ def estimation_table( return_type.write_text(out) +@suppress_performance_warnings def render_latex( body, footer, @@ -280,6 +281,39 @@ def render_latex( latex_str (str): The resulting string with Latex tabular code. """ + return _render_latex( + body=body, + footer=footer, + render_options=render_options, + show_footer=show_footer, + append_notes=append_notes, + notes_label=notes_label, + significance_levels=significance_levels, + custom_notes=custom_notes, + siunitx_warning=siunitx_warning, + show_index_names=show_index_names, + show_col_names=show_col_names, + show_col_groups=show_col_groups, + escape_special_characters=escape_special_characters, + ) + + +def _render_latex( + body, + footer, + render_options=None, + show_footer=True, + append_notes=True, + notes_label="Note:", + significance_levels=(0.1, 0.05, 0.01), + custom_notes=None, + siunitx_warning=True, + show_index_names=False, + show_col_names=True, + show_col_groups=True, + escape_special_characters=True, +): + """See docstring of render_latex for more information.""" if not pd.__version__ >= "1.4.0": raise ValueError( r"""render_latex or estimation_table with return_type="latex" requires diff --git a/tests/estimation/test_estimate_msm.py b/tests/estimation/test_estimate_msm.py index 6113f265b..6461ef3a5 100644 --- a/tests/estimation/test_estimate_msm.py +++ b/tests/estimation/test_estimate_msm.py @@ -5,6 +5,7 @@ import numpy as np import pandas as pd import pytest +from estimagic.optimization.optimize_result import OptimizeResult from estimagic.estimation.estimate_msm import estimate_msm from estimagic.shared.check_option_dicts import ( check_numdiff_options, @@ -64,6 +65,9 @@ def test_estimate_msm(simulate_moments, moments_cov, optimize_options): # check that minimization works aaae(calculated.params, expected_params) + # assert that optimization result exists and is of correct type + assert isinstance(calculated.optimize_result, OptimizeResult) + # check that cov works calculated_cov = calculated.cov() if isinstance(calculated_cov, pd.DataFrame):