Skip to content

ENH/REF: Scoretest betareg - #7907

Merged
josef-pkt merged 5 commits into
statsmodels:mainfrom
josef-pkt:scoretest_betareg
Nov 28, 2021
Merged

josef-pkt merged 5 commits into
statsmodels:mainfrom
josef-pkt:scoretest_betareg

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

starting to add score_test to multilink model

  • score_factor: return tuple, backwards incompatible but internal and newish
  • GenericLikelihoodModelResults BUG/ENH no cov_type attribute if nonrobust is not explicitly specified

score_test will be experimental, because I don't have a clear design about how to outsource the relevant multilink computations.
Also models with extra params (but not extra exog) like NBP or GPP will need a different solution than multi-exog models like BetaModel.

@josef-pkt

Copy link
Copy Markdown
Member Author

preliminary score_test works for BetaModel,
interface: exog_extra needs to be tuple instance in multi-darg case.
I also added optional r_matrix to score_test function, so robust cov works in version 3

res_restr restricted model, constant exog_precision
res_income full model, 1 slope exog_precision

aside:
pvalue in score test increases using HC0 compared to nonrobust,
pvalue in wald test decreased.
but small sample, nobs = 38, df_resid=33 in full model.

score_test(res_restr, exog_extra=(None, ex_prec[:, 1:]))
(array([7.86872382]), array([0.00502971]), 1)

score_test(res_income, params_constrained=params_restr, k_constraints=1)
(array([7.86872382]), array([0.00502971]), 1)

score_test(res_income, params_constrained=params_restr, k_constraints=1, r_matrix=r_matrix)
(array([7.86872382]), array([0.00502971]), 1)

score_test(res_restr, exog_extra=(None, ex_prec[:, 1:]), cov_type="HC0")
(6.156070581426283, 0.013096182264341212, 1)

score_test(res_income, params_constrained=params_restr, k_constraints=1, r_matrix=r_matrix, cov_type="HC0")
(6.156070581426283, 0.013096182264341212, 1)

@josef-pkt

Copy link
Copy Markdown
Member Author

discret test_discrete check_jac has general code for creating score, hessian from _factors

I'm converting multi-dimensional _factors to tuples #7827

    s1 = res1.model.score_obs(res1.params)
    sf = res1.model.score_factor(res1.params)
    if not isinstance(sf, tuple):
        s2 = sf[:, None] * exog
    else:
        sf0, sf1 = sf
        s2 = np.column_stack((sf0[:, None] * exog, sf1))

    assert_allclose(s2, s1, rtol=1e-10)

    # check hessian_factor
    h1 = res1.model.hessian(res1.params)
    hf = res1.model.hessian_factor(res1.params)
    if not isinstance(hf, tuple):
        h2 = (hf * exog.T).dot(exog)
    else:
        hf0, hf1, hf2 = hf
        h00 = (hf0 * exog.T).dot(exog)
        h10 = np.atleast_2d(hf1.T.dot(exog))
        h11 = np.atleast_2d(hf2.sum(0))
        h2 = np.vstack((np.column_stack((h00, h10.T)),
                        np.column_stack((h10, h11))))

@josef-pkt

Copy link
Copy Markdown
Member Author

I have the array versus scalar return problem in score_test

(Pdb) sc1
(array([7.86872382]), array([0.00502971]), 1)
(Pdb) sc2_hc
(6.156070581426283, 0.013096182264341212, 1)

@josef-pkt

Copy link
Copy Markdown
Member Author

I have the array versus scalar return problem in score_test

(Pdb) sc1
(array([7.86872382]), array([0.00502971]), 1)
(Pdb) sc2_hc
(6.156070581426283, 0.013096182264341212, 1)

@lgtm-com

lgtm-com Bot commented Nov 26, 2021

Copy link
Copy Markdown

This pull request introduces 2 alerts when merging 2746ada into e59be64 - view on LGTM.com

new alerts:

  • 2 for Unused local variable

score_obs = (score_factor[:, None] * ex)
else:
sf = score_factor
score_obs = np.column_stack((sf[:, :1] * ex, sf[:, 1:]))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this was not supposed to be in here.
left over from an initial try for NBP

it doesn't have the corresponding part for 2 element hessian_factor

@josef-pkt

josef-pkt commented Nov 27, 2021 •

Copy link
Copy Markdown
Member Author

I cannot make _scorehess_extra from BetaModel more generic without a way to generically figure out what the name of the extra exog_xxx in the model is.
Same problem with multi-link and Het models in new PR #7793

  • If I use a list model.exogs = [exog, exog_precision], then we get an extra reference and have to remove it in remove_data.
  • I could use string matching in _init_kwds.
  • I could follow the pattern of _init_kwds or get_init_kwds and create get_exogs or similar without having a new attribute.

I will need a convention, mainly also for #7793
I would like to avoid using only completely generic names in the individual models like exog, exog2, exog3. ...

I might start with _get_exogs method, with the option of changing the design later. That would be cheap, low maintenance, i.e. no extra reference to data, no attributes.
For models with extra params but no extra exog like NB, NBP, GPP we could use None, e.g. get_exogs returns (self.exog, None).
However, get_exogs still does not give me the attribute name of exog_xxx, which I made need for some use cases.
I avoid names when calling Betamodel,_scorehess_extra by using keyword arguments as positional (*args), which I don't really like because we usually don't guarantee keyword order.
Whatever I use needs to have a fixed order of exog_xxx, e.g. corresponding to score_factor; unordered dict is not enough.

I will need the exog_xxx names also if we use multi-part formulas, separated by | for the exog_xxx.

@pep8speaks

pep8speaks commented Nov 27, 2021 •

Copy link
Copy Markdown

Hello @josef-pkt! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-11-28 01:41:38 UTC

@josef-pkt

Copy link
Copy Markdown
Member Author

generic _scorehess_extra works for Beta, NBP and GPP, using new model._get_exogs method
(latest commit needs to be amended to clean up)

no new results.score_test methods yet. I'm using standalone function score_test(results, ....).

@josef-pkt

josef-pkt commented Nov 27, 2021 •

Copy link
Copy Markdown
Member Author

test failures are pep-8, two indentations
otherwise all green

maybe another unit test, cleanup and merge
I still have unit tests for _scorehess_extra in my notebook. Those might be helpful to identify future refactoring bugs. Currently only verified by "functional" tests through score_test.

next most likely MLEInfluence with numerical derivatives #7891

@josef-pkt

josef-pkt commented Nov 27, 2021 •

Copy link
Copy Markdown
Member Author

There's a problem with this branch or github.
Azure couldn't download the code.

amended and force pushed again

@josef-pkt
josef-pkt force-pushed the scoretest_betareg branch 2 times, most recently from 3ef1fa7 to 4247624 Compare November 28, 2021 01:41
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.

2 participants