Skip to content

circmean() changed behaviour from 0.17 to 0.18 #6420

Closed

Description

@evertrol

Commit 6f799a807bde2b4391857 appears to have changed the behaviour of scipy.stats.circmean().
Yet I can't find this documented anywhere.
Is this correct?

For example, using the following code

import numpy as np
from scipy.stats import circmean
print(circmean(np.arange(-2, 0, 0.1), np.pi, -np.pi),
      circmean(np.arange(0, 2, 0.1), np.pi, -np.pi))

produces different results before and after this commit:

# master
# -1.05 -5.33318530718

# git checkout 6f799a807bde2b4391857
# -1.05 -5.33318530718

# git checkout 4f9be64594970103e245cf5
# -1.05 0.95

Activity

  1. changed the title [-]`circmean` changed behaviour from 0.17 to 0.18[/-] [+]circmean() changed behaviour from 0.17 to 0.18[/+] on Jul 27, 2016
  2. zym1010 commented on Jul 27, 2016

    @zym1010

    by checking the code, looks that there's an additional step to make all values within range [0, 2*pi] missing...

    In [13]: import numpy as np
    
    In [14]: from scipy.stats import circmean
    
    In [15]: a=np.linspace(-0.2,0,10)
    
    In [16]: circmean(a)
    Out[16]: -0.10000000000000001
    

    clearly this doesn't look like correct behavior. Wonder how it can pass testing...

  3. pv commented on Jul 27, 2016

    @pv
    Member
  4. pv commented on Jul 27, 2016

    @pv
    Member
  5. zym1010 commented on Jul 27, 2016

    @zym1010

    @pv thanks. this is just my understanding of how circmean works.

    1. samples is assumed to lie in [low, high], and in the first step, _circfuncs_common is used to scale and shift these values to [0, 2*pi]. //well more precisely, modulo operation will be done on samples according to low and high.
    2. compute circular mean of scaled and shifted samples.
    3. transform the computed mean in the "canonical domain" back to [low, high].

    Currently, steps 2&3 are combined together incorrectly, since arctan2 produces values within [-pi, pi].

    Previously, mask in the code is to make negative values positive. Now, it's used to mask undefined values for arctan2.

    Several enhancements I propose to be added:

    1. Clarify what low and high mean in the doc.
    2. add some check to input and boundaries.
    3. the mask is inconsistent for scalar and array values, since it will only work for array values (mask.ndim > 0). So it should be fixed.
    4. clarify how undefined values (those marked nan by the mask in the new version) are handled in the doc.
  6. kleskjr commented on Jul 27, 2016

    @kleskjr
    Contributor

    I'm sorry for introducing this bug!
    This evening (CET) I will try to clear up the mess.

    I have a question concerning the normalization of values:

    What should be the default behaviour in case samples is not in [low, high] range?
    In particular, the example from @zym1010 : circmean(np.linspace(-0.2, 0, 10), 2*pi, 0).
    Should the values be normalised anyway assuming modulo of 2*pi? In this case ang = 2*pi + np.linspace(-0.2, 0, 10). Or should we expect some other behaviour: nan values, an error, or a warning?

  7. zym1010 commented on Jul 27, 2016

    @zym1010

    @kleskjr I guess for normalization, you can simply leave it as is, and maybe later on some keyword arguments such as strict can be added to _circfuncs_common to throw out some exception in case samples is not in [low, high].

    Also, I think maybe such check is not needed, as [low, high] only specifies how to to modulo on samples, and samples will be automatically processed as modulo, due to use of sin and cos.

  8. added
    defectA clear bug or issue that prevents SciPy from being installed or used as expected
    on Jul 27, 2016
  9. added this to the 0.18.1 milestone on Jul 27, 2016
  10. added a commit that references this issue on Jul 27, 2016
  11. kleskjr commented on Jul 28, 2016

    @kleskjr
    Contributor

    Tomorrow I will add some additional test(s) to cover the last misbehaviour.

  12. added a commit that references this issue on Jul 31, 2016
  13. added a commit that references this issue on Sep 2, 2016
  14. added a commit that references this issue on Sep 4, 2016
  15. added a commit that references this issue on Sep 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedscipy.stats

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions