Skip to content
Browse files

DOC: stats.distributions: update discrete example. Closes #1421.

  • Loading branch information...
1 parent df4008d commit 7d608b2640495e10441d7f21bae3a214b386e2da @rgommers committed Jan 2, 2012
Showing with 60 additions and 9 deletions.
  1. +6 −5 scipy/stats/__init__.py
  2. +54 −4 scipy/stats/distributions.py
View
11 scipy/stats/__init__.py
@@ -158,18 +158,19 @@
.. autosummary::
:toctree: generated/
- binom -- Binomial
bernoulli -- Bernoulli
- nbinom -- Negative Binomial
+ binom -- Binomial
+ boltzmann -- Boltzmann (Truncated Discrete Exponential)
+ dlaplace -- Discrete Laplacian
geom -- Geometric
hypergeom -- Hypergeometric
logser -- Logarithmic (Log-Series, Series)
- poisson -- Poisson
+ nbinom -- Negative Binomial
planck -- Planck (Discrete Exponential)
- boltzmann -- Boltzmann (Truncated Discrete Exponential)
+ poisson -- Poisson
randint -- Discrete Uniform
+ skellam -- Skellam
zipf -- Zipf
- dlaplace -- Discrete Laplacian
Statistical functions
=====================
View
58 scipy/stats/distributions.py
@@ -307,8 +307,29 @@ def instancemethod(func, obj, cls):
"""
docdict_discrete['frozennote'] = _doc_default_frozen_note
-docdict_discrete['example'] = _doc_default_example.replace('[0.9,]',
- 'Replace with reasonable value')
+_doc_default_discrete_example = \
+"""Examples
+--------
+>>> from scipy.stats import %(name)s
+>>> [ %(shapes)s ] = [<Replace with reasonable values>]
+>>> rv = %(name)s(%(shapes)s)
+
+Display frozen pmf
+
+>>> x = np.arange(0, np.minimum(rv.dist.b, 3))
+>>> h = plt.vlines(x, 0, rv.pmf(x), lw=2)
+
+Check accuracy of cdf and ppf
+
+>>> prb = %(name)s.cdf(x, %(shapes)s)
+>>> h = plt.semilogy(np.abs(x - %(name)s.ppf(prb, %(shapes)s)) + 1e-20)
+
+Random number generation
+
+>>> R = %(name)s.rvs(%(shapes)s, size=100)
+
+"""
+docdict_discrete['example'] = _doc_default_discrete_example
_doc_default_before_notes = ''.join([docdict_discrete['longsummary'],
docdict_discrete['allmethods'],
@@ -6640,10 +6661,39 @@ class hypergeom_gen(rv_discrete):
pmf(k, M, n, N) = choose(n, k) * choose(M - n, N - k) / choose(M, N),
for N - (M-n) <= k <= min(m,N)
- %(example)s
+ Examples
+ --------
+ >>> from scipy.stats import hypergeom
- """
+ Suppose we have a collection of 20 animals, of which 7 are dogs. Then if
+ we want to know the probability of finding a given number of dogs if we
+ choose at random 12 of the 20 animals, we can initialize a frozen
+ distribution and plot the probability mass function:
+ >>> [M, n, N] = [20, 7, 12]
+ >>> rv = hypergeom(M, n, N)
+ >>> x = np.arange(0, n+1)
+ >>> pmf_dogs = rv.pmf(x)
+
+ >>> fig = plt.figure()
+ >>> ax = fig.add_subplot(111)
+ >>> ax.plot(x, pmf_dogs, 'bo')
+ >>> ax.vlines(x, 0, pmf_dogs, lw=2)
+ >>> ax.set_xlabel('# of dogs in our group of chosen animals')
+ >>> ax.set_ylabel('hypergeom PMF')
+ >>> plt.show()
+
+ Instead of using a frozen distribution we can also use class methods of
+ `hypergeom` directly. To for example obtain the cumulative distribution
+ function, use:
+
+ >>> prb = hypergeom.cdf(x, M, n, N)
+
+ And to generate random numbers:
+
+ >>> R = hypergeom.rvs(M, n, N, size=10)
+
+ """
def _rvs(self, M, n, N):
return mtrand.hypergeometric(n,M-n,N,size=self._size)
def _argcheck(self, M, n, N):

0 comments on commit 7d608b2

Please sign in to comment.
Something went wrong with that request. Please try again.