8000 DOC: Add example to ``rng.beta(...)`` by linus-md · Pull Request #25413 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC: Add example to rng.beta(...) #25413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
DOC: Improve beta example
[skip actions]
[skip travis]
[skip azp]
[skip cirrus]
  • Loading branch information
linus-md committed Dec 19, 2023
commit da05724b7c42497f0117304cfbfd23a503f89040
14 changes: 8 additions & 6 deletions numpy/random/_generator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ cdef class Generator:

Examples
--------
If ``a == b`` and both are > 1, the distribution is symmetric with
mean 0.5.
The beta distribution has mean a/(a+b). If ``a == b`` and both
are > 1, the distribution is symmetric with mean 0.5.

>>> rng = np.random.default_rng()
>>> a, b, size = 2.0, 2.0, 10000
Expand All @@ -415,10 +415,12 @@ cdef class Generator:
>>> sample_left = rng.beta(a=a, b=b, size=size)
>>> sample_right = rng.beta(a=b, b=a, size=size)
>>> m_left, m_right = np.mean(sample_left), np.mean(sample_right)
>>> m_left, m_right
(0.22334081861904062, 0.7749320166133927) # may vary
>>> m_left + m_right
0.9982728352324334 # may vary
>>> print(m_left, m_right)
0.2238596793678923 0.7774613834041182 # may vary
>>> print(m_left - a/(a+b))
0.001637457145670096 # may vary
>>> print(m_right - b/(a+b))
-0.0003163943736596009 # may vary

Display the histogram of the two samples:
>>> import matplotlib.pyplot as plt
Expand Down
0