Created
September 28, 2014 00:50
-
-
Save fperez/02dc030110ca3b768591 to your computer and use it in GitHub Desktop.
binomial plot with unicode labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline | |
import matplotlib.pyplot as plt | |
from matplotlib import rc | |
# To use unicode, turn off latex | |
#rc('text', usetex=True) | |
import math | |
import numpy as np | |
import scipy as sp | |
import scipy.stats | |
from scipy.stats import binom | |
import pandas as pd | |
from IPython.html.widgets import interact, interactive, fixed | |
from IPython.html import widgets | |
from IPython.display import clear_output, display, HTML | |
def binomialHiLite(n, p, alpha): | |
''' | |
plots probability histogram for a binomial with parameters n and p, | |
highlighting the upper alpha quantile in yelow. | |
Red vertical line plotted at the mean. | |
''' | |
fig, ax = plt.subplots(nrows=1, ncols=1) | |
x = np.arange(n+1) | |
val = binom.ppf(1-alpha, n, p) | |
inx = np.searchsorted(x, val, side="right") | |
xb = x[:inx] | |
xy = x[inx:] | |
width = 1.0 | |
ax.bar(xb, binom.pmf(xb, n, p), width, color='b', align='center') | |
hilit = ax.bar(xy, binom.pmf(xy, n, p), width, color='y', align='center') | |
plt.xlim([-width,n+width]) | |
plt.axvline(x=n*p, color='r') | |
probStr = str(round(100*(1-binom.cdf(x[inx-1],n, p)),2)) | |
#label = r'$\mathbf{P}(X \ge' + str(x[inx]) + r') \approx' + probStr + '$' | |
label = u'P(X \u2265 %s) \u2248 %s' % (x[inx], probStr) | |
plt.legend([hilit[0]], | |
[label], | |
loc = 'best') | |
interact(binomialHiLite, n=widgets.IntSliderWidget(min=5, max=300, step=5, value=30),\ | |
p=widgets.FloatSliderWidget(min=0.001, max=1, step=0.001, value=.5),\ | |
alpha=widgets.FloatSliderWidget(min=0.0, max=1.0, step=0.001, value=.05) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment