Ellipse and Gaussian Distribution
Table of Contents
$$
\begin{align*}
\begin{bmatrix}x_1\\x_2\end{bmatrix} \quad &\underrightarrow{U} \quad \begin{bmatrix}y_1\\y_2\end{bmatrix} \\\\
\begin{bmatrix}y_1\\y_2\end{bmatrix} \quad &\underrightarrow{U^T} \quad \begin{bmatrix}x_1\\x_2\end{bmatrix}
\end{align*}
$$
$$ x^T \Sigma_x^{-1}x = 1 \quad \text{and} \quad \Sigma_x = \begin{bmatrix}a^2 & 0\\
0 & b^2 \end{bmatrix} $$
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
theta = np.arange(0,2*np.pi,0.01)
x1 = np.cos(theta)
x2 = np.sin(theta)
plt.figure(figsize=(6,6))
plt.plot(x1, x2)
plt.xlim([-4,4])
plt.ylim([-4,4])
plt.show()
x1 = 3*np.cos(theta);
x2 = np.sin(theta);
plt.figure(figsize=(6,6))
plt.plot(x1, x2)
plt.xlim([-4,4])
plt.ylim([-4,4])
plt.show()
U = np.matrix([[1/2, -np.sqrt(3)/2],
[np.sqrt(3)/2, 1/2]])
x = np.matrix([x1, x2])
y = U*x
print("U = \n", U)
plt.figure(figsize=(6,6))
plt.plot(y[0,:].T, y[1,:].T)
plt.xlim([-4,4])
plt.ylim([-4,4])
plt.show()
Sx = np.matrix([[9, 0],
[0, 1]])
Sy = U*Sx*U.T
print ("Sx = \n", Sx, "\n")
print ("Sy = \n", Sy)
$$A=S \Lambda S^T \qquad \text{where } S=[\upsilon_1 \; \upsilon_2] \text{ eigenvector of } A, \; \text{and }\Lambda=\begin{bmatrix} \lambda_1 & 0 \\ 0 & \lambda_2 \end{bmatrix}$$
$$\begin{align*} \text{eigen-analysis}
\begin{cases}\Sigma_y\hat{x}_1=\lambda_1 \hat{x}_1\\
\Sigma_y\hat{x}_2=\lambda_2 \hat{x}_2\\
\end{cases} \; \implies \;
& \Sigma_y \underbrace{\begin{bmatrix}\hat{x}_1 & \hat{x}_2 \end{bmatrix}}_{U} =
\underbrace{\begin{bmatrix} \hat{x}_1 & \hat{x}_2 \end{bmatrix}}_{U}
\underbrace{\begin{bmatrix}\lambda_1 & 0 \\ 0 & \lambda_2 \end{bmatrix}}_{\Lambda} \\ \\
& \Sigma_y U = U\Lambda \\
& \Sigma_y = U \Lambda U^T = U \Sigma_x U^T
\end{align*}$$
D, U = np.linalg.eig(Sy)
idx = np.argsort(-D)
D = D[idx]
U = U[:,idx]
print ("D = \n", np.diag(D))
print ("U = \n", U)
Independent ellipse in $\{\hat{x}_1, \hat{x}_2 \}$
Dependent ellipse in $\{ \hat{y}_1, \hat{y}_2 \}$
Decouple
y = np.arange(-10,10,0.1)
ProbG = 1/np.sqrt(2*np.pi)*np.exp(-1/2*y**2)
plt.figure(figsize=(10,6))
plt.plot(y, ProbG)
plt.show()
from scipy.stats import norm
ProbG2 = norm.pdf(y)
plt.figure(figsize=(10,6))
plt.plot(y, ProbG2)
plt.show()
x = np.random.randn(5000,1)
plt.figure(figsize=(10,6))
plt.hist(x, bins=51, normed=True)
plt.plot(y, ProbG2, label='G2')
plt.show()
$$
\mathcal{N}(x;\,\mu,\sigma) = \frac{1}{\sqrt{2\pi}\sigma}\exp\left(-\frac{1}{2}\frac{\left(x-\mu\right)^2}{\sigma^2}\right) $$
mu = 2
sigma = 3
x = np.arange(-10, 10, 0.1)
ProbG3 = 1/(np.sqrt(2*np.pi)*sigma) * np.exp(-1/2*(x-mu)**2/(sigma**2))
plt.figure(figsize=(10,6))
plt.plot(y,ProbG, label='G1')
plt.plot(x,ProbG3, label='G3')
plt.legend()
plt.show()
ProbG2 = norm.pdf(x, 2, 3)
plt.figure(figsize=(10,6))
plt.plot(y,ProbG, label='G1')
plt.plot(x,ProbG2, label='G2')
plt.legend()
plt.show()
x = mu + sigma*np.random.randn(5000,1)
plt.figure(figsize=(10,6))
plt.hist(x, bins=51, normed=True)
plt.plot(y,ProbG2, label='G2')
plt.ylim([0,0.4])
plt.show()
$$
\begin{align*} \mathcal{N}\big(x;\, \mu,\Sigma\big) &= \frac{1}{(2\pi)^{\frac{n}{2}}\lvert\Sigma\rvert^{\frac{1}{2}}}\exp\left(-\frac{1}{2}\left(x-\mu\right)^T\Sigma^{-1}\left(x-\mu\right)\right) \\ \\
\mu &= \text{length}\,\, n \,\,\text{column vector} \\
\Sigma &= n \times n \,\,\text{matrix (covariance matrix)} \\
\lvert\Sigma\rvert &= \text{matrix determinant}\\\\
E[x] &= \mu \\ \text{cov}(x) &= E[(x-\mu)(x-\mu)^T] = \Sigma
\end{align*}$$
$$
\begin{align*}
P\left(X_1=x_1,X_2=x_2\right) &= P_{X_1}\left(x_1\right)P_{X_2}\left(x_2\right) \\\\
&\sim \exp \left(-\frac{1}{2} \frac{\left(x_1-\mu_{x_1}\right)^2}{\sigma_{x_1}^2}\right)
\cdot \exp \left(-\frac{1}{2} \frac{\left(x_2-\mu_{x_2}\right)^2}{\sigma_{x_2}^2}\right) \\\\
&\sim\exp \left(-\frac{1}{2}\left(\frac{\left(x_1-\mu_{x_1}\right)^2}{\sigma_{x_1}^2} + \frac{\left(x_2-\mu_{x_2}\right)^2}{\sigma_{x_2}^2}\right)\right)\\
\end{align*}
$$
$$\left ( x=\begin{bmatrix} x_1 \\ x_2 \end{bmatrix}, \quad
\mu = \begin{bmatrix} \mu_1 \\ \mu_2 \end{bmatrix}, \quad
\Sigma = \begin{bmatrix}\sigma_{x_1}^2 & 0 \\ 0 & \sigma_{x_2}^2 \end{bmatrix} \right )$$
mu = np.array([0, 0])
sigma = np.eye(2)
m = 1000
x = np.random.multivariate_normal(mu, sigma, m)
print(x.shape)
plt.figure(figsize=(10,6))
plt.plot(x[:,0], x[:,1], '.')
plt.axis('equal')
plt.ylim([-8,8])
plt.show()
mu = np.array([0, 0])
sigma = np.array([[1, 0], [0, 4]])
m = 1000
x = np.random.multivariate_normal(mu, sigma, m)
plt.figure(figsize=(10,6))
plt.plot(x[:,0], x[:,1], '.')
plt.axis('equal')
plt.ylim([-8,8])
plt.show()
$$x^T\Sigma_x^{-1} x = y^T U\Sigma_x^{-1}U^Ty = y^T \Sigma_y^{-1} y\\
$$
$\Sigma_x$ : covariance matrix of $x$
$\Sigma_y$ : covariance matrix of $y$
mu = np.array([0, 0])
sigma = 1./2.*np.array([[5, -3], [-3, 5]])
m = 1000
x = np.random.multivariate_normal(mu, sigma, m)
plt.figure(figsize=(10,6))
plt.plot(x[:,0], x[:,1], '.')
plt.axis('equal')
plt.ylim([-8,8])
plt.show()
Remark
$$ \begin{align*} & x \sim \mathcal{N}(\mu_x,\Sigma_x) \text{ and } y = Ax + b \; \text{affine transformation}\\ \\ \implies \; & y \sim \mathcal{N}(\mu_y,\Sigma_y) = \mathcal{N}(A\mu_x+b,A\Sigma_xA^T)\\\\ \implies \; & y \;\; \text{is also Gaussian with} \;\; \mu_y = A\mu_x+b, \;\; \Sigma_y = A \Sigma_x A^T \end{align*}$$Given data, how to find $\Sigma_y$ and major (or minor) axis (assume $\mu_y = 0$)
$$ \Sigma_y =
\begin{bmatrix}
\text{var}(y_1) & \text{cov}(y_1,y_2)\\
\text{cov}(y_2,y_1) & \text{var}(y_2)
\end{bmatrix}$$
S = np.cov(x.T)
print ("S = \n", S)
D, U = np.linalg.eig(S)
idx = np.argsort(-D)
D = D[idx]
U = U[:,idx]
print ("U = \n", U)
print ("D = \n", D)
xp = np.arange(-10, 10)
plt.figure(figsize=(10,6))
plt.plot(x[:,0],x[:,1],'.')
plt.plot(xp, U[1,0]/U[0,0]*xp, label='u1')
plt.plot(xp, U[1,1]/U[0,1]*xp, label='u2')
plt.axis('equal')
plt.ylim([-8, 8])
plt.legend()
plt.show()
Suppose $x \sim \mathcal{N}(0, \Sigma)$, $c \in \mathbb{R}^n$ be a unit vector
$$x \mid y \sim \mathcal{N} \left(\mu_x + \Sigma_{xy}\Sigma_{y}^{-1}(y-\mu_y), \; \Sigma_{x} - \Sigma_{xy}\Sigma_{y}^{-1}\Sigma_{yx} \right)$$
$$E[x \mid y] = \mu_x + \Sigma_{xy}\Sigma_{y}^{-1}(y-\mu_y)$$
$$\text{cov}(x \mid y) = \Sigma_{x} - \Sigma_{xy}\Sigma_{y}^{-1}\Sigma_{yx} \leq \Sigma_{x}$$
mu = np.array([0, 1])
sigma = 1./2.*np.array([[5, -3], [-3, 5]])
m = 1000000
x = np.random.multivariate_normal(mu, sigma, m)
xp = np.arange(-10, 10)
plt.figure(figsize=(10,10))
plt.plot(x[:,0],x[:,1],'.')
plt.axis('equal')
plt.axis([-6, 6, -6, 6])
plt.axvline(0, 0, 1, c = 'k', linestyle = '--')
plt.axhline(0, 0, 1, c = 'k', linestyle = '--')
plt.axhline(4, 0, 1, c = 'b')
plt.axhline(1, 0.45, 0.55, c = 'k')
plt.axvline(0, 0.53, 0.63, c = 'k')
plt.show()
x1 = x[:,0]
x2 = x[:,1]
plt.figure(figsize=(10,3))
plt.xlim(-6,6)
plt.axvline(0, 0, 1, c = 'k')
plt.hist(x1, bins=51, normed=True, alpha = 0.7)
plt.figure(figsize=(10,3))
plt.xlim(-6,6)
plt.axvline(1, 0, 1, c = 'k')
plt.hist(x2, bins=51, normed=True, alpha = 0.7)
plt.show()
z = []
for i in range(m):
if x[i,1] > 3.8 and x[i,1] < 4.2:
z.append(x[i,0])
plt.figure(figsize=(10,3))
plt.hist(z, bins=51, normed=True, alpha = 0.7)
plt.axvline(0, 0, 1, c = 'k')
plt.xlim(-6,6)
plt.show()
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')