The binomial distribution is a discrete probability distribution which models the probability of having x successes in n independent Bernoulli trials with success probability p.
Usage
Binom(size = 1, prob = 0.5)
# S4 method for class 'Binom,numeric'
d(distr, x, log = FALSE)
# S4 method for class 'Binom,numeric'
p(distr, q, lower.tail = TRUE, log.p = FALSE)
# S4 method for class 'Binom,numeric'
qn(distr, p, lower.tail = TRUE, log.p = FALSE)
# S4 method for class 'Binom,numeric'
r(distr, n)
# S4 method for class 'Binom'
mean(x)
# S4 method for class 'Binom'
var(x)
# S4 method for class 'Binom'
sd(x)
# S4 method for class 'Binom'
skew(x)
# S4 method for class 'Binom'
kurt(x)
# S4 method for class 'Binom'
entro(x)
# S4 method for class 'Binom'
finf(x)
llbinom(x, size, prob)
# S4 method for class 'Binom,numeric'
ll(distr, x)
ebinom(x, size, type = "mle", ...)
# S4 method for class 'Binom,numeric'
mle(distr, x, na.rm = FALSE)
# S4 method for class 'Binom,numeric'
me(distr, x, na.rm = FALSE)
vbinom(size, prob, type = "mle")
# S4 method for class 'Binom'
avar_mle(distr)
# S4 method for class 'Binom'
avar_me(distr)
Arguments
- size
number of trials (zero or more).
- prob
numeric. Probability of success on each trial.
- distr
an object of class
Binom
.- x
For the density function,
x
is a numeric vector of quantiles. For the moments functions,x
is an object of classBinom
. For the log-likelihood and the estimation functions,x
is 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.- type
character, case ignored. The estimator type (mle or me).
- ...
extra arguments.
- na.rm
logical. Should the
NA
values be removed?
Value
Each type of function returns a different type of object:
Distribution Functions: When supplied with one argument (
distr
), thed()
,p()
,q()
,r()
,ll()
functions return the density, cumulative probability, quantile, random sample generator, and log-likelihood functions, respectively. When supplied with both arguments (distr
andx
), 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 binomial distribution is given by: $$ f(x; n, p) = \binom{n}{x} p^x (1 - p)^{n - x}, \quad N \in \mathbb{N}, \quad p \in (0, 1),$$ with \(x \in \{0, 1, \dots, N\}\).
Examples
# -----------------------------------------------------
# Binomial Distribution Example
# -----------------------------------------------------
# Create the distribution
N <- 10 ; p <- 0.7
D <- Binom(N, p)
# ------------------
# dpqr Functions
# ------------------
d(D, 0:N) # density function
#> [1] 0.0000059049 0.0001377810 0.0014467005 0.0090016920 0.0367569090
#> [6] 0.1029193452 0.2001209490 0.2668279320 0.2334744405 0.1210608210
#> [11] 0.0282475249
p(D, 0:N) # distribution function
#> [1] 0.0000059049 0.0001436859 0.0015903864 0.0105920784 0.0473489874
#> [6] 0.1502683326 0.3503892816 0.6172172136 0.8506916541 0.9717524751
#> [11] 1.0000000000
qn(D, c(0.4, 0.8)) # inverse distribution function
#> [1] 7 8
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.121060821 0.200120949 0.233474440 0.028247525 0.121060821 0.200120949
#> [7] 0.266827932 0.233474440 0.266827932 0.200120949 0.200120949 0.102919345
#> [13] 0.266827932 0.266827932 0.233474440 0.266827932 0.102919345 0.266827932
#> [19] 0.200120949 0.266827932 0.200120949 0.266827932 0.102919345 0.200120949
#> [25] 0.233474440 0.121060821 0.121060821 0.200120949 0.233474440 0.121060821
#> [31] 0.036756909 0.266827932 0.121060821 0.266827932 0.121060821 0.036756909
#> [37] 0.121060821 0.233474440 0.102919345 0.036756909 0.200120949 0.102919345
#> [43] 0.036756909 0.121060821 0.266827932 0.266827932 0.009001692 0.233474440
#> [49] 0.102919345 0.266827932 0.102919345 0.266827932 0.102919345 0.102919345
#> [55] 0.233474440 0.028247525 0.233474440 0.102919345 0.233474440 0.102919345
#> [61] 0.036756909 0.028247525 0.266827932 0.233474440 0.266827932 0.200120949
#> [67] 0.102919345 0.233474440 0.102919345 0.200120949 0.200120949 0.121060821
#> [73] 0.233474440 0.200120949 0.102919345 0.233474440 0.266827932 0.266827932
#> [79] 0.266827932 0.233474440 0.028247525 0.233474440 0.266827932 0.200120949
#> [85] 0.121060821 0.233474440 0.233474440 0.266827932 0.028247525 0.121060821
#> [91] 0.009001692 0.102919345 0.121060821 0.121060821 0.233474440 0.266827932
#> [97] 0.200120949 0.266827932 0.121060821 0.102919345
# ------------------
# Moments
# ------------------
mean(D) # Expectation
#> [1] 7
var(D) # Variance
#> [1] 2.1
sd(D) # Standard Deviation
#> [1] 1.449138
skew(D) # Skewness
#> [1] -0.2760262
kurt(D) # Excess Kurtosis
#> [1] -0.1238095
entro(D) # Entropy
#> Warning: The entropy given is an approximation in the O(1 / n) order.
#> [1] 2.58229
finf(D) # Fisher Information Matrix
#> [1] 47.61905
# List of all available moments
mom <- moments(D)
#> Warning: The entropy given is an approximation in the O(1 / n) order.
mom$mean # expectation
#> [1] 7
# ------------------
# Point Estimation
# ------------------
ll(D, x)
#> [1] -193.9824
llbinom(x, N, p)
#> [1] -193.9824
ebinom(x, size = N, type = "mle")
#> $prob
#> [1] 0.694
#>
ebinom(x, size = N, type = "me")
#> $prob
#> [1] 0.694
#>
mle(D, x)
#> $prob
#> [1] 0.694
#>
me(D, x)
#> $prob
#> [1] 0.694
#>
e(D, x, type = "mle")
#> $prob
#> [1] 0.694
#>
# ------------------
# Estimator Variance
# ------------------
vbinom(N, p, type = "mle")
#> prob
#> 0.021
vbinom(N, p, type = "me")
#> prob
#> 0.021
avar_mle(D)
#> prob
#> 0.021
avar_me(D)
#> prob
#> 0.021
v(D, type = "mle")
#> prob
#> 0.021