You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
von Mises random variate sampling broken for non-zero location parameter #4598
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()
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.
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.
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.
@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?
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.
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
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.
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.
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.
@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.
@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.
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.
@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.
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.
changed the title [-]von Mises random variate sampling broken for non-zero location parameter[/-][+]ENH: stats: circular distribution infrastructure[/+]on Jul 13, 2022
changed the title [-]ENH: stats: circular distribution infrastructure[/-][+]von Mises random variate sampling broken for non-zero location parameter[/+]on Jul 18, 2022
Activity
rgommers commentedon Mar 5, 2015
The generic
rvsmethod will treatloccompletely incorrectly for a circular distribution like this. It looks likenumpy.random.vonmisesdoes do the right thing (itsmuparameter corresponds tolocof the scipy version).scaleshouldn't change the[-pi,pi]range I think, but just make the distribution more narrow and keep the range unchanged.rgommers commentedon Mar 5, 2015
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_continuousand 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.rgommers commentedon Mar 5, 2015
There's also
vonmises_line, which looks weird. It enforces the[-pi. pi]range but does nothing extra withloc.josef-pkt commentedon Mar 5, 2015
vonmises_line is a regular distribution on an interval of the real line, not a circular distribution.
rgommers commentedon Mar 6, 2015
@josef-pkt that I see, but what's it for? I can't find a reference for it quickly.
vonmisesis 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?ev-br commentedon Mar 6, 2015
vonmises_linewas added in #2845 after an attempt to makevonmisespass the test suite (it has to be special-cased in a few places). At the moment,vonmises_lineis analogous to egcosine: it's just a univariate distribution defined on a finite interval. Possibly not a very useful one, not a great loss if removed.ev-br commentedon Mar 6, 2015
IMO making an
rv_circularclass is better than special-casing generic methods. And/or have a factory,make_circular(stats.norm)etc.josef-pkt commentedon Mar 6, 2015
vonmises_lineis 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
argriffing commentedon Mar 6, 2015
It could be confusing, in the sense of 'cognitive load' not in the sense of objective technical ambiguity.
rgommers commentedon Mar 6, 2015
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.
richardeverson commentedon Mar 6, 2015
As far as I can see, at the moment thera are only two circular distributions in
scipy.stats:vonmisesandwrapcauchy(a.k.a. Lorentzian). Unfortunatelyvonmisesis defined on [-pi, pi], butwrapcauchyon [0, 2pi]. There's no substantial difference and it's a shame thatwrapcauchyis on [0, 2pi] even though the literature I can find defines it on [-pi, pi]. It looks as ifwrapcauchyhas been around for a long time so I guess it's too late to change.Making a
rv_circularsubclass 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
locparameter is reduced to the circle on which the distribution is defined andscaleparameter is ignored.josef-pkt commentedon Mar 6, 2015
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.
aarchiba commentedon Mar 25, 2015
@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.
NeilGirdhar commentedon Apr 6, 2016
@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_linesince 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 usevonmises.ev-br commentedon Apr 6, 2016
Whether or not adding
vonmises_linewas a mistake (it very well might have been), IMO working outrv_circularor some such would be much more productive way of spending time and effort.NeilGirdhar commentedon Apr 6, 2016
@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.
josef-pkt commentedon Apr 7, 2016
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.
[-]von Mises random variate sampling broken for non-zero location parameter[/-][+]ENH: stats: circular distribution infrastructure[/+]mdhaber commentedon Jul 13, 2022
gh-7533 reported that the
vonmises.pdfdid not wrap around whenloc != 0, but that seems to be resolved. For example, withloc=1.5:Details
[-]ENH: stats: circular distribution infrastructure[/-][+]von Mises random variate sampling broken for non-zero location parameter[/+]mdhaber commentedon Jul 18, 2022
I think the original issue was closed by gh-16590. The more general issue is tracked in gh-6038 and gh-15938.