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 Rice distribution #4

Merged
merged 16 commits into from
Apr 26, 2024
Merged

Add Rice distribution #4

merged 16 commits into from
Apr 26, 2024

Conversation

kmdalton
Copy link
Member

This PR adds a learnable Rice distribution which is useful in modeling structure factors in X-ray crystallography.

@kmdalton
Copy link
Member Author

This implementation uses Rician functional forms for all the attributes. However, it is noteworthy that when nu >> sigma, the Rice distribution becomes indistinguishable from a normal distribution with mu=nu,sigma=sigma. There are further details in the wikipedia artical. My own experiments suggest that switching to a normal PDF when nu/sigma > 40 improves numerical stability. Likely this cutoff could be much lower without impacting accuracy. Further study could be beneficial here if we run into numerical issues with this distribution implementation.

@kmdalton kmdalton requested review from minhuanli and LuisA92 April 23, 2024 15:43
@kmdalton
Copy link
Member Author

Note there were some breaking changes to the setup-python action for MacOS. This PR will also change the test workflow to support python 3.8/3.9 on macos-13 rather than macos-latest.

Copy link
Collaborator

@minhuanli minhuanli left a comment

Choose a reason for hiding this comment

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

Great job and super clean codes!

.github/workflows/test.yml Show resolved Hide resolved
src/rs_distributions/distributions/rice.py Show resolved Hide resolved
src/rs_distributions/distributions/rice.py Show resolved Hide resolved
src/rs_distributions/distributions/rice.py Outdated Show resolved Hide resolved
log_z, log_nu, log_sigma = torch.log(z), torch.log(nu), torch.log(sigma)
log_a = log_nu - log_sigma
log_b = log_z - log_sigma
ab = torch.exp(log_a + log_b) # <-- argument of bessel functions
Copy link
Collaborator

Choose a reason for hiding this comment

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

again, any reason why not just:

ab = nu * z / (sigma * sigma)

tests/distributions/test_rice.py Show resolved Hide resolved
Copy link
Collaborator

@LuisA92 LuisA92 left a comment

Choose a reason for hiding this comment

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

I learned a lot from your code Kevin, thanks!

src/rs_distributions/distributions/rice.py Show resolved Hide resolved
src/rs_distributions/distributions/rice.py Outdated Show resolved Hide resolved
src/rs_distributions/distributions/rice.py Show resolved Hide resolved
tests/distributions/test_rice.py Show resolved Hide resolved
@kmdalton
Copy link
Member Author

going ahead and merging this pr to restore CI and add the Rice distribution. as discussed offline with @minhuanli, the tests in this pr do not assess the validity / accuracy of the implicit reparameterization gradients. we should research a method of testing those cheaply and programmatically across the API. i added #8 to remind us of this shortcoming.

@kmdalton kmdalton merged commit 807b47b into main Apr 26, 2024
15 checks passed
@kmdalton kmdalton deleted the rice branch April 26, 2024 13:21
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