# Chapter 3



In [1]:

%pylab inline




Populating the interactive namespace from numpy and matplotlib




In [2]:

import astroML



We present here the figure 3.2 in its original form in the book



In [3]:

from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

def banana_distribution(N=10000):
"""This generates random points in a banana shape"""
# create a truncated normal distribution
theta = np.random.normal(0, np.pi / 8, 10000)
theta[theta >= np.pi / 4] /= 2
theta[theta <= -np.pi / 4] /= 2
# define the curve parametrically
r = np.sqrt(1. / abs(np.cos(theta) ** 2 - np.sin(theta) ** 2))
r += np.random.normal(0, 0.08, size=10000)
x = r * np.cos(theta + np.pi / 4)
y = r * np.sin(theta + np.pi / 4)
return (x, y)

#------------------------------------------------------------
# Generate the data and compute the normalized 2D histogram
np.random.seed(1)
x, y = banana_distribution(10000)

Ngrid = 41
grid = np.linspace(0, 2, Ngrid + 1)

H, xbins, ybins = np.histogram2d(x, y, grid)
H /= np.sum(H)

#------------------------------------------------------------
# plot the result
fig = plt.figure(figsize=(15, 7.5)) ## We multiplied times 3 the size in both x and y axes to make it look bigger

# define axes
ax_Pxy = plt.axes((0.2, 0.34, 0.27, 0.52))
ax_Px = plt.axes((0.2, 0.14, 0.27, 0.2))
ax_Py = plt.axes((0.1, 0.34, 0.1, 0.52))
ax_cb = plt.axes((0.48, 0.34, 0.01, 0.52))
ax_Px_y = [plt.axes((0.65, 0.62, 0.32, 0.23)),
plt.axes((0.65, 0.38, 0.32, 0.23)),
plt.axes((0.65, 0.14, 0.32, 0.23))]

# set axis label formatters
ax_Px_y[0].xaxis.set_major_formatter(NullFormatter())
ax_Px_y[1].xaxis.set_major_formatter(NullFormatter())

ax_Pxy.xaxis.set_major_formatter(NullFormatter())
ax_Pxy.yaxis.set_major_formatter(NullFormatter())

ax_Px.yaxis.set_major_formatter(NullFormatter())
ax_Py.xaxis.set_major_formatter(NullFormatter())

# draw the joint probability
plt.axes(ax_Pxy)
H *= 1000
plt.imshow(H, interpolation='nearest', origin='lower', aspect='auto',
extent=[0, 2, 0, 2], cmap=plt.cm.binary)

cb = plt.colorbar(cax=ax_cb)
cb.set_label('$p(x, y)$')
plt.text(0, 1.02, r'$\times 10^{-3}$',
transform=ax_cb.transAxes)

# draw p(x) distribution
ax_Px.plot(xbins[1:], H.sum(0), '-k', drawstyle='steps')

# draw p(y) distribution
ax_Py.plot(H.sum(1), ybins[1:], '-k', drawstyle='steps')

# define axis limits
ax_Pxy.set_xlim(0, 2)
ax_Pxy.set_ylim(0, 2)
ax_Px.set_xlim(0, 2)
ax_Py.set_ylim(0, 2)

# label axes
ax_Pxy.set_xlabel('$x$')
ax_Pxy.set_ylabel('$y$')
ax_Px.set_xlabel('$x$')
ax_Px.set_ylabel('$p(x)$')
ax_Px.yaxis.set_label_position('right')
ax_Py.set_ylabel('$y$')
ax_Py.set_xlabel('$p(y)$')
ax_Py.xaxis.set_label_position('top')

# draw marginal probabilities
iy = [3 * Ngrid / 4, Ngrid / 2, Ngrid / 4]
colors = 'rgc'
axis = ax_Pxy.axis()
for i in range(3):
# overplot range on joint probability
ax_Pxy.plot([0, 2, 2, 0],
[ybins[iy[i] + 1], ybins[iy[i] + 1],
ybins[iy[i]], ybins[iy[i]]], c=colors[i], lw=1)
Px_y = H[iy[i]] / H[iy[i]].sum()
ax_Px_y[i].plot(xbins[1:], Px_y, drawstyle='steps', c=colors[i])
ax_Px_y[i].yaxis.set_major_formatter(NullFormatter())
ax_Px_y[i].set_ylabel('$p(x | %.1f)$' % ybins[iy[i]])
ax_Pxy.axis(axis)

ax_Px_y[2].set_xlabel('$x$')

ax_Pxy.set_title('Joint Probability')
ax_Px_y[0].set_title('Conditional Probability')

plt.show()






Now we repeat the plot with seaborn



In [4]:

import seaborn as sns
import pandas as pd
data = (x,y)
data = np.array(data)
data = data.T
df = pd.DataFrame(data, columns=["x", "y"])
g = sns.jointplot(x="x", y="y",data=df, kind="hex", marginal_kws={"bins":41*41/2}, gridsize=41*41/2, xlim=(0,2), ylim=(0,2))




/Users/nobleWkennamer/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py:872: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
warnings.warn(self.msg_depr % (key, alt_key))
/Users/nobleWkennamer/anaconda/lib/python2.7/site-packages/matplotlib/__init__.py:892: UserWarning: axes.color_cycle is deprecated and replaced with axes.prop_cycle; please use the latter.
warnings.warn(self.msg_depr % (key, alt_key))




In [5]:

cond1 = (y>=0.5-2./41.) & (y<=0.5+2./41.)
cond2 = (y>=1.0-2./41.) & (y<=1.0+2./41.)
cond3 = (y>=1.5-2./41.) & (y<=1.5+2./41.)




In [6]:

sns.__version__




Out[6]:

'0.6.0'




In [7]:

weights = np.ones(10000)*1./10000.




In [8]:

g1 = sns.distplot(x[cond1],hist=True, color='cyan', kde=False, hist_kws={'weights':weights[cond1]})
g2 = sns.distplot(x[cond2],hist=True, color='green', kde=False, hist_kws={'weights':weights[cond2]})
g3 = sns.distplot(x[cond3],hist=True, color='red', kde=False, hist_kws={'weights':weights[cond3]})






## Monty Hall problem

We will simulate the Monty Hall problem where you pick one out of three boxes, the host eliminates one of the boxes (knowing the one that has the prize) and we want to see what is the probability of winning by keeping the first choice or by changing the choice



In [9]:

orig_choice = np.random.random_integers(1,3,size=10000)
correct_choice = np.random.random_integers(1,3,size=10000)
change_mind = np.zeros(10000)
host_choice = np.zeros(10000)
choice = [1,2,3]
for i in range(0,10000):
if(orig_choice[i]==correct_choice[i]):
host_choice[i]=choice[correct_choice[i]-2]
else:
host_choice[i]=6-correct_choice[i]-orig_choice[i]
change_mind[i]=6-orig_choice[i]-host_choice[i]
[x]=np.where(change_mind==correct_choice)
[y]=np.where(orig_choice==correct_choice)
print len(x)/10000., len(y)/10000.




0.6682 0.3318



## Plots with Bokeh



In [10]:

from ipywidgets import interact
from bokeh.io import push_notebook
from bokeh.plotting import figure, output_file, show
from bokeh.plotting import output_notebook




In [11]:

output_notebook()




(function(global) {
function now() {
return new Date();
}

if (typeof (window._bokeh_onload_callbacks) === "undefined") {
}

function run_callbacks() {
console.info("Bokeh: all callbacks have finished");
}

console.log("Bokeh: BokehJS is being loaded, scheduling callback at", now());
return null;
}
if (js_urls == null || js_urls.length === 0) {
run_callbacks();
return null;
}
for (var i = 0; i < js_urls.length; i++) {
var url = js_urls[i];
var s = document.createElement('script');
s.src = url;
s.async = false;
run_callbacks()
}
};
s.onerror = function() {
console.warn("failed to load library " + url);
};
console.log("Bokeh: injecting script tag for BokehJS library: ", url);
}
};var js_urls = ['https://cdn.pydata.org/bokeh/release/bokeh-0.11.0.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.11.0.min.js', 'https://cdn.pydata.org/bokeh/release/bokeh-compiler-0.11.0.min.js'];

var inline_js = [
function(Bokeh) {
Bokeh.set_log_level("info");
},
function(Bokeh) {
console.log("Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-0.11.0.min.css");
Bokeh.embed.inject_css("https://cdn.pydata.org/bokeh/release/bokeh-0.11.0.min.css");
console.log("Bokeh: injecting CSS: https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.11.0.min.css");
Bokeh.embed.inject_css("https://cdn.pydata.org/bokeh/release/bokeh-widgets-0.11.0.min.css");
}
];

function run_inline_js() {
for (var i = 0; i < inline_js.length; i++) {
inline_js[i](window.Bokeh);
}
}

console.log("Bokeh: BokehJS loaded, going straight to plotting");
run_inline_js();
} else {
console.log("Bokeh: BokehJS plotting callback run at", now());
run_inline_js();
});
}
}(this));



We are going to take advantage of the interactivity of Bokeh to plot several distributions presented along the chapter 3



In [12]:

from scipy import stats
xd = np.linspace(0,10,100)
dist = stats.norm(0,1)
yd=dist.pdf(xd)
pp = figure(title="Probability distribution plot", plot_height=300, plot_width=600, y_range=(0.05,1.05))
rr = pp.line(xd,yd,color="red",line_width=3)




In [13]:

def pdist(f,par1=0,par2=1):
if f=="Gaussian": dist = stats.norm(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Uniform": dist = stats.uniform(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Binomial": dist = stats.binom(par1,par2); rr.data_source.data['y']=dist.pmf(xd)
if f=="Poisson": dist = stats.poisson(par1); rr.data_source.data['y']=dist.pmf(xd)
if f=="Cauchy": dist = stats.cauchy(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Laplace": dist = stats.laplace(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Chi2": dist = stats.chi2(par1); rr.data_source.data['y']=dist.pdf(xd)
if f=="Student t": dist = stats.t(par1); rr.data_source.data['y']=dist.pdf(xd)
if f=="Fisher f": dist = stats.f(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Beta": dist = stats.beta(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Gamma": dist = stats.gamma(par1,par2); rr.data_source.data['y']=dist.pdf(xd)
if f=="Weibull": dist = stats.dweibull(par1,0,par2); rr.data_source.data['y']=dist.pdf(xd)

push_notebook()




In [14]:

show(pp)




(function(global) {
function now() {
return new Date();
}

if (typeof (window._bokeh_onload_callbacks) === "undefined") {
}

function run_callbacks() {
console.info("Bokeh: all callbacks have finished");
}

console.log("Bokeh: BokehJS is being loaded, scheduling callback at", now());
return null;
}
if (js_urls == null || js_urls.length === 0) {
run_callbacks();
return null;
}
for (var i = 0; i < js_urls.length; i++) {
var url = js_urls[i];
var s = document.createElement('script');
s.src = url;
s.async = false;
run_callbacks()
}
};
s.onerror = function() {
console.warn("failed to load library " + url);
};
console.log("Bokeh: injecting script tag for BokehJS library: ", url);
}
};var element = document.getElementById("e04b0cd5-eee9-427b-b6dc-49815c2981d7");
if (element == null) {
console.log("Bokeh: ERROR: autoload.js configured with elementid 'e04b0cd5-eee9-427b-b6dc-49815c2981d7' but no matching script tag was found. ")
return false;
}var js_urls = [];

var inline_js = [
function(Bokeh) {
Bokeh.$(function() { var docs_json = {"1ce51e12-451d-45b3-b9c6-ebfc7d4964d8": {"version": "0.11.0", "roots": {"root_ids": ["e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"], "references": [{"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "formatter": {"type": "BasicTickFormatter", "id": "34ecd300-8170-4692-ac69-4ebeb6fbe960"}, "ticker": {"type": "BasicTicker", "id": "b8af44c8-8d29-489d-814e-aa35824ff72b"}}, "type": "LinearAxis", "id": "ff01ab7c-1318-4e43-a966-7a5de3697b11"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "overlay": {"type": "BoxAnnotation", "id": "5eab65b7-88eb-4c66-8972-dae8769c47a5"}}, "type": "BoxZoomTool", "id": "3ba5ea31-0365-4867-862e-1426b0674e9a"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "HelpTool", "id": "dd6c0682-0ea1-4ba6-8f5f-0193c9570676"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "formatter": {"type": "BasicTickFormatter", "id": "ac9bc760-ba0a-4b9d-8e42-a2dd1f364c35"}, "ticker": {"type": "BasicTicker", "id": "a47326bf-68dc-4371-af7e-61aa8451bd4e"}}, "type": "LinearAxis", "id": "e41ca148-5187-425f-b8c1-21dd1fe402bb"}, {"attributes": {"line_color": {"value": "#1f77b4"}, "line_alpha": {"value": 0.1}, "y": {"field": "y"}, "x": {"field": "x"}, "line_width": {"value": 3}}, "type": "Line", "id": "73bfe2e3-bf2e-42f3-8759-a11b0ee8089d"}, {"attributes": {}, "type": "BasicTickFormatter", "id": "34ecd300-8170-4692-ac69-4ebeb6fbe960"}, {"attributes": {"callback": null, "column_names": ["y", "x"], "data": {"y": [0.3989422804014327, 0.396912250799448, 0.39088393119995174, 0.38103950731847186, 0.36767243582235765, 0.35117291568936065, 0.3320089799750084, 0.31070448560198066, 0.2878154423606714, 0.26390616524373, 0.23952665870127499, 0.21519246100303105, 0.19136791454117102, 0.16845351275175113, 0.1467776381916519, 0.1265926800900417, 0.10807523024827295, 0.09132982391105686, 0.07639552978506677, 0.06325460519926435, 0.05184241541246927, 0.0420578610474252, 0.033773651035270635, 0.02684588450489843, 0.021122547673202742, 0.016450676280292814, 0.0126820683491598, 0.009677547205460008, 0.007309865450717592, 0.005465404924207421, 0.004044866385886478, 0.0029631585662810585, 0.00214869355083202, 0.0015422789962911054, 0.0010957722143120608, 0.0007706310423961532, 0.0005364652438418938, 0.0003696626372006643, 0.00025213805615265854, 0.00017023157566745375, 0.00011376553499639933, 7.525756870791026e-05, 4.9278619153331034e-05, 3.194006177554185e-05, 2.049188214687352e-05, 1.3013581102871508e-05, 8.180515509389409e-06, 5.090183015471749e-06, 3.1351259909120126e-06, 1.9113731247039165e-06, 1.1534660801464554e-06, 6.89021948157255e-07, 4.074085700511435e-07, 2.3844934860819945e-07, 1.3814367211801345e-07, 7.921997988730177e-08, 4.496838962795538e-08, 2.5266717140660865e-08, 1.405268066904646e-08, 7.736391024257605e-09, 4.215863326136526e-09, 2.2740683276058303e-09, 1.2141976048222072e-09, 6.417178354458208e-10, 3.357126636165939e-10, 1.7384420310237249e-10, 8.910900187352152e-11, 4.5211802357053963e-11, 2.2706538727716806e-11, 1.1288050158021126e-11, 5.554638491023806e-12, 2.7055872512705062e-12, 1.3044762131729475e-12, 6.225577737568753e-13, 2.940979738121426e-13, 1.3752234849134903e-13, 6.365366349200241e-14, 2.9163685307990903e-14, 1.3226053604903335e-14, 5.9372731511694584e-15, 2.638230011929564e-15, 1.1603985010839856e-15, 5.05208370627996e-16, 2.1772223903555597e-16, 9.287608963747761e-17, 3.9216960243814e-17, 1.639127775655324e-17, 6.7814188194379525e-18, 2.777136334211239e-18, 1.1257519545234085e-18, 4.517072848156948e-19, 1.79407401238564e-19, 7.053302204662157e-20, 2.744817691969876e-20, 1.0573125630179778e-20, 4.0314581568062645e-21, 1.5215625004581603e-21, 5.684422005029409e-22, 2.1020919225908269e-22, 7.69459862670642e-23], "x": [0.0, 0.10101010101010101, 0.20202020202020202, 0.30303030303030304, 0.40404040404040403, 0.5050505050505051, 0.6060606060606061, 0.7070707070707071, 0.8080808080808081, 0.9090909090909091, 1.0101010101010102, 1.1111111111111112, 1.2121212121212122, 1.3131313131313131, 1.4141414141414141, 1.5151515151515151, 1.6161616161616161, 1.7171717171717171, 1.8181818181818181, 1.9191919191919191, 2.0202020202020203, 2.121212121212121, 2.2222222222222223, 2.323232323232323, 2.4242424242424243, 2.525252525252525, 2.6262626262626263, 2.727272727272727, 2.8282828282828283, 2.929292929292929, 3.0303030303030303, 3.131313131313131, 3.2323232323232323, 3.3333333333333335, 3.4343434343434343, 3.5353535353535355, 3.6363636363636362, 3.7373737373737375, 3.8383838383838382, 3.9393939393939394, 4.040404040404041, 4.141414141414141, 4.242424242424242, 4.343434343434343, 4.444444444444445, 4.545454545454545, 4.646464646464646, 4.747474747474747, 4.848484848484849, 4.94949494949495, 5.05050505050505, 5.151515151515151, 5.252525252525253, 5.353535353535354, 5.454545454545454, 5.555555555555555, 5.656565656565657, 5.757575757575758, 5.858585858585858, 5.959595959595959, 6.0606060606060606, 6.161616161616162, 6.262626262626262, 6.363636363636363, 6.4646464646464645, 6.565656565656566, 6.666666666666667, 6.767676767676767, 6.8686868686868685, 6.96969696969697, 7.070707070707071, 7.171717171717171, 7.2727272727272725, 7.373737373737374, 7.474747474747475, 7.575757575757575, 7.6767676767676765, 7.777777777777778, 7.878787878787879, 7.979797979797979, 8.080808080808081, 8.181818181818182, 8.282828282828282, 8.383838383838384, 8.484848484848484, 8.585858585858587, 8.686868686868687, 8.787878787878787, 8.88888888888889, 8.98989898989899, 9.09090909090909, 9.191919191919192, 9.292929292929292, 9.393939393939394, 9.494949494949495, 9.595959595959595, 9.696969696969697, 9.797979797979798, 9.8989898989899, 10.0]}}, "type": "ColumnDataSource", "id": "fe5729c7-5df0-42ba-a5be-71333dd42749"}, {"attributes": {}, "type": "BasicTicker", "id": "a47326bf-68dc-4371-af7e-61aa8451bd4e"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "ResetTool", "id": "63f9b331-8f39-4de0-8d81-985109b12c3b"}, {"attributes": {}, "type": "ToolEvents", "id": "54b22ed9-74d8-4862-b499-8c2a66dacc9b"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "ticker": {"type": "BasicTicker", "id": "b8af44c8-8d29-489d-814e-aa35824ff72b"}}, "type": "Grid", "id": "230f0938-e734-400e-a559-0c6b633f9db3"}, {"attributes": {}, "type": "BasicTickFormatter", "id": "ac9bc760-ba0a-4b9d-8e42-a2dd1f364c35"}, {"attributes": {"line_color": {"value": "black"}, "line_alpha": {"value": 1.0}, "render_mode": "css", "bottom_units": "screen", "level": "overlay", "top_units": "screen", "fill_alpha": {"value": 0.5}, "plot": null, "left_units": "screen", "line_dash": [4, 4], "line_width": {"value": 2}, "right_units": "screen", "fill_color": {"value": "lightgrey"}}, "type": "BoxAnnotation", "id": "5eab65b7-88eb-4c66-8972-dae8769c47a5"}, {"attributes": {"callback": null}, "type": "DataRange1d", "id": "68a88fa1-6019-479f-8cfa-e264745a729f"}, {"attributes": {"line_color": {"value": "red"}, "line_width": {"value": 3}, "y": {"field": "y"}, "x": {"field": "x"}}, "type": "Line", "id": "4b001b35-e9b8-4ecc-bb7b-4892a4e7b50c"}, {"attributes": {"callback": null, "end": 1.05, "start": 0.05}, "type": "Range1d", "id": "ca6003fa-c6a7-4b01-b841-eac6b5d72138"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "PanTool", "id": "6d522ae4-8d07-4933-8529-e0ef8b4bb3b9"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "WheelZoomTool", "id": "a1834890-802b-4ab8-87c6-259c170c65b2"}, {"attributes": {}, "type": "BasicTicker", "id": "b8af44c8-8d29-489d-814e-aa35824ff72b"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "PreviewSaveTool", "id": "fe7be3b8-357c-4530-9b0c-296f70071ba1"}, {"attributes": {"x_range": {"type": "DataRange1d", "id": "68a88fa1-6019-479f-8cfa-e264745a729f"}, "title": "Probability distribution plot", "y_range": {"type": "Range1d", "id": "ca6003fa-c6a7-4b01-b841-eac6b5d72138"}, "renderers": [{"type": "LinearAxis", "id": "ff01ab7c-1318-4e43-a966-7a5de3697b11"}, {"type": "Grid", "id": "230f0938-e734-400e-a559-0c6b633f9db3"}, {"type": "LinearAxis", "id": "e41ca148-5187-425f-b8c1-21dd1fe402bb"}, {"type": "Grid", "id": "a4d6eeae-27bd-4f7a-aa78-ad1b8a5066e8"}, {"type": "BoxAnnotation", "id": "5eab65b7-88eb-4c66-8972-dae8769c47a5"}, {"type": "GlyphRenderer", "id": "1769b7c7-4c0d-4f96-ac3b-02797d13c52f"}], "below": [{"type": "LinearAxis", "id": "ff01ab7c-1318-4e43-a966-7a5de3697b11"}], "tool_events": {"type": "ToolEvents", "id": "54b22ed9-74d8-4862-b499-8c2a66dacc9b"}, "plot_height": 300, "tools": [{"type": "PanTool", "id": "6d522ae4-8d07-4933-8529-e0ef8b4bb3b9"}, {"type": "WheelZoomTool", "id": "a1834890-802b-4ab8-87c6-259c170c65b2"}, {"type": "BoxZoomTool", "id": "3ba5ea31-0365-4867-862e-1426b0674e9a"}, {"type": "PreviewSaveTool", "id": "fe7be3b8-357c-4530-9b0c-296f70071ba1"}, {"type": "ResizeTool", "id": "c6f4ad32-dd2c-4f32-9252-ad62c59a763f"}, {"type": "ResetTool", "id": "63f9b331-8f39-4de0-8d81-985109b12c3b"}, {"type": "HelpTool", "id": "dd6c0682-0ea1-4ba6-8f5f-0193c9570676"}], "left": [{"type": "LinearAxis", "id": "e41ca148-5187-425f-b8c1-21dd1fe402bb"}]}, "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29", "subtype": "Figure"}, {"attributes": {"nonselection_glyph": {"type": "Line", "id": "73bfe2e3-bf2e-42f3-8759-a11b0ee8089d"}, "data_source": {"type": "ColumnDataSource", "id": "fe5729c7-5df0-42ba-a5be-71333dd42749"}, "selection_glyph": null, "hover_glyph": null, "glyph": {"type": "Line", "id": "4b001b35-e9b8-4ecc-bb7b-4892a4e7b50c"}}, "type": "GlyphRenderer", "id": "1769b7c7-4c0d-4f96-ac3b-02797d13c52f"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "ResizeTool", "id": "c6f4ad32-dd2c-4f32-9252-ad62c59a763f"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "ticker": {"type": "BasicTicker", "id": "a47326bf-68dc-4371-af7e-61aa8451bd4e"}, "dimension": 1}, "type": "Grid", "id": "a4d6eeae-27bd-4f7a-aa78-ad1b8a5066e8"}]}, "title": "Bokeh Application"}}; var render_items = [{"notebook_comms_target": "ace62b54-6ea1-455a-8783-8c32c23267fb", "docid": "1ce51e12-451d-45b3-b9c6-ebfc7d4964d8", "elementid": "e04b0cd5-eee9-427b-b6dc-49815c2981d7", "modelid": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}]; Bokeh.embed.embed_items(docs_json, render_items); }); }, function(Bokeh) { } ]; function run_inline_js() { for (var i = 0; i < inline_js.length; i++) { inline_js[i](window.Bokeh); } } if (window._bokeh_is_loading === 0) { console.log("Bokeh: BokehJS loaded, going straight to plotting"); run_inline_js(); } else { load_libs(js_urls, function() { console.log("Bokeh: BokehJS plotting callback run at", now()); run_inline_js(); }); } }(this)); Out[14]: <bokeh.io._CommsHandle at 0x117bc4810>   In [15]: interact(pdist,f=["Gaussian","Uniform","Binomial","Poisson","Cauchy","Chi2","Laplace","Student t","Fisher f","Beta","Gamma","Weibull"],par1=(0,10),par2=(0,10))   Out[15]: <function __main__.pdist>   In [16]: nsamples = 10 x = np.random.uniform(size=nsamples) xp = np.zeros(nsamples) for i in range(0,nsamples): xp[i]=np.mean(x[0:i])-0.5 hist, edges = np.histogram(xp,bins=100, range=(-1,1)) p = figure(title="Central Limit Theorem interactive demo", plot_height=300, plot_width=600) r = p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="#036564", line_color="#033649")   /Users/nobleWkennamer/anaconda/lib/python2.7/site-packages/numpy/core/_methods.py:59: RuntimeWarning: Mean of empty slice. warnings.warn("Mean of empty slice.", RuntimeWarning)   In [17]: def update(nsamples=1): np.random.seed(123) x = np.random.uniform(size=nsamples) xp = np.zeros(nsamples) for i in range(0,nsamples): xp[i]=np.mean(x[0:i])-0.5 hist, edges = np.histogram(xp,bins=100, range=(-1,1)) r.data_source.data['top']=hist push_notebook()   In [18]: show(p)   (function(global) { function now() { return new Date(); } if (typeof (window._bokeh_onload_callbacks) === "undefined") { window._bokeh_onload_callbacks = []; } function run_callbacks() { window._bokeh_onload_callbacks.forEach(function(callback) { callback() }); delete window._bokeh_onload_callbacks console.info("Bokeh: all callbacks have finished"); } function load_libs(js_urls, callback) { window._bokeh_onload_callbacks.push(callback); if (window._bokeh_is_loading > 0) { console.log("Bokeh: BokehJS is being loaded, scheduling callback at", now()); return null; } if (js_urls == null || js_urls.length === 0) { run_callbacks(); return null; } console.log("Bokeh: BokehJS not loaded, scheduling load and callback at", now()); window._bokeh_is_loading = js_urls.length; for (var i = 0; i < js_urls.length; i++) { var url = js_urls[i]; var s = document.createElement('script'); s.src = url; s.async = false; s.onreadystatechange = s.onload = function() { window._bokeh_is_loading--; if (window._bokeh_is_loading === 0) { console.log("Bokeh: all BokehJS libraries loaded"); run_callbacks() } }; s.onerror = function() { console.warn("failed to load library " + url); }; console.log("Bokeh: injecting script tag for BokehJS library: ", url); document.getElementsByTagName("head")[0].appendChild(s); } };var element = document.getElementById("d78704b0-3c09-445c-9406-f3033ea9349e"); if (element == null) { console.log("Bokeh: ERROR: autoload.js configured with elementid 'd78704b0-3c09-445c-9406-f3033ea9349e' but no matching script tag was found. ") return false; }var js_urls = []; var inline_js = [ function(Bokeh) { Bokeh.$(function() {
var docs_json = {"b6f798ac-5ee5-4bc6-b423-d91c5b2dd3a6": {"version": "0.11.0", "roots": {"root_ids": ["e48d111c-f0f1-4e7f-a0cc-9d36476a7b29", "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"], "references": [{"attributes": {"callback": null}, "type": "DataRange1d", "id": "0e3c93c4-7eff-4169-9e45-0855b32fe3a3"}, {"attributes": {"nonselection_glyph": {"type": "Quad", "id": "c83e2f95-d4ee-4285-9bb6-502ab69d18d7"}, "data_source": {"type": "ColumnDataSource", "id": "08181e76-a636-4f16-9e31-f133dc1c9710"}, "selection_glyph": null, "hover_glyph": null, "glyph": {"type": "Quad", "id": "7755980e-e0bf-4211-a0c5-fdb19601f48a"}}, "type": "GlyphRenderer", "id": "c7b51e09-bdac-4f4f-a467-8a2561675b62"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}, "formatter": {"type": "BasicTickFormatter", "id": "89e016b4-ba14-42ca-9742-62f6ae6d1a74"}, "ticker": {"type": "BasicTicker", "id": "9c3c2b65-69bb-4b33-bbe5-241f0843c81a"}}, "type": "LinearAxis", "id": "c81a885c-8803-436b-8994-573dae17cfe8"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}, "overlay": {"type": "BoxAnnotation", "id": "dea8ed6f-a3d8-4ac0-8db1-9737336a9670"}}, "type": "BoxZoomTool", "id": "d2f56baf-61c7-4f7f-aa3f-4ea6ac963f60"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "formatter": {"type": "BasicTickFormatter", "id": "34ecd300-8170-4692-ac69-4ebeb6fbe960"}, "ticker": {"type": "BasicTicker", "id": "b8af44c8-8d29-489d-814e-aa35824ff72b"}}, "type": "LinearAxis", "id": "ff01ab7c-1318-4e43-a966-7a5de3697b11"}, {"attributes": {"line_color": {"value": "#1f77b4"}, "line_alpha": {"value": 0.1}, "right": {"field": "right"}, "fill_color": {"value": "#1f77b4"}, "bottom": {"value": 0}, "top": {"field": "top"}, "fill_alpha": {"value": 0.1}, "left": {"field": "left"}}, "type": "Quad", "id": "c83e2f95-d4ee-4285-9bb6-502ab69d18d7"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}, "formatter": {"type": "BasicTickFormatter", "id": "6d99b925-1dca-42ec-9260-ab8fc01f69f2"}, "ticker": {"type": "BasicTicker", "id": "96248cd4-98f1-4674-83da-d5a20c3014aa"}}, "type": "LinearAxis", "id": "14f261be-22d1-40c2-aec9-81715b07eced"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}}, "type": "PanTool", "id": "7343f006-b2ef-447d-8dbd-f8b55e39a2e6"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "formatter": {"type": "BasicTickFormatter", "id": "ac9bc760-ba0a-4b9d-8e42-a2dd1f364c35"}, "ticker": {"type": "BasicTicker", "id": "a47326bf-68dc-4371-af7e-61aa8451bd4e"}}, "type": "LinearAxis", "id": "e41ca148-5187-425f-b8c1-21dd1fe402bb"}, {"attributes": {"line_color": {"value": "#1f77b4"}, "line_alpha": {"value": 0.1}, "y": {"field": "y"}, "x": {"field": "x"}, "line_width": {"value": 3}}, "type": "Line", "id": "73bfe2e3-bf2e-42f3-8759-a11b0ee8089d"}, {"attributes": {"line_color": {"value": "#033649"}, "right": {"field": "right"}, "fill_color": {"value": "#036564"}, "bottom": {"value": 0}, "top": {"field": "top"}, "left": {"field": "left"}}, "type": "Quad", "id": "7755980e-e0bf-4211-a0c5-fdb19601f48a"}, {"attributes": {}, "type": "BasicTickFormatter", "id": "34ecd300-8170-4692-ac69-4ebeb6fbe960"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}}, "type": "ResizeTool", "id": "07544054-0b88-425c-a2b1-dbedbb6458e5"}, {"attributes": {"x_range": {"type": "DataRange1d", "id": "0e3c93c4-7eff-4169-9e45-0855b32fe3a3"}, "title": "Central Limit Theorem interactive demo", "y_range": {"type": "DataRange1d", "id": "833141fd-c3bc-4620-9891-cfb522b67a32"}, "renderers": [{"type": "LinearAxis", "id": "14f261be-22d1-40c2-aec9-81715b07eced"}, {"type": "Grid", "id": "be64bcc1-b352-43d1-a854-13d25237f85c"}, {"type": "LinearAxis", "id": "c81a885c-8803-436b-8994-573dae17cfe8"}, {"type": "Grid", "id": "7d346df8-2ceb-4de9-8b98-ed4ab8267426"}, {"type": "BoxAnnotation", "id": "dea8ed6f-a3d8-4ac0-8db1-9737336a9670"}, {"type": "GlyphRenderer", "id": "c7b51e09-bdac-4f4f-a467-8a2561675b62"}], "below": [{"type": "LinearAxis", "id": "14f261be-22d1-40c2-aec9-81715b07eced"}], "tool_events": {"type": "ToolEvents", "id": "601a9f08-18a2-43cb-981d-6d0b30e82e8f"}, "plot_height": 300, "tools": [{"type": "PanTool", "id": "7343f006-b2ef-447d-8dbd-f8b55e39a2e6"}, {"type": "WheelZoomTool", "id": "ab3d7c81-a67c-48b9-8d2a-9691145ad93e"}, {"type": "BoxZoomTool", "id": "d2f56baf-61c7-4f7f-aa3f-4ea6ac963f60"}, {"type": "PreviewSaveTool", "id": "a81f01a5-0e03-4d20-a5eb-107d79d1c285"}, {"type": "ResizeTool", "id": "07544054-0b88-425c-a2b1-dbedbb6458e5"}, {"type": "ResetTool", "id": "bf6444af-5f41-4730-b9c7-32e0bd9fec5e"}, {"type": "HelpTool", "id": "2c8b67f5-dbe9-4159-94a0-aaed66aeb9a8"}], "left": [{"type": "LinearAxis", "id": "c81a885c-8803-436b-8994-573dae17cfe8"}]}, "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520", "subtype": "Figure"}, {"attributes": {}, "type": "ToolEvents", "id": "601a9f08-18a2-43cb-981d-6d0b30e82e8f"}, {"attributes": {}, "type": "BasicTicker", "id": "b8af44c8-8d29-489d-814e-aa35824ff72b"}, {"attributes": {"callback": null, "column_names": ["y", "x"], "data": {"y": [0.3989422804014327, 0.396912250799448, 0.39088393119995174, 0.38103950731847186, 0.36767243582235765, 0.35117291568936065, 0.3320089799750084, 0.31070448560198066, 0.2878154423606714, 0.26390616524373, 0.23952665870127499, 0.21519246100303105, 0.19136791454117102, 0.16845351275175113, 0.1467776381916519, 0.1265926800900417, 0.10807523024827295, 0.09132982391105686, 0.07639552978506677, 0.06325460519926435, 0.05184241541246927, 0.0420578610474252, 0.033773651035270635, 0.02684588450489843, 0.021122547673202742, 0.016450676280292814, 0.0126820683491598, 0.009677547205460008, 0.007309865450717592, 0.005465404924207421, 0.004044866385886478, 0.0029631585662810585, 0.00214869355083202, 0.0015422789962911054, 0.0010957722143120608, 0.0007706310423961532, 0.0005364652438418938, 0.0003696626372006643, 0.00025213805615265854, 0.00017023157566745375, 0.00011376553499639933, 7.525756870791026e-05, 4.9278619153331034e-05, 3.194006177554185e-05, 2.049188214687352e-05, 1.3013581102871508e-05, 8.180515509389409e-06, 5.090183015471749e-06, 3.1351259909120126e-06, 1.9113731247039165e-06, 1.1534660801464554e-06, 6.89021948157255e-07, 4.074085700511435e-07, 2.3844934860819945e-07, 1.3814367211801345e-07, 7.921997988730177e-08, 4.496838962795538e-08, 2.5266717140660865e-08, 1.405268066904646e-08, 7.736391024257605e-09, 4.215863326136526e-09, 2.2740683276058303e-09, 1.2141976048222072e-09, 6.417178354458208e-10, 3.357126636165939e-10, 1.7384420310237249e-10, 8.910900187352152e-11, 4.5211802357053963e-11, 2.2706538727716806e-11, 1.1288050158021126e-11, 5.554638491023806e-12, 2.7055872512705062e-12, 1.3044762131729475e-12, 6.225577737568753e-13, 2.940979738121426e-13, 1.3752234849134903e-13, 6.365366349200241e-14, 2.9163685307990903e-14, 1.3226053604903335e-14, 5.9372731511694584e-15, 2.638230011929564e-15, 1.1603985010839856e-15, 5.05208370627996e-16, 2.1772223903555597e-16, 9.287608963747761e-17, 3.9216960243814e-17, 1.639127775655324e-17, 6.7814188194379525e-18, 2.777136334211239e-18, 1.1257519545234085e-18, 4.517072848156948e-19, 1.79407401238564e-19, 7.053302204662157e-20, 2.744817691969876e-20, 1.0573125630179778e-20, 4.0314581568062645e-21, 1.5215625004581603e-21, 5.684422005029409e-22, 2.1020919225908269e-22, 7.69459862670642e-23], "x": [0.0, 0.10101010101010101, 0.20202020202020202, 0.30303030303030304, 0.40404040404040403, 0.5050505050505051, 0.6060606060606061, 0.7070707070707071, 0.8080808080808081, 0.9090909090909091, 1.0101010101010102, 1.1111111111111112, 1.2121212121212122, 1.3131313131313131, 1.4141414141414141, 1.5151515151515151, 1.6161616161616161, 1.7171717171717171, 1.8181818181818181, 1.9191919191919191, 2.0202020202020203, 2.121212121212121, 2.2222222222222223, 2.323232323232323, 2.4242424242424243, 2.525252525252525, 2.6262626262626263, 2.727272727272727, 2.8282828282828283, 2.929292929292929, 3.0303030303030303, 3.131313131313131, 3.2323232323232323, 3.3333333333333335, 3.4343434343434343, 3.5353535353535355, 3.6363636363636362, 3.7373737373737375, 3.8383838383838382, 3.9393939393939394, 4.040404040404041, 4.141414141414141, 4.242424242424242, 4.343434343434343, 4.444444444444445, 4.545454545454545, 4.646464646464646, 4.747474747474747, 4.848484848484849, 4.94949494949495, 5.05050505050505, 5.151515151515151, 5.252525252525253, 5.353535353535354, 5.454545454545454, 5.555555555555555, 5.656565656565657, 5.757575757575758, 5.858585858585858, 5.959595959595959, 6.0606060606060606, 6.161616161616162, 6.262626262626262, 6.363636363636363, 6.4646464646464645, 6.565656565656566, 6.666666666666667, 6.767676767676767, 6.8686868686868685, 6.96969696969697, 7.070707070707071, 7.171717171717171, 7.2727272727272725, 7.373737373737374, 7.474747474747475, 7.575757575757575, 7.6767676767676765, 7.777777777777778, 7.878787878787879, 7.979797979797979, 8.080808080808081, 8.181818181818182, 8.282828282828282, 8.383838383838384, 8.484848484848484, 8.585858585858587, 8.686868686868687, 8.787878787878787, 8.88888888888889, 8.98989898989899, 9.09090909090909, 9.191919191919192, 9.292929292929292, 9.393939393939394, 9.494949494949495, 9.595959595959595, 9.696969696969697, 9.797979797979798, 9.8989898989899, 10.0]}}, "type": "ColumnDataSource", "id": "fe5729c7-5df0-42ba-a5be-71333dd42749"}, {"attributes": {}, "type": "BasicTicker", "id": "a47326bf-68dc-4371-af7e-61aa8451bd4e"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "ResetTool", "id": "63f9b331-8f39-4de0-8d81-985109b12c3b"}, {"attributes": {}, "type": "ToolEvents", "id": "54b22ed9-74d8-4862-b499-8c2a66dacc9b"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "ticker": {"type": "BasicTicker", "id": "b8af44c8-8d29-489d-814e-aa35824ff72b"}}, "type": "Grid", "id": "230f0938-e734-400e-a559-0c6b633f9db3"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "overlay": {"type": "BoxAnnotation", "id": "5eab65b7-88eb-4c66-8972-dae8769c47a5"}}, "type": "BoxZoomTool", "id": "3ba5ea31-0365-4867-862e-1426b0674e9a"}, {"attributes": {}, "type": "BasicTickFormatter", "id": "ac9bc760-ba0a-4b9d-8e42-a2dd1f364c35"}, {"attributes": {"line_color": {"value": "black"}, "line_alpha": {"value": 1.0}, "render_mode": "css", "bottom_units": "screen", "level": "overlay", "top_units": "screen", "fill_alpha": {"value": 0.5}, "plot": null, "left_units": "screen", "line_dash": [4, 4], "line_width": {"value": 2}, "right_units": "screen", "fill_color": {"value": "lightgrey"}}, "type": "BoxAnnotation", "id": "5eab65b7-88eb-4c66-8972-dae8769c47a5"}, {"attributes": {"callback": null}, "type": "DataRange1d", "id": "68a88fa1-6019-479f-8cfa-e264745a729f"}, {"attributes": {"line_color": {"value": "red"}, "line_width": {"value": 3}, "y": {"field": "y"}, "x": {"field": "x"}}, "type": "Line", "id": "4b001b35-e9b8-4ecc-bb7b-4892a4e7b50c"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}, "ticker": {"type": "BasicTicker", "id": "96248cd4-98f1-4674-83da-d5a20c3014aa"}}, "type": "Grid", "id": "be64bcc1-b352-43d1-a854-13d25237f85c"}, {"attributes": {}, "type": "BasicTicker", "id": "96248cd4-98f1-4674-83da-d5a20c3014aa"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}}, "type": "HelpTool", "id": "2c8b67f5-dbe9-4159-94a0-aaed66aeb9a8"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "HelpTool", "id": "dd6c0682-0ea1-4ba6-8f5f-0193c9570676"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "WheelZoomTool", "id": "a1834890-802b-4ab8-87c6-259c170c65b2"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "PanTool", "id": "6d522ae4-8d07-4933-8529-e0ef8b4bb3b9"}, {"attributes": {"callback": null, "end": 1.05, "start": 0.05}, "type": "Range1d", "id": "ca6003fa-c6a7-4b01-b841-eac6b5d72138"}, {"attributes": {}, "type": "BasicTickFormatter", "id": "89e016b4-ba14-42ca-9742-62f6ae6d1a74"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}}, "type": "ResetTool", "id": "bf6444af-5f41-4730-b9c7-32e0bd9fec5e"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "PreviewSaveTool", "id": "fe7be3b8-357c-4530-9b0c-296f70071ba1"}, {"attributes": {"x_range": {"type": "DataRange1d", "id": "68a88fa1-6019-479f-8cfa-e264745a729f"}, "title": "Probability distribution plot", "y_range": {"type": "Range1d", "id": "ca6003fa-c6a7-4b01-b841-eac6b5d72138"}, "renderers": [{"type": "LinearAxis", "id": "ff01ab7c-1318-4e43-a966-7a5de3697b11"}, {"type": "Grid", "id": "230f0938-e734-400e-a559-0c6b633f9db3"}, {"type": "LinearAxis", "id": "e41ca148-5187-425f-b8c1-21dd1fe402bb"}, {"type": "Grid", "id": "a4d6eeae-27bd-4f7a-aa78-ad1b8a5066e8"}, {"type": "BoxAnnotation", "id": "5eab65b7-88eb-4c66-8972-dae8769c47a5"}, {"type": "GlyphRenderer", "id": "1769b7c7-4c0d-4f96-ac3b-02797d13c52f"}], "below": [{"type": "LinearAxis", "id": "ff01ab7c-1318-4e43-a966-7a5de3697b11"}], "tool_events": {"type": "ToolEvents", "id": "54b22ed9-74d8-4862-b499-8c2a66dacc9b"}, "plot_height": 300, "tools": [{"type": "PanTool", "id": "6d522ae4-8d07-4933-8529-e0ef8b4bb3b9"}, {"type": "WheelZoomTool", "id": "a1834890-802b-4ab8-87c6-259c170c65b2"}, {"type": "BoxZoomTool", "id": "3ba5ea31-0365-4867-862e-1426b0674e9a"}, {"type": "PreviewSaveTool", "id": "fe7be3b8-357c-4530-9b0c-296f70071ba1"}, {"type": "ResizeTool", "id": "c6f4ad32-dd2c-4f32-9252-ad62c59a763f"}, {"type": "ResetTool", "id": "63f9b331-8f39-4de0-8d81-985109b12c3b"}, {"type": "HelpTool", "id": "dd6c0682-0ea1-4ba6-8f5f-0193c9570676"}], "left": [{"type": "LinearAxis", "id": "e41ca148-5187-425f-b8c1-21dd1fe402bb"}]}, "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29", "subtype": "Figure"}, {"attributes": {"callback": null}, "type": "DataRange1d", "id": "833141fd-c3bc-4620-9891-cfb522b67a32"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}, "ticker": {"type": "BasicTicker", "id": "a47326bf-68dc-4371-af7e-61aa8451bd4e"}, "dimension": 1}, "type": "Grid", "id": "a4d6eeae-27bd-4f7a-aa78-ad1b8a5066e8"}, {"attributes": {}, "type": "BasicTickFormatter", "id": "6d99b925-1dca-42ec-9260-ab8fc01f69f2"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}}, "type": "WheelZoomTool", "id": "ab3d7c81-a67c-48b9-8d2a-9691145ad93e"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}}, "type": "PreviewSaveTool", "id": "a81f01a5-0e03-4d20-a5eb-107d79d1c285"}, {"attributes": {"nonselection_glyph": {"type": "Line", "id": "73bfe2e3-bf2e-42f3-8759-a11b0ee8089d"}, "data_source": {"type": "ColumnDataSource", "id": "fe5729c7-5df0-42ba-a5be-71333dd42749"}, "selection_glyph": null, "hover_glyph": null, "glyph": {"type": "Line", "id": "4b001b35-e9b8-4ecc-bb7b-4892a4e7b50c"}}, "type": "GlyphRenderer", "id": "1769b7c7-4c0d-4f96-ac3b-02797d13c52f"}, {"attributes": {"line_color": {"value": "black"}, "line_alpha": {"value": 1.0}, "render_mode": "css", "bottom_units": "screen", "level": "overlay", "top_units": "screen", "fill_alpha": {"value": 0.5}, "plot": null, "left_units": "screen", "line_dash": [4, 4], "line_width": {"value": 2}, "right_units": "screen", "fill_color": {"value": "lightgrey"}}, "type": "BoxAnnotation", "id": "dea8ed6f-a3d8-4ac0-8db1-9737336a9670"}, {"attributes": {"callback": null, "column_names": ["right", "top", "left"], "data": {"top": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 3, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], "right": [-0.98, -0.96, -0.94, -0.92, -0.9, -0.88, -0.86, -0.84, -0.8200000000000001, -0.8, -0.78, -0.76, -0.74, -0.72, -0.7, -0.6799999999999999, -0.6599999999999999, -0.64, -0.62, -0.6, -0.5800000000000001, -0.56, -0.54, -0.52, -0.5, -0.48, -0.45999999999999996, -0.43999999999999995, -0.42000000000000004, -0.4, -0.38, -0.36, -0.33999999999999997, -0.31999999999999995, -0.29999999999999993, -0.28, -0.26, -0.24, -0.21999999999999997, -0.19999999999999996, -0.17999999999999994, -0.16000000000000003, -0.14, -0.12, -0.09999999999999998, -0.07999999999999996, -0.05999999999999994, -0.040000000000000036, -0.020000000000000018, 0.0, 0.020000000000000018, 0.040000000000000036, 0.06000000000000005, 0.08000000000000007, 0.10000000000000009, 0.1200000000000001, 0.14000000000000012, 0.15999999999999992, 0.17999999999999994, 0.19999999999999996, 0.21999999999999997, 0.24, 0.26, 0.28, 0.30000000000000004, 0.32000000000000006, 0.3400000000000001, 0.3600000000000001, 0.3800000000000001, 0.40000000000000013, 0.41999999999999993, 0.43999999999999995, 0.45999999999999996, 0.48, 0.5, 0.52, 0.54, 0.56, 0.5800000000000001, 0.6000000000000001, 0.6200000000000001, 0.6400000000000001, 0.6600000000000001, 0.6799999999999999, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 0.8200000000000001, 0.8400000000000001, 0.8600000000000001, 0.8800000000000001, 0.9000000000000001, 0.9199999999999999, 0.94, 0.96, 0.98, 1.0], "left": [-1.0, -0.98, -0.96, -0.94, -0.92, -0.9, -0.88, -0.86, -0.84, -0.8200000000000001, -0.8, -0.78, -0.76, -0.74, -0.72, -0.7, -0.6799999999999999, -0.6599999999999999, -0.64, -0.62, -0.6, -0.5800000000000001, -0.56, -0.54, -0.52, -0.5, -0.48, -0.45999999999999996, -0.43999999999999995, -0.42000000000000004, -0.4, -0.38, -0.36, -0.33999999999999997, -0.31999999999999995, -0.29999999999999993, -0.28, -0.26, -0.24, -0.21999999999999997, -0.19999999999999996, -0.17999999999999994, -0.16000000000000003, -0.14, -0.12, -0.09999999999999998, -0.07999999999999996, -0.05999999999999994, -0.040000000000000036, -0.020000000000000018, 0.0, 0.020000000000000018, 0.040000000000000036, 0.06000000000000005, 0.08000000000000007, 0.10000000000000009, 0.1200000000000001, 0.14000000000000012, 0.15999999999999992, 0.17999999999999994, 0.19999999999999996, 0.21999999999999997, 0.24, 0.26, 0.28, 0.30000000000000004, 0.32000000000000006, 0.3400000000000001, 0.3600000000000001, 0.3800000000000001, 0.40000000000000013, 0.41999999999999993, 0.43999999999999995, 0.45999999999999996, 0.48, 0.5, 0.52, 0.54, 0.56, 0.5800000000000001, 0.6000000000000001, 0.6200000000000001, 0.6400000000000001, 0.6600000000000001, 0.6799999999999999, 0.7, 0.72, 0.74, 0.76, 0.78, 0.8, 0.8200000000000001, 0.8400000000000001, 0.8600000000000001, 0.8800000000000001, 0.9000000000000001, 0.9199999999999999, 0.94, 0.96, 0.98]}}, "type": "ColumnDataSource", "id": "08181e76-a636-4f16-9e31-f133dc1c9710"}, {"attributes": {}, "type": "BasicTicker", "id": "9c3c2b65-69bb-4b33-bbe5-241f0843c81a"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "e48d111c-f0f1-4e7f-a0cc-9d36476a7b29"}}, "type": "ResizeTool", "id": "c6f4ad32-dd2c-4f32-9252-ad62c59a763f"}, {"attributes": {"plot": {"subtype": "Figure", "type": "Plot", "id": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}, "ticker": {"type": "BasicTicker", "id": "9c3c2b65-69bb-4b33-bbe5-241f0843c81a"}, "dimension": 1}, "type": "Grid", "id": "7d346df8-2ceb-4de9-8b98-ed4ab8267426"}]}, "title": "Bokeh Application"}};
var render_items = [{"notebook_comms_target": "eca79a3a-ba0f-4e5d-85e1-5721675f1ab2", "docid": "b6f798ac-5ee5-4bc6-b423-d91c5b2dd3a6", "elementid": "d78704b0-3c09-445c-9406-f3033ea9349e", "modelid": "d4dee7ae-e613-4cf2-b53a-2f3e9050e520"}];

Bokeh.embed.embed_items(docs_json, render_items);
});
},
function(Bokeh) {
}
];

function run_inline_js() {
for (var i = 0; i < inline_js.length; i++) {
inline_js[i](window.Bokeh);
}
}

console.log("Bokeh: BokehJS loaded, going straight to plotting");
run_inline_js();
} else {
console.log("Bokeh: BokehJS plotting callback run at", now());
run_inline_js();
});
}
}(this));

Out[18]:

<bokeh.io._CommsHandle at 0x10ba494d0>




In [19]:

interact(update,nsamples=(1,500))




Out[19]:

<function __main__.update>



An example of better estimators than the mean for distributions with heavy tails.



In [20]:

from scipy.stats import uniform

from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

#------------------------------------------------------------
# Generate the random distribution
N = (10 ** np.linspace(2, 4, 1000)).astype(int)
mu = 0
W = 2
rng = uniform(mu - 0.5 * W, W)  # uniform distribution between mu-W and mu+W

#------------------------------------------------------------
# Compute the cumulative mean and min/max estimator of the sample
mu_estimate_mean = np.zeros(N.shape)
mu_estimate_minmax = np.zeros(N.shape)

for i in xrange(len(N)):
x = rng.rvs(N[i])  # generate N[i] uniformly distributed values
mu_estimate_mean[i] = np.mean(x)
mu_estimate_minmax[i] = 0.5 * (np.min(x) + np.max(x))

# compute the expected scalings of the estimator uncertainties
N_scaling = 2. * W / N / np.sqrt(12)
root_N_scaling = W / np.sqrt(N * 12)

#------------------------------------------------------------
# Plot the results
fig = plt.figure(figsize=(10, 7))

# upper plot: mean statistic
ax.scatter(N, mu_estimate_mean, c='b', lw=0, s=4)

for nsig in (1, 2, 3):
ax.fill(np.hstack((N, N[::-1])),
np.hstack((nsig * root_N_scaling,
-nsig * root_N_scaling[::-1])), 'b', alpha=0.2)
ax.set_xlim(N[0], N[-1])
ax.set_ylim(-0.199, 0.199)
ax.set_ylabel(r'$\bar{\mu}$')
ax.xaxis.set_major_formatter(plt.NullFormatter())

ax.text(0.99, 0.95,
r'$\bar\mu = \mathrm{mean}(x)$',
ha='right', va='top', transform=ax.transAxes)
ax.text(0.99, 0.02,
r'$\sigma = \frac{1}{\sqrt{12}}\cdot\frac{W}{\sqrt{N}}$',
ha='right', va='bottom', transform=ax.transAxes)

# lower plot: min/max statistic
ax.scatter(N, mu_estimate_minmax, c='g', lw=0, s=4)

for nsig in (1, 2, 3):
ax.fill(np.hstack((N, N[::-1])),
np.hstack((nsig * N_scaling,
-nsig * N_scaling[::-1])), 'g', alpha=0.2)
ax.set_xlim(N[0], N[-1])
ax.set_ylim(-0.0399, 0.0399)
ax.set_xlabel('$N$')
ax.set_ylabel(r'$\bar{\mu}$')

ax.text(0.99, 0.95,
r'$\bar\mu = \frac{1}{2}[\mathrm{max}(x) + \mathrm{min}(x)]$',
ha='right', va='top', transform=ax.transAxes)
ax.text(0.99, 0.02,
r'$\sigma = \frac{1}{\sqrt{12}}\cdot\frac{2W}{N}$',
ha='right', va='bottom', transform=ax.transAxes)

plt.show()






Here is an example of computing the components of a bivariate gaussian using a sample of 1000 data values with two levels of contamination. Figure 3.23.

The robust method implemented in astroML uses the robust estimator of the variance $\sigma_{G}$ and takes into account the correlation between parameters.



In [21]:

import numpy as np
from scipy import stats
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
from astroML.stats import fit_bivariate_normal
from astroML.stats.random import bivariate_normal

# percent sign needs to be escaped if usetex is activated
import matplotlib
if matplotlib.rcParams.get('text.usetex'):
pct = r'\%'
else:
pct = r'%'

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

N = 1000

sigma1 = 2.0
sigma2 = 1.0
mu = [10, 10]
alpha_deg = 45.0
alpha = alpha_deg * np.pi / 180

#------------------------------------------------------------
# Draw N points from a multivariate normal distribution
#
#   we use the bivariate_normal function from astroML.  A more
#   general function for this is numpy.random.multivariate_normal(),
#   which requires the user to specify the full covariance matrix.
#   bivariate_normal() generates this covariance matrix for the
#   given inputs

np.random.seed(0)
X = bivariate_normal(mu, sigma1, sigma2, alpha, N)

#------------------------------------------------------------
# Create the figure showing the fits
fig = plt.figure(figsize=(15, 7.5)) ##3 times the original size
bottom=0.15, top=0.95)

# We'll create two figures, with two levels of contamination
for i, f in enumerate([0.05, 0.15]):
ax = fig.add_subplot(1, 2, i + 1)

# add outliers distributed using a bivariate normal.
X[:int(f * N)] = bivariate_normal((10, 10), 2, 4,
45 * np.pi / 180., int(f * N))
x, y = X.T

# compute the non-robust statistics
(mu_nr, sigma1_nr,
sigma2_nr, alpha_nr) = fit_bivariate_normal(x, y, robust=False)

# compute the robust statistics
(mu_r, sigma1_r,
sigma2_r, alpha_r) = fit_bivariate_normal(x, y, robust=True)

# scatter the points
ax.scatter(x, y, s=2, lw=0, c='k', alpha=0.5)

# Draw elipses showing the fits
for Nsig in [1, 3]:
# True fit
E = Ellipse((10, 10), sigma1 * Nsig, sigma2 * Nsig, alpha_deg,
ec='k', fc='none')

# Non-robust fit
E = Ellipse(mu_nr, sigma1_nr * Nsig, sigma2_nr * Nsig,
(alpha_nr * 180. / np.pi),
ec='k', fc='none', linestyle='dotted')

# Robust fit
E = Ellipse(mu_r, sigma1_r * Nsig, sigma2_r * Nsig,
(alpha_r * 180. / np.pi),
ec='k', fc='none', linestyle='dashed')

ax.text(0.04, 0.96, '%i%s outliers' % (f * 100, pct),
ha='left', va='top', transform=ax.transAxes)

ax.set_xlim(5.5, 14.5)
ax.set_ylim(5.5, 14.5)
ax.set_xlabel('$x$')

# This is a bit of a hack:
# We'll draw some lines off the picture to make our legend look better
ax.plot([0], [0], '-k', label='Input')
ax.plot([0], [0], ':k', label='Fit')
ax.plot([0], [0], '--k', label='Robust Fit')
ax.legend(loc='lower right')

if i == 0:
ax.set_ylabel('$y$')
else:
ax.yaxis.set_major_formatter(plt.NullFormatter())

plt.show()






## Correlation Coefficients

This section discusses the strengths and weaknesses of three techniques to measure to the sample correlation coefficient.

1. Pearson

• Measurement errors are not used
• Sensitive to outliers
• Measures linear relationship
2. Spearman

• Based on ranks (exactly Pearsons but on the ranks instead of the actual data).
• Measures any kind of monotonic dependency
3. Kendall

• Based on ranks (takes different between concordant and discordant pairs)
• harder to compute than Spearman.
• approaches normality faster than spearman

The nonparametric methods don't require knowledge of the distribution of the data to determine the distribution of the sample. Ranks limit outliers to the value of their ranks.



In [22]:

from astroML.stats.random import bivariate_normal
from astroML.decorators import pickle_results

# percent sign must be escaped if usetex=True
import matplotlib
if matplotlib.rcParams.get('text.usetex'):
pct = '\%'
else:
pct = '%'

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

#------------------------------------------------------------
# Set parameters for the distributions
Nbootstraps = 5000
N = 1000

sigma1 = 2.0
sigma2 = 1.0
mu = (10.0, 10.0)
alpha_deg = 45.0
alpha = alpha_deg * np.pi / 180
f = 0.01

#------------------------------------------------------------
# sample the distribution
# without outliers and with outliers
np.random.seed(0)
X = bivariate_normal(mu, sigma1, sigma2, alpha, N)

X_out = X.copy()
X_out[:int(f * N)] = bivariate_normal(mu, 2, 5,
45 * np.pi / 180., int(f * N))

# true values of rho (pearson/spearman r) and tau
# tau value comes from Eq. 41 of arXiv:1011.2009
rho_true = 0.6
tau_true = 2 / np.pi * np.arcsin(rho_true)

#------------------------------------------------------------
# Create a function to compute the statistics.  Since this
#  takes a while, we'll use the "pickle_results" decorator
#  to save the results of the computation to disk
@pickle_results('fig_correlations_dump.pkl')
def compute_results(N, Nbootstraps):
results = np.zeros((3, 2, Nbootstraps))

for k in range(Nbootstraps):
ind = np.random.randint(N, size=N)
for j, data in enumerate([X, X_out]):
x = data[ind, 0]
y = data[ind, 1]
for i, statistic in enumerate([stats.pearsonr,
stats.spearmanr,
stats.kendalltau]):
results[i, j, k] = statistic(x, y)[0]

return results

results = compute_results(N, Nbootstraps)

#------------------------------------------------------------
# Plot the results in a three-panel plot
fig = plt.figure(figsize=(10, 10))

histargs = (dict(alpha=0.5, label='No Outliers'),
dict(alpha=0.8, label='%i%s Outliers' % (int(f * 100), pct)))

distributions = ['Pearson-r', 'Spearman-r', r'Kendall-$\tau$']
xlabels = ['r_p', 'r_s', r'\tau']\

for i in range(3):
for j in range(2):
ax.hist(results[i, j], 40, histtype='stepfilled', fc='gray',
normed=True, **histargs[j])

if i == 0:
ax.legend(loc=2)

ylim = ax.get_ylim()
if i < 2:
ax.plot([rho_true, rho_true], ylim, '--k', lw=1)
ax.set_xlim(0.34, 0.701)
else:
ax.plot([tau_true, tau_true], ylim, '--k', lw=1)
ax.set_xlim(0.31, 0.48)
ax.set_ylim(ylim)

ax.text(0.98, 0.95, distributions[i], ha='right', va='top',
transform=ax.transAxes, bbox=dict(fc='w', ec='w'))

ax.set_xlabel('$%s$' % xlabels[i])
ax.set_ylabel('$N(%s)$' % xlabels[i])

plt.show()




@pickle_results: using precomputed results from 'fig_correlations_dump.pkl'



## 1-D Random Number generation

generate a uniform random number and apply the inverse CDF of the distribution of the type of random variable you would like to simulate.

This example shows how to clone a disribution and compute the inverse cdf with interpolation.



In [23]:

from scipy import stats, interpolate
from astroML.plotting import hist
from astroML.density_estimation import EmpiricalDistribution

#----------------------------------------------------------------------
# This function adjusts matplotlib settings for a uniform feel in the textbook.
# Note that with usetex=True, fonts are rendered with LaTeX.  This may
# result in an error if LaTeX is not installed on your system.  In that case,
# you can set usetex to False.
from astroML.plotting import setup_text_plots
setup_text_plots(fontsize=8, usetex=True)

#------------------------------------------------------------å
# Create a distribution and clone it
Ndata = 1000
Nclone = 100000
np.random.seed(0)

# generate an 'observed' bimodal distribution with 10000 values
dists = (stats.norm(-1.3, 0.5), stats.norm(1.3, 0.5))
fracs = (0.6, 0.4)
x = np.hstack((d.rvs(f * Ndata) for d, f in zip(dists, fracs)))

# We can clone the distribution easily with this function
x_cloned = EmpiricalDistribution(x).rvs(Nclone)

# compute the KS test to check if they're the same
D, p = stats.ks_2samp(x, x_cloned)
print "KS test: D = %.2g; p = %.2g" % (D, p)

#------------------------------------------------------------
# For the sake of this example, we need to calculate some
#  of the partial steps used by EmpiricalDistribution

# create a cumulative distribution
x.sort()
Px_cuml = np.linspace(0, 1, Ndata)

# set up an interpolation of the inverse cumulative distribution
tck = interpolate.splrep(Px_cuml, x)

# sample evenly along the cumulative distribution, and interpolate
Px_cuml_sample = np.linspace(0, 1, 10 * Ndata)
x_sample = interpolate.splev(Px_cuml_sample, tck)

#------------------------------------------------------------
# Plot the cloned distribution and the procedure for obtaining it
fig = plt.figure(figsize=(10, 10))
bottom=0.08, top=0.92)

indices = np.linspace(0, Ndata - 1, 20).astype(int)

# plot a histogram of the input
hist(x, bins='knuth', ax=ax,
histtype='stepfilled', ec='k', fc='#AAAAAA')
ax.set_ylim(0, 300)
ax.set_title('Input data distribution')
ax.set_xlabel('$x$')
ax.set_ylabel('$N(x)$')

# plot the cumulative distribution
ax.scatter(x[indices], Px_cuml[indices], lw=0, c='k', s=9)
ax.plot(x, Px_cuml, '-k')
ax.set_xlim(-3, 3)
ax.set_ylim(-0.05, 1.05)
ax.set_title('Cumulative Distribution')
ax.set_xlabel('$x$')
ax.set_ylabel('$p(<x)$')

# plot the inverse cumulative distribution and spline fit
ax.scatter(Px_cuml[indices], x[indices], lw=0, c='k', s=9)
ax.plot(Px_cuml_sample, x_sample, '-k')
ax.arrow(0.7, -3, 0, 3.5, width=0.015, fc='gray', ec='gray',
ax.arrow(0.7, 0.9, -0.69, 0, width=0.1, fc='gray', ec='gray',
ax.set_xlim(-0.05, 1.05)
ax.set_ylim(-3, 3)
ax.set_title('Inverse Cuml. Distribution')
ax.set_xlabel('$p(<x)$')
ax.set_ylabel('$x$')

# plot the resulting cloned distribution
hist(x, bins='knuth', ax=ax,
histtype='stepfilled', normed=True,
ec='#AAAAAA', fc='#DDDDDD',
label='input data')
hist(x_cloned, bins='knuth', ax=ax,
histtype='step', normed=True,
color='k', label='cloned data')
ax.set_title('Cloned Distribution')
ax.set_xlabel('$x$')
ax.set_ylabel('$p(x)dx$')
ax.text(0.75, 0.95, "KS test:\nD = %.2f\np = %.2f" % (D, p),
ha='left', va='top', transform=ax.transAxes)

plt.show()




KS test: D = 0.0028; p = 1




In [ ]: