[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose fitted parameter values of implicit fits in test statistic calls #1554

Merged
merged 40 commits into from
Oct 13, 2021

Conversation

lhenkelm
Copy link
Contributor
@lhenkelm lhenkelm commented Aug 19, 2021

Description

Resolves #1550.

Functions in pyhf.infer that call pyhf.infer.mle.fit or pyhf.infer.mle.fixed_poi_fit (except pyhf.infer.upperlimit) return the best-fit parameters if they recieve return_fitted_pars=True as a kwarg. The pyhf.infer.calculators.AymptoticCalculator has an attribute holding the best-fit parameters of the most recent five fits.

Checklist Before Requesting Reviewer

  • Tests are passing
  • "WIP" removed from the title of the pull request
  • Selected an Assignee for the PR to be responsible for the log summary

Before Merging

For the PR Assignees:

  • Summarize commit messages into a comprehensive review of the PR
* Add `return_calculator` kwarg to `pyhf.infer.hypotest`
* Add `return_fitted_pars` kwarg to `pyhf.infer.calculators.generate_asimov_data`
* Add `return_fitted_pars` kwarg to test statistic functions in `pyhf.infer.test_statistics`
* Add `fitted_pars` property to `pyhf.infer.calculators.AsymptoticCalculator`
* Add tests for return_fitted_pars options
* Add Lars Henkelmann to contributor list

@lhenkelm lhenkelm marked this pull request as draft August 19, 2021 17:36
@matthewfeickert matthewfeickert changed the title WIP: expose fitted parameter values of implicit fits in test statistic calls feat: Expose fitted parameter values of implicit fits in test statistic calls Aug 19, 2021
@matthewfeickert matthewfeickert added the feat/enhancement New feature or request label Aug 19, 2021
@matthewfeickert
Copy link
Member
matthewfeickert commented Aug 20, 2021

@lhenkelm while you're working on this can you add some commentary as well? It isn't really clear why all of what is in here so far is strictly needed.

@lhenkelm
Copy link
Contributor Author

@matthewfeickert the changes are those needed to get access to the ML estimates of the parameters in a call to pyhf.infer.hypotest. The calls stack up like so:
hypotest calls AsymptoticCalculator.teststatistic calls generate_asimov_data and (e.g.) qmu_tilde:

  • So it is necessary that generate_asimov_data and all the test statistics are able to return fitted params. (04b0c3b, a2f1c86)
  • These params then need to be handed back up through the calculator, which is what the new AsymptoticCalculator.fitted_pars is for (e804185).
  • Then finally hypotest needs the option to return the parameters as well (84a8af8).

So the changes to the implementation code are minimal. The bulk of the new lines are tests and docstrings,
which IMO are necessary because all the changes are in the user-visible behavior of documented parts of the API.

I tried to keep the new tests small/generic enough so that they are robust against new changes while covering all of the new behavior. But you guys have the experience of what parts change a lot and how, so let me know if the tests are overbearing!

For the docstrings I tried to be super explicit about describing how the return values change with new option and give plenty of examples for people that find those more useful. So in that sense I think these are necessary changes, but if you think it makes the docstrings too bloated, please tell me!

@lhenkelm lhenkelm marked this pull request as ready for review August 25, 2021 16:11
@lhenkelm
Copy link
Contributor Author

regarding the tests: they all pass locally, except one unrelated test in test_notebooks.py that also fails when I run the test suite on master.

@lukasheinrich
Copy link
Contributor
lukasheinrich commented Aug 25, 2021

thanks @lhenkelm ! on first glance this looks good, I'll do a more detailed review asap

I wonder whether at some point it makes sense to use data classes + strutural pattern matching (PEP 622 ) to replace tuple returns

@lukasheinrich
Copy link
Contributor
lukasheinrich commented Aug 31, 2021

One thing that we should discuss is whether we can somehow unify the "data classes" with cabinetry

i.e. BestFitParams (or TestStatisticFitResults see my comment above), OptimizerResult,cabinetry's FitResult etc cc @alexander-held

@alexander-held
Copy link
Member

I think it would be great to harmonize where possible. Being able to do a infer.mle.fit fit and plug the result straight into a cabinetry visualization (instead of having to use the cabinetry.fit API, or having to pack up the fit result into the object cabinetry expects) seems very useful. I would be happy to try and find a common solution to the packaging of the return values.

tests/test_infer.py Show resolved Hide resolved
src/pyhf/infer/calculators.py Outdated Show resolved Hide resolved
src/pyhf/infer/calculators.py Show resolved Hide resolved
  * no asserts about return values other than the calculator
  * collapse the separate tests into one parametrized test
instead of raising when accessed before self..teststatistic is
called, initialise to `None`
@codecov
Copy link
codecov bot commented Oct 12, 2021

Codecov Report

Merging #1554 (7f69d53) into master (9358f85) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1554      +/-   ##
==========================================
+ Coverage   97.70%   97.71%   +0.01%     
==========================================
  Files          63       63              
  Lines        4048     4067      +19     
  Branches      576      581       +5     
==========================================
+ Hits         3955     3974      +19     
  Misses         54       54              
  Partials       39       39              
Flag Coverage Δ
contrib 25.52% <41.66%> (+0.07%) ⬆️
unittests 97.49% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/pyhf/infer/__init__.py 100.00% <100.00%> (ø)
src/pyhf/infer/calculators.py 100.00% <100.00%> (ø)
src/pyhf/infer/test_statistics.py 100.00% <100.00%> (ø)

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 9358f85...7f69d53. Read the comment docs.

@kratsg
Copy link
Contributor
kratsg commented Oct 12, 2021

I appreciate the 100% coverage here (thanks a lot!). We'll merge this in once we get the all clear.

@kratsg kratsg merged commit b5bb9e3 into scikit-hep:master Oct 13, 2021
@matthewfeickert
Copy link
Member

Thanks for the PR @lhenkelm!

@matthewfeickert matthewfeickert added API Changes the public API tests pytest labels Oct 13, 2021
Comment on lines +52 to +59
It is possible to access the Asimov parameters as well:

>>> pyhf.infer.calculators.generate_asimov_data(
... mu_test, data, model, None, None, None,
... return_fitted_pars = True
... )
(array([ 60.61229858, 56.52802479, 270.06832542, 48.31545488]), array([1. , 0.97224597, 0.87553894]))

Copy link
Member

Choose a reason for hiding this comment

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

Examples should be contiguous else they break the copy button usefulness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Changes the public API feat/enhancement New feature or request tests pytest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

expose fitted parameter values of implicit fits in test statistic calls
5 participants