Computation of probabilities (Binomial and hypergeometric distribution) using R — Software.
We will compute probabilities of Binomial probability distribution
- Usage
dbinom(x, size, prob, log = FALSE)
pbinom(q, size, prob, lower.tail = TRUE, log.p = FALSE)
qbinom(p, size, prob, lower.tail = TRUE, log.p = FALSE)
rbinom(n, size, prob)
- Arguments
| column 1 | column 2 |
|---|---|
| x, q | vector of quantiles. |
| p | vector of probabilities. |
| n | number of observations. If length(n) > 1, the length is taken to be the number required. |
| size | number of trials (zero or more). |
| prob | probability of success on each trial. |
| log, log.p | logical; if TRUE, probabilities p are given as log(p). |
| lower.tail | logical; if TRUE (default), probabilities are P[X \le x]P[X≤x], otherwise, P[X > x]P[X>x]. |
Details
The binomial distribution with size = n=n and prob = p=p has density
p(x) = {n \choose x} {p}^{x} {(1-p)}^{n-x}p(x)=(
x
n
)p^x(1−p)^n−x
for x = 0, \ldots, nx=0,…,n. Note that binomial coefficients can be computed by choose in R.
If an element of x is not integer, the result of dbinom is zero, with a warning.
p(x)p(x) is computed using Loader's algorithm, see the reference below.
The quantile is defined as the smallest value xx such that F(x) \ge pF(x)≥p, where FF is the distribution function.
Values
dbinom gives the density, pbinom gives the distribution function, qbinom gives the quantile function and rbinom generates random deviates.
If size is not an integer, NaN is returned.
The length of the result is determined by n for rbinom, and is the maximum of the lengths of the numerical arguments for the other functions.
The numerical arguments other than n are recycled to the length of the result. Only the first elements of the logical arguments are used.
Example problems
- A) Let X- B(n= 10,p = 0.3). Find P(2 < X < 6) and P(X=>5)
n = 10;n
p = 0.3;p
x = 3:5;x # P[2<X<6] = p[x=3] + p[x=4] + p[x=5]
ProbA.1 = sum(dbinom(x,n,p));ProbA.1
x = 5:10;x # To find P[x => 5]
ProbA.2 = sum(dbinom(x,n,p));ProbA.2
# ProbA.2 = 1 - pbinom(4,n,p);Prob1.2 . P[X <= x] = pbinom(x, n, p).
- B) Let X-B(n = 5,p = 0.2). Find P(X= 4) and P(X <= 2)
n = 5;n
p = 0.2;p
x = 4;x # p[X = 4]
ProbB.1 = dbinom(x,n,p);ProbB.1
ProbB.2 = pbinom(2,n,p); # P[X <= 2]
- C) Let X - B(n=5, p=0.2). Find k such that P(X=> k) = 0.2552
n = 5;n
p = 0.2;p
k=qbinom(0.2552,n,p);k
Hypergeometric distribution
- Usage
dhyper(x, m, n, k, log = FALSE)
phyper(q, m, n, k, lower.tail = TRUE, log.p = FALSE)
qhyper(p, m, n, k, lower.tail = TRUE, log.p = FALSE)
rhyper(nn, m, n, k)
- Arguments
| column 1 | column 2 |
|x, q| vector of quantiles representing the number of white balls drawn without replacement from an urn which contains both black and white balls. |
| m | the number of white balls in the urn. |
| n | the number of black balls in the urn. |
| k | the number of balls drawn from the urn, hence must be in 0,1,\dots, m+n0,1,…,m+n. |
| p | probability, it must be between 0 and 1. |
| nn | number of observations. If length(nn) > 1, the length is taken to be the number required. |
| log, log.p | logical; if TRUE, probabilities p are given as log(p). |
| lower.tail | logical; if TRUE (default), probabilities are P[X \le x]P[X≤x], otherwise, P[X > x]P[X>x]. |
Details
The hypergeometric distribution is used for sampling without replacement. The density of this distribution with parameters m, n and k (named NpNp, N-NpN−Np, and nn, respectively in the reference below, where N := m+nN:=m+n is also used in other references) is given by
p(x)=(
x
m
)(
k−x
n
)/(
k
m+n
)
for x = 0, \ldots, kx=0,…,k.
Note that p(x)p(x) is non-zero only for \max(0, k-n) \le x \le \min(k, m)max(0,k−n)≤x≤min(k,m)
Example problems
- D) Let X - Hyp(N = 12,M = 8, n=3). Find P(X < 2) and P(X=2).
N = 12;N
M = 8;M
n = 3;n
x = 0:1;x # P[X < 2]
ProbD.1 = sum(dhyper(x, N - M, M, n));ProbD.1
x = 2; # P[X = 2]
ProbD.2 = dhyper(x,N-M,M,n);ProbD.2
- E) Let X - Hyp(N = 20, M = 9, n = 5). Find P(3 <= X <= 6) and P(X => 4).
N = 20;N
M = 9;M
n = 5;n
x = 3:6;x # P[3 <= X <= 6]
ProbE.1 = sum(dhyper(x,N-M,M,n));ProbE.1
ProbE.2 = 1 - phyper(3, N-M,M,n);ProbE.2