Skip to content

REF: implement _get_start_params - #5205

Closed
jbrockmendel wants to merge 2 commits into
statsmodels:mainfrom
jbrockmendel:start_params3
Closed

jbrockmendel wants to merge 2 commits into
statsmodels:mainfrom
jbrockmendel:start_params3

Conversation

@jbrockmendel

Copy link
Copy Markdown
Contributor

count_model implements a _get_start_params method that gets called at the top of fit if start_params is None. This extends that pattern up to the base class. This is done in such a way that subclass authors can completely ignore this if they want, nothing has to be changed for subclasses.

The big benefits of this:

  • _get_start_params is a reasonable scope for a method, its purpose is completely intuitive
  • many classes that currently override fit will in the future be able to override just _get_start_params
  • tighter scopes make it easier to see what is actually necessary where (e.g. the offset variables copy/pasted into count_model)

if start_params is None:
offset = getattr(self, "offset", 0) + getattr(self, "exposure", 0)
if np.size(offset) == 1 and offset == 0:
offset = None

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.

Here and below these were apparently copy/pasted from discrete_model, are not actually necessary.

return result[0] if transform else result

def _get_start_params(self):
def _get_start_params(self, **kwargs):

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.

kwargs added to keep the signature consistent

a = ((resid**2 / mu - 1) * mu**(-q)).sum() / df_resid
return a

def _get_start_params(self, **kwargs):

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.

Really similar to the version above; in the future there may be room for de-duplication

@josef-pkt

Copy link
Copy Markdown
Member

can be reviewed and used during the next discrete or GenericLikelihood review and refactoring.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

Hmm the failing test is TestGeneralizedPoisson_p1.test_fit_regularized, and AFAICT the issue is in Poisson. If I revert the change in Poisson, the test passes. So far all I've git is "huh, this is weird..."

@jbrockmendel
jbrockmendel force-pushed the start_params3 branch 2 times, most recently from 695a97b to 31aa55a Compare May 9, 2019 03:22
@coveralls

coveralls commented May 9, 2019 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 88.401% when pulling 35cab45 on jbrockmendel:start_params3 into 52b142c on statsmodels:master.

Comment thread statsmodels/base/model.py
ValueError if start_params cannot be found.
"""
if hasattr(self, 'start_params'):
start_params = self.start_params

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.

This line exists in LikelihoodModel.fit ATM. If it is never hit, we should consider removing this usage.

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.

Is tt never hit in the existing tests, or unhittable? I agree with @ChadFulton that sv should be user settable, ideally.

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.

It can be hit by setting model.start_params = foo before calling model.fit(...), but this can also be accomplished by passing start_params=foo to model.fit.

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.

attributes or methods like this are or were for subclassing.
e.g. a subclass of GenericLikelihoodModel can specify appropriate start_params without having to overwrite the fit method.

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.

This side-note can be put on the back-burner. This PR leaves the logic unchanged, so GenericLikelihoodModel subclasses can keep doing whatever they want.

@ChadFulton

Copy link
Copy Markdown
Member

I'm not opposed to pulling out the starting parameters defaults, but I would argue that it's better to expose it as public rather than private. In state space, we just use a start_params property, which I think satisfies your improvement criteria and also allows for users to interrogate the parameters (I often do this).

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

but I would argue that it's better to expose it as public rather than private.

@ChadFulton You're suggesting making the _get_start_params method public? My preference for keeping it private for now is largely based on it being easier to make it public later than the other way around.

@josef-pkt

Copy link
Copy Markdown
Member

don't change this without agreement across models and maintainers

afaik, we never agreed on a signature or structure for this common across models

@codecov

codecov Bot commented May 9, 2019 •

Copy link
Copy Markdown

Codecov Report

Merging #5205 into master will decrease coverage by 2.01%.
The diff coverage is 94.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5205      +/-   ##
==========================================
- Coverage   86.07%   84.06%   -2.02%     
==========================================
  Files         698      613      -85     
  Lines      118309    97631   -20678     
  Branches    13131    10658    -2473     
==========================================
- Hits       101840    82069   -19771     
+ Misses      13707    13139     -568     
+ Partials     2762     2423     -339     
Impacted Files Coverage Δ
statsmodels/base/model.py 87.76% <71.42%> (-0.61%) ⬇️
statsmodels/discrete/discrete_model.py 88.39% <96.36%> (+0.09%) ⬆️
statsmodels/discrete/count_model.py 93.69% <100.00%> (-0.06%) ⬇️
statsmodels/stats/descriptivestats.py 23.68% <0.00%> (-63.45%) ⬇️
statsmodels/stats/base.py 52.94% <0.00%> (-14.41%) ⬇️
statsmodels/nonparametric/tests/test_kernels.py 89.13% <0.00%> (-10.87%) ⬇️
statsmodels/tools/docstring.py 71.12% <0.00%> (-8.30%) ⬇️
statsmodels/graphics/gofplots.py 83.95% <0.00%> (-7.64%) ⬇️
statsmodels/stats/tests/test_diagnostic.py 92.65% <0.00%> (-7.19%) ⬇️
statsmodels/stats/_lilliefors.py 91.13% <0.00%> (-6.23%) ⬇️
... and 377 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 52b142c...35cab45. Read the comment docs.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

@ChadFulton are you ok with the “private for now so we keep the option to make public later” approach?

@ChadFulton

Copy link
Copy Markdown
Member

I guess maybe that's okay, but I don't think this is quite the right part to excise from fit. I would only remove the following part from fit:

if self.exog is not None:
    # fails for shape (K,)?
    start_params = [0] * self.exog.shape[1]
else:  # pragma: no cover
    raise ValueError("If exog is None, then start_params should "
                     "be specified")

and I think that this part could be put in _get_start_params, although I still think it makes as much sense to use a property, as I mentioned earlier. It seems to me that this is the actual base "starting parameters" computation, and to me that's what could be abstracted out and made available for subclasses overriding. I think the other part with the hasattr check should remain in fit, since it's about the process of selecting starting parameters.

Also, I think we should not have arguments for getting the starting parameters (i.e. the method should not include **kwargs), unless those arguments can be provided by the user to fit.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

but I don't think this is quite the right part to excise from fit.

Can you expand on this? I'm not sure I understand. The difference between what you block-quoted and the base class implementation is just the check for hasattr(self, "start_params"). It might be easier for me to follow if you give an example of what you think one of the _get_start_params methods should look like for a DiscreteModel subclass, since those are the motivating cases.

although I still think it makes as much sense to use a property

The issue with this is that in DiscreteModel subclasses the logic for finding start_params depends on some of the kwargs passed to fit. (this also gets at your last paragraph)

could be abstracted out and made available for subclasses overriding

Is the availability for subclasses to override materially different for method vs property?

I think the other part with the hasattr check should remain in fit, since it's about the process of selecting starting parameters.

This seems backwards to me. I agree that the hasattr check is "about the process of selecting starting parameters", but that's exactly the scope of _get_start_params, so I don't see why you would want to exclude it.

Incidentally, I think I would be much happier if start_params = None were set in the base class and the check became if self.start_params is not None rather than a hasattr check. But that can be a topic for another day.

@jbrockmendel

Copy link
Copy Markdown
Contributor Author

@ChadFulton gentle ping; ok with private-for-now-revisit-later?

@lgtm-com

lgtm-com Bot commented Oct 15, 2020

Copy link
Copy Markdown

This pull request fixes 2 alerts when merging 35cab45 into 52b142c - view on LGTM.com

fixed alerts:

  • 2 for Unused local variable

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.

5 participants