# Simple illustration of the MVN
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
x1 = np.linspace(-5, 5, 100)
x2 = np.linspace(-5, 5, 100)
xx1, xx2 = np.meshgrid(x1,x2)
X = np.vstack((xx1.flatten(), xx2.flatten())).T
# multivariate Gaussian
mean = np.asarray([1,1])
Sigma = np.eye(2,2)
D = 2
MVN = np.zeros(np.shape(X)[0],)
for i in np.arange(np.shape(X)[0]):
current = X[i,:]
tmpL = np.matmul(current-mean,np.linalg.inv(Sigma))
tmp = np.matmul(tmpL, (current-mean).T)
MVN[i] = 1/((2*np.pi)**(D/2) * np.linalg.det(Sigma)*(1/2))\
* np.exp(-(1/2)*(tmp))
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.plot_surface(xx1, xx2, np.reshape(MVN, np.shape(xx1)), \
alpha=0.5, cmap=cm.get_cmap('hsv'))
plt.show()
plt.contour(xx1, xx2, np.reshape(MVN, np.shape(xx1)), cmap=cm.get_cmap('hsv'))
plt.axis('equal')
plt.show()
# Simple illustration of the MVN
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
x1 = np.linspace(-5, 5, 100)
x2 = np.linspace(-5, 5, 100)
xx1, xx2 = np.meshgrid(x1,x2)
X = np.vstack((xx1.flatten(), xx2.flatten())).T
# multivariate Gaussian
mean = np.asarray([1,1])
Sigma = np.eye(2,2)
D = 2
MVN = np.zeros(np.shape(X)[0],)
for i in np.arange(np.shape(X)[0]):
current = X[i,:]
tmpL = np.matmul(current-mean,np.linalg.inv(Sigma))
tmp = np.matmul(tmpL, (current-mean).T)
MVN[i] = 1/((2*np.pi)**(D/2) * np.linalg.det(Sigma)*(1/2))\
* np.exp(-(1/2)*(tmp))
# Set up a figure twice as tall as it is wide
fig = plt.figure(figsize=plt.figaspect(.4))
fig.suptitle('Standard Multivariate Normal Distribution')
# First subplot
ax = fig.add_subplot(1,2, 1)
ax.contour(xx1, xx2, np.reshape(MVN, np.shape(xx1)), cmap=cm.get_cmap('hsv'))
ax.axis('equal')
# Second subplot
ax = fig.add_subplot(1, 2, 2, projection='3d')
surf = ax.plot_surface(xx1, xx2, np.reshape(MVN, np.shape(xx1)), \
alpha=0.5, cmap=cm.get_cmap('hsv'))
plt.show()
# Simple illustration of the MVN
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
x1 = np.linspace(-5, 5, 100)
x2 = np.linspace(-5, 5, 100)
xx1, xx2 = np.meshgrid(x1,x2)
X = np.vstack((xx1.flatten(), xx2.flatten())).T
# multivariate Gaussian
mean = np.asarray([1,1])
Sigma = np.eye(2,2)
Sigma[0,1] = .5
Sigma[1,0] = .5
D = 2
MVN = np.zeros(np.shape(X)[0],)
for i in np.arange(np.shape(X)[0]):
current = X[i,:]
tmpL = np.matmul(current-mean,np.linalg.inv(Sigma))
tmp = np.matmul(tmpL, (current-mean).T)
MVN[i] = 1/((2*np.pi)**(D/2) * np.linalg.det(Sigma)*(1/2))\
* np.exp(-(1/2)*(tmp))
# Set up a figure twice as tall as it is wide
fig = plt.figure(figsize=plt.figaspect(.4))
fig.suptitle('Adding off diagonal entries in Covariance')
# First subplot
ax = fig.add_subplot(1,2, 1)
ax.contour(xx1, xx2, np.reshape(MVN, np.shape(xx1)), cmap=cm.get_cmap('hsv'))
ax.axis('equal')
# Second subplot
ax = fig.add_subplot(1, 2, 2, projection='3d')
surf = ax.plot_surface(xx1, xx2, np.reshape(MVN, np.shape(xx1)), \
alpha=0.5, cmap=cm.get_cmap('hsv'))
plt.show()
np.linalg.eig(Sigma)
(array([1.5, 0.5]), array([[ 0.70710678, -0.70710678], [ 0.70710678, 0.70710678]]))
# Simple illustration of the MVN
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
x1 = np.linspace(-5, 5, 100)
x2 = np.linspace(-5, 5, 100)
xx1, xx2 = np.meshgrid(x1,x2)
X = np.vstack((xx1.flatten(), xx2.flatten())).T
# multivariate Gaussian
mean = np.asarray([1,1])
Sigma = np.eye(2,2)
Sigma[0,1] = .8
Sigma[1,0] = .8
D = 2
MVN = np.zeros(np.shape(X)[0],)
for i in np.arange(np.shape(X)[0]):
current = X[i,:]
tmpL = np.matmul(current-mean,np.linalg.inv(Sigma))
tmp = np.matmul(tmpL, (current-mean).T)
MVN[i] = 1/((2*np.pi)**(D/2) * np.linalg.det(Sigma)*(1/2))\
* np.exp(-(1/2)*(tmp))
# Set up a figure twice as tall as it is wide
fig = plt.figure(figsize=plt.figaspect(.4))
fig.suptitle('Adding off diagonal entries in Covariance')
# First subplot
ax = fig.add_subplot(1,2, 1)
ax.contour(xx1, xx2, np.reshape(MVN, np.shape(xx1)), cmap=cm.get_cmap('hsv'))
ax.axis('equal')
# Second subplot
ax = fig.add_subplot(1, 2, 2, projection='3d')
surf = ax.plot_surface(xx1, xx2, np.reshape(MVN, np.shape(xx1)), \
alpha=0.5, cmap=cm.get_cmap('hsv'))
plt.show()
np.linalg.eig(Sigma)
(array([1.8, 0.2]), array([[ 0.70710678, -0.70710678], [ 0.70710678, 0.70710678]]))
# Simple illustration of the MVN
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
x1 = np.linspace(-5, 5, 100)
x2 = np.linspace(-5, 5, 100)
xx1, xx2 = np.meshgrid(x1,x2)
X = np.vstack((xx1.flatten(), xx2.flatten())).T
# multivariate Gaussian
mean = np.asarray([1,1])
Sigma = np.eye(2,2)
Sigma[0,1] = -.5
Sigma[1,0] = -.5
D = 2
MVN = np.zeros(np.shape(X)[0],)
for i in np.arange(np.shape(X)[0]):
current = X[i,:]
tmpL = np.matmul(current-mean,np.linalg.inv(Sigma))
tmp = np.matmul(tmpL, (current-mean).T)
MVN[i] = 1/((2*np.pi)**(D/2) * np.linalg.det(Sigma)*(1/2))\
* np.exp(-(1/2)*(tmp))
# Set up a figure twice as tall as it is wide
fig = plt.figure(figsize=plt.figaspect(.4))
fig.suptitle('Adding off diagonal entries in Covariance')
# First subplot
ax = fig.add_subplot(1,2, 1)
ax.contour(xx1, xx2, np.reshape(MVN, np.shape(xx1)), cmap=cm.get_cmap('hsv'))
ax.axis('equal')
# Second subplot
ax = fig.add_subplot(1, 2, 2, projection='3d')
surf = ax.plot_surface(xx1, xx2, np.reshape(MVN, np.shape(xx1)), \
alpha=0.5, cmap=cm.get_cmap('hsv'))
plt.show()
np.linalg.eig(Sigma)
(array([1.5, 0.5]), array([[ 0.70710678, 0.70710678], [-0.70710678, 0.70710678]]))
# Simple illustration of the MVN
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
x1 = np.linspace(-5, 5, 100)
x2 = np.linspace(-5, 5, 100)
xx1, xx2 = np.meshgrid(x1,x2)
X = np.vstack((xx1.flatten(), xx2.flatten())).T
# multivariate Gaussian
mean = np.asarray([1,1])
Sigma = np.eye(2,2)
Sigma[0,1] = -.8
Sigma[1,0] = -.8
D = 2
MVN = np.zeros(np.shape(X)[0],)
for i in np.arange(np.shape(X)[0]):
current = X[i,:]
tmpL = np.matmul(current-mean,np.linalg.inv(Sigma))
tmp = np.matmul(tmpL, (current-mean).T)
MVN[i] = 1/((2*np.pi)**(D/2) * np.linalg.det(Sigma)*(1/2))\
* np.exp(-(1/2)*(tmp))
# Set up a figure twice as tall as it is wide
fig = plt.figure(figsize=plt.figaspect(.4))
fig.suptitle('Adding off diagonal entries in Covariance')
# First subplot
ax = fig.add_subplot(1,2, 1)
ax.contour(xx1, xx2, np.reshape(MVN, np.shape(xx1)), cmap=cm.get_cmap('hsv'))
ax.axis('equal')
# Second subplot
ax = fig.add_subplot(1, 2, 2, projection='3d')
surf = ax.plot_surface(xx1, xx2, np.reshape(MVN, np.shape(xx1)), \
alpha=0.5, cmap=cm.get_cmap('hsv'))
plt.show()