Bernoulli Distribution is a special case of Binomial distribution where only a single trial is performed. It is a discrete probability distribution for a Bernoulli trial (a trial that has only two outcomes i.e. either success or failure). For example, it can be represented as a coin toss where the probability of getting the head is 0.5 and getting a tail is 0.5. It is a probability distribution of a random variable that takes value 1 with probability p and the value 0 with probability q=1-p. The Bernoulli distribution is a special case of the binomial distribution with n=1.
The probability mass function f of this distribution, over possible outcomes k, is given by :
The above relation can also be expressed as:
In R Programming Language, there are 4 built-in functions to for Bernoulli distribution and all of them are discussed below.
dbern()
dbern( ) function in R programming measures density function of Bernoulli distribution.
Syntax: dbern(x, prob, log = FALSE)
Parameter:
- x: vector of quantiles
- prob: probability of success on each trial
- log: logical; if TRUE, probabilities p are given as log(p)
In statistics, it is given by below formula:
Example:
R
# import Rlab library
library
(Rlab)
# x values for the
# dbern( ) function
x <-
seq
(0, 10, by = 1)
# using dbern( ) function
# to x to obtain corresponding
# Bernoulli PDF
y <-
dbern
(x, prob = 0.7)
# plot dbern values
plot
(y, type =
"o"
)
Output:
pbern()
pbern( ) function in R programming giver the distribution function for the Bernoulli distribution. The distribution function or cumulative distribution function (CDF) or cumulative frequency function, describes the probability that a variate X takes on a value less than or equal to a number x.
Syntax: pbern(q, prob, lower.tail = TRUE, log.p = FALSE)
Parameter:
- q: vector of quantiles
- prob: probability of success on each trial
- lowe.tail: logical
- log.p: logical; if TRUE, probabilities p are given as log(p).
Example:
R
# import Rlab library
library
(Rlab)
# x values for the
# pbern( ) function
x <-
seq
(0, 10, by = 1)
# using pbern( ) function
# to x to obtain corresponding
# Bernoulli CDF
y <-
pbern
(x, prob = 0.7)
# plot pbern values
plot
(y, type =
"o"
)
Output:
The above plot represents the Cumulative Distribution Function of Bernoulli Distribution in R.
qbern()
qbern( ) gives the quantile function for the Bernoulli distribution. A quantile function in statistical terms specifies the value of the random variable such that the probability of the variable being less than or equal to that value equals the given probability.
Syntax: qbern(p, prob, lower.tail = TRUE, log.p = FALSE)
Parameter:
- p: vector of probabilities.
- prob: probability of success on each trial.
- lower.tail: logical
- log.p: logical; if TRUE, probabilities p are given as log(p).
Example:
R
# import Rlab library
library
(Rlab)
# x values for the
# qbern( ) function
x <-
seq
(0, 1, by = 0.2)
# using qbern( ) function
# to x to obtain corresponding
# Bernoulli QF
y <-
qbern
(x, prob = 0.5,lower.tail =
TRUE
, log.p =
FALSE
)
# plot qbern values
plot
(y, type =
"o"
)
Output:
rbern()
rbern( ) function in R programming is used to generate a vector of random numbers which are Bernoulli distributed.
Syntax: rbern(n, prob)
Parameter:
- n: number of observations.
- prob: number of observations.
Example:
R
# import Rlab library
library
(Rlab)
set.seed
(98999)
# sample size
N <- 1000
# generate random variables using
# rbern( ) function
random_values <-
rbern
(N, prob = 0.5)
# print the values
print
(random_values)
# plot of randomly
# drawn density
hist
(random_values,breaks = 10,main =
""
)
Output:
[1] 0 0 1 1 0 0 0 0 0 1 0 0 0 1 0 1 1 0 1 0 1 1 1 0 1 0 1 1 0 1 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 1 1 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 0 0 0 0 1
[68] 1 1 1 0 0 1 0 1 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 1 0 1 0 0 0 0 1 0 0 1 1 0 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 1 1 1 0 1
[135] 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 1 0 1 1 1 1 1 0 0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 0 0
[202] 1 1 1 1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 1 1 0 0 1 0 0 1 1 1 0 1 1 0 1 1 0 0 0 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 0 1 0
[269] 0 0 1 1 0 1 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 1 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