8000 DOC: Add example for ``np.random.default_rng().geometric()`` (#25357) · numpy/numpy@284a2f0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 284a2f0

Browse files
authored
DOC: Add example for np.random.default_rng().geometric() (#25357)
* DOC: Add example for ``rng.geometric()`` [skip actions] [skip travis] [skip azp] [skip cirrus] * DOC: Improve plot * DOC: add examples to _generator.py * DOC: add examples to _generator.py
1 parent a43c068 commit 284a2f0

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

numpy/random/_generator.pyx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,19 +3423,34 @@ cdef class Generator:
34233423
out : ndarray or scalar
34243424
Drawn samples from the parameterized geometric distribution.
34253425
3426+
References
3427+
----------
3428+
3429+
.. [1] Wikipedia, "Geometric distribution",
3430+
https://en.wikipedia.org/wiki/Geometric_distribution
3431+
34263432
Examples
34273433
--------
3428-
Draw ten thousand values from the geometric distribution,
3429-
with the probability of an individual success equal to 0.35:
3434+
Draw 10.000 values from the geometric distribution, with the
3435+
probability of an individual success equal to ``p = 0.35``:
34303436
3437+ >>> p, size = 0.35, 10000
34313438
>>> rng = np.random.default_rng()
3432-
>>> z = rng.geometric(p=0.35, size=10000)
3439+
>>> sample = rng.geometric(p=p, size=size)
34333440
3434-
How many trials succeeded after a single run?
3441+
What proportion of trials succeeded after a single run?
34353442
3436-
>>> (z == 1).sum() / 10000.
3437-
0.34889999999999999 # random
3443+
>>> (sample == 1).sum()/size
3444+
0.34889999999999999 # may vary
34383445
3446+
The geometric distribution with ``p=0.35`` looks as follows:
3447+
3448+
>>> import matplotlib.pyplot as plt
3449+
>>> count, bins, _ = plt.hist(sample, bins=30, density=True)
3450+
>>> plt.plot(bins, (1-p)**(bins-1)*p)
3451+
>>> plt.xlim([0, 25])
3452+
>>> plt.show()
3453+
34393454
"""
34403455
return disc(&random_geometric, &self._bitgen, size, self.lock, 1, 0,
34413456
p, 'p', CONS_BOUNDED_GT_0_1,

0 commit comments

Comments
 (0)
0