Skip to content

WIP: Dimensions Mixins - #3725

Closed
jbrockmendel wants to merge 5 commits into
statsmodels:masterfrom
jbrockmendel:dimensions
Closed

jbrockmendel wants to merge 5 commits into
statsmodels:masterfrom
jbrockmendel:dimensions

Conversation

@jbrockmendel

Copy link
Copy Markdown
Contributor

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 ondf_resid and df_model for the time being.

There are three Mixin classes that define a nobs property, plus one that defines a wnobs property. 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.garch and tsa.arma_mle use nar and nma instead of k_ar and k_ma)

Formalizing nobs avoids the need for kludges like setting nobs dynamically in VAR.fit, AR.fit or ARMA.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 VARResults does not inherit from the base Results class, so NobsMixin needs to be added in directly. Other than that mixing it in to model.Model and model.Results is it.

_nobs_total is based on a similar attribute ntotobs that is defined for VARResults.

KExogMixin and L1Estimator should be self-explanatory. NEQsMixin sets neqs=1 in 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 NEQsMixin also defines k_endog, but with a note that I may need to go double-check the Multinomial case.

KTrendMixin is largely self-explanatory. SARIMAX defines k_trend as int(np.sum(self.polynomial_trend)); KTrendMixin handles this case, but requires shuffling around some names inside SARIMAX.

Finally _get_k_constant is a preview for the next installment, which will discuss df_model and df_resid.

* Directories not worked through are duration, multivariate, nonparametric, and non-tsa portions of sandbox.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread statsmodels/base/model.py


class Results(object):
class Results(dimensions.NEQsMixin, dimensions.NobsMixin):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@josef-pkt

Copy link
Copy Markdown
Member

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.
It can still be a useful way of experimenting to see the consequences of the attributes and at least partially making things more generic and consistent.

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.
If you would like to work on parts that require less discussion about the core of statsmodels, then there are older PRs like for example Tobit that wait for adoption. (Tobit will need Stata usage to get the unit test numbers.)

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

BTW: I'm starting to run out of time for general refactoring issues until August.

Good call. I'm spending too much time here too. Glad we're making some headway though. Enjoy your trip.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

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.

OK. Next time I need to procrastinate on real work I'll clean those out.

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.

VARMAX.__init__ makes a super(...).__init__ call that goes into MLEModel.__init__, from there to TimeSeriesModel.__init__, then LikelihoodModel.__init__, and finally Model.__init__. Same thing for VARMAXResults. Admittedly this is an extreme example, but figuring out where an attribute is defined is decidedly not simple under the status quo.

That said, I'm not wedded to the idea of lots of tiny mixin classes like in this PR.

@josef-pkt

Copy link
Copy Markdown
Member

about inheritance chains:
It took me a long time to figure out which class on which level of the hierarchy defines a method or an attribute. But, at least we are using so far only a straight chain instead of using a tree or diamonds or ....
Also, there is quite a bit of logic behind this in going from most general and generic case to the specific models.
(plus using Eclipse helps me a lot in jumping around.)

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

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 self.nobs, self.neqs, self.k_endog... are set.

My least favorite change is a kludge for demonstration purposes: attaching endog and exog to Results objects. That can be reverted in a more polished version. For now it ensures that we can use the same Mixin classes for Model and Result classes.

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. self.endog.shape[0] is used that could be replaced with self.nobs.

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

jbrockmendel added a commit to jbrockmendel/statsmodels that referenced this pull request Jun 23, 2017
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.
@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