Skip to content

DOC: improve the vonmises docstring example - #5814

Merged
charris merged 1 commit into
numpy:masterfrom
argriffing:vonmises-docstring
Apr 29, 2015
Merged

charris merged 1 commit into
numpy:masterfrom
argriffing:vonmises-docstring

Conversation

@argriffing

Copy link
Copy Markdown
Contributor

closes #5811

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tested? I note that y is no longer normalized.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, y is not divided by its maximum value anymore. But why were they doing that originally? I think that with this change, both the histogram and pdf should integrate to ~1.0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm guessing that the curve was supposed to pass through the tops of the histogram bars. Might be worth running a check...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No, the normed argument of hist normalizes the area not the maximum. So in http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.vonmises.html the histogram has area 1.0 but the density curve has maximum 1.0, and it's mostly a coincidence that the maximum of the histogram is also near 1.0. If not for this coincidence, the problems mentioned in #5811 probably would have been found sooner.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not arguing at this point ;) I'd just like to see the plot...

@argriffing

Copy link
Copy Markdown
Contributor Author

I'm not arguing at this point ;) I'd just like to see the plot...

import numpy as np

mu, kappa = 0.0, 4.0 # mean and dispersion
s = np.random.vonmises(mu, kappa, 1000)

# Display the histogram of the samples, along with
# the probability density function:

import matplotlib.pyplot as plt
from scipy.special import i0
plt.hist(s, 50, normed=True)
x = np.linspace(-np.pi, np.pi, num=51)
y = np.exp(kappa*np.cos(x-mu))/(2*np.pi*i0(kappa))
plt.plot(x, y, linewidth=2, color='r')
plt.savefig('vonmises.png')

vonmises

This depends on the random number seed, and it uses the same number of samples and bins as before. Increasing the number of samples will make the histogram agree more closely with the curve.

charris added a commit that referenced this pull request Apr 29, 2015
DOC: improve the vonmises docstring example
@charris
charris merged commit bd821f5 into numpy:master Apr 29, 2015
@charris

charris commented Apr 29, 2015

Copy link
Copy Markdown
Member

Great, thanks @argriffing .

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

numpy.random.vonmises documentation contains incorrect code example.

3 participants