-
-
Notifications
You must be signed in to change notification settings - Fork 29
ENH: Add exact Cramér-von Mises two-sample p-value gufunc #118
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
Open
fbourgey
wants to merge
10
commits into
scipy:main
Choose a base branch
from
fbourgey:cramer_von_mises_gufunc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
612941b
ENH: Add device-safe gcd and lcm functions, and implement exact p-val…
fbourgey 1916a52
TST: Add unit tests for exact p-value in Cramér-von Mises two-sample …
fbourgey 39d5b55
ENH: Include <cuda/std/numeric> for numeric functions and ensure prop…
fbourgey eb69bc4
REF: gcd and lcm implementations; use cuda lcm instead
fbourgey 6c02e1f
REF/TST: Replace custom lcm implementation with std::lcm in pval_cvm_…
fbourgey 272722f
ENH: use std::max
fbourgey b756d3b
ENH: Refactor Cramér-von Mises two-sample test to separate frequency …
fbourgey 439a027
TST: update test
fbourgey 163d6fd
ENH: Add helper functions for Cramér-von Mises frequency table genera…
fbourgey fdbef3a
ENH: Add tests for take_from_discrete_sf and edge cases in pval_cvm_2…
fbourgey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| #include "../testing_utils.h" | ||
| #define MDSPAN_USE_PAREN_OPERATOR 1 | ||
| #include <xsf/stats.h> | ||
| #include <xsf/third_party/kokkos/mdspan.hpp> | ||
|
|
||
| /* | ||
| // Reference values computed with scipy.stats._hypotests._pval_cvm_2samp_exact | ||
|
|
||
| import numpy as np | ||
| from scipy import stats | ||
|
|
||
| rng = np.random.default_rng(seed=42) | ||
|
|
||
| list_m = rng.integers(3, 30, size=5) | ||
| list_n = rng.integers(3, 30, size=5) | ||
| rtol = 1e-10 | ||
|
|
||
| for m, n in zip(list_m, list_n): | ||
| x = rng.standard_normal(m) | ||
| y = rng.standard_normal(n) | ||
| res = stats.cramervonmises_2samp(x, y, method="exact") | ||
| T = res.statistic | ||
| # Convert normalized statistic T to the unnormalized U | ||
| U = m * n * (m + n) * T + m * n * (4 * m * n - 1) / 6 | ||
| p_value = stats._hypotests._pval_cvm_2samp_exact(U, m, n) | ||
| assert np.isclose(res.pvalue, p_value, rtol=rtol), "The p-values do not match!" | ||
| print(f"U={U}, m={m}, n={n}, p-value={p_value}") | ||
| */ | ||
| TEST_CASE("take_from_discrete_sf test", "[take_from_discrete_sf][xsf_tests]") { | ||
| std::vector<double> pmf = {0.125, 0.375, 0.375, 0.125}; | ||
| std::mdspan pmf_span(pmf.data(), pmf.size()); | ||
|
|
||
| REQUIRE(xsf::take_from_discrete_sf(pmf_span, -1) == 1.0); | ||
| REQUIRE(xsf::take_from_discrete_sf(pmf_span, 0) == 1.0); | ||
| REQUIRE(xsf::take_from_discrete_sf(pmf_span, 1) == 0.875); | ||
| REQUIRE(xsf::take_from_discrete_sf(pmf_span, 3) == 0.125); | ||
| REQUIRE(xsf::take_from_discrete_sf(pmf_span, 4) == 0.0); | ||
| } | ||
|
|
||
| TEST_CASE("pval_cvm_2samp_exact test", "[pval_cvm_2samp_exact][xsf_tests]") { | ||
| using test_case = std::tuple<double, int, int, double, double>; | ||
| auto [s, m, n, pval_expected, rtol] = GENERATE( | ||
| test_case{12559.0, 5, 26, 0.11812654860485784, 1e-10}, test_case{8901.0, 23, 5, 0.9907610907610908, 1e-10}, | ||
| test_case{119376.0, 20, 21, 0.5716351061359124, 1e-10}, test_case{8862.0, 14, 8, 0.2679738562091503, 1e-10}, | ||
| test_case{3491.0000000000005, 14, 5, 0.34657722738218094, 1e-10} | ||
| ); | ||
|
|
||
| const int64_t lcm = std::lcm(m, n); | ||
| const int64_t K = (m + n) * lcm * lcm + 1; | ||
|
|
||
| std::vector<int64_t> buf1((m + 1) * K, 0); | ||
| std::vector<int64_t> buf2((m + 1) * K, 0); | ||
|
|
||
| using mdspan_2d = std::mdspan<int64_t, std::dextents<size_t, 2>>; | ||
| mdspan_2d gs(buf1.data(), static_cast<size_t>(m + 1), static_cast<size_t>(K)); | ||
| mdspan_2d next_gs(buf2.data(), static_cast<size_t>(m + 1), static_cast<size_t>(K)); | ||
|
|
||
| xsf::cvm_2samp_freq_table(m, n, gs, next_gs); | ||
| auto pval = xsf::pval_cvm_2samp_exact(s, m, n, gs); | ||
| const double rel_error = xsf::extended_relative_error(pval, pval_expected); | ||
| CAPTURE(s, m, n, K, pval, pval_expected, rel_error); | ||
| REQUIRE(rel_error <= rtol); | ||
| } | ||
|
|
||
| TEST_CASE("pval_cvm_2samp_exact edge cases", "[pval_cvm_2samp_exact][xsf_tests]") { | ||
| using test_case = std::tuple<double, int, int, double>; | ||
| auto [s, m, n, pval_expected] = GENERATE(test_case{0.0, 3, 3, 1.0}, test_case{1e6, 3, 3, 0.0}); | ||
|
|
||
| const int64_t lcm = std::lcm(m, n); | ||
| const int64_t K = (m + n) * lcm * lcm + 1; | ||
|
|
||
| std::vector<int64_t> buf1((m + 1) * K, 0); | ||
| std::vector<int64_t> buf2((m + 1) * K, 0); | ||
|
|
||
| using mdspan_2d = std::mdspan<int64_t, std::dextents<size_t, 2>>; | ||
| mdspan_2d gs(buf1.data(), static_cast<size_t>(m + 1), static_cast<size_t>(K)); | ||
| mdspan_2d next_gs(buf2.data(), static_cast<size_t>(m + 1), static_cast<size_t>(K)); | ||
|
|
||
| xsf::cvm_2samp_freq_table(m, n, gs, next_gs); | ||
| auto pval = xsf::pval_cvm_2samp_exact(s, m, n, gs); | ||
| CAPTURE(s, m, n, K, pval, pval_expected); | ||
| REQUIRE(pval == pval_expected); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry about the CUDA side. I still have to figure out cupy/cupy#9839 before we can even try these in CuPy. I'm pretty sure just adding
using cuda::std::gcdwon't work in all cases, and we actually need wrappers for stdlib functions like the other ones in this file. I recall I had suggested usingusinglike this when Irwin first set this up, but there was a reason he had done things the way he did.