Skip to content

Conversation

mdhaber
Copy link
Contributor

@mdhaber mdhaber commented May 20, 2024

Reference issue

Toward gh-20544

What does this implement/fix?

Adds array API support to stats.chisquare and stats.power_divergence. Preserves but deprecates support for masked arrays per generalization of the conversation about gstd.

Additional information

Consider reviewing diff with "Hide whitespace" checked. I moved some test functions into classes.

@mdhaber mdhaber added scipy.stats enhancement A new feature or improvement array types Items related to array API support and input array validation (see gh-18286) labels May 20, 2024
@mdhaber mdhaber requested review from person142 and steppi as code owners May 20, 2024 18:19
case.f_obs, case.f_exp, case.ddof, case.axis,
2/3, case.cr)

def test_basic_masked(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This didn't actually pass masked arrays into the function because check_power_divergence converted mobs back into a regular array, so I just deleted it.

Comment on lines +7931 to +7941
def _m_sum(a, *, axis, preserve_mask, xp):
if np.ma.isMaskedArray(a):
sum = a.sum(axis)
return sum if preserve_mask else np.asarray(sum)
return xp.sum(a, axis=axis)


def _m_mean(a, *, axis, keepdims, xp):
if np.ma.isMaskedArray(a):
return np.asarray(a.mean(axis=axis, keepdims=keepdims))
return xp.mean(a, axis=axis, keepdims=keepdims)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These and the _m_ functions above will be removed in two versions, when masked array support is removed.


ddof = np.asarray(ddof)
expected_p = stats.distributions.chi2.sf(expected_stat,
num_obs - 1 - ddof)
assert_allclose(p, expected_p)

def test_basic(self):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@parametrized now.

xp_assert_close(stat, xp.asarray([5.71428571, 2.66666667]))
xp_assert_close(pval, xp.asarray([0.01682741, 0.10247043]))

def test_power_divergence_against_cressie_read_data(self, xp):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved up from below.

Copy link
Member

@tupui tupui left a comment

Choose a reason for hiding this comment

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

LGTM, changes are clear, same with the tests.

@tupui tupui merged commit 95e27a8 into scipy:main May 22, 2024
@tupui tupui added this to the 1.14.0 milestone May 22, 2024
@mdhaber
Copy link
Contributor Author

mdhaber commented May 22, 2024

Thanks @tupui!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
array types Items related to array API support and input array validation (see gh-18286) enhancement A new feature or improvement scipy.special scipy.stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants