Skip to content

WIP: ENH Robust nonlinear estimators - #3261

Open
josef-pkt wants to merge 4 commits into
statsmodels:mainfrom
josef-pkt:robust_nonlinear
Open

josef-pkt wants to merge 4 commits into
statsmodels:mainfrom
josef-pkt:robust_nonlinear

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Nov 7, 2016 •

Copy link
Copy Markdown
Member

(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.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 87.844% when pulling 8bfed94 on josef-pkt:robust_nonlinear into c3493d9 on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 87.972% when pulling e47841e on josef-pkt:robust_nonlinear into 7c723cf on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same at 87.972% when pulling e47841e on josef-pkt:robust_nonlinear into 7c723cf on statsmodels:master.

@pep8speaks

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 11:1: F403 'from statsmodels.miscmodels.robust_genericmle import *' used; unable to detect undefined names
Line 17:12: F405 'sm' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 18:17: F405 'sm' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 28:12: F405 'sm' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 36:15: F405 'sm' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 36:37: F405 'sm' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 37:41: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 44:15: F405 'sm' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 46:50: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 51:11: F405 'MEstimator' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 62:12: F405 'MEstimator' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 80:12: F405 'MEstimatorHD' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 102:15: F405 'RLMIterative' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 102:43: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 102:80: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 105:16: F405 'RLMIterative' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 105:44: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 106:48: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 110:16: F405 'RLMIterative' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 110:44: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 110:81: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 141:12: F405 'MentenNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 154:13: F405 'MentenNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 154:33: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 155:25: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 160:29: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 187:12: F405 'ExpNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 196:13: F405 'ExpNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 196:33: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 197:25: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 202:29: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 211:14: F405 'sigmoid' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 230:13: F405 'sig_start' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 237:12: F405 'SigmoidNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 248:13: F405 'SigmoidNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 248:34: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 249:25: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 254:29: F405 'rscale' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 263:1: E402 module level import not at top of file
Line 297:12: F405 'SigmoidNL' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle
Line 297:33: F405 'rnorms' may be undefined, or defined from star imports: statsmodels.miscmodels.robust_genericmle

Line 13:1: F401 'statsmodels.base.model.GenericLikelihoodModel' imported but unused
Line 16:1: F401 'statsmodels.robust.scale as rscale' imported but unused
Line 17:1: F401 'statsmodels.robust.robust_linear_model as rlm' imported but unused

Line 16:1: F401 'nltk.corpus.reader.rte.norm' imported but unused
Line 280:49: F811 redefinition of unused 'norm' from line 16

@josef-pkt

Copy link
Copy Markdown
Member Author

rebased, no merge conflict

no unit tests

@lgtm-com

lgtm-com Bot commented Feb 16, 2022

Copy link
Copy Markdown

This pull request introduces 17 alerts when merging 9e659dd into 8c67ccb - view on LGTM.com

new alerts:

  • 4 for Unused local variable
  • 4 for Unused import
  • 4 for Module is imported with 'import' and 'import from'
  • 3 for Variable defined multiple times
  • 1 for Unreachable code
  • 1 for 'import *' may pollute namespace

@josef-pkt josef-pkt mentioned this pull request Nov 29, 2022
6 tasks
@josef-pkt

Copy link
Copy Markdown
Member Author

Warning: robust norms allow for complex evaluation (mostly?) however the np.abs usage makes complex step derivatives incorrect.
#8801 (comment)

@josef-pkt

Copy link
Copy Markdown
Member Author

code does not include references
e.g. no mention of Dutter

Which is Huber-Dutter?
Is there something missing in this PR?

@josef-pkt

Copy link
Copy Markdown
Member Author

The models here look more interesting than I remember

e.g.
the GMM version uses joint moment conditions for mean and variance
That would be useful if we want to have robust standard errors without having a symmetric distribution #1379, i.e. scale and mean are not (asymptotically) orthogonal in distribution.

However, no references, and I don't find much in the pile of articles that I printed a long time ago.
I don't remember which references I used or if those models are "home made".
The implementation reusing existing models for sure is specific to statsmodels.

R robust base
nlrob has no references at al
https://search.r-project.org/CRAN/refmans/robustbase/html/nlrob.html
the algorithm page only references the standard articles for linear models, AFAICS
https://search.r-project.org/CRAN/refmans/robustbase/html/nlrob-algos.html

Huber, Ronchetti 2009 section 7.8 COMPUTATION OF REGRESSION M-ESTIMATES, page 175
uses nonlinear mean function, resid = y - f_i(theta)
The objective function in equ. (7.113) is Huber-Dutter
so maybe I used a book for this PR.

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.

@josef-pkt

Copy link
Copy Markdown
Member Author

AFAICS, the examples here do not look at inference
i.e. not clear whether cov_params, .... work and work correctly.

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.
Other parts might also be outdated, e.g. fixes in main for robust. Usage of GenericLikelihood has likely changed.

Eventually we want to have this as robust.nonlinear, but for merging it there it will likely need work.

What I would like right now is mainly something that uses scipy optimizer instead of irls and special iterative methods.
huber-dutter would be a good start, which should be proposal 2 (I guess) with same tuning parameter.

return self.loglikeobs(p)

import statsmodels.tools.numdiff as nd
grad_scale = nd.approx_fprime(np.atleast_1d(scale), func)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to be generalized to nonlinear fit

pass

def fit(self, start_params=None, start_scale=None, update_scale=True,
optim_options=None):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update to new scale options in RLM fit



class RLMIterative(rlm.RLM):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs nonlinear fit with start_params and maxiter

exog = self.exog
return exog

def fit_iterative(self, start_params=None, **kwds):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs maxiter if we do not want full convergence for each updated scale

@josef-pkt

Copy link
Copy Markdown
Member Author

with generic optimizer, loglike is negative of loss function
comment in jac
# Todo verify signs, derivative of loglike or nloglike ?
sign is irrelevant for moment conditions, but we need score_obs matching "loglike" (still missnomer outside MLE)

I stopped using nloglike for loss function in GenericLikelihoodModel.

@josef-pkt

Copy link
Copy Markdown
Member Author

There is a book for R
Riazoshams, Hossein, Habshah Midi, and Gebrenegus Ghilagaber. 2018. Robust Nonlinear Regression: With Application Using R. Hoboken: John Wiley & Sons.
(36 citations in google scholar)

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
https://cran.r-project.org/web/packages/nlr/index.html

@josef-pkt

Copy link
Copy Markdown
Member Author

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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is hardcoded for linear mean function

@josef-pkt josef-pkt left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

partial read through


#nobs = len(self.endog)
resid_scaled = (self.endog - np.dot(self.exog, params_loc)) / scale
scale_fac = 0.491

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this?

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))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, ...

@josef-pkt

Copy link
Copy Markdown
Member Author

loss function
rho(resid / scale) + scale_bias * log(scale)

derivatives
g = rho
a = scale_bias
r(b) is residual function with params b

d/db(g(r(b)/s) + a log(s)) = (r'(b) g'(r(b)/s))/s
d/ds(g(r(b)/s) + a log(s)) = (a s - r(b) g'(r(b)/s))/s^2 = a/s - (r(b) g'(r(b)/s))/s^2

d^2/db^2(g(r(b)/s) + a log(s)) = (r'(b)^2 g''(r(b)/s) + s r''(b) g'(r(b)/s))/s^2

d^2/ds^2(g(r(b)/s) + a log(s)) = (-a s^2 + r(b)^2 g''(r(b)/s) + 2 s r(b) g'(r(b)/s))/s^4 = 
   -a/s^2 + (r(b)^2 g''(r(b)/s))/s^4 + (2 r(b) g'(r(b)/s))/s^3

d^2/(db ds)(g(r(b)/s) + a log(s)) = -(r'(b) (r(b) g''(r(b)/s) + s g'(r(b)/s)))/s^3 =
    r'(b) (-(r(b) g''(r(b)/s))/s^3 - (g'(r(b)/s))/s^2)

from wolframalpha

and similarly for HD

@josef-pkt

josef-pkt commented Jun 7, 2024 •

Copy link
Copy Markdown
Member Author

trying out version rebased on main
example file generic mle runs without errors

start_params
It looks like example "exp" and "menton" use "good" starting params in example file
Sigmoid has a "self start" function sig_start(y, x) in robust_genericmle. sig_start does not take into account exog besides constant.

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)

image

exp example

image

@josef-pkt

Copy link
Copy Markdown
Member Author

name for mean function classes ? base class

PredictModel?
Predictor

I don't want mean_model because we use it model for submodels that are estimation Model classes (in multipart models, ZI,...)

@josef-pkt

josef-pkt commented Jun 7, 2024 •

Copy link
Copy Markdown
Member Author

example file try_robust_gmm:
version in this PR fails on both examples, first example with wrong length of xnames, second example with singular matrix linalg error

I locally have an updated file (maybe from after the previous rebase)
First example works.
Second example still has singular matrix linalg error during fit when computing
covparams = self.calc_cov_params(moms, gradmoms, **kwds)

omegahat looks fine but gradmoms has many (near-)zeros

ipdb> p np.linalg.inv(omegahat)
array([[ 5.69819599e+02,  1.46181645e+02, -4.46288432e+04],
       [ 1.46181645e+02,  1.90183795e+02, -4.39574465e+04],
       [-4.46288432e+04, -4.39574465e+04,  1.15332753e+07]])

ipdb> gradmoms
array([[ 0.00000000e+00,  0.00000000e+00, -1.17807062e-02],
       [-1.38777878e-13, -1.38777878e-13, -1.04169871e-01],
       [ 0.00000000e+00,  0.00000000e+00, -6.85749842e-04]])

something is wrong with robust GMM, at least in example

running variations, e.g. use ls params as start, increasing maxiter:
even when it converges, the predictions don't look good, most of the time worse than ls.
With maxiter > 1, it looks like it does not converge to any meaning full solution, e.g. converge to flat line or to exploding at one end (of x)

correction
meef_scale in _RobustGMM for biweight still had the bug workaround
# TODO: still the bug in TukeyBiweight + 0.16666667 in this branch
after dropping the 0.16666, I get good converging results with RobustGMM

eg (no seed set)
image

@josef-pkt

josef-pkt commented Jun 8, 2024 •

Copy link
Copy Markdown
Member Author

tuning parameters, scale bias

we need to compute them for HD scale moment condition.

MEstimatorHD has stats.norm.expect(lambda t: t*norm.psi(t) - norm.rho(t)) in comment for scale_bias.
However, it just uses the default norm and tuning parameter is not chosen for specific efficiency or breakdown point.
HD uses name scale_fac instead of scale_bias

RobustGMM uses M-scale
default is HuberT for mean moment condition and TukeyBiweight(1) for scale moment condition
stats.norm.expect(lambda x, *args: rnorms.TukeyBiweight(1).rho(x)) for scale_bias, and
meef_scale = lambda x: rnorms.TukeyBiweight(1).rho(x)-scale_bias for mom_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 meef_scale
"M Estimating Equations" ?

Also, meef_scale here includes the scale_bias

@josef-pkt

Copy link
Copy Markdown
Member Author

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).
But not in first round of models here.

@josef-pkt

Copy link
Copy Markdown
Member Author

The generic mle in this PR still has many

statsmodels\base\model.py:2754: UserWarning: df_resid differs from nobs - k_params
  warnings.warn("df_resid differs from nobs - k_params")

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:

  • base class for single objective function,
    • plus HD and mle type as subclasses (do we need fixed scale)
  • class for iterative with separate moment conditions, not clear: with and without inference for scale,
    • RLM mean inference with auxiliary scale (no joint cov_params, inference for mean-scale) (*)
    • joint inference mean, scale based on joint moment conditions, (essentially what current RobustGMM does)

(*) 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:
I guess I will (mostly) drop jac: we have score_obs or momcond for moment conditions, and will use deriv, deriv2 in nonlinear function classes.

Aside:
Optimization in statsmodels.base uses hardcoded "loglike" as objective function. That's still annoying (and a misnomer in RLM and GMM).
I might add an option that a model can specify a loss_function to be used in base.model optimizers.
score, score_obs and hessian ? Those could remain misnomers because they are mostly internal.
Current GMM uses momcond but also score, and gradient_momcond. momcond corresponds to score_obs.
Note: GMM has an objective function so hessian, second derivative of objective function should be symmetric. However, only gradient_momcond is used.
Current GMM has no optimizer in the fit methods that uses hessian information.

@josef-pkt

Copy link
Copy Markdown
Member Author

'ex1' example RobustGMM has negative scale estimate.
linear model, sign of scale should be irrelevant, but we should make it stay positive.

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.
Here we use linear (link) scale, so we get undetermined sign and possibly problems with scale=0.

@josef-pkt

Copy link
Copy Markdown
Member Author

(some design notes while I have internet)

  • stale model: e.g. fit and fit_iterative in HD have different number of params, scale_fixed needed in fit_iterative.
    • calling fit and fit_iterative on the same model will leave some attributes possibly for the wrong results instance
    • one possibility would be to put fit_iterative in a subclass, so each model can only do one case.
  • params with or without scale
    • predict, predict_jac only need params_loc and does not use scale, where do we trim off the scale.
    • results instance can have params without scale if scale_fixed was used, i.e. scale is auxiliary. Cov_params and inference only for mean params.
    • can we make it consistent?, e.g. add scale to params even if auxiliary, either with bse(scale)=zero or nan, or full cov_params using M/GMM estimator.
      Under symmetric distribution and symmetric norm, the mean parameter and scale are (asymptotically, in expectation) orthogonal). So we can ignore scale_bse, for inference for mean parameters (including predict)
      Other option: default to only have cov_params for mean parameters, i.e. drop scale from inference. If not we can explicitly make it block-diagonal.
  • Results class: GenericLikelihoodModelResults assumes MLE for some post_estimation results, e.g. aic, summary has method=mle, ...
    • we might need a more limited results class (BetaModel specifies the results class returned by super fit)
  • ...

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 __init__ so we need separate models for fixed_df and estimated df and we don't have a conflict estimating or fixing df parameter.

The HD class seems to work quite well, with single objective and with iterative auxiliary scale.
Main work is to figure out how to handle different options.

@josef-pkt

Copy link
Copy Markdown
Member Author

more pain

  • HD with redescending norm
    • has a global minimum with scale equal (close to) zero. In this case all observations are trimmed for mean estimation. e.g. using basinhopping only 2 or 3 observations have score_obs for mean parameters different from zero. The score for scale is nonzero and optimum does not satisfy the first order condition w.r.t. scale (corner solution).
    • bfgs converges to a local optimum with good scale estimate. Many of the other optimizers also converge to very small scale.
    • minimize with newton methods often fail with singular hessian.
    • Iterative estimation has no problems because it forces that FOC w.r.t. scale is satisfied, or we have mad as scale estimate.
    • a weakly redescending norm might also work without zero scale.
  • Mestimator class, I don't understand how I came up with the objective function, it works and for HuberT produces similar predictions to using HD with HuberT.
    (A simple rewrite of objective function did not work, i.e. either I use the current version or I switch to a different derivation of the objective function. current version uses a scale_fac and not a scale_bias correction.)
  • I might run out of time to finish this for the next release

@josef-pkt

Copy link
Copy Markdown
Member Author

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:

  • make RNLM (RobustNonlinearModel) class in analogy to RLM, with the same options for auxiliary scale estimation and scipy optimizers for mean function estimation for given scale.
  • then we could get resistant estimators (S, MM) in analogy to linear case
  • Huber-Dutter or likelihood/loss model can be separate and do only joint mean-scale estimation. The moment condition for mean parameters will be the same as in RNLM except for a scaling factor. (This should have clean loss functions and not rescaled as in current version)

The sandwich article for RLM recommends using joint inference for mean-scale so that standard errors are also valid for asymmetric distributions.
That could be done with joint estimation or stacking moment conditions as in GMM.

(I have one set of messy changes with unfinished refactoring that I will add in a "dirty" commit.)

@josef-pkt

Copy link
Copy Markdown
Member Author

this pr is the 2016 version
locally I was working on a "..._rebased" version, but did not open new, now PR #9431

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants