Skip to content

Conversation

Aniketsy
Copy link
Contributor

#32647

This PR adds support for weighted quantile and percentile computations to jax.numpy.quantile, jax.numpy.nanquantile, jax.numpy.percentile, and jax.numpy.nanpercentile .

Please let me know if my approach or fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thankyou !

Copy link
Contributor

Summary of Changes

Hello @Aniketsy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the jax.numpy module by introducing support for weighted quantile and percentile calculations. By integrating an optional weights argument into the existing quantile, nanquantile, percentile, and nanpercentile functions, users can now perform more sophisticated statistical analyses where individual data points contribute differently to the overall distribution. The change involves modifying function signatures and implementing the core weighted logic within the internal _quantile function, ensuring accurate results across various interpolation methods and proper handling of NaN values.

Highlights

  • Weighted Quantile/Percentile Support: The jax.numpy.quantile, jax.numpy.nanquantile, jax.numpy.percentile, and jax.numpy.nanpercentile functions now accept an optional weights parameter, enabling weighted computations.
  • Core Weighted Logic Implementation: The internal _quantile function has been extended to handle weighted inputs, including sorting data and weights, calculating cumulative normalized weights, and determining quantile values based on various interpolation methods.
  • NaN Handling with Weights: The weighted quantile logic correctly handles NaN values by masking them and adjusting weights accordingly when squash_nans is enabled.
  • New Test Case: A new unit test, test_weighted_quantile_linear, has been added to tests/lax_numpy_reducers_test.py to validate the correctness of the weighted quantile calculation.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for weighted quantiles and percentiles, which is a valuable addition. The overall approach is sound, but there are a couple of significant issues in the implementation within _quantile. Firstly, there's a structural issue where the new logic for weighted quantiles is added after the axis variable has been processed, leading to dead code and incorrect behavior for NaN handling when axis is None. Secondly, the implementation for the weighted case appears to be incorrect when q (the quantiles) is a vector. I've provided detailed comments and suggestions for fixes. I also recommend expanding the test suite to cover these edge cases to ensure the feature is robust.

@Aniketsy
Copy link
Contributor Author

@jakevdp please review these changes, when you get a chance. I'm not sure if i correctly placed -handle weights also all tests are not covered yet, should i add. Thanks!

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.

1 participant