Skip to content

ENH/Design: Mixin, Subclasses for censoring, truncation, .... #7922

Open

Description

@josef-pkt

#7638 general issue for censoring and selection
other issues for truncation

What class and/or mixin structure can we use to build models that are modifications like censoring and truncation in a modular way.

Eg. PenalizedMixin and MixedMixin essentially modify the existing loglike but don't need new distribution specific parts like cdf in truncated and censored models. Those mixins can reuse existing models.

In censored models we need additionally the conditional cdf and the indicators for the "type", i.e. whether and which censoring.
This need to modify loglike, score, ....

The closest we currently have to this are zero-inflated models, with separate sub-models for the two parts, and subclasses for distribution specific parts.

One possibility is to use the GLM pattern, one generic model class and delegation to dfamilies.
PR #7793: multilink models partially delegate to dfamilies, but not in API, i.e. needs subclasses.
dfamilies will have the advantage of being reusable, in contrast to current models like Poisson where everything is hard coded in the model.

Another possibility is to use subclasses of some CensoredBaseModel, TruncatedBaseModel. Subclasses can reuse existing models or dfamilies or scipy distributions depending on what's available.
e.g. models need to implement a cdf method, which we already have in a few models.

Mixin class would still need to add the loglike parts for the censored, observations.
Pure truncation (only values in specified interval are observed) would need to add the rescaling by cdf, i.e. doesn't need a mask.

Partially related: control function, instrumental variables for continuous endogenous regressor. his should also work generically for many MLE models.

And then we have combinations of modifiers, e.g. a censored mixed effect (unobserved heterogenity) models.

And count models, discrete random variables, need separate handling for cdf, sf because of masspoints (<= versus <, > versus >=)
(cdf(b) - cdf(a) is half open interval (a, b] )

Activity

  1. added this to the 0.14 milestone on Dec 2, 2021
  2. josef-pkt commented on Dec 22, 2021

    @josef-pkt
    MemberAuthor

    brief experiment for truncated Poisson

    We cannot have Poisson as super class, otherwise it will be difficult to avoid incorrect inheritance, i.e. all methods affected by truncation will need to be overwritten. In my experiment with including Poisson in super classes for multiple inheritance, fit didn't work for truncation because score and hessian were the inherited untruncated Poisson versions.
    (Maybe I also have to check PenalizedMixin again, as I keep adding new methods to the model class. Some of them might need the penalization override.)

    Delegation to an attached Poisson model similar to the ZI model works:

    The following seems to work correctly, parameter estimates look close to dgp (not sure yet about upper, right truncation).
    I inherit from GenericLikelihoodModel because LikelihoodModel, CountModel, ..., classes don't have fallback numdiff score and hessian. I should add those.

    missing piece: cdf evaluated at other endog. Poisson.cdf only allows evaluation at self.endog given linpred as arg.

    I think all of the Poisson.predict should apply. Truncation is only an estimation problem. Prediction extends to the truncated part. Same for get_prediction.
    If we can reuse model methods like predict, then it is better to delegate to a submodel than to a dfamily.
    I guess the results class can inherit from PoissonResults. (I'm not sure about get_diagnostic. We cannot test for zero-inflation in a zero-truncated sample)

    class TruncatedPoisson(GenericLikelihoodModel):
        
        def __init__(self, endog, exog=None, low=None, upp=None, model=None, **kwargs):
            
            kwargs.update({"low": low, "upp": upp})
            #self.low = low
            #self.upp = upp
            super().__init__(endog, exog=exog, **kwargs)
            self.model = model
            
            
        def cdf(self, linpred, endog=None):
            y = self.endog if endog is None else endog
            return stats.poisson.cdf(y, np.exp(linpred))
        
        def prob_trunc(self, linpred):
            return self.cdf(linpred, endog=self.upp) - self.cdf(linpred, endog=self.low-1)
            
        def loglikeobs(self, params):
            
            linpred = self.model.predict(params, which="linear")
            prob = self.prob_trunc(linpred)
            ll = self.model.loglikeobs(params) - np.log(prob)
            return ll
        
        def predict(self, params, exog, **kwds):
            return self.model.predict(params, exog, **kwds)
    

    aside: because of broadcasting, low and upp can be scalar or array.

    #3802 old GSOC PR for truncated, censored and hurdle count models
    Reviewing and refactoring that will be quite a bit or work.
    It looks like I never created my own clone of that PR.

    brief look:
    I'm defining low, upp truncation points by closed interval of no truncation, e.g. zero truncation has low, upp = 1, inf
    I'm assuming only endog that are in the no-truncation range are in the sample.
    The PR version seems to select observations in no-truncation range.
    e.g. for hurdle model we have the full untruncated sample, but we intentionally look only at the truncated y>0 subsample.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions