Генерация распределения бернулли питон

How to model the Bernoulli distribution in Python

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

Bernoulli distribution is a particular case of the discrete probability distribution function (PDF) in which the number of observations is one, due to the conduction of a single experiment.

Bernoulli’s trial has two possible outcomes: success or failure, 1 or 0, yes or no, etc.

Formula

If p is the probability of an event having this distribution, and x is a random variable, then the probability mass function (PMF) of the Bernoulli distribution is as follows:

Here, x is either 0 or 1, and the value of p ranges from 0 to 1.

The mean of Bernoulli’s distribution is equal to p, while its variance is p(1-p).

Implementation in Python

SciPy is a built-in module of Python used for mathematical and scientific computations. The scipy.stats package is a sub-package that contains multiple probability distributions, frequency and summary statistics, masked statistics, correlation functions, statistical tests, and other features.

We use the scipy.stats package to import bernoulli in Python.

Syntax

from scipy.stats import bernoulli

Code

The Python code for the bar plot that uses the probability mass function to represent the Bernoulli distribution is given below.

This is a case of the Bernoulli distribution with p = 0.8. The outcome of the experiment can take the values of 0 and 1. The Bernoulli random variable’s values can be either 0 or 1.

We use the PMF function to calculate the probability of various random variable values.

# Import the required libraries
from scipy.stats import bernoulli
import matplotlib.pyplot as plt
# Instance of Bernoulli distribution with parameter p = 0.8
bd=bernoulli(0.8)
# Outcome of random variable either 0 or 1
x=[0,1]
# For the visualization of the bar plot of Bernoulli's distribution
plt.figure(figsize=(10,10))
plt.xlim(-2, 2)
plt.bar(x, bd.pmf(x), color='blue')
# For labelling of Bar plot
plt.title('Bernoulli distribution (p=0.8)', fontsize='20')
plt.xlabel('Values of random variable x (0, 1)', fontsize='20')
plt.ylabel('Probability', fontsize='20')
plt.show()

Explanation

  • Lines 2–3: We import the required libraries.
  • Line 6: We make an instance of Bernoulli’s distribution with the parameter p=0.8 .
  • Line 9: The experiment’s result could be 0 or 1 , so we place it in x .
  • Lines 12–14: We use these commands to make a bar plot of the PDF of Bernoulli’s distribution.
  • Lines 17–19: We label and title the bar plot.

Learn in-demand tech skills in half the time

Источник

Implement Bernoulli Distribution Using Python NumPy

Implement Bernoulli Distribution Using Python NumPy

  1. Bernoulli Distribution
  2. Binomial Distribution
  3. Implement Bernoulli Distribution With the Help of NumPy in Python
Читайте также:  Android exit app java

As a really important topic in Statistics, the Bernoulli distribution is an essential aspect of Data Science, Machine Learning, and other related fields because it helps deal with data. This tutorial will demonstrate the Bernoulli distribution and how it can be implemented in Python with the help of the NumPy library.

When we talk about Bernoulli distribution, the term Binomial Distribution always comes to mind. These terms are alike but differ in some of their workings.

These two distributions come in handy for making problems easier at different times. It is important to understand and effectively differentiate between these terms, as described in the article below.

Bernoulli Distribution

In simple terms, when there are only two possible outcomes in a single trial, this given data is appropriate for implementing Bernoulli Distribution.

The direct implementation of Bernoulli distribution can be done with the help of the SciPy library. However, the NumPy library can also be indirectly utilized to implement this distribution.

To explain it more clearly, let us assign Bernoulli Distribution as D(b) .

Binomial Distribution

The concept of Binomial Distribution can be defined as something that deals with a group or a set of Bernoulli Trials. The number of this group or set can usually be defined.

In simpler words, Binomial Distribution deals with multiple trials of an individual event, compared to Bernoulli Distribution which deals with a single trial of an individual event.

With respect to the above-taken assignment of Bernoulli Distribution as D(b) , the binomial distribution would be assigned as D(n,b) . This assignment reflects on the relationship between these terms.

Implement Bernoulli Distribution With the Help of NumPy in Python

You must be wondering what is the significance of Binomial Distribution when talking about Bernoulli Distribution. Well, there is a great significance.

The NumPy library directly uses the function NumPy.random.binomial() to implement the Binomial Distribution. The syntax of the NumPy.random.binomial() function can be seen below.

random.binomial(n, p, size=None) 
  • n : This is the number of trials. It can be inputted as float or integer but is eventually truncated into an int value.
  • p : This is the probability of success; it will always be more than 0 or less than 1 . It is a float value.
  • size : This helps provide the shape of the output. If set to None , it only provides a single value.

When we give the value of the number of sets of Bernoulli trials to be 1 in this case, we can indirectly implement the Bernoulli Distribution inside the Binomial Distribution.

The following code uses the NumPy.random.binomial function to implement Bernoulli Distribution in Python.

We take an example of a coin (having only two possibilities, heads and tails) being thrown 4 times. When we take n as 1 , it qualifies as Bernoulli Distribution rather than Binomial Distribution, which is how we will proceed in the code.

import numpy as dragon n = 1 p = 0.5 x = dragon.random.binomial(n, p, size=4) print(x) 

The above code provides the following output:

Читайте также:  Html container 100 width

Here, we showed the result of flipping a single coin tested 4 times. It is important to note that as it is a random trial, rerunning the program would probably generate a different set of outputs.

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

Источник

scipy.stats.bernoulli#

As an instance of the rv_discrete class, bernoulli object inherits from it a collection of generic methods (see below for the full list), and completes them with details specific for this particular distribution.

The probability mass function for bernoulli is:

bernoulli takes \(p\) as shape parameter, where \(p\) is the probability of a single success and \(1-p\) is the probability of a single failure.

The probability mass function above is defined in the “standardized” form. To shift distribution use the loc parameter. Specifically, bernoulli.pmf(k, p, loc) is identically equivalent to bernoulli.pmf(k — loc, p) .

>>> import numpy as np >>> from scipy.stats import bernoulli >>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots(1, 1) 

Calculate the first four moments:

>>> p = 0.3 >>> mean, var, skew, kurt = bernoulli.stats(p, moments='mvsk') 

Display the probability mass function ( pmf ):

>>> x = np.arange(bernoulli.ppf(0.01, p), . bernoulli.ppf(0.99, p)) >>> ax.plot(x, bernoulli.pmf(x, p), 'bo', ms=8, label='bernoulli pmf') >>> ax.vlines(x, 0, bernoulli.pmf(x, p), colors='b', lw=5, alpha=0.5) 

Alternatively, the distribution object can be called (as a function) to fix the shape and location. This returns a “frozen” RV object holding the given parameters fixed.

Freeze the distribution and display the frozen pmf :

>>> rv = bernoulli(p) >>> ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, . label='frozen pmf') >>> ax.legend(loc='best', frameon=False) >>> plt.show() 

../../_images/scipy-stats-bernoulli-1_00_00.png

Check accuracy of cdf and ppf :

>>> prob = bernoulli.cdf(x, p) >>> np.allclose(x, bernoulli.ppf(prob, p)) True 
>>> r = bernoulli.rvs(p, size=1000) 

rvs(p, loc=0, size=1, random_state=None)

pmf(k, p, loc=0)

Probability mass function.

logpmf(k, p, loc=0)

Log of the probability mass function.

cdf(k, p, loc=0)

Cumulative distribution function.

logcdf(k, p, loc=0)

Log of the cumulative distribution function.

Survival function (also defined as 1 — cdf , but sf is sometimes more accurate).

logsf(k, p, loc=0)

Log of the survival function.

ppf(q, p, loc=0)

Percent point function (inverse of cdf — percentiles).

isf(q, p, loc=0)

Inverse survival function (inverse of sf ).

stats(p, loc=0, moments=’mv’)

Mean(‘m’), variance(‘v’), skew(‘s’), and/or kurtosis(‘k’).

entropy(p, loc=0)

(Differential) entropy of the RV.

expect(func, args=(p,), loc=0, lb=None, ub=None, conditional=False)

Expected value of a function (of one argument) with respect to the distribution.

Читайте также:  Php запрос на обновление страницы

median(p, loc=0)

Median of the distribution.

Variance of the distribution.

Standard deviation of the distribution.

interval(confidence, p, loc=0)

Confidence interval with equal areas around the median.

Источник

Bernoulli Distribution with Python from Scratch

How to code bernoulli distribution from scratch for curious

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:

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() a=1
rvs = np.append(rvs,a)
else:
a=0
rvs = np.append(rvs,a)
return rvs
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() 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 accidentbernoulli.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.

Источник

Оцените статью