// 13.12.3 Cubic Spline Interpolation #region CubicSplineInterpolator public static void LogisticI() { Console.WriteLine("Vectors initialised: "); int N = 280; double a = -15.0; // Left of interval double b = 15.0; // Right of interval double h = (b - a) / N; int startIndex = 0; Vector <double> xarr = new Vector <double>(N + 1, startIndex, 0.0); Vector <double> yarr = new Vector <double>(N + 1, startIndex, 0.0); for (int j = xarr.MinIndex; j <= xarr.MaxIndex; j++) { xarr[j] = a + h * j; yarr[j] = Potpourri.Sigmoid1(xarr[j]); // yarr[j] = Potpourri.Sigmoid2(xarr[j]); } Console.WriteLine("Vectors initialised: "); int FirstDeriv = 1; CubicSplineInterpolator csi = new CubicSplineInterpolator(xarr, yarr, FirstDeriv, 10, 10); // Display arrays in Excel ExcelMechanisms exl = new ExcelMechanisms(); exl.printOneExcel <double>(xarr, csi.Curve(), "Logistic I", "x", "value", "I"); // Now choose 1st order derivative at zero double leftBC = 1.0; double rightBC = -1.0; CubicSplineInterpolator csi2 = new CubicSplineInterpolator(xarr, yarr, FirstDeriv, leftBC, rightBC); // Display arrays in Excel exl.printOneExcel(xarr, csi2.Curve(), "Logistic II", "x", "value", "II"); }
// RECALL: void printOneExcel(Vector<double> x, // Vector<double> functionResult,string title) public void displayinExcel(OptionValueType yval) { string text = "Value"; if (yval == OptionValueType.Delta) { text = "Delta"; } if (yval == OptionValueType.Gamma) { text = "Gamma"; } if (yval == OptionValueType.Vega) { text = "Vega"; } if (yval == OptionValueType.Theta) { text = "Theta"; } if (yval == OptionValueType.Rho) { text = "Rho"; } if (yval == OptionValueType.Coc) { text = "Coc"; } Vector <double> yarr = calculate(yval); // cout << "array ..."; int yy; cin >> yy; // print(yarr); ExcelMechanisms excel = new ExcelMechanisms(); excel.printOneExcel(XARR, yarr, text, text, text, text); }
// 13.12.1 The 101 Example, from A to Z public static void Example101() { // My excel mechanism ExcelMechanisms exl = new ExcelMechanisms(); // I Create initial t and r arrays. Vector <double> t = new Vector <double>(new double[] { 0.1, 1, 4, 9, 20, 30 }, 0); Vector <double> r = new Vector <double>(new double[] { 0.081, 0.07, 0.044, 0.07, 0.04, 0.03 }, 0); // Compute log df Vector <double> logDF = new Vector <double>(r.Size, r.MinIndex); for (int n = logDF.MinIndex; n <= logDF.MaxIndex; ++n) { logDF[n] = Math.Log(Math.Exp(-t[n] * r[n])); } // exl.printOneExcel<double>(t, logDF, "logDF", "time", "logDF", "logDF"); // II Hyman interpolator HymanHermiteInterpolator_V4 myInterpolatorH = new HymanHermiteInterpolator_V4(t, logDF); // Create the abscissa values f (hard-coded for the moment) int M = 299; Vector <double> term = new Vector <double>(M, 1); term[term.MinIndex] = 0.1; double step = 0.1; for (int j = term.MinIndex + 1; j <= term.MaxIndex; j++) { term[j] = term[j - 1] + step; } // III Compute interpolated values Vector <double> interpolatedlogDFH = myInterpolatorH.Curve(term); // exl.printOneExcel<double>(term, interpolatedlogDFH, // "Hyman cubic", "time", "int logDF", "int logDF"); // IV Compute continuously compounded risk free rate from the ZCB Z(0,t), // using equation (3)Hagan and West (2008). Vector <double> rCompounded = new Vector <double>(interpolatedlogDFH.Size, interpolatedlogDFH.MinIndex); for (int j = rCompounded.MinIndex; j <= rCompounded.MaxIndex; j++) { rCompounded[j] = -interpolatedlogDFH[j] / term[j]; } exl.printOneExcel <double>(term, rCompounded, "RCompound Hyman Cubic", "time", "r continuously comp.", "r cont"); // V Compute discrete forward rates using equation (6) from Hagan and West (2008) Vector <double> f = new Vector <double>(rCompounded.Size, rCompounded.MinIndex); f[f.MinIndex] = 0.081; for (int j = f.MinIndex + 1; j <= rCompounded.MaxIndex; j++) { f[j] = (rCompounded[j] * term[j] - rCompounded[j - 1] * term[j - 1]) / (term[j] - term[j - 1]); } exl.printOneExcel <double>(term, f, "Hyman Cubic", "time", "discrete forward", "dis fwd"); }
// Class ListedSTFutOption and AssocMatrix public static void Example4() { // Stir (Short Term Interest Rate ) Option example. // (Please check details on www.euronext.com) Example is based on Black Formula // I create my option STFutOption opx1 = new STFutOption(1, 97.935, 98.25, 175, 0.4756); // option deltas Console.WriteLine("{0}, {1}", opx1.Price(), opx1.Delta()); opx1.SwitchCallPut(); Console.WriteLine("{0}, {1}", opx1.Price(), opx1.Delta()); // Call and put checking deltas and call put parity double S = 97.935; double K = 98.25; double DaysToExpiry = 175; STFutOption Call = new STFutOption(1, S, K, 175, 0.4756); STFutOption Put = new STFutOption(-1, S, K, 175, 0.4756); Console.WriteLine("{0},{1},{2}", Call.Delta(), Put.Delta(), Call.Delta() - Put.Delta()); // Call/Put delta are reversed Console.WriteLine("{0}", Call.Price() - Put.Price() - S + K); // Call/Put parity // I will crate a matrix showing deltas of a option, for different underlying price (rows) and passing the time (columns) // header Row: different value of underlying. the center is the current one // header column: passing the time, less days to maturity // Time passing - column int d_columns = 20; // how many days from and including today double shift_c = 1.0; // interval in days between each columns // Changing value of underlying int d_rows = 15; // how many values plus or minus the value double shift_h = 0.10; // interval in underlying NumericMatrix <double> deltaMatrix = new NumericMatrix <double>(d_rows * 2 + 1, d_columns); // Underlying value double d_r = -d_rows; Set <double> underlying = new Set <double>(); for (int i = 0; i < deltaMatrix.Rows; i++) { underlying.Insert(S + d_r * shift_h); d_r++; } // Days to maturity double d_c = 0; Set <double> days = new Set <double>(); for (int i = 0; i < deltaMatrix.Columns; i++) { days.Insert(DaysToExpiry + d_c * shift_c); d_c--; } // Populate DeltaMatrix int my_r = 1; int my_c = 1; foreach (double valueS in underlying) { Call.new_S = valueS; foreach (double valueDays in days) { Call.new_DaysToExpiry = valueDays; deltaMatrix[my_r, my_c] = Call.Delta(); my_c++; } my_r++; my_c = 1; } // Creating AssocMatrix AssocMatrix <double, double, double> OutMatrix = new AssocMatrix <double, double, double>(underlying, days, deltaMatrix); // Print associative matrices in Excel, to "StirDeltas" sheet ExcelMechanisms exl = new ExcelMechanisms(); exl.printAssocMatrixInExcel <double, double, double>(OutMatrix, "StirDeltas"); }
/* public static double EarlyImpl(double P, double S) * { * * double K = 10.0; * * if (P > K - S) * { * return P; * } * return K - S; * }*/ // This could be made into a member function of Option public static void Main() { // Phase I: Create and initialise the option IOptionFactory fac = getFactory(); int N = 200; Console.Write("Number of time steps: "); N = Convert.ToInt32(Console.ReadLine()); double S; Console.Write("Underlying price: "); S = Convert.ToDouble(Console.ReadLine()); Option opt = fac.create(); double k = opt.T / N; // Create basic lattice double discounting = Math.Exp(-opt.r * k); // Phase II: Create the binomial method and forward induction BinomialLatticeStrategy binParams = getStrategy(opt.sig, opt.r, k, S, opt.K, N); // Factory BinomialMethod bn = new BinomialMethod(discounting, binParams, N); bn.modifyLattice(S); // Phase III: Backward Induction and compute option price Vector <double> RHS = new Vector <double>(bn.BasePyramidVector()); if (binParams.bType == BinomialType.Additive) { RHS[RHS.MinIndex] = S * Math.Exp(N * binParams.downValue()); for (int j = RHS.MinIndex + 1; j <= RHS.MaxIndex; j++) { RHS[j] = RHS[j - 1] * Math.Exp(binParams.upValue() - binParams.downValue()); } } Vector <double> Pay = opt.PayoffVector(RHS); double pr = bn.getPrice(Pay); Console.WriteLine("European {0}", pr); // Binomial method with early exercise BinomialMethod bnEarly = new BinomialMethod(discounting, binParams, N, opt.EarlyImpl); bnEarly.modifyLattice(S); Vector <double> RHS2 = new Vector <double>(bnEarly.BasePyramidVector()); Vector <double> Pay2 = opt.PayoffVector(RHS2); double pr2 = bnEarly.getPrice(Pay2); Console.WriteLine("American {0}", pr2); // Display in Excel; first create array of asset mesh points int startIndex = 0; Vector <double> xarr = new Vector <double>(N + 1, startIndex); xarr[xarr.MinIndex] = 0.0; for (int j = xarr.MinIndex + 1; j <= xarr.MaxIndex; j++) { xarr[j] = xarr[j - 1] + k; } // Display lattice in Excel ExcelMechanisms exl = new ExcelMechanisms(); try { // public void printLatticeInExcel(Lattice<double> lattice, Vector<double> xarr, string SheetName) string sheetName = "Lattice"; exl.printLatticeInExcel(bnEarly.getLattice(), xarr, sheetName); } catch (Exception e) { Console.WriteLine(e); } }
public static void BilinearInterpolation2() { // Create mesh arrays int startIndex = 0; // Number of subdivisions N,M in the x and y directions int N = 4; int M = 3; Vector <double> x1arr = new Vector <double>(N + 1, startIndex, 0.0); double a = 0.0; double b = 1.0; double h1 = (b - a) / (double)N; x1arr[x1arr.MinIndex] = a; for (int j = x1arr.MinIndex + 1; j <= x1arr.MaxIndex; j++) { x1arr[j] = x1arr[j - 1] + h1; } Vector <double> x2arr = new Vector <double>(M + 1, startIndex, 0.0); double h2 = (b - a) / (double)M; x1arr[x1arr.MinIndex] = a; for (int j = x2arr.MinIndex + 1; j <= x2arr.MaxIndex; j++) { x2arr[j] = x2arr[j - 1] + h2; } Console.WriteLine(x1arr); Console.WriteLine(x2arr); // Control values; NumericMatrix <double> Control = new NumericMatrix <double>(N + 1, M + 1, startIndex, startIndex); Func <double, double, double> func = (double x1, double x2) => x1 + x2; for (int i = Control.MinRowIndex; i <= Control.MaxRowIndex; i++) { for (int j = Control.MinColumnIndex; j <= Control.MaxColumnIndex; j++) { Control[i, j] = func(x1arr[i], x2arr[j]); } } BilinearInterpolator myInterpolator = new BilinearInterpolator(x1arr, x2arr, Control); double x = 0.1; double y = 0.7; double value = myInterpolator.Solve(x, y); Console.WriteLine("Interpolated value: {0}", value); // Take center point (xm, ym) of each element and interpolate on it NumericMatrix <double> InterpolatedMatrix = new NumericMatrix <double>(N, M, startIndex, startIndex); // Abscissa points of new interpolated matrix Vector <double> Xarr = new Vector <double>(InterpolatedMatrix.Rows, startIndex, 0.0); Vector <double> Yarr = new Vector <double>(InterpolatedMatrix.Columns, startIndex, 0.0); for (int i = InterpolatedMatrix.MinRowIndex; i <= InterpolatedMatrix.MaxRowIndex; i++) { for (int j = InterpolatedMatrix.MinColumnIndex; j <= InterpolatedMatrix.MaxColumnIndex; j++) { Xarr[i] = 0.5 * (x1arr[i] + x1arr[i + 1]); // xm Yarr[j] = 0.5 * (x2arr[j] + x2arr[j + 1]); // ym InterpolatedMatrix[i, j] = myInterpolator.Solve(Xarr[i], Yarr[j]); } } // Present the interpolated matrix in Excel ExcelMechanisms driver = new ExcelMechanisms(); string title = "Interpolated matrix"; driver.printMatrixInExcel <double>(InterpolatedMatrix, Xarr, Yarr, title); }
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 MatrixCapletWithRateCurve() { #region Inputs // We load a set of data (not real) DataForCapletExample2 d = new DataForCapletExample2(); RateSet rs = d.GetRateSet(); double[] T = d.T(); string[] avT = d.avTString(); List <double[]> VolArr = d.VolArr(); double[] strike = d.strike(); // We build our multi curve SingleCurveBuilderStandard <OnLogDf, SimpleCubicInterpolator> Curve = new SingleCurveBuilderStandard <OnLogDf, SimpleCubicInterpolator>(rs, OneDimensionInterpolation.LogLinear); // discount curve #endregion #region Testing all available MonoStrikeCapletVolBuilder // Uncomment one of these CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderInputInterpLinear> B = new CapletMatrixVolBuilder <MonoStrikeCapletVolBuilderInputInterpLinear>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderInputInterpCubic> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderInputInterpCubic>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitStd> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitStd>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitSmooth>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitPWL> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitPWL>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitCubic> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitCubic>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitFunct> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderBestFitFunct>(avT, Curve, strike, VolArr); // CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderPWC> B = // new CapletMatrixVolBuilder<MonoStrikeCapletVolBuilderPWC>(avT, Curve, strike, VolArr); #endregion List <double[]> VolSilos = B.CapletVolMatrix; #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 VolSilos) { yarrs.Add(new Vector <double>(arr, 1)); } exl.printInExcel <double>(xarr, labels, yarrs, "Caplet Vol Input Interpolation", "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 }
// Print on excel forward rate using different curve builder for OIS fwd 3m public static void CheckFwdRatesOIS3m() { #region Inputs // ref date Date refDate = (new Date(DateTime.Now)).mod_foll(); // I populate market rates set: from file, from real time, ... RateSet mktRates = new RateSet(refDate); mktRates.Add(2.338e-2, "1d", BuildingBlockType.EURDEPO); // mktRates.Add(2.272e-2, "1w", BuildingBlockType.EONIASWAP); // mktRates.Add(2.241e-2, "2w", BuildingBlockType.EONIASWAP); // mktRates.Add(2.16e-2, "3w", BuildingBlockType.EONIASWAP); // mktRates.Add(2.226e-2, "1m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.299e-2, "2m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.323e-2, "3m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.344e-2, "4m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.371e-2, "5m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.39e-2, "6m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.41e-2, "7m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.4316e-2, "8m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.449e-2, "9m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.466e-2, "10m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.48e-2, "11m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.529e-2, "15m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.565e-2, "18m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.603e-2, "21m", BuildingBlockType.EONIASWAP); // mktRates.Add(2.493e-2, "1Y", BuildingBlockType.EONIASWAP); mktRates.Add(2.644e-2, "2Y", BuildingBlockType.EONIASWAP); mktRates.Add(2.849e-2, "3Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.08e-2, "4Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.292e-2, "5Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.471e-2, "6Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.621e-2, "7Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.748e-2, "8Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.86e-2, "9Y", BuildingBlockType.EONIASWAP); mktRates.Add(3.965e-2, "10Y", BuildingBlockType.EONIASWAP); mktRates.Add(4.064e-2, "11Y", BuildingBlockType.EONIASWAP); mktRates.Add(4.155e-2, "12Y", BuildingBlockType.EONIASWAP); // From here interpolation is need mktRates.Add(4.358e-2, "15Y", BuildingBlockType.EONIASWAP); mktRates.Add(4.48e-2, "20Y", BuildingBlockType.EONIASWAP); mktRates.Add(4.465e-2, "25Y", BuildingBlockType.EONIASWAP); mktRates.Add(4.415e-2, "30Y", BuildingBlockType.EONIASWAP); List <IRateCurve> CurveList = new List <IRateCurve>(); // list containing curve List <string> CurveString = new List <string>(); // list containing labels #endregion end Inputs #region building curve SingleCurveBuilderStandard <OnDf, LinearInterpolator> C1 = new SingleCurveBuilderStandard <OnDf, LinearInterpolator>(mktRates, OneDimensionInterpolation.Linear); SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator> C2 = new SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator>(mktRates); #endregion end building curve // populate lists CurveList.Add(C1); CurveString.Add(C1.ToString()); CurveList.Add(C2); CurveString.Add(C2.ToString()); #region printing output // I get the longer eonia swap available from the input data SwapStyle LS = (SwapStyle)mktRates.GetArrayOfBB().Last(); Schedule s = new Schedule(refDate, LS.endDate, "3m", Rule.Backward, LS.swapLeg1.SwapBusDayRollsAdj, "0d", LS.swapLeg1.SwapBusDayPayAdj); Dc dc = Dc._Act_360; Date[] FromDate = s.fromDates; Date[] ToDate = s.toDates; int N = FromDate.Length; List <Vector <double> > Fwds = new List <Vector <double> >(); double[] dt = new double[N]; for (int i = 0; i < N; i++) { dt[i] = FromDate[0].YF(ToDate[i], Dc._30_360); } foreach (IRateCurve myC in CurveList) { double[] fwd = new double[N]; for (int i = 0; i < N; i++) { double yf = FromDate[i].YF(ToDate[i], dc); double df_ini = myC.Df(FromDate[i]); double df_end = myC.Df(ToDate[i]); fwd[i] = ((df_ini / df_end) - 1) / yf; } Fwds.Add(new Vector <double>(fwd)); } ExcelMechanisms exl = new ExcelMechanisms(); exl.printInExcel(new Vector <double>(dt), CurveString, Fwds, "Fwd 3M", "time", "rate"); // .printInExcel<T> #endregion end printing output }
// Print on excel forward rate using different curve builder for 3m public static void CheckFwdRatesVs3m() { #region Inputs // Start input Date refDate = (new Date(DateTime.Now)).mod_foll(); // I populate market rates set: from file, from real time, ... RateSet mktRates = new RateSet(refDate); // Depos mktRates.Add(0.434e-2, "3m", BuildingBlockType.EURDEPO); // Swap Vs 3M mktRates.Add(0.813e-2, "1Y", BuildingBlockType.EURSWAP3M); mktRates.Add(1.096e-2, "2Y", BuildingBlockType.EURSWAP3M); mktRates.Add(1.322e-2, "3Y", BuildingBlockType.EURSWAP3M); mktRates.Add(1.529e-2, "4Y", BuildingBlockType.EURSWAP3M); mktRates.Add(1.709e-2, "5Y", BuildingBlockType.EURSWAP3M); mktRates.Add(1.862e-2, "6Y", BuildingBlockType.EURSWAP3M); mktRates.Add(1.991e-2, "7Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.101e-2, "8Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.197e-2, "9Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.285e-2, "10Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.443e-2, "12Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.614e-2, "15Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.711e-2, "20Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.671e-2, "25Y", BuildingBlockType.EURSWAP3M); mktRates.Add(2.589e-2, "30Y", BuildingBlockType.EURSWAP3M); #endregion end Inputs #region building curve SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator> C1 = new SingleCurveBuilderInterpBestFit <OnLogDf, SimpleCubicInterpolator>(mktRates); double firstFixing = 0.434e-2; SingleCurveBuilderSmoothingFwd <OnLogDf, SimpleCubicInterpolator> C2 = new SingleCurveBuilderSmoothingFwd <OnLogDf, SimpleCubicInterpolator>(mktRates, firstFixing); SingleCurveBuilderStandard <OnLogDf, LinearInterpolator> C3 = new SingleCurveBuilderStandard <OnLogDf, LinearInterpolator>(mktRates, OneDimensionInterpolation.Linear); #endregion end building curve List <IRateCurve> CurveList = new List <IRateCurve>(); // list containing curve List <string> CurveString = new List <string>(); // list containing labels // populate lists CurveList.Add(C1); CurveString.Add(C1.ToString()); CurveList.Add(C2); CurveString.Add(C2.ToString()); CurveList.Add(C3); CurveString.Add(C3.ToString()); #region printing output // I get the longer swap SwapStyle LS = (SwapStyle)mktRates.GetArrayOfBB().Last(); Dc dc = Dc._Act_360; Date[] FromDate = LS.scheduleLeg2.fromDates; Date[] ToDate = LS.scheduleLeg2.toDates; int N = FromDate.Length; List <Vector <double> > Fwds = new List <Vector <double> >(); double[] dt = new double[N]; for (int i = 0; i < N; i++) { dt[i] = FromDate[0].YF(ToDate[i], Dc._30_360); } foreach (IRateCurve myC in CurveList) { double[] fwd = new double[N]; for (int i = 0; i < N; i++) { double yf = FromDate[i].YF(ToDate[i], dc); double df_ini = myC.Df(FromDate[i]); double df_end = myC.Df(ToDate[i]); fwd[i] = ((df_ini / df_end) - 1) / yf; } Fwds.Add(new Vector <double>(fwd)); } ExcelMechanisms exl = new ExcelMechanisms(); exl.printInExcel(new Vector <double>(dt), CurveString, Fwds, "Fwd vs 3M", "time", "rate"); // .printInExcel<T> #endregion end printing output }
public void C9_9_ExcelOutput() { var opt = _testOption; var steps = 50; var S = opt.K; double k = opt.T / steps; double discounting = Math.Exp(-opt.r * k); var binParams = new CRRStrategy(opt.sig, opt.r, k);; // Factory var bn = new BinomialMethod(discounting, binParams, steps, opt.EarlyImpl); bn.ModifyLattice(opt.K); // Phase III: Backward Induction and compute option price Vector <double> RHS = new Vector <double>(bn.BasePyramidVector()); if (binParams.BinomialType == BinomialType.Additive) { RHS[RHS.MinIndex] = S * Math.Exp(steps * binParams.DownValue); for (int j = RHS.MinIndex + 1; j <= RHS.MaxIndex; j++) { RHS[j] = RHS[j - 1] * Math.Exp(binParams.UpValue - binParams.DownValue); } } var pay = opt.PayoffVector(RHS); var pr = bn.GetPrice(pay); // Display lattice in Excel var file = Path.GetTempFileName(); file = Path.ChangeExtension(file, "xlsx"); ExcelMechanisms exl = new ExcelMechanisms(file); try { // Display in Excel; first create array of asset mesh points int startIndex = 0; Vector <double> xarr = new Vector <double>(steps + 1, startIndex); xarr[xarr.MinIndex] = 0.0; for (int j = xarr.MinIndex + 1; j <= xarr.MaxIndex; j++) { xarr[j] = xarr[j - 1] + k; } string sheetName = "Lattice"; exl.printLatticeInExcel(bn.GetOptionLattice, xarr, sheetName); exl.Save(); } catch (Exception e) { Console.WriteLine(e); } Assert.AreEqual(pr, 3.0732, 0.01); }