.beta()
In the random
module of NumPy, the .beta()
method generates random samples from a Beta distribution, which is defined by two shape parameters, a
and b
. This distribution is commonly used in Bayesian inference to model the distribution of probabilities and in order statistics due to its flexibility in shape.
The Beta distribution has the probability function:
Where B
represents the beta function:
Syntax
numpy.random.beta(a, b, size=None)
Parameters:
a
(float or array_like of floats): The alpha shape parameter. This must be a positive value.b
(float or array_like of floats): The beta shape parameter. This must also be positive.size
(Optional): Defines the shape of the output array. If not provided, the behavior depends on whethera
andb
are scalars or arrays.
Return value:
In NumPy, the .beta()
function returns a randomly drawn sample or an array of samples from beta distribution configured according to a
and b
.
- If
size
isNone
, a single random value is returned if botha
andb
are scalars. Otherwise,np.broadcast(a, b).size
samples are returned. - If
size
is specified, an array of randomly generated values is returned, with the shape of the array determined bysize
.
Example: Generating Random Values from a Beta Distribution
The example below shows how to generate random values from a beta distribution configured with an alpha and beta value:
import numpy as np# Generate 5 random values from a beta distribution with an alpha of 3 and a beta of 4result = np.random.beta(3, 4, size = 5)print(result)
A possible output of this code can be:
[0.14092969 0.52861406 0.15658351 0.545189 0.47077243]
This code randomly draws 5 values from a beta distribution with an alpha of 3 and a beta of 4.
Note: The output may vary with each execution because the values are randomly generated.
Codebyte Example
In this codebyte example, we sample 5 values from a beta distribution with an alpha or (a
) of 2 and a beta or (b
) of 5:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours