How to generate random numbers from Beta distribution in Numpy

Shapes of Beta distribution and their parameters
Shapes of Beta distribution and their parameters

In this tutorial, we will learn how to generate random numbers from Beta distribution in Python using Numpy. We will use Numpy’s Random Generator object with. random module’s beta() function to generate random numbers from beta distribution.

We will start with learning the basics of Beta distribution. Then we will learn how to generate a single random number, a 1_D array of random numbers, and 2-D array or matrix of random numbers from Beta distribution using Numpy’s beta() function.

What is Beta Distribution

The beta distribution is a continuous probability distribution that is defined on the interval [0, 1] and is commonly used to model the probability of success in a binomial process (i.e., a series of independent, two-outcome events). It is parameterized by two shape parameters, alpha and beta, which control the shape of the distribution.

The beta distribution is useful/popular for a few reasons. Since it is defined on the interval [0, 1], it is very well suited for modeling probabilities. And also depending on the values of alpha and beta, it is flexible to change the shape of Beta distribution. Here is an example of different shapes t of beta distribution and their corresponding parameters.

Shapes of Beta distribution and their parameters

Let us get started by loading Numpy.

import numpy as np

The basic syntax of generating random number in Numpy is this, where a, b are the parameters alpha and beta and it controls shape of the beta distribution.

random.Generator.beta(a, b, size=None)

Generating a random number from beta distribution

We will generate a random number from three different set of parameters alpha (a) and beta(b).

First let us set the random generator object by instantiating using default_rng() function with a seed.

rng = np.random.default_rng(2022)

We can generate a random number with a = 2 and b = 2

rng.beta(a=2, b=2)

0.1706219925464454

Here we generate a random number with a = 1 and b = 5 using Numpy’s beta() function.

rng.beta(a=1, b=5)

0.19496266482763683

Here we generate a random number with a = 5 and b = 1 using Numpy’s beta() function.

rng.beta(a=5, b=1)

0.9043150823003117

Generating a 1-D array of random numbers from Beta distribution

Here we use the same shape parameters a and b as before, but this time we specify the argument size=10 to generate 10 random numbers from beta distribution.

rng.beta(a=2, b=2,  size=10)

array([0.57780648, 0.35569291, 0.41385092, 0.60801077, 0.68471871,
       0.5471684 , 0.91006246, 0.60178419, 0.78839202, 0.37842288])

Here the 1-d array with random numbers from beta distribution is generated using a =5 and b=1.

rng.beta(a=5, b=1,  size=10)

array([0.92888649, 0.97324572, 0.98370505, 0.8273475 , 0.90235001,
       0.82462658, 0.97997441, 0.9662021 , 0.81329731, 0.92557757])

Here the 1-d array with random numbers from beta distribution is generated using a =1 and b=5.

rng.beta(a=1, b=5,  size=10)

array([0.06307234, 0.18047868, 0.00069429, 0.30016567, 0.16456846,
       0.06142862, 0.10499444, 0.0668597 , 0.24838117, 0.0554329 ])

In both of the examples above we can clearly see the trend of skewing towards 1 and 0 is clear.

Generating a 2-D array of random numbers from Beta distribution

In these examples below, we show how to generate a matrix of dimension 3×3 with random numbers samples from beta distribution.

First, we use a=1 and b=5 as shape parameters to generate random numbers between 0 to 1, but skewed towards 0 with Numpy’s beta() function.

rng.beta(a=1, b=5,  size=(3,3))

array([[0.09601718, 0.22013485, 0.3938391 ],
       [0.13066181, 0.03355825, 0.13609113],
       [0.07612266, 0.05309854, 0.12993868]])

First, we use a=5 and b=1 as shape parameters to generate random numbers between 0 to 1, but skewed towards 1 with Numpy’s beta() function.

rng.beta(a=5, b=1,  size=(3,3))
array([[0.54353228, 0.69803459, 0.98964326],
       [0.91476447, 0.85729941, 0.61320125],
       [0.95792108, 0.95349669, 0.80066977]])