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 static void MatrixCaplet() { #region Data // We load a set of data (not real) DataForCapletExample2 d = new DataForCapletExample2(); double[] df = d.df(); double[] yf = d.yf(); double[] T = d.T(); double[] avT = d.avT(); List <double[]> VolArr = d.VolArr(); double[] fwd = d.fwd(); double[] strike = d.strike(); List <double[]> VolSilos = new List <double[]>(); foreach (double[] cVol in VolArr) { LinearInterpolator LinearInt = new LinearInterpolator(avT, cVol); VolSilos.Add(LinearInt.Curve(T)); } #endregion // Here you can change the MonoStrikeCaplet Builder CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderPWC> B = new CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderPWC>(T, df, fwd, yf, avT, VolSilos, strike); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth> B = new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth>(T, df, fwd, yf, avT, VolSilos, strike); // and more... #region print results ExcelMechanisms exl = new ExcelMechanisms(); Vector <double> xarr = new Vector <double>(T, 1); List <string> labels = new List <string>(); foreach (double myD in strike) { labels.Add(myD.ToString()); } ; List <Vector <double> > yarrs = new List <Vector <double> >(); foreach (double[] arr in B.CapletVolMatrix) { yarrs.Add(new Vector <double>(arr, 1)); } 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 }
// linear interpolated rate for each starting time in T public double[] AllFwd_Linear(double[] knownRatesStart, double[] knownFwd) { LinearInterpolator LI = new LinearInterpolator(knownRatesStart, knownFwd); return(LI.Curve(T)); // getting linear interpolated data }