Skip to content

Commit

Permalink
Fix bug in _find_roots, add tol argument to _LM (#110)
Browse files Browse the repository at this point in the history
* Remove rogue line_profiler and print statements.

* Test.

* Changelog.
  • Loading branch information
mlondschien authored Aug 21, 2024
1 parent 3026275 commit bfd62ba
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------

Expand Down
1 change: 1 addition & 0 deletions ivmodels/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions ivmodels/tests/lagrange_multiplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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,
)
)

Expand Down
2 changes: 1 addition & 1 deletion ivmodels/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tests/tests/test_lagrange_multiplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
]
)

Expand Down

0 comments on commit bfd62ba

Please sign in to comment.