diff options
Diffstat (limited to 'python/graphics.py')
| -rw-r--r-- | python/graphics.py | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/python/graphics.py b/python/graphics.py index f9935b5d..cc538aa8 100644 --- a/python/graphics.py +++ b/python/graphics.py @@ -3,6 +3,10 @@ import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.colors import LinearSegmentedColormap +from scipy.interpolate import griddata +from scipy.stats import norm +from scipy.optimize import curve_fit + def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'): ''' Function to offset the "center" of a colormap. Useful for @@ -54,7 +58,6 @@ def shiftedColorMap(cmap, start=0, midpoint=0.5, stop=1.0, name='shiftedcmap'): return newcmap - def plot_time_color_map(df, spread_shock, attr="pnl", path=".", color_map=cm.RdYlGn, index='IG', centered = True): val_date = df.index[0].date() @@ -111,3 +114,31 @@ def plot_color_map(df, spread_shock, vol_shock, attr="pnl", path=".", index='IG' fig.colorbar(chart, shrink=.8) #fig.savefig(os.path.join(path, "vol_spread_color_map"+ attr+ "_{}.png".format(val_date))) + +def plot_prob_map(df, attr="pnl", path=".", color_map=cm.RdYlGn, index='IG'): + + val_date = df.index[0].date() + df = df.reset_index() + df['days'] = (df['date'] - val_date).dt.days + series = df[attr] + + days_defined = np.linspace(df.days.min(), df.days.max(), 1000) + prob_defined = np.linspace(0.001, .999, 1000) + + midpoint = 1 - series.max() / (series.max() + abs(series.min())) + shifted_cmap = shiftedColorMap(color_map, midpoint=midpoint, name='shifted') + + resampled = griddata((df.days, df.prob), series, (days_defined[None, :], + prob_defined[:, None]), method='linear') + + #plot + fig, ax = plt.subplots() + chart = ax.imshow(resampled.reshape(days_defined.size, prob_defined.size), + extent=(df.days.min(), df.days.max(), 0, 1), + aspect='auto', interpolation='bilinear', cmap=shifted_cmap) + + ax.set_xlabel('Days') + ax.set_ylabel('Probability') + ax.set_title('{} of Trade'.format(attr.title())) + + fig.colorbar(chart, shrink=.8)
\ No newline at end of file |
