public static void CapletVol20Y_InputInterp() { #region Data // We load a set of data (not real) DataForCapletExample1 d = new DataForCapletExample1(); double[] df = d.df(); double[] yf = d.yf(); double[] T = d.T(); double[] fwd = d.fwd(); double[] atmfwd = d.atmfwd(); double[] capVol = d.capVol(); double[] avT = d.avT(); double[] avcapVol = d.avcapVol(); #endregion // All Data available double[] CapletVol = Formula.CapletVolBootstrapping(T, df, fwd, yf, capVol, atmfwd); // Cubic input SimpleCubicInterpolator CubicInt = new SimpleCubicInterpolator(avT, avcapVol); double[] cubicInterpCapVol = CubicInt.Curve(T); double[] CapletVolCubicInput = Formula.CapletVolBootstrapping(T, df, fwd, yf, cubicInterpCapVol, atmfwd); // Linear Input LinearInterpolator LinearInt = new LinearInterpolator(avT, avcapVol); double[] linearInterpCapVol = LinearInt.Curve(T); double[] CapletVolLinearInput = Formula.CapletVolBootstrapping(T, df, fwd, yf, linearInterpCapVol, atmfwd); #region print results ExcelMechanisms exl = new ExcelMechanisms(); Vector <double> CapletVol_ = new Vector <double>(CapletVol, 1); Vector <double> CapletVolCubicInput_ = new Vector <double>(CapletVolCubicInput, 1); Vector <double> CapletVolLinearInput_ = new Vector <double>(CapletVolLinearInput, 1); Vector <double> xarr = new Vector <double>(T, 1); List <string> labels = new List <string>() { "CapletVol All input", "CapVol Cubic Input", "CapVol Linear Input" }; List <Vector <double> > yarrs = new List <Vector <double> >() { CapletVol_, CapletVolCubicInput_, CapletVolLinearInput_ }; exl.printInExcel <double>(xarr, labels, yarrs, "Caplet Vol Input Interpolation", "Term", "Volatility"); #endregion }
public void Solve() { #region data DataForCapletExample1 d = new DataForCapletExample1(); df = d.df(); yf = d.yf(); T = d.T(); fwd = d.fwd(); atmfwd = d.atmfwd(); x = d.avT(); // available maturity for market data y = d.avcapVol(); // available Cap volatility from market capPremium = new double[x.Length]; #endregion end data double cP = 0.0; // Calculate Cap price using Cap volatility available for (int i = 0; i < x.Length; i++) { int maxJ = Array.IndexOf(T, x[i]); // right index cP = 0.0; for (int j = 0; j <= maxJ; j++) { cP += Formula.CapletBlack(T[j], yf[j], 1, atmfwd[maxJ], y[i], df[j], fwd[j]); } capPremium[i] = cP; // collecting values } #region Setting up minimisation // Starting missing caplet vol guess double[] VolGuess = Enumerable.Repeat(y[0], y.Length).ToArray(); double epsg = 0.000000000001; // original setting double epsf = 0; double epsx = 0; int maxits = 0; alglib.minlmstate state; alglib.minlmreport rep; // Number of equation to match int NConstrains = x.Length; // see alglib documentation alglib.minlmcreatev(NConstrains, VolGuess, 0.000001, out state); alglib.minlmsetcond(state, epsg, epsf, epsx, maxits); alglib.minlmoptimize(state, function_fvec, null, null); alglib.minlmresults(state, out VolGuess, out rep); #endregion // Minimisation Done! // Uncomment to change interpolator LinearInterpolator interp = new LinearInterpolator(x, VolGuess); // SimpleCubicInterpolator interp = new SimpleCubicInterpolator(x, VolGuess); double[] Vols = interp.Curve(T); // Vols from interpolator #region print results ExcelMechanisms exl = new ExcelMechanisms(); Vector <double> CapVol = new Vector <double>(Vols, 1); Vector <double> xarr = new Vector <double>(T, 1); List <string> labels = new List <string>() { "CapVol" }; List <Vector <double> > yarrs = new List <Vector <double> >() { CapVol }; exl.printInExcel <double>(xarr, labels, yarrs, "Caplet vs Cap Vol", "Term", "Volatility"); #endregion }
public static void SimpleBootstrap20Y() { #region data // We load a set of data (not real) DataForCapletExample1 data1 = new DataForCapletExample1(); double[] df = data1.df(); double[] yf = data1.yf(); double[] T = data1.T(); int N = df.Length; double[] fwd = new double[N]; double[] atmfwd = new double[N]; double[] capletVol = new double[N]; double[] capPrice = new double[N]; double df_ini = data1.df_ini; double[] capVol = data1.capVol(); #endregion // calculate fwd fwd[0] = ((df_ini / df[0]) - 1) / yf[0]; for (int i = 1; i < df.Length; i++) { fwd[i] = ((df[i - 1] / df[i]) - 1) / yf[i]; } // calculate ATM strike double summ = 0.0; for (int i = 0; i < df.Length; i++) { summ += yf[i] * df[i]; atmfwd[i] = (df_ini - df[i]) / summ; } double shorterCap = 0.0; // calculate cap price using flat vol for (int i = 0; i < N; i++) { shorterCap = 0.0; for (int j = 0; j <= i; j++) { capPrice[i] += Formula.CapletBlack(T[j], yf[j], 100, atmfwd[i], capVol[i], df[j], fwd[j]); } for (int j = 0; j < i; j++) { shorterCap += Formula.CapletBlack(T[j], yf[j], 100, atmfwd[i], capletVol[j], df[j], fwd[j]); } NumMethod.myMethodDelegate fname = s => capPrice[i] - shorterCap - Formula.CapletBlack(T[i], yf[i], 100, atmfwd[i], s, df[i], fwd[i]); capletVol[i] = NumMethod.NewRapNum(fname, 0.20); } #region print results ExcelMechanisms exl = new ExcelMechanisms(); Vector <double> CapletVol = new Vector <double>(capletVol, 1); Vector <double> CapVol = new Vector <double>(capVol, 1); Vector <double> xarr = new Vector <double>(T, 1); List <string> labels = new List <string>() { "CapletVol", "CapVol" }; List <Vector <double> > yarrs = new List <Vector <double> >() { CapletVol, CapVol }; exl.printInExcel <double>(xarr, labels, yarrs, "Caplet vs Cap Vol", "Term", "Volatility"); #endregion }