The world’s leading publication for data science, AI, and ML professionals.

Bernoulli Distribution with Python from Scratch

How to code bernoulli distribution from scratch for curious

Photo by Chris Ried on Unsplash
Photo by Chris Ried on Unsplash

To solve a problem, the common ready libraries are used but we don’t search the function how to work and how to serve our purpose. More importantly, if we need to modify a function, we don’t know how to do that.

So, the purpose of this article is how to code bernoulli Probability distributions and their some properties in a simple way, without using ready libraries like "SciPy" and gain some basic skills from scratch.

The bernoulli distribution is a discrete distribution that is used when a random experiment is performed and only two results are obtained such as good-bad, positive-negative, success-failure. Those statements are used to describe the probabilities of an event. Bernoulli trial is the simple way to represent an experiment like the outcome of a coin heads or tails, the result of an exam pass or failure, etc.

Probability Function

If X is a random variable and p is a probability of an event with this distribution, then:

bernoulli probability mass function
bernoulli probability mass function

Python Code:

#bernoulli probability mass function:
def pmf(x,p):
    f = p**x*(1-p)**(1-x)
    return f

Mean

The mean or expected value E(x) of Bernoulli random variable X is:

Python Code:

# expected value of x
def mean(p):
    return p

Variance and Standart Deviation

The variance of random variable X is:

Python Code:

#variance of x
def var(p):
    return p*(1-p)
#standard deviation is root of variance
def std(p):
    return var(p)**(1/2)

Generate Random Variates

To generate random variates corresponding to Bernoulli distribution

Python Code

import numpy as np
#size is a parameter that how many number generates
def rvs(p,size=1):
    rvs = np.array([])
    for i in range(0,size):
        if np.random.rand() <= p:
            a=1
            rvs = np.append(rvs,a)
        else:
            a=0
            rvs = np.append(rvs,a)
    return rvs

Let put them together

import numpy as np
#created a bernoulli class

class bernoulli():
    def pmf(x,p):
        """
        probability mass function        
        """
        f = p**x*(1-p)**(1-x)
        return f

    def mean(p):
        """
        expected value of bernoulli random variable
        """
        return p

    def var(p):
        """
        variance of bernoulli random variable
        """
        return p*(1-p)

    def std(p):
        """
        standart deviation of bernoulli random variable
        """
        return bernoulli.var(p)**(1/2)

    def rvs(p,size=1):
        """
        random variates
        """
        rvs = np.array([])
        for i in range(0,size):
            if np.random.rand() <= p:
                a=1
                rvs = np.append(rvs,a)
            else:
                a=0
                rvs = np.append(rvs,a)
        return rvs

Example

Suppose that a formula-1 racer has a 0.2 probability of having an accident. So that X = 1 if an accident occurs, and X = 0 otherwise. Find the expectation, variance and standart deviation of X and generate random variate 10 times.

p=0.2 # probability of having an accident
bernoulli.mean(p) # return -> 0.2
bernoulli.var(p) # return -> 0.16
bernoulli.std(p) # return -> 0.4
#each execution generates random numbers, so array may be change
bernoulli.rvs(p,size=10) 
#return-> array([0., 0., 0., 0., 1., 0., 1., 0., 0., 1.])

I hope this article is useful and gives you a different perspective.

References:

Bernoulli Distribution https://www.wikiwand.com/en/Bernoulli_distribution

Probability Distribution https://www.wikiwand.com/en/Probability_distribution


Related Articles