18
18
KernelWeightMatrix ,
19
19
OneWayClusteredWeightMatrix )
20
20
from linearmodels .iv .results import IVGMMResults , IVResults , OLSResults
21
+ from linearmodels .typing import Numeric , OptionalNumeric
22
+ from linearmodels .typing .iv import ArrayLike , OptionalArrayLike
21
23
from linearmodels .utility import (WaldTestStatistic , has_constant , inv_sqrth ,
22
24
missing_warning )
23
25
@@ -105,11 +107,13 @@ class IVLIML(object):
105
107
IV2SLS, IVGMM, IVGMMCUE
106
108
"""
107
109
108
- def __init__ (self , dependent , exog , endog , instruments , * , weights = None ,
109
- fuller = 0 , kappa = None ):
110
+ def __init__ (self , dependent : ArrayLike , exog : OptionalArrayLike ,
111
+ endog : OptionalArrayLike , instruments : OptionalArrayLike , * ,
112
+ weights : OptionalArrayLike = None , fuller : Numeric = 0 ,
113
+ kappa : OptionalNumeric = None ):
110
114
111
115
self .dependent = IVData (dependent , var_name = 'dependent' )
112
- nobs = self .dependent .shape [0 ]
116
+ nobs = self .dependent .shape [0 ] # type: int
113
117
self .exog = IVData (exog , var_name = 'exog' , nobs = nobs )
114
118
self .endog = IVData (endog , var_name = 'endog' , nobs = nobs )
115
119
self .instruments = IVData (instruments , var_name = 'instruments' , nobs = nobs )
@@ -573,7 +577,9 @@ class IV2SLS(IVLIML):
573
577
IVLIML, IVGMM, IVGMMCUE
574
578
"""
575
579
576
- def __init__ (self , dependent , exog , endog , instruments , * , weights = None ):
580
+ def __init__ (self , dependent : ArrayLike , exog : OptionalArrayLike ,
581
+ endog : OptionalArrayLike , instruments : OptionalArrayLike , * ,
582
+ weights : OptionalArrayLike = None ):
577
583
self ._method = 'IV-2SLS'
578
584
super (IV2SLS , self ).__init__ (dependent , exog , endog , instruments ,
579
585
weights = weights , fuller = 0 , kappa = 1 )
@@ -675,8 +681,10 @@ class IVGMM(IVLIML):
675
681
IV2SLS, IVLIML, IVGMMCUE
676
682
"""
677
683
678
- def __init__ (self , dependent , exog , endog , instruments , * , weights = None ,
679
- weight_type = 'robust' , ** weight_config ):
684
+ def __init__ (self , dependent : ArrayLike , exog : OptionalArrayLike ,
685
+ endog : OptionalArrayLike , instruments : OptionalArrayLike , * ,
686
+ weights : OptionalArrayLike = None ,
687
+ weight_type : str = 'robust' , ** weight_config ):
680
688
self ._method = 'IV-GMM'
681
689
self ._result_container = IVGMMResults
682
690
super (IVGMM , self ).__init__ (dependent , exog , endog , instruments , weights = weights )
@@ -914,8 +922,10 @@ class IVGMMCUE(IVGMM):
914
922
IV2SLS, IVLIML, IVGMM
915
923
"""
916
924
917
- def __init__ (self , dependent , exog , endog , instruments , * , weights = None ,
918
- weight_type = 'robust' , ** weight_config ):
925
+ def __init__ (self , dependent : ArrayLike , exog : OptionalArrayLike ,
926
+ endog : OptionalArrayLike , instruments : OptionalArrayLike , * ,
927
+ weights : OptionalArrayLike = None ,
928
+ weight_type : str = 'robust' , ** weight_config ):
919
929
self ._method = 'IV-GMM-CUE'
920
930
super (IVGMMCUE , self ).__init__ (dependent , exog , endog , instruments , weights = weights ,
921
931
weight_type = weight_type , ** weight_config )
@@ -1017,7 +1027,7 @@ def j(self, params, x, y, z):
1017
1027
g_bar = (z * eps ).mean (0 )
1018
1028
return nobs * g_bar .T @ w @ g_bar .T
1019
1029
1020
- def estimate_parameters (self , starting , x , y , z , display = False ):
1030
+ def estimate_parameters (self , starting , x , y , z , display = False , opt_options = None ):
1021
1031
r"""
1022
1032
Parameters
1023
1033
----------
@@ -1031,6 +1041,9 @@ def estimate_parameters(self, starting, x, y, z, display=False):
1031
1041
Instrument matrix (nobs by ninstr)
1032
1042
display : bool
1033
1043
Flag indicating whether to display iterative optimizer output
1044
+ opt_options : dict, optional
1045
+ Dictionary containing additional keyword arguments to pass to
1046
+ scipy.optimize.minimize.
1034
1047
1035
1048
Returns
1036
1049
-------
@@ -1047,11 +1060,18 @@ def estimate_parameters(self, starting, x, y, z, display=False):
1047
1060
scipy.optimize.minimize
1048
1061
"""
1049
1062
args = (x , y , z )
1050
- res = minimize (self .j , starting , args = args , options = {'disp' : display })
1063
+ opt_options = {} if opt_options is None else opt_options
1064
+ options = {'disp' : display }
1065
+ if 'options' in opt_options :
1066
+ opt_options = opt_options .copy ()
1067
+ options .update (opt_options .pop ('options' ))
1068
+
1069
+ res = minimize (self .j , starting , args = args , options = options , ** opt_options )
1051
1070
1052
1071
return res .x [:, None ], res .nit
1053
1072
1054
- def fit (self , * , starting = None , display = False , cov_type = 'robust' , ** cov_config ):
1073
+ def fit (self , * , starting = None , display = False , cov_type = 'robust' , opt_options = None ,
1074
+ ** cov_config ):
1055
1075
r"""
1056
1076
Estimate model parameters
1057
1077
@@ -1064,6 +1084,10 @@ def fit(self, *, starting=None, display=False, cov_type='robust', **cov_config):
1064
1084
Flag indicating whether to display optimization output
1065
1085
cov_type : str, optional
1066
1086
Name of covariance estimator to use
1087
+ opt_options : dict, optional
1088
+ Additional options to pass to scipy.optimize.minimize when
1089
+ optimizing the objective function. If not provided, defers to
1090
+ scipy to choose an appropriate optimizer.
1067
1091
**cov_config
1068
1092
Additional parameters to pass to covariance estimator
1069
1093
@@ -1080,10 +1104,6 @@ def fit(self, *, starting=None, display=False, cov_type='robust', **cov_config):
1080
1104
is provided.
1081
1105
1082
1106
Starting values are computed by IVGMM.
1083
-
1084
- .. todo::
1085
-
1086
- * Expose method to pass optimization options
1087
1107
"""
1088
1108
1089
1109
wy , wx , wz = self ._wy , self ._wx , self ._wz
@@ -1103,7 +1123,8 @@ def fit(self, *, starting=None, display=False, cov_type='robust', **cov_config):
1103
1123
if len (starting ) != self .exog .shape [1 ] + self .endog .shape [1 ]:
1104
1124
raise ValueError ('starting does not have the correct number '
1105
1125
'of values' )
1106
- params , iters = self .estimate_parameters (starting , wx , wy , wz , display )
1126
+ params , iters = self .estimate_parameters (starting , wx , wy , wz , display ,
1127
+ opt_options = opt_options )
1107
1128
eps = wy - wx @ params
1108
1129
wmat = inv (weight_matrix (wx , wz , eps ))
1109
1130
@@ -1140,6 +1161,7 @@ class _OLS(IVLIML):
1140
1161
statsmodels.regression.linear_model.GLS
1141
1162
"""
1142
1163
1143
- def __init__ (self , dependent , exog , * , weights = None ):
1164
+ def __init__ (self , dependent : ArrayLike , exog : OptionalArrayLike , * ,
1165
+ weights : OptionalArrayLike = None ):
1144
1166
super (_OLS , self ).__init__ (dependent , exog , None , None , weights = weights , kappa = 0.0 )
1145
1167
self ._result_container = OLSResults
0 commit comments