Repository navigation
Conversation
c04625d to
e47841e
Compare
e47841e to
9e659dd
Compare
|
Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:
|
|
rebased, no merge conflict no unit tests |
|
This pull request introduces 17 alerts when merging 9e659dd into 8c67ccb - view on LGTM.com new alerts:
|
|
Warning: robust |
|
code does not include references Which is Huber-Dutter? |
|
The models here look more interesting than I remember e.g. However, no references, and I don't find much in the pile of articles that I printed a long time ago. R robust base Huber, Ronchetti 2009 section 7.8 COMPUTATION OF REGRESSION M-ESTIMATES, page 175 in the GMM version here I used meef scale (i.e. I guess that's rho and not Huber-Dutter) However, now I would prefer the version with auxiliary scale instead of joint mean-scale estimation especially for inference, and to allow more generic mix of moment conditions for mean and scale as I have it now in RLM. |
|
AFAICS, the examples here do not look at inference I guess this should get an interactive rebase to split out the example code. Last commit is obsolete. That leaves 2 commits, one for generic mle and one for gmm. Eventually we want to have this as What I would like right now is mainly something that uses scipy optimizer instead of irls and special iterative methods. |
| return self.loglikeobs(p) | ||
|
|
||
| import statsmodels.tools.numdiff as nd | ||
| grad_scale = nd.approx_fprime(np.atleast_1d(scale), func) |
There was a problem hiding this comment.
replace with explicit derivative, chi function
| #self.scale_fixed = rscale.mad(res_it.resid, center=0) | ||
| for i in range(10): | ||
| fittedvalues = res_it.predict() | ||
| self.scale_fixed = rscale.mad(self.endog - fittedvalues, center=0) |
There was a problem hiding this comment.
needs option for arbitrary scale_func, e.g. MScale
| if optim_options is None: | ||
| optim_options = {} | ||
| maxiter = optim_options.get('maxiter', 10) | ||
| res_loc = self.fit_loc(self.endog, self.exog).fit()#, weights=None) |
There was a problem hiding this comment.
needs to be generalized to nonlinear fit
| pass | ||
|
|
||
| def fit(self, start_params=None, start_scale=None, update_scale=True, | ||
| optim_options=None): |
There was a problem hiding this comment.
update to new scale options in RLM fit
|
|
||
|
|
||
| class RLMIterative(rlm.RLM): | ||
|
|
There was a problem hiding this comment.
with auxiliary scale as in RLM
| weights = self.norm_loc.weights(resid / scale) | ||
| if self.weights_prior is not None: | ||
| weights *= self.weights_prior | ||
| res_loc = self.fit_loc(self.endog, self.exog, weights=weights).fit() |
There was a problem hiding this comment.
needs nonlinear fit with start_params and maxiter
| exog = self.exog | ||
| return exog | ||
|
|
||
| def fit_iterative(self, start_params=None, **kwds): |
There was a problem hiding this comment.
this uses auxiliary scale, while fit uses joint or fixed scale
| for i in range(10): | ||
| fittedvalues = res_it.predict() | ||
| self.scale_fixed = rscale.mad(self.endog - fittedvalues, center=0) | ||
| res_it = self.fit(res_it.params, method='bfgs') |
There was a problem hiding this comment.
needs maxiter if we do not want full convergence for each updated scale
|
with generic optimizer, loglike is negative of loss function I stopped using nloglike for loss function in GenericLikelihoodModel. |
|
There is a book for R Theory is pretty short but might provide some overview and references. AFAIR, I never looked at the book before However their package nlr has been removed from CRAN |
|
I cannot figure out how to get a (unconstrained) optimization problem with gradient for S-estimation, so we could use it with scipy global optimizers. AFAICS, the usual moment conditions for S-estimation have asymmetric derivative matrix, i.e. it is not a symmetric Hessian from a simple optimization problem. (The only way I currently see is to use derivatives from the implicit function to compute gradient ds / dbeta, and optimizing only wrt beta.) |
|
|
||
|
|
||
| class MEstimator(GenericLikelihoodModel): | ||
|
|
There was a problem hiding this comment.
this is hardcoded for linear mean function
josef-pkt
left a comment
There was a problem hiding this comment.
partial read through
|
|
||
| #nobs = len(self.endog) | ||
| resid_scaled = (self.endog - np.dot(self.exog, params_loc)) / scale | ||
| scale_fac = 0.491 |
| scale_fac = 0.491 | ||
| rho_fac = scale * scale_fac | ||
| objective = (rho_fac * self.norm(resid_scaled / scale_fac) + | ||
| rho_fac * np.log(scale * scale_fac)) |
There was a problem hiding this comment.
what is rho_fac?
this looks like analogue to mle, with log(scale)
shouldn't it have scale_bias as factor in second term scale_bias * log(scale)
scale_bias should not be in the first term norm(...)
| self.scale_fixed = None | ||
| # TODO verify scale_fac | ||
| # stats.norm.expect(lambda t: t*norm.psi(t) - norm.rho(t)) | ||
| self.scale_fac = 0.35508 |
There was a problem hiding this comment.
another scale_fac with different value ?
Note moment condition for s for HD differs from log(s) mle-like version
so fisher consistency requires different expectation
| exog = self.exog | ||
| return np.dot(exog, params[:self.exog.shape[1]]) | ||
|
|
||
| def predict_jac(self, params, exog=None): |
There was a problem hiding this comment.
derivative of mean function w.r.t. params
| fitted_values = self.predict(params, self.exog) | ||
| resid_pearson = (self.endog - fitted_values) / scale | ||
|
|
||
| mom_loc = self.norm.psi(resid_pearson)[:,None] * self.instrument |
There was a problem hiding this comment.
Is this the only definition for use of instruments? reference Ronchetti?
psi(resid) * instruments
versus
psi(resid * instruments)
?
check derivative for linear model, if instruments = exog (no endogeneity)
|
|
||
|
|
||
| class _RobustGMM(gmm.GMM): | ||
| """Experimental class for robust M-estimation using estimating equations |
There was a problem hiding this comment.
this is not generic GMM, for arbitrary moment conditions.
This assumes additive residual = y - predict.
| a, b = params | ||
| return a * x / (np.exp(b) + x) | ||
|
|
||
| class MentenNL(MEstimatorHD): |
There was a problem hiding this comment.
Should we use predict, mean class to delegate to instead of subclassing the estimator class?
I guess that would be better. We might want to add extra attributes or methods to a class for the mean function.
e.g. parameter bounds (assuming not binding but useful for optimization).
param_names ?
plus we can reuse those with other estimators, nonlinear least squares, ...
|
loss function derivatives from wolframalpha and similarly for HD |
|
trying out version rebased on main start_params In the plots, HuberT norm looks semi-robust enough that we could use it as starting case for biweight. example plot for sigmoid (no seed set)
|
|
name for mean function classes ? base class PredictModel? I don't want mean_model because we use it model for submodels that are estimation Model classes (in multipart models, ZI,...) |
|
example file I locally have an updated file (maybe from after the previous rebase) omegahat looks fine but gradmoms has many (near-)zeros something is wrong with robust GMM, at least in example running variations, e.g. use ls params as start, increasing maxiter: correction |
|
tuning parameters, scale bias we need to compute them for HD scale moment condition. MEstimatorHD has RobustGMM uses M-scale I don't remember why those norms and norm parameters Also, I still don't know what the breakdown points are if mean and scale are based on different norms. And I still don't remember why I called the var/scale function Also, meef_scale here includes the scale_bias |
|
weights_prior RLMIterative has weights_prior but they are only used in mean estimation and not in scale estimation We need freq_weights and var_weights in RLM and derivative models, so we can add Mallow's and Schweppe's weights for generalize M-estimation (GM). |
|
The generic mle in this PR still has many and similar warnings I leave this PR for summer vacation I guess I will move nonlinear function classes to statsmodels.tools.nonlinear_func, so those can be reused by different (estimation) models. (likely with pattern similar to links, norms and transformation classes) class structure, maybe:
(*) we need this for inference with fixed scale MM-estimator, and most likely for S-estimator (in analogy to linear and multivaritate mean-cov models) For model with auxiliary scale, we need weighted nonlinear leastsquare for fit_loc, based on scipy leastsq which should be better than explicit coding of NL-WLS iteration. naming: Aside: |
|
'ex1' example RobustGMM has negative scale estimate. In general, for optimization w.r.t. scale we use a link function, e.g. exp, to force scale (or heteroscedasticity) estimates to be positive. |
|
(some design notes while I have internet)
I don't remember right now how RLM handles different scale estimation options. But scale is always auxiliary in RLM. aside: miscmodel TLinearModel requires fixed_df to be specified in The HD class seems to work quite well, with single objective and with iterative auxiliary scale. |
|
more pain
|
|
I'm giving up here again, for now. The methods work pretty well, especially the version that treats scale as auxiliary parameter, but the class structure is a mess. The main problem is that using global optimization for jointly scale and mean parameters does not work (scale -> 0 with redescending norm). new approach going forward:
The sandwich article for RLM recommends using joint inference for mean-scale so that standard errors are also valid for asymmetric distributions. (I have one set of messy changes with unfinished refactoring that I will add in a "dirty" commit.) |
|
this pr is the 2016 version |



(another PR to consolidate my robust regression code)
I added it for now to miscmodels because the models subclass GenericLikelihoodModel.
this includes a Huber-Dutter model class, see related #3258 (i.e. could be used for linear model with AR(1) errors and innovation outliers)
Also includes a few examples, classes for nonlinear functions like sigmoid and menten. Related, lmfit includes now more predefined nonlinear functions. We had related discussion around the time I wrote this in 2014. https://mail.scipy.org/pipermail/scipy-user/2014-March/035552.html
scipy.optimize leastsq also includes now some robust objective functions like Huber, since GSOC 2015
inference: AFAIR, I didn't check much, however we have now cov_type="HC0" which is the basic M-estimator sandwich also in GenericLikelihoodModel, and that should be the default for these type of functions. (RLM has an additional small sample correction based on Huber 197?)
analytical derivatives: I guess they can be build up from the different components
Hu, Hongchang. 2013. “Asymptotic Normality of Huber–Dutter Estimators in a Linear Model with AR(1) Processes.” Journal of Statistical Planning and Inference 143 (3): 548–62. doi:10.1016/j.jspi.2012.08.012.
has expressions for score and hessian (?) for Huber-Dutter that could be useful. (He uses a "nonstandard" robust norm function based on normal cdf, instead of Huber or bisquare.)
not clear (I never read about this): What do we use for global optimization, possibly many local optima with redescending weight functions? This might be more of a problem for generic nonlinear cases than for linear models. (Use basinhopping instead of the usual random starting values as in LTS?)
maybe still to come: simple and short class as prototype for RobustGMM, i.e. start with robust estimating equations which is useful if the estimating equations for mean and scale don't come from a consistent objective function.