Repository navigation
WIP: Dimensions Mixins - #3725
jbrockmendel wants to merge 5 commits into
Conversation
Demonstrate mixing them in at the Model and Results levels. They will cause breakage until we remove all of the other places where these attributes are set
| @property | ||
| def wnobs(self): | ||
| if (self.freq_weights is not None) and (self.freq_weights.shape[0] == self.endog.shape[0]): | ||
| wnobs = bn.nansum(self.freq_weights) # self.freq_weights.sum() |
There was a problem hiding this comment.
use np.nansum, bottleneck is not a statsmodels dependency
the test failures on travis are uninformative about this PR, because they never get over importing this module
There was a problem hiding this comment.
I'll change that on the next pass, but test are still going to overwhelmingly fail. We'll get exceptions as soon as an affected class tries to set one of these attributes. Didn't bother deleting all those places since this is just for exposition.
|
|
||
|
|
||
| class Results(object): | ||
| class Results(dimensions.NEQsMixin, dimensions.NobsMixin): |
There was a problem hiding this comment.
the same mixin will not work for Model and Results
a large number of test failures, break early because results instances don't have endog
AttributeError: 'ARResults' object has no attribute 'endog'
There was a problem hiding this comment.
You're right. As mentioned elsewhere, for the purposes of keeping this example small I didn't port the necessary retro-fitting.
GenericLikelihoodModelResults attaches model.endog, model.exog, and sets nobs. LikelihoodModelResults does not. My preference would be to consistently set commonly-needed attributes in LikelihoodModelResults (or even Results), but there are any number of ways to approach this. Ideally I'd want to avoid pinning data-like objects to results.
The point is that these are all commonly-needed attributes and we should never have to guess whether or not they exist and/or have been initialized.
|
The main point here would be to see where, how much and which type of problems those attributes cause. However, there are still preliminary problems that are not directly related to the use of the attributes. Independently of the attribute question, we will not be using Mixins for this kind of things mainly because it makes it even more difficult to figure out where in the code some attributes or properties or methods are defined. When we use mixins it will be part of some larger modularized functionality. BTW: I'm starting to run out of time for general refactoring issues until August. I will be leaving for Europe in less than 3 weeks, and need to get some issues out of the way and PRs merged until then. |
Good call. I'm spending too much time here too. Glad we're making some headway though. Enjoy your trip. |
OK. Next time I need to procrastinate on real work I'll clean those out.
That said, I'm not wedded to the idea of lots of tiny mixin classes like in this PR. |
|
about inheritance chains: |
|
Just pushed new commit. Unlike the last one, this touches the code in a lot of places. I tried to make sure the diff excluded anything not directly pertinent to the discussion, but some things slipped through. A lot of the changes (my favorite ones) are just deleting places where My least favorite change is a kludge for demonstration purposes: attaching There are a whole bunch of optional edits that I chose to exclude to make for a briefer diff. Specifically, there are tons of places where e.g. Finally there are a handful of places where the expected logic fails. Those are marked with TODO or FIXME comments, generally to the effect of "TODO: self.exog.shape[1] should match self.k_exog here but does not." |
in `MNLogit.hessian`. Relates to statsmodels#3725. While not an actual bug, the current behavior is definitely misleading. The code currently defines `J` in a way that equals `self.J - 1`. The variable name is sufficiently distinctive that a reader (i.e. me an hour ago) can easily make the incorrect inference that `J == self.J`. This PR just fixes that.
Follow-up to discussion in #1664. This PR is for exposition purposes only, i.e. it will cause many test failures.
To hold this to a reasonable size, this includes dimension-related attributes and holds off on
df_residanddf_modelfor the time being.There are three Mixin classes that define a
nobsproperty, plus one that defines awnobsproperty. Between these they cover every case in the subset of code I've worked through to date*. In some cases applying them requires normalizing names as discussed in #3689. (e.g.sandbox.tsa.garchandtsa.arma_mleusenarandnmainstead ofk_arandk_ma)Formalizing
nobsavoids the need for kludges like settingnobsdynamically inVAR.fit,AR.fitorARMA.loglike_css. These examples are all special cases that don't quite fit into the class hierarchy; this helps bring them into line.Note that
VARResultsdoes not inherit from the base Results class, soNobsMixinneeds to be added in directly. Other than that mixing it in tomodel.Modelandmodel.Resultsis it._nobs_totalis based on a similar attributentotobsthat is defined forVARResults.KExogMixinandL1Estimatorshould be self-explanatory.NEQsMixinsetsneqs=1in many cases. There are a handful of places where that generalizes logic between univariate and multivariate cases (the most obvious cases being in information criteria).Note that
NEQsMixinalso definesk_endog, but with a note that I may need to go double-check the Multinomial case.KTrendMixinis largely self-explanatory. SARIMAX definesk_trendasint(np.sum(self.polynomial_trend));KTrendMixinhandles this case, but requires shuffling around some names inside SARIMAX.Finally
_get_k_constantis a preview for the next installment, which will discussdf_modelanddf_resid.* Directories not worked through are
duration,multivariate,nonparametric, and non-tsa portions ofsandbox.