Skip to content

Circular Statistics: astropy vs scipy #13749

Open

Description

@pllim

Seems like scipy has some circular statistics support that might overlap with astropy a little. However, API is not exactly the same and I am not sure if both sides cover all the same use cases. There is a need to do the following:

  1. Compare https://docs.astropy.org/en/latest/stats/circ.html with https://docs.scipy.org/doc/scipy/reference/stats.html#circular-statistical-functions
  2. Who do we talk to on the scipy side about this?
  3. How do we make them 100% compatible?
  4. Have a plan and roadmap to deprecate what we have and point people upstream.
  5. If Option 4 is not possible, clearly document why and when user should choose one over the other.

circmean

astropy: astropy.stats.circstats.circmean(data[, axis, weights])
Computes the circular mean angle of an array of circular data.

scipy: scipy.stats.circmean(samples[, high, low, axis, nan_policy])
Compute the circular mean for samples in a range.

circstd

astropy: astropy.stats.circstats.circstd(data[, axis, weights, method])
Computes the circular standard deviation of an array of circular data.

scipy: scipy.stats.circstd(samples[, high, low, axis, ...])
Compute the circular standard deviation for samples assumed to be in the range [low to high].

circvar

astropy: astropy.stats.circstats.circvar(data[, axis, weights])
Computes the circular variance of an array of circular data.

scipy: circvar(samples[, high, low, axis, nan_policy])
Compute the circular variance for samples assumed to be in a range.

Activity

  1. dschmitz89 commented on Mar 11, 2023

    @dschmitz89

    Hi astropy people,

    I recently worked on circular/directional stats in scipy. Indeed there is some duplication, it would be great to collaborate/coordinate a little bit.

    Before talking about APIs, I wanted to point out recent additions to scipy that could be helpful for astronomy:

    • we implemented a numerical fitting method for the von Mises distribution (see here) that gives a quick and accurate result also for the concentration parameter $\kappa$. astropy currently uses an approximation for the concentration parameter. You could consider using the scipy method instead (after a deprecation cycle maybe). Be aware that this is only available in scipy's main branch and not released yet.
    • directional_stats is a generalization of circmean and circvar that works in arbitrary dimensions. I would expect that especially $n=3$ must be very interesting for astropy.
    • the new distribution uniform_direction enables to sample uniformly from the surface of a hyphersphere
    • I am currently working on implementing the Von Mises-Fisher distribution, a generalization of the Von Mises distribution (see here).

    One function from astropy that would be useful for scipy would be the Rayleigh test for uniformity on the hypersphere. Can your implementation be generalized to $n$ dimensions maybe?

    Regarding the duplication of circmean, circvar and circstd: could astropy just use the scipy methods under the hood for some time and then maybe deprecate them in the long run? From what I see, scipy's implementation lets the user choose axis and a nan policy (not available in astropy) but does not allow weights yet (available in astropy). Those could be added if we decide to go this route.

    CC @mdhaber

  2. pllim commented on Mar 13, 2023

    @pllim
    MemberAuthor

    Thank you for the update! Looks like a bulk of these were implemented here by @mirca back in #4472 . Our current stats subpackage maintainer is @larrybradley . Personally, I am very interested in re-using scipy but the problem is getting devs time to work on this campaign that would probably drag on for months if not years.

    For a feature that is not in any released scipy, we would be hesitant to use. It is much simpler for us to switch over if the feature is already in our minversion of scipy, which we could consider bumping as long as it adheres to APE 18.

    scipy>=1.5

  3. mhvk commented on Mar 13, 2023

    @mhvk
    Contributor

    Bit from the sidelines, but yes to using scipy where possible - these routines are not astronomy specific! But it will be a bit of a slow trajectory, obviously.

  4. mdhaber commented on Mar 13, 2023

    @mdhaber

    Just thought I'd note that the SciPy and Astropy definitions of circular variance were different in the past. SciPy's was corrected in 1.9.0 by gh-5747scipy/scipy#5747, so they are consistent now. (The documentation of Astropy's circvar mentions the discrepancy; that could be updated.)

  5. larrybradley commented on Mar 13, 2023

    @larrybradley
    Member

    Thanks, @mdhaber. I'll update the astropy circvar docs.

    The scipy PR that made them consistent is scipy/scipy#5747

  6. mdhaber commented on Dec 16, 2025

    @mdhaber
  7. pllim commented on Dec 17, 2025

    @pllim
    Author
  8. mdhaber commented on Aug 23, 2026

    @mdhaber

    Thanks @pllim. For future participants, my interest in #13749 (comment) has expired, but I thought I'd median that SciPy's circular median PR (scipy/scipy#24166) has merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions