-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtransformations.Rmd
215 lines (170 loc) · 8.39 KB
/
transformations.Rmd
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
# Transformations {#transformations}
## Motivating Example {-}
You buy fencing for a square enclosure. The length of fencing that you buy is uniformly distributed between
0 and 4 meters. What is the distribution of the *area* of the enclosure?
Most people realize that the area must be between 0 and 1 square meters,
since the length of each side must be between 0 and 1 meters, and
the square of a number between 0 and 1 is also between 0 and 1. But how likely
are the values in between?
## Theory {-}
We can formalize the above example as follows. The length of fencing, $L$, is a
$\text{Uniform}(a=0, b=4)$ random variable, and we are interested in the
distribution of $A = (L/4)^2$.
First, let's simulate the distribution of $A$. The Colab below begins with a
uniform random variable $L$, defines $A = (L / 4)^2$, and displays 10000 simulations of
both $L$ and $A$.
<script src="https://gist.github.com/dlsun/952e02f66b471c7f368c74755b05b8a3.js"></script>
In the simulation above, $L$ is equally likely to take on
all values between 0 and 4. This is not surprising because $L$ was defined to be this way
(a uniform distribution between 0 and 4).
The distribution of $A$ is more interesting. It is much
more concentrated toward the lower values than the higher values. In hindsight,
this makes sense because $(L / 4)$ is a number between 0 and 1, and numbers between 0
and 1 get smaller when we square them.
How do we derive the p.d.f. of $A$? This is a special case of a more general problem.
We have a random variable $X$ whose distribution we know, and we want to find the distribution of
$Y = g(X)$, a transformation of $X$. The strategy for these problems is this:
1. Calculate the c.d.f. of $Y$ (by rewriting the event in terms of $X$ and using the known distribution of $X$).
2. Take the derivative to obtain the p.d.f. of $Y$ (by Definition \@ref(def:pdf)).
<iframe width="560" height="315" src="https://www.youtube.com/embed/avr6r81o60Y" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
```{example}
Let's apply this strategy to the example at the beginning of the lesson.
First, we find the c.d.f. of $A$, the area of the enclosure. We write out the definition
of the c.d.f., express $A$ in terms of $L$, and rearrange the expression to make
the probability easy to calculate.
\begin{align*}
F_A(x) &= P(A \leq x) \\
&= P\left((L/4)^2 \leq x\right) \\
&= P(L \leq 4 \sqrt{x}) & \text{(if $x \geq 0$)} \\
&= \begin{cases} 0 & x < 0 \\ \sqrt{x} & 0 \leq x \leq 1 \\ 1 & x > 1 \end{cases}.
\end{align*}
Now we take the derivative to get the p.d.f.
\[ f_A(x) = \frac{d}{dx} F_A(x) = \begin{cases} \frac{1}{2\sqrt{x}} & 0 \leq x \leq 1 \\ 0 & \text{otherwise} \end{cases}. \]
We graph the p.d.f.s of $L$ and $A$ below. Compare these with our simulations above.
```{r, echo=FALSE, fig.show = "hold", fig.align = "center",fig.cap="Distributions of $L$ and $A$", fig.asp=0.6}
par(mgp=c(1, 1, 0))
t <- seq(0, 4, by=0.1)
plot(t, rep(1/4, length(t)), type="l", lwd=3, col="blue",
xaxt="n", yaxt="n", bty="n",
xlab="x", ylab="Probability Density Function f(x)",
xlim=c(-0.5, 4.5), ylim=c(-0.05, 2.1))
axis(1, pos=0)
axis(2, pos=0)
lines(c(-0.5, 0), c(0, 0), lwd=3, col="blue")
lines(c(4, 4.5), c(0, 0), lwd=3, col="blue")
t <- seq(0.01, 1, by=0.01)
lines(t, 1 / (2 * sqrt(t)), lwd=2, col="orange")
axis(1, pos=0)
axis(2, pos=0)
lines(c(-0.5, 0), c(0, 0), lwd=2, col="orange")
lines(c(1, 4.5), c(0, 0), lwd=2, col="orange")
legend(3.8, 2, c("L", "A"),
lwd=c(3, 2), col=c("blue", "orange"))
```
Now, we derive formulas for the two most common types of transformations:
shifting by a constant and scaling by a constant.
```{theorem shift-transform, name="Shift Transformation"}
Let $X$ be a continuous random variable with p.d.f. $f_X$.
The p.d.f. of $Y = X + b$ is
\begin{equation}
f_Y(y) = f_X(y - b).
(\#eq:shift-transform)
\end{equation}
```{proof}
We can express the c.d.f. of $Y$ in terms of the c.d.f. of $X$:
\begin{align*}
F_Y(y) &= P(Y \leq y) \\
&= P(X + b \leq y) \\
&= P(X \leq y - b) \\
&= F_X(y - b).
\end{align*}
Taking the derivative, we see that the p.d.f. is
\[ f_Y(y) = \frac{d}{dy} F_Y(y) = f_X(y - b). \]
The figure below illustrates a hypothetical p.d.f. for a random variable $X$ and the
corresponding p.d.f. for the shifted random variable $Y = X + 1$. Notice how the p.d.f.
is exactly the same, except shifted to the right by 1, as we would expect.
```{r, echo=FALSE, fig.show = "hold", fig.align = "center",fig.cap="Example of a Shift Transformation", fig.asp=0.6}
par(mgp=c(1, 1, 0))
t <- seq(-1, 1, by=0.1)
plot(t, 1 - abs(t), type="l", lwd=3,
xaxt="n", yaxt="n", bty="n",
xlab="x", ylab="Probability Density Function f(x)",
xlim=c(-2.5, 2.5), ylim=c(-0.05, 1.1))
axis(1, pos=0)
lines(c(-2.5, -1), c(0, 0), lwd=3)
lines(c(1, 2.5), c(0, 0), lwd=3)
t <- seq(0, 2, by=0.1)
lines(t, 1 - abs(t - 1), lwd=2, col="blue")
axis(1, pos=0)
lines(c(-2.5, 0), c(0, 0), lwd=2, col="blue")
lines(c(2, 2.5), c(0, 0), lwd=2, col="blue")
legend(-2.5, 1.1, c(expression(f[X]), expression(f[Y])),
lwd=c(3, 2), col=c("black", "blue"))
```
```{theorem scale-transform, name="Scale Transformation"}
Let $X$ be a continuous random variable with p.d.f. $f_X$.
The p.d.f. of $Y = aX$, where $a > 0$ is a constant, is
\begin{equation}
f_Y(y) = \frac{1}{a} f_X(\frac{y}{a}).
(\#eq:scale-transform)
\end{equation}
```{proof}
We can express the c.d.f. of $Y$ in terms of the c.d.f. of $X$:
\begin{align*}
F_Y(y) &= P(Y \leq y) \\
&= P(aX \leq y) \\
&= P(X \leq \frac{y}{a}) \\
&= F_X(\frac{y}{a}).
\end{align*}
Taking the derivative (don't forget the chain rule!), we see that the p.d.f. is
\[ f_Y(y) = \frac{d}{dy} F_Y(y) = \frac{1}{a} f_X(\frac{y}{a}). \]
The figure below illustrates a hypothetical p.d.f. for a random variable $X$ and the
corresponding p.d.f. for the scaled random variable $Y = 2X$. Notice that the p.d.f.
is the same shape, just wider and shorter.
```{r, echo=FALSE, fig.show = "hold", fig.align = "center",fig.cap="Example of a Scale Transformation", fig.asp=0.6}
par(mgp=c(1, 1, 0))
t <- seq(-1, 1, by=0.1)
plot(t, 1 - abs(t), type="l", lwd=3,
xaxt="n", yaxt="n", bty="n",
xlab="x", ylab="Probability Density Function f(x)",
xlim=c(-2.5, 2.5), ylim=c(-0.05, 1.1))
axis(1, pos=0)
lines(c(-2.5, -1), c(0, 0), lwd=3)
lines(c(1, 2.5), c(0, 0), lwd=3)
t <- seq(-2, 2, by=0.1)
lines(t, (1 - abs(t / 2)) / 2, lwd=2, col="red")
axis(1, pos=0)
lines(c(-2.5, -2), c(0, 0), lwd=2, col="red")
lines(c(2, 2.5), c(0, 0), lwd=2, col="red")
legend(-2.5, 1.1, c(expression(f[X]), expression(f[Y])),
lwd=c(3, 2), col=c("black", "red"))
```
The factor of $\frac{1}{a}$ on the inside in
\@ref(eq:scale-transform) is what makes the p.d.f. wider, and the factor of $\frac{1}{a}$ on the
outside is what makes it shorter. It is necessary to make the p.d.f. shorter if we make it
wider, since the total area under a p.d.f. must always be 1.
## Essential Practice {-}
1. You inflate a spherical balloon in a single breath. If the volume of air you exhale in a
single breath (in cubic inches) is $\text{Uniform}(a=36\pi, b=288\pi)$ random variable, what
is the p.d.f. of the radius of the balloon (in inches)?
2. The lifetime of a lightbulb (in hours) follows an
$\text{Exponential}(\lambda=\frac{1}{1200})$ distribution.
Find the p.d.f. of the lifetime of the lightbulb in _days_
(assuming the lightbulb is on 24 hours per day).
3. A lighthouse on a shore is shining light toward the ocean at a
random angle $U$ (measured in radians), where $U$ follows a
$\text{Uniform}(a=-\frac{\pi}{2}, b=\frac{\pi}{2})$ distribution.
Consider a line which is parallel to the shore and 1 mile away from the
shore, as illustrated below. An angle of 0 would mean the ray of
light is perpendicular to the shore, while an angle of $\pi/2$
would mean the ray is along the shore, shining to the right
from the perspective of the figure.
![](https://github.com/dlsun/probability/blob/master/docs/bookdown-demo_files/figure-html/lighthouse.png?raw=true)
Let $X$ be the point that the light hits on the line, where the
line's origin is the point on the line that is closest to the
lighthouse. Find the p.d.f. of $X$.
(_Hint:_ Use trigonometry to write $X$ as a function of $U$.)
## Additional Practice {-}
1. The radius of a circle (in inches) is chosen from an
$\text{Exponential}(\lambda=1.5)$ distribution. Find the
p.d.f. of the _area_ of the circle.