The range and the histogram of the joint pmf of rolling a dice and recording the larger value as $X$ and the smaller value as $Y$. The blue dot is the point $(\mu_X,\mu_Y)$ and it is the center of the joint distribution.
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
# setup the figure and axes
fig = plt.figure(figsize=(15,15))
ax = fig.add_subplot(111, projection='3d')
meanx=1
meany=4
sigx=0.3
sigy=0.2
rho = 0.6
def pdf(X,Y):
z_x = (X-meanx)/sigx
z_y = (Y-meany)/sigy
Z = np.exp(-(z_y**2 - 2*rho*z_x*z_y + z_x**2)/(2*(1-rho**2)))/(2*np.pi*sigx*sigy*np.sqrt(1-rho**2))
return Z
x = np.linspace(meanx-3*sigx, meanx+3*sigx, 2000)
y = np.linspace(meany-3*sigy, meany+3*sigy, 2000)
X, Y = np.meshgrid(x, y)
Z = pdf(X, Y)
ax.plot_wireframe(X, Y, Z, linewidth=0.3, antialiased=True)
# ax.bar3d(x_cord-0.5, y_cord-0.5, z_cord, x_width, y_width, z_height, shade=True, alpha=1, color='#039be5', edgecolor='black')
ax.view_init(20,40)
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.9,0.7, r" $\mu_X$={}".format(meanx)+"\n"+r" $\mu_Y=${}".format(meany)+"\n"+
r" $\sigma_X=${}".format(sigx)+"\n"+r" $\sigma_Y=${}".format(sigy)+
"\n"+r" $\rho=${}".format(rho),ha="left", va="top",
backgroundcolor=(0.1, 0.1, 1, 0.2), fontsize="large")
plt.title("Bivariate normal pdf")
plt.show();
# nbi:hide_in
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
plt.figure(figsize=(12,8))
meanx=1
meany=4
sigx=0.3
sigy=0.2
rho = 0.9
def pdf(X,Y):
z_x = (X-meanx)/sigx
z_y = (Y-meany)/sigy
Z = np.exp(-(z_y**2 - 2*rho*z_x*z_y + z_x**2)/(2*(1-rho**2)))/(2*np.pi*sigx*sigy*np.sqrt(1-rho**2))
return Z
x = np.linspace(meanx-3*sigx, meanx+3*sigx, 2000)
y = np.linspace(meany-3*sigy, meany+3*sigy, 2000)
X, Y = np.meshgrid(x, y)
Z = pdf(X, Y)
plt.contourf(X, Y, Z, 20, cmap = "Blues")
cb=plt.colorbar()
cb.remove()
xval = np.linspace(meanx-3*sigx, meanx+3*sigx, 100)
yval = (xval-meanx)*rho*sigy/sigx + meany
plt.plot(xval,yval, linewidth=0.6, label="least squares line", color="g")
plt.scatter(meanx, meany, color = "red", s=20 , label ="mean")
plt.title("Contour of bivariate normal pdf with its least squares line")
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.8,0.8, r" $\mu_X$={}".format(meanx)+"\n"+r" $\mu_Y=${}".format(meany)+"\n"+
r" $\sigma_X=${}".format(sigx)+"\n"+r" $\sigma_Y=${}".format(sigy)+
"\n"+r" $\rho=${}".format(rho),ha="left", va="top",
backgroundcolor=(0.1, 0.1, 1, 0.2), fontsize="large")
plt.show();