def stats(self, alpha=0.05, start=0, batches=100): """ Generate posterior statistics for node. """ from utils import hpd, quantiles from numpy import sqrt try: trace = np.squeeze(np.array(self.trace(), float)[start:]) n = len(trace) if not n: print 'Cannot generate statistics for zero-length trace in', self.__name__ return return { 'n': n, 'standard deviation': trace.std(0), 'mean': trace.mean(0), '%s%s HPD interval' % (int(100*(1-alpha)),'%'): hpd(trace, alpha), 'mc error': batchsd(trace, batches), 'quantiles': quantiles(trace) } except: print 'Could not generate output statistics for', self.__name__ return
def write_vals(self,saveTo=None): """ Saves estimated parameters to csv file. Input: - m (Model) - saveTo (str) - path and name of csv file where results should be saved. Defaults to mod.saveTo+'-posteriorValues.csv'. """ m=self if saveTo==None: fname=m.saveTo+'-posteriorValues.csv' else: fname=saveTo f=open(fname,'w') f.write('\t'.join(['Parameter','mean','median','95% HPD','std'])+'\n') for v in m.parameters: trac=getattr(m,v+'s') hpdi=ut.hpd(trac,0.95) form='%.2f' if v.startswith('p') or v.startswith('k') or v.startswith('e'): form='%.2e' f.write('\t'.join([v,form%trac.mean(),form%sap(trac,50),('['+form+', '+form+']')%hpdi,form%trac.std()])+'\n') f.close() if saveTo==None: print "Saved posterior median and confidence intervals for each parameter, see "+ m.name+'-posterior_values.csv' else: print "Saved posterior median and confidence intervals for each parameter, see "+ saveTo
def histogram(data, name, nbins=None, datarange=(None, None), format='png', suffix='', path='./', rows=1, columns=1, num=1, last=True, fontmap = {1:10, 2:8, 3:6, 4:5, 5:4}, verbose=1): # Internal histogram specification for handling nested arrays try: # Stand-alone plot or subplot? standalone = rows==1 and columns==1 and num==1 if standalone: if verbose>0: print 'Generating histogram of', name figure() subplot(rows, columns, num) #Specify number of bins (10 as default) uniquevals = len(unique(data)) nbins = nbins or uniquevals*(uniquevals<=25) or int(4 + 1.5*log(len(data))) # Generate histogram hist(data.tolist(), nbins, histtype='stepfilled') xlim(datarange) # Plot options title('\n\n %s hist'%name, x=0., y=1., ha='left', va='top', fontsize='medium') ylabel("Frequency", fontsize='x-small') # Plot vertical lines for median and 95% HPD interval quant = calc_quantiles(data) axvline(x=quant[50], linewidth=2, color='black') for q in hpd(data, 0.05): axvline(x=q, linewidth=2, color='grey', linestyle='dotted') # Smaller tick labels tlabels = gca().get_xticklabels() setp(tlabels, 'fontsize', fontmap[rows]) tlabels = gca().get_yticklabels() setp(tlabels, 'fontsize', fontmap[rows]) if standalone: if not os.path.exists(path): os.mkdir(path) if not path.endswith('/'): path += '/' # Save to file savefig("%s%s%s.%s" % (path, name, suffix, format)) #close() except OverflowError: print '... cannot generate histogram'
def plot_coefs(coefs, label, offset, errorbars=True, marker='o'): "Plots the coefficients of a logistic regression analysis" x = np.array((0, 1, 2, 3)) if errorbars: y = [coef_series.mean() for coef_series in coefs] yerr = [[], []] for yy, coef_series in zip(y, coefs): uplim, lolim = utils.hpd(coef_series) yerr[0].append(yy - lolim) yerr[1].append(uplim - yy) plt.errorbar(x + offset, y, yerr, fmt='none', ecolor='black') else: y = coefs plt.plot(x + offset, y, marker, label=label)
def plot_stay_probs(probs, condition, legend): "Plots stay probabilities with error bars" y = [prob.median() for prob in probs] plt.title(condition) plt.ylim(0, 1) plt.bar((0, 2), (y[0], y[2]), color=COLOR_COMMON, label='Common') plt.bar((1, 3), (y[1], y[3]), color=COLOR_RARE, label='Rare') plt.xticks((0.5, 2.5), ('Rewarded', 'Unrewarded')) yerr = [[], []] for yy, prob_series in zip(y, probs): uplim, lolim = hpd(prob_series) yerr[0].append(yy - lolim) yerr[1].append(uplim - yy) plt.errorbar((0, 1, 2, 3), y, yerr, fmt='none', ecolor='black') plt.xlabel('Previous outcome') plt.ylabel('Stay probability') if legend: plt.legend(loc='best', title='Previous transition')
def stats(self, alpha=0.05, start=0, batches=100, chain=None): """ Generate posterior statistics for node. :Parameters: alpha : float The alpha level for generating posterior intervals. Defaults to 0.05. start : int The starting index from which to summarize (each) chain. Defaults to zero. batches : int Batch size for calculating standard deviation for non-independent samples. Defaults to 100. chain : int The index for which chain to summarize. Defaults to None (all chains). """ from utils import hpd, quantiles from numpy import sqrt try: trace = np.squeeze(np.array(self.trace(burn=start, chain=chain), float)) n = len(trace) if not n: print 'Cannot generate statistics for zero-length trace in', self.__name__ return return { 'n': n, 'standard deviation': trace.std(0), 'mean': trace.mean(0), '%s%s HPD interval' % (int(100*(1-alpha)),'%'): hpd(trace, alpha), 'mc error': batchsd(trace, batches), 'quantiles': quantiles(trace) } except: print 'Could not generate output statistics for', self.__name__ return
def interval(self, alpha): return tuple(hpd(self, 1 - alpha))