def show_irs_vs_tenure(irs_threshold, tenure, no_banks, threshold, autocontinue=False): if not autocontinue: a = raw_input("Do you want to see IRS value vs tenure?[Y/n]") if (autocontinue or a != 'n'): for t in threshold: for b in no_banks: im_data = np.zeros((len(irs_threshold), len(tenure))) for i, irst in enumerate(irs_threshold): for j, ten in enumerate(tenure): agg_id = get_aggregate_id(b, t, irst, ten) x, y, r, raw = get_aggregate_dist(agg_id) err, alp = get_hump_error(x, y, raw) im_data[i, j] = min(alp, 10) if alp > 10: print "Large value for alpha" print alp #im = get_color_image(im_data) min_value = np.min(1) max_value = np.max(10) fig = pplot.figure() ax = fig.add_subplot(111) ax.set_ylabel("IRS Value") ax.set_yticklabels(np.arange(0, 9)) ax.set_xlabel("Term to maturity") ax.set_xticklabels([0, 50, 100, 250, 400, 550, 700, 850]) ax.set_title("Heatmap for %d banks, threshold %d" % (b, t)) cax = ax.imshow(im_data, origin='lower', interpolation='nearest', vmin=min_value, vmax=max_value) cb = fig.colorbar(cax) cb.set_ticks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) cb.set_ticklabels([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) cb.set_label('Alpha', rotation=270) # pplot.show() filename = 'alpha_irs_ten_%d_banks_%d_threshold.png' % (b, t) path = '/home/xxxxx/Programming/GitRepos/scriptie/figs/heatmaps/alpha/' fl = '%s%s' % (path, filename) pplot.savefig(fl, bbox_inches='tight') pplot.close(fig) if not autocontinue: try: a = raw_input("Do you want to continue [Y/n]") if (a == 'n'): return except KeyboardInterrupt: exit(0)
def run_svm(threshold, no_banks,irs_threshold,tenure): for t in threshold: for b in no_banks: points = np.dstack(np.meshgrid(irs_threshold, tenure)).reshape(-1, 2) alpha_class = np.zeros(len(points)) for i,x in enumerate(points): irst = x[0] ten = x[1] agg_id = get_aggregate_id(b,t,irst,ten) x,y,r,raw = get_aggregate_dist(agg_id) _,alp = get_hump_error(x,y,raw) if alp <= 3: alpha_class[i] = 1 else: alpha_class[i] = 0 svc = svm.SVC(kernel='poly').fit(points,alpha_class) xx, yy = np.meshgrid(np.arange(min(irs_threshold),max(irs_threshold), 0.02), np.arange(min(tenure),max(tenure), 0.02)) Z = svc.predict(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) pplot.figure() pplot.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8) pplot.scatter(points[:,0],points[:,1], c=alpha_class) pplot.show()
def show_mulitple(aggregate_ids): fig = pplot.figure() ax = fig.add_subplot(111) ax.set_xscale('log') ax.set_yscale('log') cmap = pplot.get_cmap('YlOrRd') n = len(aggregate_ids) for (i,aggregate_id) in enumerate(aggregate_ids): x,y,run,raw = get_aggregate_dist(aggregate_id) strpol = (run.no_banks,run.max_tenure,run.max_irs_value,run.threshold) l = "%d banks, %d tenure, %d IRS Value, %d threshold"%strpol err,alpha = get_hump_info(aggregate_id) xn = x#/run.no_banks yn = gaussian_filter1d([yi*pow(xi,alpha) for xi,yi in zip(x,y)],4) ax.scatter(xn,yn, color=cmap((i+1)/n),label=l) # Shrink current axis's height by 10% on the bottom box = ax.get_position() ax.set_position([box.x0, box.y0, box.width*0.75, box.height]) # Put a legend below current axis ax.legend(loc='center left', bbox_to_anchor=(1, 0.5), fancybox=True, shadow=True, ncol=1) pplot.show()
def show_irs_vs_threshold(irs_threshold, tenure, no_banks, threshold, autocontinue=False): if not autocontinue: a = raw_input("Do you want to see IRS value vs threshold?[Y/n]") if (autocontinue or a != 'n'): for ten in tenure: for b in no_banks: im_data = np.zeros((len(irs_threshold), len(threshold))) for i, irst in enumerate(irs_threshold): for j, t in enumerate(threshold): agg_id = get_aggregate_id(b, t, irst, ten) x, y, r, raw = get_aggregate_dist(agg_id) err, alp = get_hump_error(x, y, raw) im_data[i, j] = round(alp, 2) #im = get_color_image(im_data) min_value = np.min(1) max_value = np.max(10) fig = pplot.figure() ax = fig.add_subplot(111) ax.set_ylabel("IRS Value") ax.set_yticklabels(np.arange(0, 9)) ax.set_xlabel("Threshold") ax.set_xticklabels([0, 10, 15, 20, 25, 30]) ax.set_title("Heatmap for %d banks, term to maturity %d" % (b, ten)) cax = ax.imshow(im_data, origin='lower', interpolation='nearest', vmin=min_value, vmax=max_value) cb = fig.colorbar(cax) cb.set_ticks([min_value, max_value]) cb.set_ticklabels([min_value, max_value]) cb.set_label('P(x > Last powerlaw point)', rotation=270) filename = 'alpha_irs_thresh_%d_banks_%d_tenure.png' % (b, ten) path = '/home/..../Programming/GitRepos/scriptie/figs/heatmaps/alpha/' fl = '%s%s' % (path, filename) pplot.savefig(fl, bbox_inches='tight') pplot.close(fig) if not autocontinue: try: a = raw_input("Do you want to continue [Y/n]") if (a == 'n'): return except KeyboardInterrupt: exit(0)
def show_single(aggregate_id): x,y,run = get_aggregate_dist(aggregate_id) fig = pplot.figure() ax = fig.add_subplot(111) ax.set_xscale('log') ax.set_yscale('log') ax.scatter(x,y) pplot.show()
def moments_by_type(tpe): query = """SELECT aggregate_type.aggregate_id FROM aggregate_type INNER JOIN run ON run.aggregate_id = aggregate_type.aggregate_id WHERE type_id = %d and run.no_banks = 200 GROUP BY aggregate_type.aggregate_id""" % tpe session = get_session() aggregate_ids = session.execute(query).fetchall() session.close() moments = np.zeros((len(aggregate_ids))) for i, aggregate_id in enumerate(aggregate_ids): _, _, _, d = get_aggregate_dist(aggregate_id[0]) moments[i] = stats.moment(d, 3) print moments[i], aggregate_id return moments
def show_irs_vs_threshold(irs_threshold, tenure, no_banks, threshold, moment): a = raw_input("Do you want to see IRS value vs threshold?[Y/n]") if (a != 'n'): for ten in tenure: for b in no_banks: im_data = np.zeros((len(irs_threshold), len(threshold))) for i, irst in enumerate(irs_threshold): for j, t in enumerate(threshold): agg_id = get_aggregate_id(b, t, irst, ten) x, y, r, raw = get_aggregate_dist(agg_id) m = stats.moment(raw, moment) im_data[i, j] = np.log(m) fig = pplot.figure() ax = fig.add_subplot(111) ax.set_ylabel("IRS Value") ax.set_yticklabels(np.arange(0, 9)) ax.set_xlabel("Threshold") ax.set_xticklabels([0, 10, 15, 20, 25, 30]) ax.set_title("Heatmap for %d banks, tenure %d" % (b, ten)) cax = ax.imshow(im_data, origin='lower', interpolation='nearest', vmin=np.min(im_data), vmax=np.max(im_data), cmap=clrs.Reds) cb = fig.colorbar(cax, ticks=[np.min(im_data), np.max(im_data)]) pplot.show() try: a = raw_input("Do you want to continue [Y/n]") if (a == 'n'): return except KeyboardInterrupt: exit(0)
if __name__ == '__main__': irs_threshold = np.arange(1,9) tenure = [50,100,250,400,550,700,850] no_banks = [100, 200]#np.arange(100,600,100) threshold = np.arange(10,35,5) a = raw_input("Do you want to see IRS value vs tenure?[Y/n]") if(a != 'n'): for t in threshold: for b in no_banks: im_data = np.zeros((len(irs_threshold),len(tenure))) for i,irst in enumerate(irs_threshold): for j,ten in enumerate(tenure): agg_id = get_aggregate_id(b,t,irst,ten) x,y,r,raw = get_aggregate_dist(agg_id) err,alp = get_hump_error(x,y,raw) im_data[i,j] = alp fig = pplot.figure() ax = fig.add_subplot(111) ax.set_ylabel("IRS Value") ax.set_yticklabels(np.arange(0,9)) ax.set_xlabel("Tenure") ax.set_xticklabels([0,50,100,250,400,550,700,850]) ax.set_title("Heatmap for %d banks, threshold %d:Alpha"%(b,t)) cax = ax.imshow(im_data,origin='lower',interpolation='nearest', vmin=np.min(im_data),vmax=np.max(im_data)) pplot.colorbar(cax)
def get_frequencies(no_banks, aggregate_id): x, y, r, raw = get_aggregate_dist(aggregate_id) _, alp = get_hump_error(x, y, raw) cumv = get_hump_cumulative_value(x,y) return [alp, cumv]
def get_weight(aggregate_id): x, y, _, raw = get_aggregate_dist(aggregate_id) _, alp = get_hump_error(x, y, raw) cumv = get_hump_cumulative_value(x,y) return (aggregate_id, alp, cumv)