%matplotlib inline from collections import defaultdict import json import numpy as np import scipy as sp import matplotlib.pyplot as plt import pandas as pd from matplotlib import rcParams import matplotlib.cm as cm import matplotlib as mpl #colorbrewer2 Dark2 qualitative color table dark2_colors = [(0.10588235294117647, 0.6196078431372549, 0.4666666666666667), (0.8509803921568627, 0.37254901960784315, 0.00784313725490196), (0.4588235294117647, 0.4392156862745098, 0.7019607843137254), (0.9058823529411765, 0.1607843137254902, 0.5411764705882353), (0.4, 0.6509803921568628, 0.11764705882352941), (0.9019607843137255, 0.6705882352941176, 0.00784313725490196), (0.6509803921568628, 0.4627450980392157, 0.11372549019607843)] rcParams['figure.figsize'] = (10, 6) rcParams['figure.dpi'] = 150 rcParams['axes.color_cycle'] = dark2_colors rcParams['lines.linewidth'] = 2 rcParams['axes.facecolor'] = 'white' rcParams['font.size'] = 14 rcParams['patch.edgecolor'] = 'white' rcParams['patch.facecolor'] = dark2_colors[0] rcParams['font.family'] = 'StixGeneral' def remove_border(axes=None, top=False, right=False, left=True, bottom=True): """ Minimize chartjunk by stripping out unnecesasry plot borders and axis ticks The top/right/left/bottom keywords toggle whether the corresponding plot border is drawn """ ax = axes or plt.gca() ax.spines['top'].set_visible(top) ax.spines['right'].set_visible(right) ax.spines['left'].set_visible(left) ax.spines['bottom'].set_visible(bottom) #turn off all ticks ax.yaxis.set_ticks_position('none') ax.xaxis.set_ticks_position('none') #now re-enable visibles if top: ax.xaxis.tick_top() if bottom: ax.xaxis.tick_bottom() if left: ax.yaxis.tick_left() if right: ax.yaxis.tick_right() pd.set_option('display.width', 500) pd.set_option('display.max_columns', 100) f= lambda x,y: np.exp(-(x*x*y*y+x*x+y*y-8*x-8*y)/2.) xx=np.linspace(-1,8,100) yy=np.linspace(-1,8,100) xg,yg = np.meshgrid(xx,yy) z=f(xg.ravel(),yg.ravel()) z2 = z.reshape(xg.shape) z2 plt.contourf(xg,yg,z2) N = 40000 x=np.zeros(N+1) y=np.zeros(N+1) #Initialize x and y. x[0]=1. y[0]=6. sig = lambda z,i: np.sqrt(1./(1.+z[i]*z[i])) mu = lambda z,i: 4./(1.+z[i]*z[i]) for i in range(1,N,2): sig_x = sig(y,i-1) mu_x = mu(y,i-1) x[i] = np.random.normal(mu_x, sig_x) y[i] = y[i-1] sig_y = sig(x, i) mu_y = mu(x, i) y[i+1] = np.random.normal(mu_y, sig_y) x[i+1] = x[i] def traceplot(z): plt.plot(range(len(z)),z,'.') traceplot(x) plt.hist(x, bins=50); traceplot(y) plt.hist(y, bins=50); plt.contourf(xg,yg,z2, alpha=0.6) plt.scatter(x,y, alpha=0.1, c='k', s=5) plt.plot(x[:100],y[:100], c='r', alpha=0.3, lw=1) plt.acorr(x-np.mean(x), maxlags=100, lw=1 , normed=True); plt.acorr(y-np.mean(y), maxlags=100, lw=1 , normed=True); def effectiveSampleSize(data, stepSize = 1) : """ Effective sample size, as computed by BEAST Tracer.""" samples = len(data) assert len(data) > 1,"no stats for short sequences" maxLag = min(samples//3, 1000) gammaStat = [0,]*maxLag #varGammaStat = [0,]*maxLag varStat = 0.0; if type(data) != np.ndarray : data = np.array(data) normalizedData = data - data.mean() for lag in range(maxLag) : v1 = normalizedData[:samples-lag] v2 = normalizedData[lag:] v = v1 * v2 gammaStat[lag] = sum(v) / len(v) #varGammaStat[lag] = sum(v*v) / len(v) #varGammaStat[lag] -= gammaStat[0] ** 2 # print lag, gammaStat[lag], varGammaStat[lag] if lag == 0 : varStat = gammaStat[0] elif lag % 2 == 0 : s = gammaStat[lag-1] + gammaStat[lag] if s > 0 : varStat += 2.0*s else : break # standard error of mean # stdErrorOfMean = Math.sqrt(varStat/samples); # auto correlation time act = stepSize * varStat / gammaStat[0] # effective sample size ess = (stepSize * samples) / act return ess esx = effectiveSampleSize(x) esy = effectiveSampleSize(y) print "Effective Size for x: ", esx, " of ", len(x), " samples, rate of", esx/len(x)*100, "%." print "Effective Size for y: ", esy, " of ", len(y), " samples, rate of", esy/len(y)*100, "%."