BMLR Chapter 3
Cornell College
STA 363 Spring 2025 Block 8
\[\begin{equation*} P(Y = y) = p^y(1-p)^{1-y} \quad \textrm{for} \quad y = 0, 1. \end{equation*}\]
Next we consider \(P(Y = 1)\), and are interested in the probability of exactly 1 success anywhere among the 4 trials.
How many different ways can we get exactly 1 success in the four trials?
To find the probability of this what else do we need to count?
What is the probability?
There are \(\binom{4}{1} = 4\) ways to have exactly 1 success in 4 trials, \(P(Y = 1) = \binom{4}{1}p^1(1-p)^{4-1} = (4)(0.25)(0.75)^3\).
\[\begin{equation} P(Y=y) = \binom{n}{y} p^y (1-p)^{n-y} \quad \textrm{for} \quad y = 0, 1, \ldots, n. \end{equation}\]
\[\begin{align*} P(Y=y) &= \binom{1}{y} p^y(1-p)^{1-y} \\ &= p^y(1-p)^{1-y}\quad \textrm{for}\quad y = 0, 1, \end{align*}\] a Bernoulli distribution! - In fact, Bernoulli random variables are a special case of binomial random variables where \(n=1\).
dbinom(y, n, p)
, which outputs the probability of \(y\) successes given \(n\) trials with probability \(p\), i.e., \(P(Y=y)\) for \(Y \sim \textrm{Binomial}(n,p)\).\[\begin{equation} P(Y=y) = \binom{y + r - 1}{r-1} (1-p)^{y}(p)^r \quad \textrm{for}\quad y = 0, 1, \ldots, \infty. \end{equation}\]
#negativeBinomialPlots
plotNBinom <- function(p, r){
ynb <- 0:10000
nbd <- tibble(x = rnbinom(ynb, r, p))
#breaks <- pretty(range(nbd$x), n = nclass.FD(nbd$x), min.n = 1) # pretty binning
#bwidth <- breaks[2] - breaks[1]
ggplot(nbd, aes(x = x)) +
geom_histogram(aes(y=..count../sum(..count..)), binwidth = .25) +
labs(title = paste("p = ", p, ", r = ", r),
x = "number of failures",
y = "probability") +
xlim(-1,30)
}
NBin1 <- plotNBinom(0.35, 3)
NBin2 <- plotNBinom(0.35, 5)
NBin3 <- plotNBinom(0.70, 5)
grid.arrange(NBin1, NBin2, NBin3, ncol = 1)
\[\begin{align*} P(Y=y) &= \binom{y}{0} (1-p)^yp \\ &= (1-p)^yp \quad \textrm{for} \quad y = 0, 1, \ldots, \infty, \end{align*}\] which is the probability mass function of a geometric random variable!
Thus, a geometric random variable is, in fact, a special case of a negative binomial random variable.
While negative binomial random variables typically are expressed as above using binomial coefficients (expressions such as \(\binom{x}{y}\)), we can generalize our definition to allow non-integer values of \(r\).
R function dnbinom(y, r, p)
for the probability of \(y\) failures before the \(r\)th success given probability \(p\).
\[\begin{equation} P(Y=y) = \frac{e^{-\lambda}\lambda^y}{y!} \quad \textrm{for} \quad y = 0, 1, \ldots, \infty, \end{equation}\] where \(\lambda\) is the mean or expected count in the unit of time or space of interest. - This probability mass function has \(\operatorname{E}(Y) = \lambda\) and \(\operatorname{SD}(Y) = \sqrt{\lambda}\).
dpois(y, lambda)
outputs the probability of \(y\) events given \(\lambda\).#poissonPlots
plotPois <- function(lam){
yp = 0:10000 # possible values
pd <- data.frame(x=rpois(yp, lam)) # generate random deviates
breaks <- pretty(range(pd$x), n = nclass.FD(pd$x), min.n = 1) # pretty binning
#bwidth <- breaks[2] - breaks[1]
ggplot(pd, aes(x = x)) + geom_histogram(aes(y=..count../sum(..count..)), binwidth = .25) +
xlab("number of events") + ylab("probability") +
labs(title=paste("Poisson lambda = ", lam)) + xlim(-1,13)
}
Pois1 <- plotPois(0.5)
Pois2 <- plotPois(1)
Pois3 <- plotPois(5) + scale_y_continuous(breaks = c(0, 0.1))
grid.arrange(Pois1,Pois2,Pois3,ncol=1)
\[\begin{align*} P(a \le Y \le b) = \int_a^b f(y)dy. \end{align*}\]
A few properties of continuous random variables:
\[\begin{equation} f(y) = \lambda e^{-\lambda y} \quad \textrm{for} \quad y > 0, \end{equation}\] - \(\operatorname{E}(Y) = 1/\lambda\) and \(\operatorname{SD}(Y) = 1/\lambda\).
Exponential distributions with \(\lambda = 0.5, 1,\) and \(5\).
#exponentialPlots
x=seq(0,4,by=0.01) # possible values
probex1 <- dexp(x,.5) # P(Y=y)
probex2 <- dexp(x,1)
probex3 <- dexp(x,5)
Expdf <- tibble(x,probex1, probex2, probex3) %>%
rename(x = x,
`0.5` = probex1,
`1` = probex2,
`5` = probex3) %>%
gather(2:4, key = "Lambda", value = "value") %>%
mutate(Lambda = factor(Lambda, levels = c("0.5", "1", "5")))
ggplot(data = Expdf, aes(x = x, y = value, color = Lambda)) +
geom_line(aes(linetype = Lambda)) +
xlab("values") + ylab("density") +
labs(title = "Exponential Distributions") +
xlim(0,4) + ylim(0,3)
pexp(y, lambda)
outputs the probability \(P(Y < y)\) given \(\lambda\).\[\begin{equation} f(y) = \frac{\lambda^r}{\Gamma(r)} y^{r-1} e^{-\lambda y}\quad \textrm{for} \quad y >0. \end{equation}\]
x <- seq(0, 7, by = 0.01)
`r = 1, lambda = 1` <- dgamma(x, 1, rate = 1)
`r = 2, lambda = 1` <- dgamma(x, 2, rate = 1)
`r = 5, lambda = 5` <- dgamma(x, 5, rate = 5)
`r = 5, lambda = 7` <- dgamma(x, 5, rate = 7)
gammaDf <- tibble(x, `r = 1, lambda = 1`, `r = 2, lambda = 1`, `r = 5, lambda = 5`, `r = 5, lambda = 7`) %>%
gather(2:5, key = "Distribution", value = "value") %>%
mutate(Distribution = factor(Distribution,
levels = c("r = 2, lambda = 1",
"r = 1, lambda = 1",
"r = 5, lambda = 5",
"r = 5, lambda = 7")))
ggplot(data = gammaDf, aes(x = x, y = value,
color = Distribution)) +
geom_line(aes(linetype = Distribution)) +
xlab("values") + ylab("density") +
labs(title = "Gamma Distributions") +
theme(legend.title = element_blank())
\[\begin{align*} f(y) &= \frac{\lambda}{\Gamma(1)} y^{1-1} e^{-\lambda y} \\ &= \lambda e^{-\lambda y} \quad \textrm{for} \quad y > 0, \end{align*}\] an exponential distribution. - Just as how the geometric distribution was a special case of the negative binomial, exponential distributions are in fact a special case of gamma distributions!
pgamma(y, r, lambda)
outputs the probability \(P(Y < y)\) given \(r\) and \(\lambda\).Would not fit on a slide
Click HERE
The “web” of distributions: https://www.acsu.buffalo.edu/~adamcunn/probability/poisson.html
These slides are based on content in BMLR: Chapter 1 - Review of Multiple Linear Regression