From a5895901aee08a02336b1aa8bf9dd21eccbc50c1 Mon Sep 17 00:00:00 2001 From: Malte Londschien Date: Tue, 20 Aug 2024 22:15:32 +0200 Subject: [PATCH 1/3] Remove rogue line_profiler and print statements. --- ivmodels/summary.py | 1 + ivmodels/tests/lagrange_multiplier.py | 12 ++++++++++-- ivmodels/utils.py | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ivmodels/summary.py b/ivmodels/summary.py index aa8919c..c18efbe 100644 --- a/ivmodels/summary.py +++ b/ivmodels/summary.py @@ -145,6 +145,7 @@ def fit(self, X, y, Z=None, C=None, *args, **kwargs): fit_intercept = self.kclass.fit_intercept for name in feature_names: + idx = np.where(np.array(all_feature_names) == name)[0][0] mask = np.zeros(len(all_feature_names), dtype=bool) mask[idx] = True diff --git a/ivmodels/tests/lagrange_multiplier.py b/ivmodels/tests/lagrange_multiplier.py index 56a41fa..179fe41 100644 --- a/ivmodels/tests/lagrange_multiplier.py +++ b/ivmodels/tests/lagrange_multiplier.py @@ -67,6 +67,8 @@ class _LM: gamma_0: list of str or np.ndarray of dimension (mw), optional, default=None Initial value for the minimization. If ``str``, must be one of "liml" or "zero". If ``None``, ``"liml"`` is used. + tol: float, optional, default=None + Tolerance for the optimization algorithm. """ def __init__( @@ -81,8 +83,8 @@ def __init__( W_proj=None, optimizer="bfgs", gamma_0=None, + tol=None, ): - self.X = X self.y = y.reshape(-1, 1) self.W = W @@ -116,6 +118,7 @@ def __init__( self.gamma_0 = ["liml"] if gamma_0 is None else gamma_0 if isinstance(self.gamma_0, str): self.gamma_0 = [self.gamma_0] + self.tol = tol def liml(self, beta=None): """ @@ -299,7 +302,12 @@ def _derivative(gamma): results.append( scipy.optimize.minimize( - objective, jac=jac, hess=hess, x0=gamma_0, method=self.optimizer + objective, + jac=jac, + hess=hess, + x0=gamma_0, + method=self.optimizer, + tol=self.tol, ) ) diff --git a/ivmodels/utils.py b/ivmodels/utils.py index 969dd0e..a93123e 100644 --- a/ivmodels/utils.py +++ b/ivmodels/utils.py @@ -240,7 +240,7 @@ def _find_roots(f, a, b, tol, max_value, max_eval, n_points=50, max_depth=5): sgn = np.sign(b - a) if np.isinf(b): grid = np.ones(n_points) * a - grid[1:] += sgn * np.logspace(tol, np.log10(max_value), n_points - 1) + grid[1:] += sgn * np.logspace(np.log10(tol), np.log10(max_value), n_points - 1) else: grid = np.linspace(a, b, n_points) From ec5a437082f4607e22b6e6e62e4f0fcd91b18b94 Mon Sep 17 00:00:00 2001 From: Malte Londschien Date: Wed, 21 Aug 2024 12:24:00 +0200 Subject: [PATCH 2/3] Test. --- tests/tests/test_lagrange_multiplier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/tests/test_lagrange_multiplier.py b/tests/tests/test_lagrange_multiplier.py index 61457cf..5491b23 100644 --- a/tests/tests/test_lagrange_multiplier.py +++ b/tests/tests/test_lagrange_multiplier.py @@ -36,7 +36,7 @@ def test__LM__init__(n, mx, mw, k): [ np.all(np.isclose(lm1.__dict__[k], lm2.__dict__[k])) for k in lm1.__dict__.keys() - if k not in ["optimizer", "gamma_0"] + if k not in ["optimizer", "gamma_0", "tol"] ] ) From 6dc7683d7266246882d2bc7f57a49c7fef5baba6 Mon Sep 17 00:00:00 2001 From: Malte Londschien Date: Wed, 21 Aug 2024 12:27:59 +0200 Subject: [PATCH 3/3] Changelog. --- CHANGELOG.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c568c9a..c69bb44 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -8,6 +8,13 @@ Changelog - The Wald test now supports robust covariance estimation. +**Other changes:** + +- One can now pass the tolerance parameter `tol` to the optimization algorithm in + :func:`~ivmodels.tests.lagrange_multiplier.lagrange_multiplier_test` and + :func:`~ivmodels.tests.lagrange_multiplier.inverse_lagrange_multiplier_test` via the + `kwargs`. + 0.4.0 - 2024-08-08 ------------------