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

ENH: vectorize cramervonmises_2samp #58

Open
wants to merge 1 commit into
base: perm_ttest_efficiency
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions scipy/stats/_hypotests.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,21 +690,24 @@ def _pval_cvm_2samp_exact(s, nx, ny):
rangex = np.arange(nx)
rangey = np.arange(ny)

us = []

# x and y are all possible partitions of ranks from 0 to nx + ny - 1
# The following is the fully vectorized version of
# > for x, y in _all_partitions(nx, ny):
# > u = nx * np.sum((x - rangex)**2)
# > u += ny * np.sum((y - rangey)**2)
# > us.append(u)
# where x and y are all possible partitions of ranks from 0 to nx + ny - 1
# into two sets of length nx and ny
# Here, ranks are from 0 to nx + ny - 1 instead of 1 to nx + ny, but
# this does not change the value of the statistic.
for x, y in _all_partitions(nx, ny):
# compute the statistic
u = nx * np.sum((x - rangex)**2)
u += ny * np.sum((y - rangey)**2)
us.append(u)

# compute the values of u and the frequencies
u, cnt = np.unique(us, return_counts=True)
return np.sum(cnt[u >= s]) / np.sum(cnt)
all_partitions = list(_all_partitions(nx, ny))
all_partitions_x = np.array([x for x, y in all_partitions])
all_partitions_y = np.array([y for x, y in all_partitions])
us = (
nx * ((all_partitions_x - rangex[None, :]) ** 2).sum(axis=1)
+ ny * ((all_partitions_y - rangey[None, :]) ** 2).sum(axis=1)
)

return (us>=s).mean()


def cramervonmises_2samp(x, y, method='auto'):
Expand Down