Date Tags Python

I was recently studying Bayesian Methods for Hackers. I decided to give the static interactive widgets in IPython a try. What better than controlling the parameters of distributions to get a better understanding. This post is heavily borrowed from BMH book and from Jake's post on static widgets in IPython notebook.

In [1]:
%matplotlib inline
In [2]:
import numpy as np
import pandas as pd
import scipy.stats as stats
import seaborn
import matplotlib.pyplot as plt
from ipywidgets import StaticInteract, RangeWidget
In [3]:
a=range(10)
def plot_poisson(lambda_arg=5):
    fig, ax = plt.subplots(figsize=(4,3))
    ax.bar(a,stats.poisson.pmf(a, lambda_arg),label="$\lambda = %.1f$" % lambda_arg)
    ax.set_ylabel('PMF at $k$')
    ax.set_xlabel('$k$')
    ax.set_ylim((0,0.4))
    ax.legend()
    return fig

Poisson Distribution

$$ P(Z=k) = \frac{\lambda^ke^{-\lambda}}{k!} $$

  • Is a discrete distribution with $\lambda$ as the parameter
  • The expected value of this distribution is $\lambda$
In [5]:
StaticInteract(plot_poisson, lambda_arg=RangeWidget(1,12,1))
Out[5]:
lambda_arg:

As we increase $\lambda$, we can see the PMF takes higher probabilities for hgher $k$.

Exponential Distribution

$$ f_z(Z|\lambda) = \lambda e^{-\lambda z} $$

  • Is a continuous distribution with $\lambda$ as the parameter
  • The expected value of this distribution is $\frac{1}{\lambda}$
In [6]:
a = np.linspace(0, 4, 100)
expo = stats.expon
lambda_ = [0.5, 1]

def plot_exponential(lambda_arg):
    fig, ax = plt.subplots(figsize=(4,3))
    ax.plot(a, expo.pdf(a, scale=1. / lambda_arg), label="$\lambda = %.1f$" % lambda_arg)
    ax.fill_between(a, expo.pdf(a, scale=1. / lambda_arg), alpha=.33)
    ax.legend()
    ax.set_ylabel("PDF at $z$")
    ax.set_xlabel("$z$")
    ax.set_ylim(0, 2.0)
    return fig
In [7]:
StaticInteract(plot_exponential, lambda_arg=RangeWidget(0.2,2,0.2))
Out[7]:
lambda_arg:

As we increase $\lambda$, the distribution takes higher probabilities for lower $z$.

Feel free to comment or edit this post. It is also available on nbviewer here. For some reason, the MathJax is not working as it should and hence the equations look messy. Anyone has a clue?


Comments

comments powered by Disqus