Skip to content

Instantly share code, notes, and snippets.

@andrewfowlie
Created September 12, 2017 01:39
Show Gist options
  • Save andrewfowlie/284e9a920b4d4a7110fc6c1d53434f64 to your computer and use it in GitHub Desktop.
Save andrewfowlie/284e9a920b4d4a7110fc6c1d53434f64 to your computer and use it in GitHub Desktop.
Illustration of Bayesian evidence and Occam's razor
"""
Plot evidences for simple and complicated models
================================================
by Andrew Fowlie.
"""
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from scipy.stats import norm, uniform
matplotlib.rc('font', **{'size': 22})
matplotlib.rc('text', usetex=False)
plt.xkcd()
fig, ax = plt.subplots(figsize=(12, 8))
# x-axis data
x = np.linspace(-10, 10, 100)
# Good simple model
y = norm.pdf(x, 0, 1)
ax.plot(x, y, 'Brown', linewidth=2, label='Good simple model')
ax.text(-6, 0.3,
'Good simple model:\nconcentrated\nprobability mass\nat observed data.',
color='Brown')
plt.fill_between(x, y, 0, color='Brown', alpha=0.5)
# Bad simple model
y = norm.pdf(x, 7, 1)
ax.plot(x, y, 'Olive', linewidth=2, label='Bad simple model')
ax.text(3, 0.4,
'Bad simple model:\nprobability mass\nwasted here.',
color='Olive')
plt.fill_between(x, y, 0, color='Olive', alpha=0.5)
# Complicated model
y = uniform.pdf(x, -7, 14)
ax.plot(x, y, 'RoyalBlue', linewidth=2, label='Complicated model')
ax.text(-9, 0.1,
'OK complicated model:\nspreads probability\nmass thinly.',
color='RoyalBlue')
plt.fill_between(x, y, 0, color='RoyalBlue', alpha=0.5)
# Appearance
plt.xlabel(r'Data')
plt.ylabel(r'Evidence, Prob(Data | Model)')
plt.title('"Naturalness" or Occam\'s razor')
ax.set_xlim(-10, 10)
ax.set_ylim(0, 0.6)
ax.legend(loc='upper left')
ax.xaxis.set_major_locator(MaxNLocator(1))
plt.setp(ax, xticklabels=['', 'Observed data'], yticklabels=[])
plt.savefig('evidence.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment