Skip to content

von Mises random variate sampling broken for non-zero location parameter #4598

Closed

Description

@richardeverson

vonmises.rvs() uses the standard scipy.stats machinery to change the location of samples. This is done by adding the loc parameter to the samples generated by self._rvs(), in .../stats/_distn_infrastructure.py

    vals = self._rvs(*args)
    if self._size is not None:
        vals = reshape(vals, size)

    vals = vals * scale + loc

However, if loc != 0 this shifts them outside the range [-pi, pi] on which they should live.

Here's an illustration that clearly shows samples outside [-pi, pi]

In [21]: from scipy.stats import vonmises
In [22]: r = vonmises.rvs(kappa=1, size=1000)
In [23]: hist(r, 100, label='loc = 0')
In [24]: r = vonmises.rvs(kappa=1, loc=2, size=1000)
In [25]: hist(r, 100, label='loc = 2')
In [26]: legend()

vonmises

Not sure what the best approach to correct this is. Presumably the rvs method needs to test whether the distribution lives on a circle and then

  vals = fmod(vals+pi, 2*pi)-pi

to map the values back to the circle. The documentation is not clear on how the scale parameter relates to the range interval on which the vM distribution is defined, but if the intention is to scale the circle to (-piscale, piscale) (which is roughly what happens at the moment) then the fmod statement needs to take account of the scaling. I could submit a PR if it's clear what the desired behaviour is.

Activity

  1. added
    defectA clear bug or issue that prevents SciPy from being installed or used as expected
    on Mar 5, 2015
  2. rgommers commented on Mar 5, 2015

    @rgommers
    Member

    The generic rvs method will treat loc completely incorrectly for a circular distribution like this. It looks like numpy.random.vonmises does do the right thing (its mu parameter corresponds to loc of the scipy version).

    scale shouldn't change the [-pi,pi] range I think, but just make the distribution more narrow and keep the range unchanged.

  3. rgommers commented on Mar 5, 2015

    @rgommers
    Member

    I'm not quite sure what the best way to fix this is. It's possible to special-case the distribution in the generic methods, but maybe a cleaner way is to subclass rv_continuous and make a generic "rv_circular`` class. That would also be useful for the wrapped normal distribution that was suggested as an addition in gh-4548, as well as other circular distributions.

  4. rgommers commented on Mar 5, 2015

    @rgommers
    Member

    There's also vonmises_line, which looks weird. It enforces the [-pi. pi] range but does nothing extra with loc.

  5. josef-pkt commented on Mar 5, 2015

    @josef-pkt
    Member

    vonmises_line is a regular distribution on an interval of the real line, not a circular distribution.

  6. rgommers commented on Mar 6, 2015

    @rgommers
    Member

    @josef-pkt that I see, but what's it for? I can't find a reference for it quickly. vonmises is supposed to be an analog to the normal distribution on a circle, so why put it back on a [-pi, pi] interval on the real line?

  7. ev-br commented on Mar 6, 2015

    @ev-br
    Member

    vonmises_line was added in #2845 after an attempt to make vonmises pass the test suite (it has to be special-cased in a few places). At the moment, vonmises_line is analogous to eg cosine: it's just a univariate distribution defined on a finite interval. Possibly not a very useful one, not a great loss if removed.

  8. ev-br commented on Mar 6, 2015

    @ev-br
    Member

    IMO making an rv_circular class is better than special-casing generic methods. And/or have a factory, make_circular(stats.norm) etc.

  9. josef-pkt commented on Mar 6, 2015

    @josef-pkt
    Member

    vonmises_line is just a distribution on a fixed interval, there doesn't need to be any special reason for it.
    When someone just wants to fit all distribution to a dataset to find the best one, then vonmises_line is a candidate that works correctly on the real line.
    The main reason to add the limits was so it fits the standard pattern of the distributions

  10. argriffing commented on Mar 6, 2015

    @argriffing
    Contributor

    there doesn't need to be any special reason for it.

    It could be confusing, in the sense of 'cognitive load' not in the sense of objective technical ambiguity.

  11. rgommers commented on Mar 6, 2015

    @rgommers
    Member

    Hmm, I see that I didn't object to adding it in gh-2845. I don't understand why anymore though. "just a distribution" is not a reason, there are an infinite amount of them. We'd like Scipy to contain only distributions known in literature and preferably not being too obscure I'd think. Unless I'm missing something, a Von Mises distribution is always defined on a circle in the papers and online references I can dig up so quickly.

  12. richardeverson commented on Mar 6, 2015

    @richardeverson
    Author

    As far as I can see, at the moment thera are only two circular distributions in scipy.stats: vonmises and wrapcauchy (a.k.a. Lorentzian). Unfortunately vonmises is defined on [-pi, pi], but wrapcauchy on [0, 2pi]. There's no substantial difference and it's a shame that wrapcauchy is on [0, 2pi] even though the literature I can find defines it on [-pi, pi]. It looks as if wrapcauchy has been around for a long time so I guess it's too late to change.

    Making a rv_circular subclass is fine, but it would need two subclasses or special cases to cope with [-pi, pi] and [0,2pi] cases. This seems like the neatest solution, although perhaps overkill.

    I suggest that for circular distributions the loc parameter is reduced to the circle on which the distribution is defined and scale parameter is ignored.

  13. josef-pkt commented on Mar 6, 2015

    @josef-pkt
    Member

    I worked on circular distributions quite a bit, but it's not public code yet. I ended up parameterizing the base interval, including half circle for symmetric, undirected random variables, and units in degrees or radians or 24 hour clock.

    part of it is here https://github.com/rc/dist_mixtures/blob/master/dist_mixtures/mixture_von_mises.py
    for mixtures of vonmises distributions.

  14. aarchiba commented on Mar 25, 2015

    @aarchiba
    Contributor

    @josef-pkt - if you're going to provide other ways to measure angle, 0-1 is very standard in pulsar astronomy. Mostly it's not a problem to convert as needed, but if you're providing degrees this would be a natural alternative. Scale would be a natural way to specify this.

    One thing to watch out for is the behaviour of the CDF - cdf(x) and cdf(x+1 turn) should differ by one, so it's not safe to reduce arguments modulo one turn. More generally I'd prefer that PDFs and CDFs be evaluable anywhere, simply for convenience.

    Also if there's a circular statistics class it might provide different summary statistics - the circular mean includes some information on concentration, for example, as well as location, and the higher Fourier coefficients are useful too.

  15. NeilGirdhar commented on Apr 6, 2016

    @NeilGirdhar
    Contributor

    @EVBR "vonmises_line was added in #2845 after an attempt to make vonmises pass the test suite"
    @josef-pkt "When someone just wants to fit all distribution to a dataset to find the best one, then vonmises_line is a candidate that works correctly on the real line."

    These are not good reasons in my opinion to add a distribution. I suggest removing vonmises_line since it's confusing and has no statistical meaning. If someone really wants to interpret a line segment as a a circle, they should actually map their line segment to radians and use vonmises.

  16. ev-br commented on Apr 6, 2016

    @ev-br
    Member

    Whether or not adding vonmises_line was a mistake (it very well might have been), IMO working out rv_circular or some such would be much more productive way of spending time and effort.

  17. NeilGirdhar commented on Apr 6, 2016

    @NeilGirdhar
    Contributor

    @ev-br Agreed. You're also going to have spherical distributions in n-dimensions eventually I assume, so it would be good to sort this out general — not just for circular distributions.

  18. josef-pkt commented on Apr 7, 2016

    @josef-pkt
    Member

    vonmises works very well as it is, for reuse by users and not as a circular distribution out of the box.
    I build mixture of vonmises distribution on the half circle with it for directional data.
    It needs manual shifting and modulo transformation to get it into the rights space (half circle in [0, 180] or [-90, 90] degrees in our case).

    adding .a and .b was so it works correctly for the generic calculations so we can ran the test battery on it. It should be possible for users to shift .a and .b but I never tried that.

  19. added
    enhancementA new feature or improvement
    and removed
    defectA clear bug or issue that prevents SciPy from being installed or used as expected
    on Jul 13, 2022
  20. changed the title [-]von Mises random variate sampling broken for non-zero location parameter[/-] [+]ENH: stats: circular distribution infrastructure[/+] on Jul 13, 2022
  21. mdhaber commented on Jul 13, 2022

    @mdhaber
    Contributor

    gh-7533 reported that the vonmises.pdf did not wrap around when loc != 0, but that seems to be resolved. For example, with loc=1.5:

    image

    Details
    import numpy as np
    from scipy.stats import vonmises
    import matplotlib.pyplot as plt
    x = np.linspace(-np.pi, np.pi, 100)
    y = vonmises.pdf(x, kappa=2, loc=1.5, scale=1) # , size=10000)
    r = vonmises.rvs(kappa=2, loc=1.5, scale=1, size=10000)
    plt.hist(r, 100, label='histogram', density=True)
    plt.plot(x, y, label='pdf')
    plt.legend()
    
  22. changed the title [-]ENH: stats: circular distribution infrastructure[/-] [+]von Mises random variate sampling broken for non-zero location parameter[/+] on Jul 18, 2022
  23. mdhaber commented on Jul 18, 2022

    @mdhaber
    Contributor

    I think the original issue was closed by gh-16590. The more general issue is tracked in gh-6038 and gh-15938.

  24. added this to the 1.10.0 milestone on Aug 2, 2022
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

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions