def calibslice(lvol): lvolf = np.maximum(0, lvol) interped = intp.interp1d(calibki, lvolf, kind=1, fill_value="extrapolate") lvolj = interped(R[i - 1]) volj = volji * lvolj #print(lvol) R[i] = R[i - 1] * dffor[i - 1] / dfdom[i - 1] * np.exp( -volj * volj * t / 2 + paths[i - 1] * volj * math.sqrt(t)) driftadj = np.mean(R[i]) / fwd R[i] = R[i] / driftadj volsim = [ BS.volfromsim(R[i], fwd, strk, i * t) for strk in calibki ] #err = np.linalg.norm(np.log(volsim) - np.log(volcalibi)) err = np.linalg.norm(volsim - volcalibi) #if i == 1 : # print(err, volsim, volcalibi) return err cdf0 = vu.cdf(BS.mccdf(R[i])) err = np.linalg.norm( np.log(cdf0.probinterpinv(cumppt)) - np.log(cdf1.probinterpinv(cumppt))) print(err) return err * err
def getstrikevol(self, strikes, mat): simul = self.getsimulation(500000) fwd = simul.mean() print("Fwd", fwd) fwd = 15.0 strikevol = np.zeros((strikes.shape[0], 2)) strikevol[:, 0] = strikes strikevol[:, 1] = np.array( [BS.volfromsim(simul, fwd, stri, mat) for stri in strikes]) return strikevol
def testquadsmile(): fwd = 15.0 T = 10 a = 0.5 b = 0.0001 c = 1.05 stvol = np.ones([1000, 2]) stvol[:, 0] = np.exp(np.linspace(np.log(0.5), np.log(100), 1000)) stvol[:, 1] = 0.2 stvol[:, 1] = 0.2 * du.quad(np.log(stvol[:, 0] / fwd) / 0.2 / T, a, b, c) vol = logvolslice(stvol, fwd, T) cdfvol = vol.cumdf.strikeprob plt.plot(cdfvol[:, 0], cdfvol[:, 1]) plt.title("cdf") plt.show() pdfvol = vol.cumdf.getpdf() plt.plot(pdfvol[:, 0], pdfvol[:, 1]) plt.title("pdf") plt.show() strvol = vol.cumdf.getstrikevol(stvol[:, 0], vol.mat) plt.plot(stvol[:, 0], stvol[:, 1], label="input quadvoL") plt.plot(strvol[:, 0], strvol[:, 1], label="vol from cdf") plt.title("vol from cdf") plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) plt.show() xran = np.random.normal(size=10000) vols = 0.2 sim = fwd * np.exp(-vols * vols * T / 2 + vols * xran * math.sqrt(T)) lbound = vol.cumdf.getstrike(0.01) ubound = vol.cumdf.getstrike(0.99) print(lbound, ubound) x = np.linspace(ubound, lbound, 30) y = [BS.volfromsim(sim, sim.mean(), stri, T) for stri in x] plt.plot(x, y) plt.title("vol from cdf") plt.show() print(vol.deltastrike(0.25, 1), vol.deltastrike(0.25, -1)) print(vol.getrr(0.25), vol.getfly(0.25)) print(vol.deltastrike(0.1, 1), vol.deltastrike(0.1, -1)) print(vol.getrr(0.1), vol.getfly(0.1)) strikevolsmile = fivepointtostrikevol(15, 0.20, -0.06, 0.01, -0.12, 0.03, 5) plt.plot(strikevolsmile[:, 0], strikevolsmile[:, 1]) plt.title("interpolated smile") plt.show() return vol