def plot_heatmap_at_iteration(self, iteration, **options): '''I line up the actual grid with interpolating grid so there is no interpolation, actually. Options: min_max_scale: [min, max] of the scale colorbar_decimals: number of decimals used in the colorbar ''' ax = plt.subplot() ternary.plot_heatmap(lambda x: self.interp_histogram_at_iteration(iteration, x), steps = self.num_individuals, boundary=True, **options) scale = self.num_individuals ternary.draw_boundary(scale=scale, ax=ax) plt.gca().set_aspect('equal', adjustable='box') plt.xlim([-.1*scale,1.1*scale]) plt.ylim([-0.13*scale, scale]) plt.text(1.01 * scale, -.05*scale, r'$f_1$', fontsize=35 ) plt.text(-.08 * scale, -.05*scale, r'$f_3$', fontsize=35 ) plt.text(.46*scale, .9*scale, r'$f_2$', fontsize=35) plt.gca().yaxis.set_visible(False) plt.gca().xaxis.set_visible(False) plt.grid(False) # Plot the fractional generation generation_formatted = '%.2f' % self.frac_gen[iteration] textbox = r'$N=' + str(self.num_individuals) + r'$' '\n' textbox += 'Num Simulations: ' + r'$10^' + str(int(np.log10(self.num_simulations))) + r'$' + '\n' textbox += 'Generation: ' + generation_formatted plt.text(.71*scale, .85*scale, textbox, fontsize=15, linespacing=1.75)
def plot_crps(cls, data, show_3d=False): """ Plot performance graph for all CRPs (Constant Rebalanced Portfolios). :param data: Stock prices. :param show_3d: Show CRPs on a 3-simplex, works only for 3 assets. 简易performance绘图模型,仅用于3种特定资产回报情况。 """ def _crp(data): B = list(tools.simplex_mesh(2, 100)) crps = CRP.run_combination(data, b=B) x = [b[0] for b in B] y = [c.total_wealth for c in crps] return x, y # init import ternary data = data.dropna(how='any') data = data / data.ix[0] dim = data.shape[1] # plot prices if dim == 2 and not show_3d: fig, axes = plt.subplots(ncols=2, sharey=True) data.plot(ax=axes[0], logy=True) else: data.plot(logy=False) if show_3d: assert dim == 3, '3D plot works for exactly 3 assets.' plt.figure() fun = lambda b: CRP(b).run(data).total_wealth ternary.plot_heatmap(fun, steps=20, boundary=True) elif dim == 2: x, y = _crp(data) s = pd.Series(y, index=x) s.plot(ax=axes[1], logy=True) plt.title('CRP performance') plt.xlabel('weight of {}'.format(data.columns[0])) elif dim > 2: fig, axes = plt.subplots(ncols=dim - 1, nrows=dim - 1) for i in range(dim - 1): for j in range(i + 1, dim): x, y = _crp(data[[i, j]]) ax = axes[i][j - 1] ax.plot(x, y) ax.set_title('{} & {}'.format(data.columns[i], data.columns[j])) ax.set_xlabel('weights of {}'.format(data.columns[i]))
def plot_crps(cls, data, show_3d=False): """ Plot performance graph for all CRPs (Constant Rebalanced Portfolios). :param data: Stock prices. :param show_3d: Show CRPs on a 3-simplex, works only for 3 assets. """ def _crp(data): B = list(tools.simplex_mesh(2, 100)) crps = CRP.run_combination(data, b=B) x = [b[0] for b in B] y = [c.total_wealth for c in crps] return x, y # init import ternary data = data.dropna(how='any') data = data / data.ix[0] dim = data.shape[1] # plot prices if dim == 2 and not show_3d: fig, axes = plt.subplots(ncols=2, sharey=True) data.plot(ax=axes[0], logy=True) else: data.plot(logy=False) if show_3d: assert dim == 3, '3D plot works for exactly 3 assets.' plt.figure() fun = lambda b: CRP(b).run(data).total_wealth ternary.plot_heatmap(fun, steps=20, boundary=True) elif dim == 2: x,y = _crp(data) s = pd.Series(y, index=x) s.plot(ax=axes[1], logy=True) plt.title('CRP performance') plt.xlabel('weight of {}'.format(data.columns[0])) elif dim > 2: fig, axes = plt.subplots(ncols=dim-1, nrows=dim-1) for i in range(dim-1): for j in range(i + 1, dim): x,y = _crp(data[[i,j]]) ax = axes[i][j-1] ax.plot(x,y) ax.set_title('{} & {}'.format(data.columns[i], data.columns[j])) ax.set_xlabel('weights of {}'.format(data.columns[i]))
def plot_heatmap_at_iteration(self, iteration, **options): '''I line up the actual grid with interpolating grid so there is no interpolation, actually. Options: min_max_scale: [min, max] of the scale colorbar_decimals: number of decimals used in the colorbar ''' ax = plt.subplot() ternary.plot_heatmap( lambda x: self.interp_histogram_at_iteration(iteration, x), steps=self.num_individuals, boundary=True, **options) scale = self.num_individuals ternary.draw_boundary(scale=scale, ax=ax) plt.gca().set_aspect('equal', adjustable='box') plt.xlim([-.1 * scale, 1.1 * scale]) plt.ylim([-0.13 * scale, scale]) plt.text(1.01 * scale, -.05 * scale, r'$f_1$', fontsize=35) plt.text(-.08 * scale, -.05 * scale, r'$f_3$', fontsize=35) plt.text(.46 * scale, .9 * scale, r'$f_2$', fontsize=35) plt.gca().yaxis.set_visible(False) plt.gca().xaxis.set_visible(False) plt.grid(False) # Plot the fractional generation generation_formatted = '%.2f' % self.frac_gen[iteration] textbox = r'$N=' + str(self.num_individuals) + r'$' '\n' textbox += 'Num Simulations: ' + r'$10^' + str( int(np.log10(self.num_simulations))) + r'$' + '\n' textbox += 'Generation: ' + generation_formatted plt.text(.71 * scale, .85 * scale, textbox, fontsize=15, linespacing=1.75)
def heatmap_example(func, steps=100, boundary=True): ternary.plot_heatmap(func, steps=steps, boundary=boundary) ternary.draw_boundary(scale=steps)