Skip to content

Conversation

mdhaber
Copy link
Contributor

@mdhaber mdhaber commented Jul 1, 2024

Reference issue

Toward gh-20544

What does this implement/fix?

Vectorizes alexandergovern for n-D arrays (i.e. adds native axis support).

Additional information

I've also made some tweaks here that will reduce the diff when we add array API support.

@mdhaber mdhaber added scipy.stats enhancement A new feature or improvement labels Jul 1, 2024
Comment on lines -4377 to -4023
if np.any([(sample == sample[0]).all() for sample in samples]):
msg = "An input array is constant; the statistic is not defined."
warnings.warn(stats.ConstantInputWarning(msg), stacklevel=2)
return AlexanderGovernResult(np.nan, np.nan)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See " Special case: statistic is NaN when variance is zero" below.
To produce a warning, I was thinking I'd replace means - var_w with _demean(means, var_w) when gh-21068 merges.

warnings.warn(stats.ConstantInputWarning(msg), stacklevel=2)
return AlexanderGovernResult(np.nan, np.nan)
samples = _alexandergovern_input_validation(samples, nan_policy, axis)
samples = [np.moveaxis(sample, axis, -1) for sample in samples]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Important for the rest of the calculation:

  • axis 0 corresponds with separate samples
  • axis -1 is along a given sample
  • any other axes are batch dimensions.

raise ValueError("Input sample size must be greater than one.")
if np.isinf(sample).any():
raise ValueError("Input samples must be finite.")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't want to error out for infs in a single slice. Instead, the user will get a warning and NaN.

Comment on lines +7515 to +7518
args_int16 = [np.asarray(arg, dtype=np.int16) for arg in args]
args_int32 = [np.asarray(arg, dtype=np.int32) for arg in args]
args_uint8 = [np.asarray(arg, dtype=np.uint8) for arg in args]
args_float64 = [np.asarray(arg, dtype=np.float64) for arg in args]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Arrays happen to be the same size, but conceptually there are several separate samples.

@mdhaber mdhaber marked this pull request as draft July 1, 2024 03:20
@mdhaber mdhaber marked this pull request as ready for review July 22, 2024 06:04
@mdhaber
Copy link
Contributor Author

mdhaber commented Jul 22, 2024

@lucascolley re #21097 (comment), here's an example of adding axis to a function. Note that this one already has the _axis_nan_policy decorator, so I didn't need to add any tests.

Copy link
Contributor

@dschmitz89 dschmitz89 left a comment

Choose a reason for hiding this comment

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

LGTM, one small question left.

@lucascolley lucascolley added this to the 1.15.0 milestone Jul 22, 2024
@lucascolley lucascolley added the needs-release-note a maintainer should add a release note written by a reviewer/author to the wiki label Jul 22, 2024
@dschmitz89
Copy link
Contributor

Thanks @mdhaber .

@dschmitz89 dschmitz89 merged commit a1ea3b4 into scipy:main Jul 23, 2024
@lucascolley lucascolley removed the needs-release-note a maintainer should add a release note written by a reviewer/author to the wiki label Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.stats
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants