The Bernoulli distribution is a discrete probability distribution which takes the value 1 with probability \(p\) and the value 0 with probability \(1 - p\), where \(0 \leq p \leq 1\).
Usage
Bern(prob = 0.5)
dbern(x, prob, log = FALSE)
pbern(q, prob, lower.tail = TRUE, log.p = FALSE)
qbern(p, prob, lower.tail = TRUE, log.p = FALSE)
rbern(n, prob)
# S4 method for class 'Bern,numeric'
d(distr, x, log = FALSE)
# S4 method for class 'Bern,numeric'
p(distr, q, lower.tail = TRUE, log.p = FALSE)
# S4 method for class 'Bern,numeric'
qn(distr, p, lower.tail = TRUE, log.p = FALSE)
# S4 method for class 'Bern,numeric'
r(distr, n)
# S4 method for class 'Bern'
mean(x)
# S4 method for class 'Bern'
median(x)
# S4 method for class 'Bern'
mode(x)
# S4 method for class 'Bern'
var(x)
# S4 method for class 'Bern'
sd(x)
# S4 method for class 'Bern'
skew(x)
# S4 method for class 'Bern'
kurt(x)
# S4 method for class 'Bern'
entro(x)
# S4 method for class 'Bern'
finf(x)
llbern(x, prob)
# S4 method for class 'Bern,numeric'
ll(distr, x)
ebern(x, type = "mle", ...)
# S4 method for class 'Bern,numeric'
mle(distr, x, na.rm = FALSE)
# S4 method for class 'Bern,numeric'
me(distr, x, na.rm = FALSE)
vbern(prob, type = "mle")
# S4 method for class 'Bern'
avar_mle(distr)
# S4 method for class 'Bern'
avar_me(distr)Arguments
- prob
- numeric. Probability of success. 
- x
- For the density function, - xis a numeric vector of quantiles. For the moments functions,- xis an object of class- Bern. For the log-likelihood and the estimation functions,- xis the sample of observations.
- log, log.p
- logical. Should the logarithm of the probability be returned? 
- q
- numeric. Vector of quantiles. 
- lower.tail
- logical. If TRUE (default), probabilities are \(P(X \leq x)\), otherwise \(P(X > x)\). 
- p
- numeric. Vector of probabilities. 
- n
- number of observations. If - length(n) > 1, the length is taken to be the number required.
- distr
- an object of class - Bern.
- type
- character, case ignored. The estimator type (mle or me). 
- ...
- extra arguments. 
- na.rm
- logical. Should the - NAvalues be removed?
Value
Each type of function returns a different type of object:
- Distribution Functions: When supplied with one argument ( - distr), the- d(),- p(),- q(),- r(),- ll()functions return the density, cumulative probability, quantile, random sample generator, and log-likelihood functions, respectively. When supplied with both arguments (- distrand- x), they evaluate the aforementioned functions directly.
- Moments: Returns a numeric, either vector or matrix depending on the moment and the distribution. The - moments()function returns a list with all the available methods.
- Estimation: Returns a list, the estimators of the unknown parameters. Note that in distribution families like the binomial, multinomial, and negative binomial, the size is not returned, since it is considered known. 
- Variance: Returns a named matrix. The asymptotic covariance matrix of the estimator. 
Details
The probability mass function (PMF) of the Bernoulli distribution is given by: $$ f(x; p) = p^x (1 - p)^{1 - x}, \quad p \in (0, 1), \quad x \in \{0, 1\}.$$
Examples
# -----------------------------------------------------
# Bernoulli Distribution Example
# -----------------------------------------------------
# Create the distribution
p <- 0.7
D <- Bern(p)
# ------------------
# dpqr Functions
# ------------------
d(D, c(0, 1)) # density function
#> [1] 0.3 0.7
p(D, c(0, 1)) # distribution function
#> [1] 0.3 1.0
qn(D, c(0.4, 0.8)) # inverse distribution function
#> [1] 1 1
x <- r(D, 100) # random generator function
# alternative way to use the function
df <- d(D) ; df(x) # df is a function itself
#>   [1] 0.7 0.3 0.7 0.7 0.7 0.7 0.7 0.7 0.3 0.3 0.3 0.7 0.7 0.7 0.7 0.7 0.7 0.7
#>  [19] 0.7 0.3 0.7 0.7 0.3 0.7 0.3 0.3 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.7
#>  [37] 0.3 0.3 0.7 0.7 0.7 0.7 0.7 0.7 0.7 0.3 0.3 0.3 0.3 0.7 0.7 0.7 0.7 0.7
#>  [55] 0.7 0.3 0.7 0.3 0.7 0.7 0.3 0.7 0.7 0.7 0.7 0.7 0.3 0.7 0.7 0.7 0.7 0.7
#>  [73] 0.7 0.7 0.7 0.7 0.7 0.7 0.3 0.7 0.7 0.7 0.7 0.7 0.7 0.3 0.7 0.7 0.7 0.7
#>  [91] 0.3 0.7 0.7 0.3 0.3 0.7 0.7 0.3 0.7 0.7
# ------------------
# Moments
# ------------------
mean(D) # Expectation
#> [1] 0.7
median(D) # Median
#> [1] 1
mode(D) # Mode
#> [1] 1
var(D) # Variance
#> [1] 0.21
sd(D) # Standard Deviation
#> [1] 0.4582576
skew(D) # Skewness
#> [1] -0.8728716
kurt(D) # Excess Kurtosis
#> [1] -1.238095
entro(D) # Entropy
#> [1] 0.6108643
finf(D) # Fisher Information Matrix
#> [1] 4.761905
# List of all available moments
mom <- moments(D)
mom$mean # expectation
#> [1] 0.7
# ------------------
# Point Estimation
# ------------------
ll(D, x)
#> [1] -56.00264
llbern(x, p)
#> [1] -56.00264
ebern(x, type = "mle")
#> $prob
#> [1] 0.76
#> 
ebern(x, type = "me")
#> $prob
#> [1] 0.76
#> 
mle(D, x)
#> $prob
#> [1] 0.76
#> 
me(D, x)
#> $prob
#> [1] 0.76
#> 
e(D, x, type = "mle")
#> $prob
#> [1] 0.76
#> 
mle("bern", x) # the distr argument can be a character
#> $prob
#> [1] 0.76
#> 
# ------------------
# Estimator Variance
# ------------------
vbern(p, type = "mle")
#> prob 
#> 0.21 
vbern(p, type = "me")
#> prob 
#> 0.21 
avar_mle(D)
#> prob 
#> 0.21 
avar_me(D)
#> prob 
#> 0.21 
v(D, type = "mle")
#> prob 
#> 0.21