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

Added icdf for Rice and skewnormal distributions #7095

Closed
wants to merge 3 commits into from
Closed

Added icdf for Rice and skewnormal distributions #7095

wants to merge 3 commits into from

Conversation

ParamThakkar123
Copy link

@ParamThakkar123 ParamThakkar123 commented Jan 10, 2024

Description

Added icdf for Rice and SkewNormal distributions

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

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

Copy link

welcome bot commented Jan 10, 2024

Thank You Banner
💖 Thanks for opening this pull request! 💖 The PyMC community really appreciates your time and effort to contribute to the project. Please make sure you have read our Contributing Guidelines and filled in our pull request template to the best of your ability.

Copy link

codecov bot commented Jan 12, 2024

Codecov Report

Attention: 17 lines in your changes are missing coverage. Please review.

Comparison is base (7bb2ccd) 92.21% compared to head (8822346) 92.12%.
Report is 3 commits behind head on main.

❗ Current head 8822346 differs from pull request most recent head 5a2265f. Consider uploading reports for the commit 5a2265f to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #7095      +/-   ##
==========================================
- Coverage   92.21%   92.12%   -0.10%     
==========================================
  Files         101      101              
  Lines       16912    16934      +22     
==========================================
+ Hits        15595    15600       +5     
- Misses       1317     1334      +17     
Files Coverage Δ
pymc/distributions/continuous.py 96.31% <22.72%> (-1.49%) ⬇️

@ricardoV94
Copy link
Member

Hi

The icdf must be written with PyTensor operations and tests are needed. Have a look at previous PRs that introduced icdf methods.

Copy link
Member

@larryshamalama larryshamalama left a comment

Choose a reason for hiding this comment

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

Thanks for your PR @ParamThakkar123. As @ricardoV94, can you provide corresponding tests for your icdf functions? See PR #6747 for a nice reference.

Also, I am not sure in the math of your icdf functions... Can you provide some justification? Passing tests would help a lot here, but some arguments as to what is happening would be beneficial

@@ -2993,6 +2996,29 @@ def logp(value, mu, sigma, alpha):
tau > 0,
msg="tau > 0",
)
def icdf(prob, mu, sigma, alpha, max_iter=100, tol=1e-8):
Copy link
Member

Choose a reason for hiding this comment

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

What justifies the icdf method having a different signature compared to the logp method mentioned above?

x -= derivative

res = mu + sigma * x
res = check_icdf_value(res, prob)
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain in words (or math) what is going on here?

Copy link
Author

@ParamThakkar123 ParamThakkar123 Feb 2, 2024

Choose a reason for hiding this comment

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

The provided code defines a function icdf that aims to find the inverse cumulative distribution function (ICDF) for a specific probability distribution.
cdf Function:

Calculates the cumulative distribution function (CDF) for a given input
x using a specific probability distribution formula.
The distribution appears to be a modified version, involving terms like pt.exp (likely indicating exponentiation), iv (modified Bessel function of the first kind), and ive (modified Bessel function of the second kind).
cdf_derivative Function:

Calculates the derivative of the CDF with respect to
x. This derivative is used in the Newton-Raphson method, which is an iterative numerical technique for finding roots (or zeros) of a function.
Newton-Raphson Iteration (newton):

Uses the Newton-Raphson method to iteratively find a value of x such that
cdf(x)−prob=0.
The fprime argument is provided with the derivative function (cdf_derivative) to guide the iteration.
check_icdf_value Function:

Performs some checks on the calculated ICDF value (res) and potentially modifies it.

Checks whether certain parameters, in this case,
σ, satisfy a condition (in this case, >0 σ>0).
If the condition is not met, it likely raises an error or handles the situation accordingly.
Overall Workflow:

The main function icdf combines these components to find the ICDF for a given probability (prob) using the Newton-Raphson method.
It includes checks to ensure the validity of the ICDF value and the parameters involved.

Copy link
Member

@ricardoV94 ricardoV94 Feb 2, 2024

Choose a reason for hiding this comment

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

Sorry to ask but are you a bot?

The code will clearly not work with PyTensor variables.

Copy link
Author

Choose a reason for hiding this comment

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

No. I am not a bot

Copy link
Author

Choose a reason for hiding this comment

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

I'll fix everything and make a new pull request

Copy link
Member

Choose a reason for hiding this comment

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

Calculates the cumulative distribution function (CDF) for a given input x using a specific probability distribution formula. The distribution appears to be a modified version, involving terms like pt.exp (likely indicating exponentiation), iv (modified Bessel function of the first kind), and ive (modified Bessel function of the second kind). cdf_derivative Function:

Performs some checks on the calculated ICDF value (res) and potentially modifies it.

What do you mean by "likely indicating", "potentially modifies it"? This wording seems strange to me.

Again, none of this will work with PyTensor variables. If you are relying on AI to generate responses and code changes, we cannot accept them

return ((1 - x**2 / sigma**2) * pt.exp(-x**2 / (2 * sigma**2)) * iv(0, x * nu / sigma**2)
+ (x / sigma**2) * pt.exp(-x**2 / (2 * sigma**2)) * ive(0, x * nu / sigma**2)) * nu / sigma**2

approx_icdf = newton(cdf, x0, fprime=cdf_derivative, tol=tol, maxiter=max_iter)
Copy link
Member

Choose a reason for hiding this comment

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

Will newton from scipy work with PyTensor variables?

if pt.abs(diff) < tol:
return mu + sigma * x

derivative = norm.pdf(x) - 2 * alpha * norm.pdf(alpha * x)
Copy link
Member

Choose a reason for hiding this comment

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

does norm.pdf and norm.ppf work with PyTensor objects?

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.

3 participants