Import library and setup


In [12]:
%matplotlib inline

import os
from math import sqrt
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal

fig_directory = '../figures'
if not os.path.exists(fig_directory):
    os.makedirs(fig_directory)

matplotlib.rcParams['xtick.direction'] = 'out'
matplotlib.rcParams['ytick.direction'] = 'out'

save_fig = True
fig_size = (6, 4.5)
fig_dpi = 200

Plot isocontours function


In [19]:
def plot_isocontours(mu, sigma, question='Isocontours'):
    x, y = np.mgrid[-5:5:.01, -5:5:.01]
    pos = np.empty(x.shape + (2,))
    pos[:, :, 0] = x; pos[:, :, 1] = y
    rv = multivariate_normal(mu, sigma)
    fig = plt.figure(figsize=fig_size)
    plt.title(question)
    plt.xlim(-5.0, 5.0)
    plt.ylim(-5.0, 5.0)
    plt.grid()
    plt.contourf(x, y, rv.pdf(pos))
    plt.show();
    if save_fig:
        fig.savefig('{0}/p3_{1}.png'.format(fig_directory, question), dpi=fig_dpi)

def plot_isocontours2(mu1, sigma1, mu2, sigma2, question='Isocontours'):
    x, y = np.mgrid[-5:5:.01, -5:5:.01]
    pos = np.empty(x.shape + (2,))
    pos[:, :, 0] = x; pos[:, :, 1] = y
    rv1 = multivariate_normal(mu1, sigma1)
    rv2 = multivariate_normal(mu2, sigma2)
    fig = plt.figure(figsize=fig_size)
    plt.title(question)
    plt.xlim(-5.0, 5.0)
    plt.ylim(-5.0, 5.0)
    plt.grid()
    plt.contourf(x, y, rv1.pdf(pos) -rv2.pdf(pos))
    plt.show();
    if save_fig:
        fig.savefig('{0}/p3_{1}.png'.format(fig_directory, question), dpi=fig_dpi)

Plot

$f(\mu, \Sigma), \mu = \begin{bmatrix}1 \\ 1\end{bmatrix}, \Sigma = \begin{bmatrix}2 & 0 \\ 0 & 1\end{bmatrix}$


In [20]:
plot_isocontours([1, 1], [[2, 0], [0, 1]], 'a')


$f(\mu, \Sigma), \mu = \begin{bmatrix}-1 \\ 2\end{bmatrix}, \Sigma = \begin{bmatrix}3 & 1 \\ 1 & 2\end{bmatrix}$


In [21]:
plot_isocontours([-1, 2], [[3, 1], [1, 2]], 'b')


$f(\mu_1, \Sigma_1) - f(\mu_2, \Sigma_2), \mu_1 = \begin{bmatrix}0 \\ 2\end{bmatrix}, \mu_2 = \begin{bmatrix}2 \\ 0\end{bmatrix}, \Sigma_1 = \Sigma_2 = \begin{bmatrix}1 & 1 \\ 1 & 2\end{bmatrix}$


In [22]:
plot_isocontours2([0, 2], [[1, 1], [1, 2]],
                  [2, 0], [[1, 1], [1, 2]], 'c')


$f(\mu_1, \Sigma_1) - f(\mu_2, \Sigma_2), \mu_1 = \begin{bmatrix}0 \\ 2\end{bmatrix}, \mu_2 = \begin{bmatrix}2 \\ 0\end{bmatrix}, \Sigma_1 = \begin{bmatrix}1 & 1 \\ 1 & 2\end{bmatrix}, \Sigma_2 = \begin{bmatrix}3 & 1 \\ 1 & 2\end{bmatrix}$


In [23]:
plot_isocontours2([0, 2], [[1, 1], [1, 2]],
                  [2, 0], [[3, 1], [1, 2]], 'd')


$f(\mu_1, \Sigma_1) - f(\mu_2, \Sigma_2), \mu_1 = \begin{bmatrix}1 \\ 1\end{bmatrix}, \mu_2 = \begin{bmatrix}-1 \\ -1\end{bmatrix}, \Sigma_1 = \begin{bmatrix}1 & 0 \\ 0 & 2\end{bmatrix}, \Sigma_2 = \begin{bmatrix}2 & 1 \\ 1 & 2\end{bmatrix}$


In [24]:
plot_isocontours2([1, 1], [[1, 0], [0, 2]],
                  [-1, -1], [[2, 1], [1, 2]], 'e')



In [ ]: