aboutsummaryrefslogtreecommitdiffstats
path: root/python/analytics/option.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/analytics/option.py')
-rw-r--r--python/analytics/option.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/python/analytics/option.py b/python/analytics/option.py
index 0e8aec51..1079f9d5 100644
--- a/python/analytics/option.py
+++ b/python/analytics/option.py
@@ -672,6 +672,42 @@ class ProbSurface(QuoteSurface):
"""computes the prob for a given moneyness and term."""
return expit(self[surface_id](T, strike))
+ def quantile_spread(self, T, prob, surface_id):
+ """computes the spread for a given probability and term."""
+ l_prob = logit(prob)
+ def prob_calib(x, T, surface_id):
+ return l_prob- self[surface_id](T, x)
+ eta = 1.5
+ a = .1
+ b = 50 * eta
+ while True:
+ if prob_calib(b, T, surface_id) > 0:
+ break
+ b *= eta
+ prog = brentq(prob_calib, a, b, args=(T, surface_id), full_output=True)
+ if prog[1].converged is True:
+ return prog[0]
+ else:
+ return ValueError("unable to converge")
+
+ def quantile_plot(self, surface_id):
+ fig = plt.figure()
+ ax = fig.gca(projection='3d')
+ min, max = .001, .999
+ time = self[surface_id].T
+ y = np.arange(min, max, 0.01)
+ x = np.arange(time[0], time[-1], 0.01)
+ z = np.array([]).reshape(0, y.size)
+ for xx in x:
+ zz = [self.quantile_spread(xx, yy, surface_id) for yy in y]
+ z = np.vstack((z, zz))
+ xx, yy = np.meshgrid(x, y)
+ surf = ax.plot_surface(xx, yy, z.T,
+ cmap=cm.viridis)
+ ax.set_xlabel("Year fraction")
+ ax.set_ylabel("Probability")
+ ax.set_zlabel("Spread")
+
def plot(self, surface_id):
fig = plt.figure()
ax = fig.gca(projection='3d')