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
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)
In [20]:
plot_isocontours([1, 1], [[2, 0], [0, 1]], 'a')
In [21]:
plot_isocontours([-1, 2], [[3, 1], [1, 2]], 'b')
In [22]:
plot_isocontours2([0, 2], [[1, 1], [1, 2]],
[2, 0], [[1, 1], [1, 2]], 'c')
In [23]:
plot_isocontours2([0, 2], [[1, 1], [1, 2]],
[2, 0], [[3, 1], [1, 2]], 'd')
In [24]:
plot_isocontours2([1, 1], [[1, 0], [0, 2]],
[-1, -1], [[2, 1], [1, 2]], 'e')
In [ ]: