.beta()

Anonymous contributor's avatar
Anonymous contributor
Published May 26, 2025Updated Jun 13, 2025
Contribute to Docs

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:

f(x;α,β)=1B(α,β)xα1(1x)β1f\left(x; \alpha, \beta \right) = \frac{1}{B\left(\alpha, \beta \right)} x^{\alpha -1} \left(1-x \right)^{\beta -1}

Where B represents the beta function:

B(α,β)=01tα1(1t)β1dtB(\alpha, \beta) = \int_0^1 t^{\alpha -1} (1-t)^{\beta -1 } dt

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 whether a and b 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 is None, a single random value is returned if both a and b 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 by size.

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 4
result = 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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy