Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Nested / Picture In Picture Plots Example
import matplotlib.pyplot as plt
import pandas as pd
font_title = {'family': 'roboto',
'color': '#0f3567',
'weight': 'bold',
'size': 16,
}
font_mini_title = {'family': 'roboto',
'color': '#0f3567',
'weight': 'normal',
'size': 8,
}
font_axes = {'family': 'futura',
'color': '#16509f',
'weight': 'normal',
'size': 12,
}
fig = plt.figure(figsize=(12,10))
ax1 = fig.add_axes([0.1, 0.1, 0.85, 0.85])
ax2 = fig.add_axes([0.6, 0.15, 0.24, 0.24])
# ax1.text(0.01, 0.95, "ax1", size=12)
# ax2.text(0.05, 0.9, "ax2", size=12)
n = len(train_data)
sig = all_metrics['intrinsic_return_L']>0
sig_both = (all_metrics['intrinsic_return_L']>0) & (all_metrics['sharpe_ratio'] - 2/n > 0)
sig_sharpe_only = (all_metrics['intrinsic_return_L']<0) & (all_metrics['sharpe_ratio'] - 2/n > 0)
sig_intrinsic_only = (all_metrics['intrinsic_return_L']>0) & (all_metrics['sharpe_ratio'] - 2/n < 0)
sig_neither = (all_metrics['intrinsic_return_L']< 0) & (all_metrics['sharpe_ratio'] - 2/n < 0)
ax1.scatter(all_metrics[sig_neither]['intrinsic_return'],all_metrics[sig_neither]['sharpe_ratio'], c='b', alpha=0.05, marker='o')
ax1.scatter(all_metrics[sig_both]['intrinsic_return'],all_metrics[sig_both]['sharpe_ratio'], c='r', marker='^')
ax1.scatter(all_metrics[sig_intrinsic_only]['intrinsic_return'],all_metrics[sig_intrinsic_only]['sharpe_ratio'], c='r', marker='x')
ax1.scatter(all_metrics[sig_sharpe_only]['intrinsic_return'],all_metrics[sig_sharpe_only]['sharpe_ratio'], c='b', marker='x', alpha=0.2)
intrinsic_sig_SE = np.abs(all_metrics[sig][['intrinsic_return_L','intrinsic_return_U']].values - all_metrics[sig]['intrinsic_return'].values.reshape((-1,1)))
intrinsic_no_sig_SE = np.abs(all_metrics[~sig][['intrinsic_return_L','intrinsic_return_U']].values - all_metrics[~sig]['intrinsic_return'].values.reshape((-1,1)))
ax1.errorbar(all_metrics[sig]['intrinsic_return'], all_metrics[sig]['sharpe_ratio'], xerr=intrinsic_sig_SE.T, c='r', linewidth=1,ls='none', capsize=4, marker=None, alpha=0.1, label='95% CI')
# ax1.errorbar(all_metrics[~sig]['intrinsic_return'], all_metrics[~sig]['sharpe_ratio'], xerr=intrinsic_no_sig_SE.T, c='k', linewidth=1,ls='none', capsize=4, marker=None, alpha=0.05, label='95% CI')
xlim_lower = all_metrics['intrinsic_return'].min() - 0.0005
ax1.set_xlim(xlim_lower, all_metrics['intrinsic_return'].max()+ 0.0005)
ax = ax1
ax.set_title('Sharpe Ratio vs Intrinsic Return', loc='left', fontdict=font_title)
ax.set_xlabel('Intrinsic Daily Return', fontdict=font_axes)
ax.set_ylabel("Sharpe Ratio", fontdict=font_axes)
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["bottom"].set_edgecolor((0,0,0,0.5))
ax.set_facecolor((0,0,0,0.02))
ax.fill_between(np.array([xlim_lower,0]),y1=ax.get_ylim()[0],y2=ax.get_ylim()[1],color='k',alpha=0.05)
ax.legend(['Insignificant', 'Signficant by Both','Signficant by Intrinsic','Signficant by Sharpe'])
ax.axvline(0, linestyle='--', alpha=0.4, linewidth=1, c='k')
ax.axhline(0, linestyle='--', alpha=0.4, linewidth=1, c='k')
# plt.show()
ax = ax2
ax.spines["right"].set_visible(False)
ax.spines["top"].set_visible(False)
ax.spines["left"].set_visible(False)
ax.spines["bottom"].set_visible(False)
ax.set_facecolor((0,0,0,0.05))
sig = all_metrics['sharpe_ratio'] - 2/n > 0
ax2.scatter(all_metrics[sig]['intrinsic_return'], all_metrics[sig]['sharpe_ratio'], c='r', alpha=0.1)
ax2.scatter(all_metrics[~sig]['intrinsic_return'], all_metrics[~sig]['sharpe_ratio'], c='b', alpha=0.05)
xlim_lower = all_metrics['intrinsic_return'].min() - 0.0005
ax2.set_xlim(xlim_lower, all_metrics['intrinsic_return'].max()+ 0.0005)
ax2.set_xticks([0])
ax2.set_yticks([0])
ax.set_title('Zoomed Out: Sharpe Ratio and Intrinsic\nReturn Are +ve Correlated, but Sharpe Conf. Int.\n Are Overconfident', loc='left', fontdict=font_mini_title)
ax2.legend(['Signficant by Sharpe'])
ax.axvline(0, linestyle='-', alpha=0.4, linewidth=0.5, c='k')
ax.axhline(0, linestyle='-', alpha=0.4, linewidth=0.5, c='k')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment