diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/analytics/option.py | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/python/analytics/option.py b/python/analytics/option.py index 9adbed8b..ab4c369a 100644 --- a/python/analytics/option.py +++ b/python/analytics/option.py @@ -564,32 +564,36 @@ class ProbSurface(QuoteSurface): quotes.rec_mid /= quotes.forward_annuity sign = -1. prob_pay = np.concatenate([sign * np.gradient(df.pay_mid, df.strike) - for _, df in quotes.groupby('expiry')]) + for _, df in quotes.groupby('expiry')]) prob_rec = np.concatenate([1 + sign * np.gradient(df.rec_mid, df.strike) - for _, df in quotes.groupby('expiry')]) + for _, df in quotes.groupby('expiry')]) prob = bn.nanmean(np.stack([prob_pay, prob_rec]), axis=0) prob = np.clip(prob, 1e-10, None, out=prob) - f = SmoothBivariateSpline(quotes.time.values[~np.isnan(prob)], - quotes.strike.values[~np.isnan(prob)], - logit(prob[~np.isnan(prob)])) - self._surfaces[surface_id] = f + quotes['prob'] = prob + quotes.dropna(subset=['prob'], inplace=True) + spline = lambda df: CubicSpline(df.strike, logit(df.prob), bc_type="natural") + h = quotes.sort_values('strike').groupby('time').apply(spline) + self._surfaces[surface_id] = MyInterp(h.index.values, h.values) return self._surfaces[surface_id] else: return self._surfaces[surface_id] def tail_prob(self, T, strike, surface_id): - """computes the vol for a given moneyness and term.""" + """computes the prob for a given moneyness and term.""" return expit(self[surface_id](T, strike)) def plot(self, surface_id): fig = plt.figure() ax = fig.gca(projection='3d') + min, max = self._quotes.strike.min(), self._quotes.strike.max() surf = self[surface_id] - time, strike = surf.get_knots() - xx, yy = np.meshgrid(np.arange(time[0], time[-1], 0.01), - np.arange(strike[0], strike[-1], 0.01)) - surf = ax.plot_surface(xx, yy, expit(surf.ev(xx, yy)), - cmap=cm.viridis, vmax=1) + time = surf.T + y = np.arange(min, max, 0.1) + x = np.arange(time[0], time[-1], 0.01) + xx, yy = np.meshgrid(x, y) + z = np.vstack([expit(surf(xx, y)) for xx in x]) + surf = ax.plot_surface(xx, yy, z.T, + cmap=cm.viridis) ax.set_xlabel("Year fraction") ax.set_ylabel("Strike") ax.set_zlabel("Tail Probability") |
