# HIDDEN
from datascience import *
from prob140 import *
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
%matplotlib inline
import math
from scipy import stats
from sympy import *
init_printing()
from IPython.display import YouTubeVideo
YouTubeVideo('K4NlpU_Kd_0')
Loading...
🎥 See More
Loading...
Let random variables X and Y have the joint density defined by
f(x,y)={30(y−x)4,0<x<y<10otherwise
def jt_dens(x,y):
if y < x:
return 0
else:
return 30 * (y-x)**4
Plot_3d(x_limits=(0,1), y_limits=(0,1), f=jt_dens, cstride=4, rstride=4)
Then the possible values of (X,Y) are in the upper right hand triangle of the unit square.
You can see the reasoning behind this calculation in the graph below. The blue strip shows the event {X∈dx} for a value of x very near 0.25. To find the volume P(X∈dx), we hold x fixed and add over all y.
By analogy with the discrete case, fX is sometimes called the marginal density of X.
In our example, the possible values of (X,Y) are the upper left hand triangle as shown above. So for each fixed x, the possible values of Y go from x to 1.
Correspondingly, the density of Y can be found by fixing y and integrating over x as follows:
fY(y)=∫xf(x,y)dxfor all y
In our example, the joint density surface indicates that Y is more likely to be near 1 than near 0, which is confirmed by calculation. Remember that y>x and therefore for each fixed y, the possible values of x are 0 through y.
For 0<y<1,
fY(y)=∫0y30(y−x)4dx=6y5
# NO CODE
y_vals = np.arange(0, 1.01, 0.01)
f_Y = 6*y_vals**5
plt.plot(y_vals, f_Y, color='darkblue', lw=2)
plt.xlabel('$y$')
plt.ylabel('$f_Y(y)$', rotation=0)
plt.title('$f_Y$: Density of $Y$');
This gives us a division rule for densities. For a fixed value x, the conditional density of Y given X=x is defined by
fY∣X=x(y)=fX(x)f(x,y)for all y
Since X has a density, we know that P(X=x)=0 for all x. But the ratio above is of densities, not probabilities. It might help your intuition to think of “given X=x” to mean “given that X is just around x”.
Visually, the shape of this conditional density is the vertical cross section at x of the joint density graph above. The numerator determines the shape, and the denominator is part of the constant that makes the density integrate to 1.
Note that x is constant in this formula; it is the given value of X. So the denominator fX(x) is the same for all the possible values of y.
To see that the conditional density does integrate to 1, let’s do the integral.
In our example, let x=0.4 and consider finding the conditional density of Y given X=0.4. Under that condition, the possible values of Y are in the range 0.4 to 1, and therefore
The figure below shows the overlaid graphs of the density of Y and the conditional density of Y given X=0.4. You can see that the conditional density is more concentrated on large values of Y, because under the condition X=0.4 you know that Y can’t be small.
# NO CODE
plt.plot(y_vals, f_Y, color='darkblue', lw=2, label='Density of $Y$')
new_y = np.arange(0.4, 1.01, 0.01)
dens = (5/(0.6**5)) * (new_y - 0.4)**4
plt.plot(new_y, dens, color='gold', lw=2, label='Density of $Y$ given $X=0.4$')
plt.legend()
plt.xlim(0, 1)
plt.xlabel('$y$');
We can use conditional densities to find probabilities and expectations, just as we would use an ordinary density. Here are some examples of calculations. In each case we will set up the integrals and then use SymPy.
Now we will use the conditional density to find a conditional expectation. Remember that in our example, given that X=0.4 the possible values of Y go from 0.4 to 1.
You can condition X on Y in the same way. By analogous arguments, for any fixed value of y the conditional density of X given Y=y is
fX∣Y=y(x)=fY(y)f(x,y)for all x
All the examples in this section and the previous one have started with a joint density function that apparently emerged out of nowhere. In the next section, we will study a context in which they arise.