Computing probabilities

R provides basic commands that compute probabilities for our special families of distributions:

Each family has its own commands, but the pattern is the same.

Evaluating the PMF

To evaluate the PMF \(P(X=x)\), you use a d- command:

dbinom(x, size, prob)
dgeom(x, prob)
dpois(x, lambda)

For example:

# P(X = 0)
dbinom(0, 5, 0.5)       
[1] 0.03125
# P(X = 1)
dbinom(1, 5, 0.5)
[1] 0.15625
# P(X = 0 or X = 1)
dbinom(c(0, 1), 5, 0.5)
[1] 0.03125 0.15625
# Add up every probability
sum(dbinom(0:5, 5, 0.2))
[1] 1
# P(0 <= Z <- 6)
dpois(0:6, 5)
[1] 0.006737947 0.033689735 0.084224337 0.140373896 0.175467370 0.175467370
[7] 0.146222808

Notice that these d- commands are vectorized.

Evaluating the CDF

To evaluate the CDF \(F(q) = P(X\leq q)\), you use a p- command:

pbinom(q, size, prob)
pgeom(q, prob)
ppois(q, lambda)

Notice right away that the p- commands and d- commands are consistent with one another:

# P(X <= 2)
pbinom(2, 5, 0.3)
[1] 0.83692
# P(X = 0) + P(X = 1) + P(X = 2)
sum(dbinom(0:2, 5, 0.3))
[1] 0.83692