Skip to content

Commit 99e06fc

Browse files
authored
Merge pull request #645 from bashtage/future-fixes
MAINT: Remove deprecated options in SciPy
2 parents 775ce62 + 3b4cec7 commit 99e06fc

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

azure-pipelines.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ jobs:
2121
- template: ci/azure_template_posix.yml
2222
parameters:
2323
name: Linux
24-
vmImage: ubuntu-20.04
24+
vmImage: ubuntu-latest
2525

2626
- template: ci/azure_template_windows.yml
2727
parameters:
2828
name: Windows
29-
vmImage: windows-2019
29+
vmImage: windows-latest

linearmodels/asset_pricing/model.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ def fit(
10021002
# 2. Step 1 using w = inv(s) from SV
10031003
callback = callback_factory(self._j, args, disp=disp)
10041004
_default_options: dict[str, Any] = {"callback": callback}
1005-
options = {"disp": bool(disp), "maxiter": max_iter}
1005+
options = {"maxiter": max_iter}
10061006
opt_options = {} if opt_options is None else opt_options
10071007
options.update(opt_options.get("options", {}))
10081008
_default_options.update(opt_options)
@@ -1031,7 +1031,7 @@ def fit(
10311031
params,
10321032
args=args,
10331033
callback=callback,
1034-
options={"disp": bool(disp), "maxiter": max_iter},
1034+
options={"maxiter": max_iter},
10351035
)
10361036
params = opt_res.x
10371037
obj = opt_res.fun
@@ -1047,7 +1047,7 @@ def fit(
10471047
params,
10481048
args=cue_args,
10491049
callback=callback,
1050-
options={"disp": bool(disp), "maxiter": max_iter},
1050+
options={"maxiter": max_iter},
10511051
)
10521052
params = opt_res.x
10531053

linearmodels/iv/model.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from __future__ import annotations
66

7-
from typing import Any, TypeVar, Union, cast
7+
from typing import Any, Callable, TypeVar, Union, cast
88
import warnings
99

1010
from numpy import (
@@ -16,11 +16,14 @@
1616
average,
1717
c_,
1818
column_stack,
19+
dtype,
1920
eye,
21+
float64,
2022
isscalar,
2123
logical_not,
2224
nan,
2325
nanmean,
26+
ndarray,
2427
ones,
2528
sqrt,
2629
squeeze,
@@ -1511,7 +1514,7 @@ def estimate_parameters(
15111514
x: linearmodels.typing.data.Float64Array,
15121515
y: linearmodels.typing.data.Float64Array,
15131516
z: linearmodels.typing.data.Float64Array,
1514-
display: bool = False,
1517+
display: int = 0,
15151518
opt_options: dict[str, Any] | None = None,
15161519
) -> tuple[linearmodels.typing.data.Float64Array, int]:
15171520
r"""
@@ -1525,8 +1528,8 @@ def estimate_parameters(
15251528
Regressand matrix (nobs by 1)
15261529
z : ndarray
15271530
Instrument matrix (nobs by ninstr)
1528-
display : bool
1529-
Flag indicating whether to display iterative optimizer output
1531+
display : int
1532+
Number of iterations between displaying. Set to 0 to suppress output.
15301533
opt_options : dict
15311534
Dictionary containing additional keyword arguments to pass to
15321535
scipy.optimize.minimize.
@@ -1549,12 +1552,34 @@ def estimate_parameters(
15491552
if opt_options is None:
15501553
opt_options = {}
15511554
assert opt_options is not None
1552-
options = {"disp": display}
1555+
options = {}
15531556
if "options" in opt_options:
15541557
opt_options = opt_options.copy()
15551558
options.update(opt_options.pop("options"))
15561559

1557-
res = minimize(self.j, starting, args=args, options=options, **opt_options)
1560+
def callback_factory(
1561+
_disp: int,
1562+
) -> Callable[[ndarray[tuple[int], dtype[float64]]], None]:
1563+
d = {"iter": 0}
1564+
1565+
def _callback(params: ndarray[tuple[int], dtype[float64]]) -> None:
1566+
fval = self.j(params, *args)
1567+
if _disp > 0 and (d["iter"] % _disp == 0):
1568+
print("Iteration: {}, Objective: {}".format(d["iter"], fval))
1569+
d["iter"] += 1
1570+
1571+
return _callback
1572+
1573+
callback = callback_factory(display)
1574+
1575+
res = minimize(
1576+
self.j,
1577+
starting,
1578+
args=args,
1579+
options=options,
1580+
callback=callback,
1581+
**opt_options,
1582+
)
15581583

15591584
return res.x[:, None], res.nit
15601585

linearmodels/tests/iv/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def test_gmm_cue_optimization_options(small_data):
404404
res_none = mod.fit(display=False)
405405
opt_options = dict(method="BFGS", options={"disp": False})
406406
res_bfgs = mod.fit(display=False, opt_options=opt_options)
407-
opt_options = dict(method="L-BFGS-B", options={"disp": False})
407+
opt_options = dict(method="L-BFGS-B")
408408
res_lbfgsb = mod.fit(display=False, opt_options=opt_options)
409409
assert res_none.iterations > 2
410410
assert res_bfgs.iterations > 2

0 commit comments

Comments
 (0)