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

Implementing original qp PDF constructor #150

Merged
merged 17 commits into from
Jan 27, 2023

Conversation

drewoldag
Copy link
Collaborator

Modified the quantile distribution class to allow users to select from a variety of pdf reconstructors to best support their use case. The pdf construction algorithms include the original qp cdf spline derivative, a new average-of-splines approach, piecewise linear, and piecewise constant.

This branch also includes a new demo notebook with examples of how the quantile distribution class works, how to change pdf constructors, and includes a variety of example input distributions that are approximated by quantile distribution, and reconstructed with each of the different pdf construction algorithms.

@drewoldag drewoldag self-assigned this Jan 25, 2023
@drewoldag
Copy link
Collaborator Author

When I run unit tests locally I see the following tests failing:

FAILED tests/qp/test_auto.py::PDFTestCase::test_convert_quant_0 - ValueError: convert -1.91e+00 9.50e-01 {'atol': 0.39999999999999997}
FAILED tests/qp/test_auto.py::PDFTestCase::test_convert_quant_1 - ValueError: convert -1.91e+00 9.50e-01 {'atol': 0.39999999999999997}
FAILED tests/qp/test_auto.py::PDFTestCase::test_pdf_quant - ValueError: cdf -1.47e-02 8.88e-01 {'atol': 0.2}

@codecov
Copy link

codecov bot commented Jan 25, 2023

Codecov Report

Base: 100.00% // Head: 100.00% // No change to project coverage 👍

Coverage data is based on head (3c4f283) compared to base (0a5d77d).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #150   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        31    +6     
  Lines         2047      2117   +70     
=========================================
+ Hits          2047      2117   +70     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

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

Impacted Files Coverage Δ
src/qp/metrics/pit.py 100.00% <100.00%> (ø)
src/qp/quant_pdf.py 100.00% <100.00%> (ø)
src/qp/quantile_pdf_constructors/__init__.py 100.00% <100.00%> (ø)
...ntile_pdf_constructors/abstract_pdf_constructor.py 100.00% <100.00%> (ø)
...quantile_pdf_constructors/cdf_spline_derivative.py 100.00% <100.00%> (ø)
...p/quantile_pdf_constructors/dual_spline_average.py 100.00% <100.00%> (ø)
...qp/quantile_pdf_constructors/piecewise_constant.py 100.00% <100.00%> (ø)
...c/qp/quantile_pdf_constructors/piecewise_linear.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@drewoldag drewoldag marked this pull request as ready for review January 25, 2023 22:13



class quant_piecewise_gen(Pdf_rows_gen):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Completely removed this class because it is now superfluous. It was originally created so that an alternative pdf reconstruction algorithm could be implemented. Now that the pdf reconstruction algorithms are separated into their own classes and selectable at run time, we no longer need this class.

@@ -64,6 +72,13 @@ def pad_quantiles(quants, locs):

return quants_out, locs_out

DEFAULT_PDF_CONSTRUCTOR = 'piecewise_linear'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Selecting this PDF constructor as the default for now, only because there are unit tests with hard coded quant_gen.pdf() output values that will fail if we use a different constructor.

It would be preferable to use cdf_spline_derivative, however, I would like to defer that work to be done by (or in collaboration with) a researcher.

@@ -136,12 +144,63 @@ def locs(self):
"""Return the locations at which those quantiles are reached"""
return self._locs

@property
def pdf_constructor_name(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This and the following three new methods are the machinery that allow the user to dynamically select pdf constructors at run time.

@drewoldag drewoldag linked an issue Jan 25, 2023 that may be closed by this pull request
src/qp/quant_pdf.py Outdated Show resolved Hide resolved
# ! create an issue (or fix) if the spline fit fails, can fall back to a simpler interpolator ???
self._interpolation_functions = [
InterpolatedUnivariateSpline(self._locations[i,:], self._quantiles, k=spline_order, ext=1).derivative()
for i in range(0,number_of_locations)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this line do anything?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, while it doesn't look like it, it does. Lines 51-54 represent a list comprehension spread out over several lines to try to make it more readable.

Usually list comprehension is a single line like:
output = [do_thing(i) for i in list_of_i_values]

But here, do_thing(i) is a long line, and I felt like it was more readable if it was split up.

tests/qp/test_ensemble.py Show resolved Hide resolved
Copy link
Collaborator

@delucchi-cmu delucchi-cmu left a comment

Choose a reason for hiding this comment

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

Someone else should probably check the math, but the structure looks good!

@sschmidt23 sschmidt23 self-requested a review January 27, 2023 22:08
Copy link
Collaborator

@sschmidt23 sschmidt23 left a comment

Choose a reason for hiding this comment

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

I think things look good, two minor points, though:
If you want to keep the temp_quantile_parameterization_under_the_curve_impl.ipynb notebook, maybe rename without the "temp" in the notebook name.

In demo.ipynb it looks like there's a problem with the pdf/cdf/ppf/etc... when the demo creates a single qp.quant_gen distribution with a single set of quants and locs rather than an ensemble. Calling .pdf .ppf .cdf throws a

TypeError: _parse_args() missing 1 required positional argument: 'row'

error from scipy, i.e. there is no row argument from a single distribution rather than an ensemble ( a qp.quant Ensemble with a single distribution works fine, though). We should fix this or update the demo notebook so that it executes.

@sschmidt23 sschmidt23 self-requested a review January 27, 2023 22:59
Copy link
Collaborator

@sschmidt23 sschmidt23 left a comment

Choose a reason for hiding this comment

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

Looks good, thanks Drew!

@sschmidt23 sschmidt23 merged commit be6954a into main Jan 27, 2023
@sschmidt23 sschmidt23 deleted the issue/8/original-qp-exploration branch January 27, 2023 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Piecewise non-constant quantile implementation
4 participants