Skip to content

Demonstrate idea for _results_class property - #3877

Closed
jbrockmendel wants to merge 1 commit into
statsmodels:masterfrom
jbrockmendel:constructor
Closed

jbrockmendel wants to merge 1 commit into
statsmodels:masterfrom
jbrockmendel:constructor

Conversation

@jbrockmendel

Copy link
Copy Markdown
Contributor

This is not intended as a real PR, but a demonstration of an idea to discuss.

It would be helpful to be able to associate a Model subclass with the appropriate Results and ResultsWrapper subclasses. (In some cases there are also L1 variants of these classes, presumably a long tail of other cases I'm forgetting). The obvious option is not viable:

class SomeModel(LikelihoodModel):
    _results_cls = (SomeModelResults, SomeModelResultsWrapper)

because in most cases SomeModelResults is not yet defined. We could change that by shifting stuff around, but the option I'm leaning towards is based on pandas _constructor property:

class SomeModel(LikelihoodModel):
    @property
    def _results_class(self):
        return (SomeModelResults, SomeModelResultsWrapper)        

The upshot of this is that we can unify a bunch of wrapping logic and move things up the class hierarchy. I did a quick pass at demonstrating this for MLEModel and DiscreteModel for this PR.

#noci

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

No apparent interest. Closing.

@jbrockmendel
jbrockmendel deleted the constructor branch October 5, 2017 23:10
@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Looks like something similar was implemented in ZeroInflatedNegativeBinomialP.__init__ as self.result_class, self.result_class_wrapper, self.result_class_reg, self.result_class_reg_wrapper. So there may be some traction after all. If this were implemented as a property further upstream e.g. in DiscreteModel then bit by bit we could generalize+remove a ton of duplicate code.

@josef-pkt

Copy link
Copy Markdown
Member

I thought we had used this somewhere before but my github search comes up empty, i.e. I don't remember where this came from and there should (?) be an issue open
#3755 (comment)

two benefits

  • we can drop redundant fit that only choose the correct results class and wrap
  • we could create at the top level of the fit hierarchy the correct results class already (instead of handing it down via mlefit)

to the first: This only works if the individual fit methods can be kept empty. In Zero Inflated we added a _get_start_params method to make it model specific without needing a model specific fit. This would have to be added higher up when we want to improve start_params in more models. Also, it will only work for similar models.

to the second: right now the result creation in fit via mlefit works from base to the final model. If base.LikelihoodModel already creates the correct results class, then we skip the top to bottom sequence. I guess that shouldn't be a problem, but needs to be tried out and checked.
(possible side effect: We currently jump in super(...).fit over some classes. It might be possible to change this, but I don't remember bow if there is any serious reason and we just want to avoid a redundant step.)

In any case, it might not be possible to mix the two result_class and mlefit propagation in the same hierarchy. For example GenericZeroInflated has the final fit. None of the subclasses define fit, which avoids any ambiguity with mlefit versus result_class.

So my guess is that it is still tricky to go much higher up with this in the hierarchy, even though I think it would make the fit methods overall better structured.

BinaryModel would be a similar candidate as ZeroInflated that should be relatively straight forward, except I just opened an issue why MultinomialModel subclasses BinaryModel which AFAICS doesn't add anything and makes the code or class hierarchy more complicated than needed.

(For other cases like fit_constrained we have the additional problem of needing to retrofit the results instance. I the full results instance is created higher up in the hierarchy, then it will be more difficult to add additional keywords to results.__init__, most likely, maybe not.)

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Today has been exhausting. I'm going to learn to love duplicate code, embrace FUD.

@josef-pkt

Copy link
Copy Markdown
Member

(to end an exhausting day)

One item I remembered and agree with:
in section "how to develop a <...> codebase that can be scaled indefinitely without succumbing to complexity":
"Favoring verbose but simple code over compact but overly clever code (this means not just avoiding stupid code golf tricks, but also, for example, preferring some degree of code duplication if it reduces cyclomatic complexity)"
http://fredrikj.net/blog/2017/10/mpmath-1-0-and-a-ten-year-retrospective/
(or replace cyclomatic complexity by other code readability criteria)

and one of the Zen's
"Although practicality beats purity."
but the previous one "Special cases aren't special enough to break the rules."
might cause problems if we don't know what the rules are supposed to be.

(It's all trade-offs, based on pretty diffuse priors with only partially identified likelihoods :)
(maybe cite ambiguity aversion)

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Special cases aren't special enough to break the rules

Holy spin-kicking jehosaphat. [deep breath]. [count to 100]. I have not been advocating breaking any [expletive deleted] rules.

Suggesting that we should fix SyntaxErrors before users report them is not about "purity".

Suggesting that NameErrors waiting to happen might worth addressing is not "overly clever".

The onboarding cost for a potential contributor is positively related to the size of the codebase. The cognitive burden for existing contributors/maintainers is positively related to the size of the codebase. Suggesting that there is a cost to keeping around code that can do nothing but raise RuntimeErrors, that no one uses and never will, that are pure clutter[...] and offering to fix these things is not a "stupid code golf trick", and may His Noodly Appendage strike down anyone who says otherwise.

Not long ago I found myself trying to track down the difference between sandbox.utils_old.fullrank vs tools.tools.fullrank. Figuring that out required figuring out the difference between np_matrix_rank and utils_old.rank. Then tracking down scipy.linalg.svdvals, and it turns out they are only different when the input is a 2-d array of size 0. Both will raise, but with different errors. Don't try to tell me that code duplication reduces complexity.

You've won. You've broken my spirit. But please don't frakkin high-road me.

@josef-pkt

Copy link
Copy Markdown
Member

My last comment was about our code, not about you. Sorry, if I didn't say this well enough.

Our code is breaking some "programming rules", either because we have a locally good solution, but nor enough global consistency yet, or because we don't know yet what the global rules/patterns are supposed to be, or because breaking programming rules in our code is "practical".

(The overall size of the code base is irrelevant as long as we can concentrate on the parts that are relevant for specific developments.)

@josef-pkt

Copy link
Copy Markdown
Member

(Overall size does matter however. I proposed topic weeks to reduce my "cognitive overload" from having too many issues on too many different topics that need more than superficial attention.)

@bashtage bashtage added the rejected Used for PRs with changes that are not acceptable label Jul 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

rejected Used for PRs with changes that are not acceptable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants