44
55from __future__ import annotations
66
7- from typing import Any , TypeVar , Union , cast
7+ from typing import Any , Callable , TypeVar , Union , cast
88import warnings
99
1010from numpy import (
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
0 commit comments