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

Add icdf functions for Beta, Gamma, Chisquared and StudentT distributions #6845

Merged
merged 2 commits into from
Mar 25, 2024

Conversation

amyoshino
Copy link
Member

@amyoshino amyoshino commented Aug 4, 2023

What is this PR about?
Adds ICDF functions to Beta, Gamma, Chisquared and StudentT distributions.

Issue #6612

This is a work in progress. The quantile functions depend on two main functions:

  • Inverse of the regularized incomplete beta function: betaincinv
  • Inverse to the regularized lower incomplete gamma function: gammaincinv

Those are implemented in SciPy but not in Pytensor yet. When importing the two functions from scipy.special and plugging them in the desired formula, I get a TypeError like this below:

"TypeError: ufunc 'betaincinv' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''"

image

It looks like the most straightforward way to make these icdf functions work is to implement them in Pytensor, in the same way the pt.betainc has been implemented

Any comments about if this is the right path? If so I am happy to open an issue in Pytensor as a feature request, and possibly helping on its development if nobody else wants to take it. But if there is any ideas on how to make it simpler than that, let me know.

References:

Checklist

Major / Breaking Changes

  • ...

New features

  • ...

Bugfixes

  • ...

Documentation

  • ...

Maintenance

  • ...

📚 Documentation preview 📚: https://pymc--6845.org.readthedocs.build/en/6845/

@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.97%. Comparing base (a033261) to head (262d868).

❗ Current head 262d868 differs from pull request most recent head 00c1f34. Consider uploading reports for the commit 00c1f34 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6845      +/-   ##
==========================================
- Coverage   92.29%   90.97%   -1.33%     
==========================================
  Files         100      100              
  Lines       16875    16887      +12     
==========================================
- Hits        15575    15363     -212     
- Misses       1300     1524     +224     
Files Coverage Δ
pymc/distributions/continuous.py 97.81% <100.00%> (+0.02%) ⬆️

... and 14 files with indirect coverage changes

@amyoshino
Copy link
Member Author

amyoshino commented Jan 7, 2024

@ricardoV94 now that pymc-devs/pytensor#502 is merged, we are able to complete this PR.

The tests are failing because betaincinv and gammaincinv cannot be imported from pytensor.tensor.math, I guess it is matter of time to pytensor to be updated. I will restart the checks when it happens. Locally all tests are passing.

Let me know what you think.

@amyoshino amyoshino changed the title WIP: Add icdf functions for Beta, Gamma, Chisquared and StudentT distributions Add icdf functions for Beta, Gamma, Chisquared and StudentT distributions Jan 7, 2024
@amyoshino amyoshino marked this pull request as ready for review January 7, 2024 19:02
@ricardoV94
Copy link
Member

You can see which PyTensor version is being installed in the CI.

pymc/distributions/continuous.py Outdated Show resolved Hide resolved
Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

Couple more comments. Looks great otherwise

pymc/distributions/continuous.py Outdated Show resolved Hide resolved
pymc/distributions/continuous.py Outdated Show resolved Hide resolved
pymc/distributions/continuous.py Outdated Show resolved Hide resolved
tests/distributions/test_continuous.py Outdated Show resolved Hide resolved
tests/distributions/test_continuous.py Outdated Show resolved Hide resolved
@ricardoV94
Copy link
Member

Yeah it's strange it's still installing 2.18.4, even though 2.18.5 was released already some days ago. We may need to be just a bit more patient?

@amyoshino
Copy link
Member Author

Yeah it's strange it's still installing 2.18.4, even though 2.18.5 was released already some days ago. We may need to be just a bit more patient?

Right! That's what I thought, I saw the pytenor release 4 days ago and thought it was a good experience to try it now and see if the update immediately applies. No rush on it, it will work soon.

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@amyoshino amyoshino marked this pull request as draft January 8, 2024 12:27
@amyoshino amyoshino force-pushed the icdf_gamma_beta branch 5 times, most recently from c1d3d35 to babc479 Compare January 9, 2024 02:00
@amyoshino amyoshino marked this pull request as ready for review January 9, 2024 02:42
@ricardoV94
Copy link
Member

@maresb any idea why CI is not yet installing the last PyTensor? Was there an issue on the conda side?

@maresb
Copy link
Contributor

maresb commented Jan 9, 2024

There was a glitch in the deployment of 2.18.5 to anaconda.org.

I reran it, so hopefully it fixes things.

@maresb maresb closed this Jan 9, 2024
@maresb maresb reopened this Jan 9, 2024
@maresb
Copy link
Contributor

maresb commented Jan 9, 2024

It uploaded successfully on anaconda.org, so I restarted the tests.

@amyoshino
Copy link
Member Author

Interesting way to restart the tests! I searched for and the recommendation I found was to push an empty commit, but I think this is nicer.

@ricardoV94
Copy link
Member

ricardoV94 commented Jan 9, 2024

You can also just ask a dev with write permissions. We can restart them from GitHub interface

@ricardoV94
Copy link
Member

ricardoV94 commented Jan 29, 2024

Is the nan coming from pm.logcdf(pm.Gamma.dist(alpha=2, beta=5), np.inf)?

@ricardoV94
Copy link
Member

@amyoshino
Copy link
Member Author

pm.logcdf(pm.Gamma.dist(alpha=2, beta=5), np.inf)

Indeed, this code returns nan. I tried to fix it writing the special case for np.inf in the gamma.c function but it didn't work out yet. Maybe there are other places I need to change in order to make it work.

`DEVICE double GammaP (double n, double x)
{ /* --- regularized Gamma function P /
if ((n <= 0) || (x < 0)) return NPY_NAN; /
check the function arguments /
if (x <= 0) return 0; /
treat x = 0 as a special case */
if (x < n+1) return _series(n, x) *exp(n *log(x) -x -logGamma(n));
if (isinf(n)) {
if (isinf(x)) return NPY_NAN;
return 0;
}
if (isinf(x)) return 1;
return 1 -_cfrac(n, x) *exp(n log(x) -x -logGamma(n));
} /
GammaP() */

/--------------------------------------------------------------------/

DEVICE double GammaQ (double n, double x)
{ /* --- regularized Gamma function Q /
if ((n <= 0) || (x < 0)) return NPY_NAN; /
check the function arguments /
if (x <= 0) return 1; /
treat x = 0 as a special case */
if (x < n+1) return 1 -_series(n, x) *exp(n *log(x) -x -logGamma(n));
if (isinf(n)) {
if (isinf(x)) return NPY_NAN;
return 1;
}
if (isinf(x)) return 0;
return _cfrac(n, x) *exp(n log(x) -x -logGamma(n));
} /
GammaQ() */`

Even with this, the pm.logcdf(pm.Gamma.dist(alpha=2, beta=5), np.inf) still returns nan.

@ricardoV94
Copy link
Member

You need to delete pytensor cache or bump the Op c_cache_version number. Try pytensor-cache clear or pytensor cache-clear I never remember. If clear doesn't work use purge

@amyoshino
Copy link
Member Author

Thanks for the pointers! You were on point in all suggestions, the logcdf problem and the solution to it.

After changing the gamma.c function and pytensor-cache clear and pytensor-cache purge (I needed that too), the Truncated icdf worked as expected and all the text_mixture.py tests pass.

I will open an issue and a PR in pytensor to change the gamma.c code.

@ricardoV94
Copy link
Member

ricardoV94 commented Feb 5, 2024

That PR should probably do a similar fix for the complementary gammainc function (gammaincc?)

Edit: I see you were already onto it

@ricardoV94
Copy link
Member

@amyoshino PyTensor new release should be in conda soon. We will need to update the pin on PyTensor version here, as it was a "major" release (or in a separate PR if there are other things that need fix)

https://github.com/pymc-devs/pytensor/releases/tag/rel-2.19.0

@amyoshino
Copy link
Member Author

@amyoshino PyTensor new release should be in conda soon. We will need to update the pin on PyTensor version here, as it was a "major" release (or in a separate PR if there are other things that need fix)

https://github.com/pymc-devs/pytensor/releases/tag/rel-2.19.0

That's awesome!! Looking forward to it to be updated!
Let me know if I need to do anything with respect to this PR as part of the process.

@ricardoV94
Copy link
Member

You need to bump the PyTensor dependency constraints on the PyMC side

@amyoshino
Copy link
Member Author

Ah, I see! Alright, let me try that then! Thanks for the pointer.

@amyoshino
Copy link
Member Author

Just pushed the bump dependency modifications in here. Seems like there is one test failing though, which wasn't failing when I ran the test suite locally.

@ricardoV94
Copy link
Member

@amyoshino I'm taking core of the Pytensor bump in #7203

@ricardoV94
Copy link
Member

Just pushed the bump dependency modifications in here. Seems like there is one test failing though, which wasn't failing when I ran the test suite locally.

May have been an unlucky seed, rerunning it

@amyoshino
Copy link
Member Author

amyoshino commented Mar 21, 2024

@amyoshino I'm taking core of the Pytensor bump in #7203

In much better hands now 😄

Just pushed the bump dependency modifications in here. Seems like there is one test failing though, which wasn't failing when I ran the test suite locally.

May have been an unlucky seed, rerunning it

It was indeed a bad luck, the only failing part was the mypy, I guess because of the very outdated repo. I rebased and left only the previous commits here, so it is failing now because PyTensor is still 2.18. I think the way to go then is to wait for your PR to be merged and try again, right?

@ricardoV94
Copy link
Member

I think the way to go then is to wait for your PR to be merged and try again, right?

Right. I was hoping it was an easier bump, but it also unpins a Python major version :D

@amyoshino
Copy link
Member Author

Cool! It looks like the bump is merged! Looking forward to restart the checks when you think is appropriate.

@ricardoV94
Copy link
Member

Cool! It looks like the bump is merged! Looking forward to restart the checks when you think is appropriate.

You can rebase anytime to incorporate the bump

@amyoshino
Copy link
Member Author

amyoshino commented Mar 22, 2024

Cool! It looks like the bump is merged! Looking forward to restart the checks when you think is appropriate.

You can rebase anytime to incorporate the bump

Ah, true! I got so excited that the bump was merged and this potentially can be merged after so long that I forgot that 😅 My apologies haha

EDIT: Good news! I think it is ready to go now

…ions

removing unnecessary parameterization test on studentT test
removing unnecessary test on chisquared test
Copy link
Member

@ricardoV94 ricardoV94 left a comment

Choose a reason for hiding this comment

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

Amazing work @amyoshino !!

@ricardoV94 ricardoV94 merged commit abe7bc9 into pymc-devs:main Mar 25, 2024
21 checks passed
@twiecki
Copy link
Member

twiecki commented Mar 25, 2024

Congrats, this was a tricky one!

@amyoshino
Copy link
Member Author

That was fun! 😄
Thank you for the support through this journey!

@amyoshino amyoshino deleted the icdf_gamma_beta branch March 28, 2024 17:03
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.

None yet

4 participants