def plot_posterior_nodes(model, param_nodes, bins=100, lb=None, ub=None): sns.set_style('white') sns.despine() #title='Generic Title' if param_nodes=='z': nodes=get_nodes(model, 'z') xlabel='Mean Starting-Point' + r'$\/(\mu_{z})$' lb=.52 ub=.67 elif param_nodes=='vf': nodes=get_nodes(model, 'vf') xlabel='Mean Face Drift-Rate' + r'$\/(\mu_{vF})$' lb=.35 ub=1.2 elif param_nodes=='vh': nodes=get_nodes(model, 'vh') xlabel='Mean House Drift-Rate' + r'$\/(\mu_{vH})$' lb=-1.15 ub=-.25 else: print "Must provide argument: 'z', 'vf', or 'vh'" fig=plt.figure() fig.subplots_adjust(top=0.95, wspace=0.12, left=0.12, right=0.88, bottom=0.16) sns.despine() #fig.suptitle(title, fontsize=20) #fig.suptitle(title, fontsize=40) if lb is None: lb = min([min(node.trace()[:]) for node in nodes]) if ub is None: ub = max([max(node.trace()[:]) for node in nodes]) x_data = np.linspace(lb, ub, 600) #colors=['Green', 'LimeGreen', 'Black', 'Cyan', 'Blue'] colors=['Red','Magenta', 'Black', 'Cyan', 'Blue'] color_i=0 for node in nodes: trace = node.trace()[:] #hist = interpolate_trace(x_data, trace, range=(trace.min(), trace.max()), bins=bins) hist = interpolate_trace(x_data, trace, range=(lb, ub), bins=bins) plt.plot(x_data, hist, label=node.__name__, lw=2., color=colors[color_i]) plt.fill_between(x_data, hist, 0, label=node.__name__, color=colors[color_i], alpha=0.3) ax=plt.gca() ax.set_xlim(lb, ub) plt.setp(ax.get_yticklabels(), visible=False) sns.despine() ax.set_ylabel('Probability Mass', fontsize=22, labelpad=12) ax.set_xlabel(xlabel, fontsize=22, labelpad=13) plt.setp(ax.get_xticklabels(), fontsize=18) plt.locator_params(axis='x', nbins=10) color_i+=1 #leg = plt.legend(loc='best', fancybox=True) #leg.get_frame().set_alpha(0.5) plt.ylim(ymin=0) plt.savefig(str(param_nodes)+'_posterior_nodes.png', dpi=600)
def savage_dickey(pos, post_trace, range=(-.3,.3), bins=40, prior_trace=None, prior_y=None): """Calculate Savage-Dickey density ratio test, see Wagenmakers et al. 2010 at http://dx.doi.org/10.1016/j.cogpsych.2009.12.001 :Arguments: pos : float position at which to calculate the savage dickey ratio at (i.e. the spec hypothesis you want to test) post_trace : numpy.array trace of the posterior distribution :Optional: prior_trace : numpy.array trace of the prior distribution prior_y : numpy.array prior density pos range : (int,int) Range over which to interpolate and plot bins : int Over how many bins to compute the histogram over :Note: Supply either prior_trace or prior_y. """ x = np.linspace(range[0], range[1], bins) if prior_trace is not None: # Prior is provided as a trace -> histogram + interpolate prior_pos = interpolate_trace(pos, prior_trace, range=range, bins=bins) elif prior_y is not None: # Prior is provided as a density for each point -> interpolate to retrieve positional density import scipy.interpolate prior_pos = prior_y #scipy.interpolate.InterpolatedUnivariateSpline(x, prior_y)(pos) else: assert ValueError, "Supply either prior_trace or prior_y keyword arguments" # Histogram and interpolate posterior trace at SD position posterior_pos = interpolate_trace(pos, post_trace, range=range, bins=bins) # Calculate Savage-Dickey density ratio at pos sav_dick = prior_pos / posterior_pos return sav_dick
def plot_posteriors(nodes, bins=50): from kabuki.utils import interpolate_trace figure() lb = min([min(node.trace()[:]) for node in nodes]) ub = max([max(node.trace()[:]) for node in nodes]) x_data = np.linspace(lb, ub, 300) for node in nodes: trace = node.trace()[:] #hist = interpolate_trace(x_data, trace, range=(trace.min(), trace.max()), bins=bins) hist = interpolate_trace(x_data, trace, range=(lb, ub), bins=bins) plt.plot(x_data, hist, label=node.__name__, lw=2.) leg = plt.legend(loc='best', fancybox=True) leg.get_frame().set_alpha(0.5)
def plot_posterior_nodes(nodes, bins=50): """Plot interpolated posterior of a list of nodes. :Arguments: nodes : list of pymc.Node's List of pymc.Node's to plot the posterior of bins : int (default=50) How many bins to use for computing the histogram. """ from kabuki.utils import interpolate_trace figure() lb = min([min(node.trace()[:]) for node in nodes]) ub = max([max(node.trace()[:]) for node in nodes]) x_data = np.linspace(lb, ub, 300) for node in nodes: trace = node.trace()[:] #hist = interpolate_trace(x_data, trace, range=(trace.min(), trace.max()), bins=bins) hist = interpolate_trace(x_data, trace, range=(lb, ub), bins=bins) plt.plot(x_data, hist, label=node.__name__, lw=2.) leg = plt.legend(loc='best', fancybox=True) leg.get_frame().set_alpha(0.5)