-
Notifications
You must be signed in to change notification settings - Fork 11
/
poisson.qmd
223 lines (154 loc) · 4.58 KB
/
poisson.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
::: {#fig-poissons layout-ncol=2}
![Siméon Denis Poisson](images/poisson.jpg){width=40%, height=40%}
![[Les Poissons](https://youtu.be/UoJxBEQRLd0?t=12)](images/poissons.jpeg){width=40%, height=40%}
"Les Poissons"
:::
---
:::::{#def-poisson}
#### Poisson distribution
$$\rangef{Y} = \set{0, 1, 2, ...} = \Nat$$
$$\P(Y = y) = \frac{\mu^{y} e^{-\mu}}{y!}, y \in \Nat$$ {#eq-pois-pmf}
::: notes
(see @fig-pois-pmf)
:::
$$\P(Y \le y) = e^{-\mu} \sum_{j=0}^{\floor{y}}\frac{\mu^j}{j!}$$ {#eq-pois-cdf}
::: notes
(see @fig-pois-cdfs)
:::
:::::
---
```{r}
#| label: fig-pois-pmf
#| fig-cap: "Poisson PMFs, by mean parameter $\\mu$"
library(dplyr)
pois_dists = tibble(
mu = c(0.5, 1, 2, 5, 10, 20)) |>
reframe(
.by = mu,
x = 0:30
) |>
mutate(
`P(X = x)` = dpois(x, lambda = mu),
`P(X <= x)` = ppois(x, lambda = mu),
mu = factor(mu)
)
library(ggplot2)
library(latex2exp)
plot0 = pois_dists |>
ggplot(
aes(
x = x,
y = `P(X = x)`,
fill = mu,
col = mu)) +
theme(legend.position = "bottom") +
labs(
fill = latex2exp::TeX("$\\mu$"),
col = latex2exp::TeX("$\\mu$"),
y = latex2exp::TeX("$\\Pr_{\\mu}(X = x)$"))
plot1 = plot0 +
geom_col(position = "identity", alpha = .5) +
facet_wrap(~mu)
# geom_point(alpha = 0.75) +
# geom_line(alpha = 0.75)
print(plot1)
```
---
```{r}
#| label: fig-pois-cdfs
#| fig-cap: "Poisson CDFs"
library(ggplot2)
plot2 =
plot0 +
geom_step(alpha = 0.75) +
aes(y = `P(X <= x)`) +
labs(y = latex2exp::TeX("$\\Pr_{\\mu}(X \\leq x)$"))
print(plot2)
```
---
:::{#exr-pois-dist-funs}
#### Poisson distribution functions
```{r}
#| label: def-mu-exr-pois
#| include: false
mu = 3.75
```
Let $X \sim \Pois(\mu = `r mu`)$.
Compute:
- $\P(X = 4 | \mu = `r mu`)$
- $\P(X \le 7 | \mu = `r mu`)$
- $\P(X > 5 | \mu = `r mu`)$
:::
---
::: solution
- $\P(X=4) = `r dpois(x = 4, lambda = mu)`$
- $\P(X\le 7) = `r ppois(q = 7, lambda = mu)`$
- $\P(X > 5) = `r ppois(q = 5, lambda = mu, lower.tail = FALSE)`$
:::
---
:::{#thm-poisson-properties}
#### Properties of the Poisson distribution
If $X \sim \Pois(\mu)$, then:
* $\Expp[X] = \mu$
* $\Varr(X) = \mu$
* $\P(X=x) = \frac{\mu}{x} \P(X = x-1)$
* For $x < \mu$, $\P(X=x) > \P(X = x-1)$
* For $x = \mu$, $\P(X=x) = \P(X = x-1)$
* For $x > \mu$, $\P(X=x) < \P(X = x-1)$
* $\arg \max_{x} \P(X=x) = \floor{\mu}$
:::
:::{#exr-poisson-properties}
Prove @thm-poisson-properties.
:::
---
::: {.solution .smaller}
$$
\begin{aligned}
\text{E}[X]
&= \sum_{x=0}^\infty x \cdot P(X=x)\\
&= 0 \cdot P(X=0) + \sum_{x=1}^\infty x \cdot P(X=x)\\
&= 0 + \sum_{x=1}^\infty x \cdot P(X=x)\\
&= \sum_{x=1}^\infty x \cdot P(X=x)\\
&= \sum_{x=1}^\infty x \cdot \frac{\lambda^x e^{-\lambda}}{x!}\\
&= \sum_{x=1}^\infty x \cdot \frac{\lambda^x e^{-\lambda}}{x \cdot (x-1)!} & [\text{definition of factorial ("!") function}]\\
&= \sum_{x=1}^\infty \frac{\lambda^x e^{-\lambda}}{ (x-1)!}\\
&= \sum_{x=1}^\infty \frac{(\lambda \cdot \lambda^{x-1}) e^{-\lambda}}{ (x-1)!}\\
&= \lambda \cdot \sum_{x=1}^\infty \frac{( \lambda^{x-1}) e^{-\lambda}}{ (x-1)!}\\
&= \lambda \cdot \sum_{y=0}^\infty \frac{( \lambda^{y}) e^{-\lambda}}{ (y)!} &[\text{substituting } y \eqdef x-1]\\
&= \lambda \cdot 1 &[\text{because PDFs sum to 1}]\\
&= \lambda\\
\end{aligned}
$$
See also <https://statproofbook.github.io/P/poiss-mean>.
For the variance, see <https://statproofbook.github.io/P/poiss-var>.
:::
---
#### Accounting for exposure
If the exposures/observation durations, denoted $T=t$ or $N=n$, vary between observations, we model:
$$\mu = \lambda\cdot t$$
$\lambda$ is interpreted as the "expected event rate per unit of exposure"; that is,
$$\lambda = \frac{\Expp[Y|T=t]}{t}$$
:::{.callout-important}
The exposure magnitude, $T$, is *similar* to a covariate in linear or logistic regression.
However, there is an important difference: in count regression, **there is no intercept corresponding to $\Expp[Y|T=0]$**.
In other words, this model assumes that if there is no exposure, there can't be any events.
:::
:::{#thm-exposure-log-scale}
If $\mu = \lambda\cdot t$, then:
$$\log \mu = \log{\lambda} + \log{t}$$
:::
:::{#def-offset}
#### Offset
When the linear component of a model involves a term without an unknown coefficient,
that term is called an **offset**.
:::
---
:::{#thm-sum-pois}
If $X$ and $Y$ are independent Poisson random variables with means
$\mu_X$ and $\mu_Y$, their sum, $Z=X+Y$, is also a Poisson random variable, with mean
$\mu_Z = \mu_X + \mu_Y$.
:::
---
::: proof
See <https://web.stanford.edu/class/archive/cs/cs109/cs109.1206/lectureNotes/LN12_independent_rvs.pdf>, Example 3.
:::