Bernoulli Distribution with Python from Scratch | by Deniz Ağırdan | Towards Data Science

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.
If X is a random variable and p is a probability of an event with this distribution, then:
Python Code:
#bernoulli probability mass function:def pmf(x,p):    f = p**x*(1-p)**(1-x)    return f
The mean or expected value E(x) of Bernoulli random variable X is:
Python Code:
# expected value of xdef mean(p):    return p
The variance of random variable X is:
Python Code:
#variance of xdef var(p):    return p*(1-p)#standard deviation is root of variancedef std(p):    return var(p)**(1/2)
To generate random variates corresponding to Bernoulli distribution
Python Code
import numpy as np#size is a parameter that how many number generatesdef 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
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 accidentbernoulli.mean(p) # return -> 0.2bernoulli.var(p) # return -> 0.16bernoulli.std(p) # return -> 0.4#each execution generates random numbers, so array may be changebernoulli.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.
Bernoulli Distribution https://www.wikiwand.com/en/Bernoulli_distribution
Probability Distribution https://www.wikiwand.com/en/Probability_distribution