public void TestSwap() { // Make the swap var rate = 0.08; var payFixed = true; double notional = 1000000; var startDate = new Date(2016, 9, 17); var tenor = Tenor.Years(5); var swap = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor); // Set up the model var valueDate = new Date(2016, 9, 17); Date[] dates = { new Date(2016, 9, 17), new Date(2026, 9, 17) }; double[] rates = { 0.07, 0.07 }; IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate, dates, rates); IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, FloatingIndex.JIBAR3M, dates, rates); var curveSim = new DeterminsiticCurves(discountCurve); curveSim.AddRateForecast(forecastCurve); var coordinator = new Coordinator(curveSim, new List <Simulator>(), 1); // Run the valuation var value = coordinator.Value(new Product[] { swap }, valueDate); var refValue = -41838.32; // See RateProductTest.xlsx Assert.AreEqual(refValue, value, 0.01); }
public void TestCoordinatorAllData() { // Make the swap var rate = 0.07; var payFixed = true; double notional = 1000000; var startDate = new Date(2016, 9, 17); var tenor = Tenor.Years(5); var swap = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor); // Set up the model var valueDate = new Date(2016, 9, 17); var a = 0.05; var vol = 0.01; var flatCurveRate = 0.07; var hullWiteSim = new HullWhite1F(Currency.ZAR, a, vol, flatCurveRate, flatCurveRate, valueDate); hullWiteSim.AddForecast(FloatingIndex.JIBAR3M); var coordinator = new Coordinator(hullWiteSim, new List <Simulator>(), 5000); var date = valueDate; var endDate = valueDate.AddTenor(tenor); var fwdValueDates = new List <Date>(); while (date < endDate) { fwdValueDates.Add(date); date = date.AddTenor(Tenor.Days(10)); } var allDetails = coordinator.GetValuePaths(new Product[] { swap }, valueDate, fwdValueDates.ToArray()); allDetails.GetNames(); }
public void TestSwapHW() { // Make the swap var rate = 0.08; var payFixed = true; double notional = 1000000; var startDate = new Date(2016, 9, 17); var tenor = Tenor.Years(5); var swap = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor); // Set up the model var valueDate = new Date(2016, 9, 17); var a = 0.05; var vol = 0.01; var flatCurveRate = 0.07; var hullWiteSim = new HullWhite1F(Currency.ZAR, a, vol, flatCurveRate, flatCurveRate, valueDate); hullWiteSim.AddForecast(FloatingIndex.JIBAR3M); var coordinator = new Coordinator(hullWiteSim, new List <Simulator>(), 10000); // Run the valuation var value = coordinator.Value(new Product[] { swap }, valueDate); var refValue = -41838.32; // See RateProductTest.xlsx Assert.AreEqual(refValue, value, 4000); }
public void Init() { // Set up the model valueDate = new Date(2016, 9, 17); var a = 0.05; var vol = 0.01; var flatCurveRate = 0.07; hullWiteSim = new HullWhite1F(Currency.ZAR, a, vol, flatCurveRate, flatCurveRate, valueDate); hullWiteSim.AddForecast(FloatingIndex.JIBAR3M); // Make the underlying swap var rate = 0.07; var payFixed = true; double notional = 1000000; var startDate = new Date(2016, 9, 17); var tenor = Tenor.Years(5); swapPay = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor); swapRec = IRSwap.CreateZARSwap(rate, !payFixed, notional, startDate, tenor); // Full set of exercise dates exDates = new List <Date> { new Date(2017, 9, 17), new Date(2018, 9, 17), new Date(2019, 9, 17), new Date(2020, 9, 17) }; }
private Product[] GetListOfSwaps() { int N = 50000; Date anchorDate = new Date(2016, 11, 21); double rate = 0.08; bool payFixed = true; double notional = 1000000; double[,] swapDist = new double[, ] { { 0.171 }, { 0.148 }, { 0.101 }, { 0.094 }, { 0.108 }, { 0.056 }, { 0.041 }, { 0.049 }, { 0.047 }, { 0.056 }, { 0.013 }, { 0.013 }, { 0.010 }, { 0.011 }, { 0.011 }, { 0.004 }, { 0.003 }, { 0.005 }, { 0.007 }, { 0.006 }, { 0.004 }, { 0.004 }, { 0.007 }, { 0.005 }, { 0.006 }, { 0.006 }, { 0.003 }, { 0.003 }, { 0.002 }, { 0.005 } }; IRandomNumberGenerator <double> generator1 = new ZigguratUniformGenerator(0, 1); IRandomNumberGenerator <double> generator365 = new ZigguratUniformGenerator(1, 365); double[,] cumSum = swapDist.CumulativeSum(1); Product[] allSwaps = new Product[N]; for (int swapNum = 0; swapNum < N; swapNum++) { double x = generator1.Generate(); int years = 0; while (years < cumSum.GetLength(0) && x > cumSum[years, 0]) { years++; } int days = (int)Math.Round(generator365.Generate()); Date endDate = anchorDate.AddTenor(new Tenor(days, 0, 0, years)); Date startDate = endDate.AddTenor(Tenor.Years(-years - 1)); allSwaps[swapNum] = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, Tenor.Years(years + 1)); } return(allSwaps); }
public static IRSwap CreateZARSwap([ExcelArgument(Description = "First reset date of the swap")] Date startDate, [ExcelArgument(Description = "Tenor of swap, must be a whole number of years. Example '5Y'.")] Tenor tenor, [ExcelArgument(Description = "The fixed rate paid or received")] double rate, [ExcelArgument(Description = "Is the fixed rate paid? Enter 'TRUE' for yes.")] bool payFixed, [ExcelArgument(Description = "Flat notional for all dates.")] double notional) { return(IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor)); }
public void TestCurveStripTwoZARDiscount() { var valueDate = new Date(2017, 1, 13); var zar = Currency.ZAR; // Empty curves var zarDiscUSDColl = new ZeroRatesCurveForStripping(valueDate, zar); var zarDisc = new ZeroRatesCurveForStripping(valueDate, zar); var jibarCurve = new ForwardRatesCurveForStripping(valueDate, FloatingIndex.JIBAR3M); // Models var modelZARDiscUSDColl = new DeterminsiticCurves(zarDiscUSDColl); modelZARDiscUSDColl.AddRateForecast(jibarCurve); var coordZARDiscUSDColl = new Coordinator(modelZARDiscUSDColl, new List <Simulator>(), 1); coordZARDiscUSDColl.SetThreadedness(false); var modelZARDisc = new DeterminsiticCurves(zarDisc); modelZARDisc.AddRateForecast(jibarCurve); // same jibar curve in both coordinators. var coordZARDisc = new Coordinator(modelZARDisc, new List <Simulator>(), 1); coordZARDisc.SetThreadedness(false); //Instruments for USDColl discounting curve. var mcs = new MultiCurveStripper(valueDate); var depo1 = MakeDepo(valueDate, 0.07, 24); var depo2 = MakeDepo(valueDate, 0.072, 48); mcs.AddDiscounting(depo1, () => coordZARDiscUSDColl.Value(depo1, valueDate), 1.0, 1.0, zarDiscUSDColl); mcs.AddDiscounting(depo2, () => coordZARDiscUSDColl.Value(depo2, valueDate), 1.0, 1.0, zarDiscUSDColl); Product swapUSDColl1 = IRSwap.CreateZARSwap(0.07, true, 1.0, valueDate, Tenor.Months(24)); Product swapUSDColl2 = IRSwap.CreateZARSwap(0.072, true, 1.0, valueDate, Tenor.Months(48)); mcs.AddForecast(swapUSDColl1, () => coordZARDiscUSDColl.Value(swapUSDColl1, valueDate), 0, 1, jibarCurve, FloatingIndex.JIBAR3M); mcs.AddForecast(swapUSDColl2, () => coordZARDiscUSDColl.Value(swapUSDColl2, valueDate), 0, 1, jibarCurve, FloatingIndex.JIBAR3M); Product swapNoColl1 = IRSwap.CreateZARSwap(0.0709, true, 1.0, valueDate, Tenor.Months(36)); Product swapNoColl2 = IRSwap.CreateZARSwap(0.0719, true, 1.0, valueDate, Tenor.Months(48)); mcs.AddDiscounting(swapNoColl1, () => coordZARDisc.Value(swapNoColl1, valueDate), 0, 1, zarDisc); mcs.AddDiscounting(swapNoColl2, () => coordZARDisc.Value(swapNoColl2, valueDate), 0, 1, zarDisc); mcs.Strip(); Assert.AreEqual(1.0, coordZARDiscUSDColl.Value(depo1, valueDate), 1e-6); Assert.AreEqual(1.0, coordZARDiscUSDColl.Value(depo2, valueDate), 1e-6); Assert.AreEqual(0.0, coordZARDiscUSDColl.Value(swapUSDColl2, valueDate), 1e-6); Assert.AreEqual(0.0, coordZARDiscUSDColl.Value(swapUSDColl2, valueDate), 1e-6); //Assert.AreNotEqual(0.0, coordZARDisc.Value(swapUSDColl1, valueDate), 1e-6); No discount sensitivity Assert.AreNotEqual(0.0, coordZARDisc.Value(swapUSDColl2, valueDate), 1e-6); Assert.AreEqual(0.0, coordZARDisc.Value(swapNoColl1, valueDate), 1e-6); Assert.AreEqual(0.0, coordZARDisc.Value(swapNoColl2, valueDate), 1e-6); }
public void TestFloatLeg() { // Make the reference swap var rate = 0.0; var payFixed = true; double notional = 1000000; var startDate = new Date(2016, 9, 17); var tenor = Tenor.Years(1); var swap = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor); // Make a FloatLeg var resetDates = new Date[4]; var paymentDates = new Date[4]; var accrualFractions = new double[4]; var runningDate = new Date(2016, 9, 17); for (var i = 0; i < 4; i++) { resetDates[i] = new Date(runningDate); paymentDates[i] = resetDates[i].AddMonths(3); accrualFractions[i] = (paymentDates[i] - resetDates[i]) / 365.0; runningDate = paymentDates[i]; } var floatLeg = new FloatLeg(Currency.ZAR, paymentDates, new[] { 1e6, 1e6, 1e6, 1e6 }, resetDates, new[] { FloatingIndex.JIBAR3M, FloatingIndex.JIBAR3M, FloatingIndex.JIBAR3M, FloatingIndex.JIBAR3M }, new double[] { 0, 0, 0, 0 }, accrualFractions); // Set up the model var valueDate = new Date(2016, 9, 17); Date[] dates = { new Date(2016, 9, 17), new Date(2026, 9, 17) }; double[] rates = { 0.07, 0.07 }; IDiscountingSource discountCurve = new DatesAndRates(Currency.ZAR, valueDate, dates, rates); IFloatingRateSource forecastCurve = new ForecastCurve(valueDate, FloatingIndex.JIBAR3M, dates, rates); var curveSim = new DeterminsiticCurves(discountCurve); curveSim.AddRateForecast(forecastCurve); // Run the valuation var coordinator = new Coordinator(curveSim, new List <Simulator>(), 1); var swapValue = coordinator.Value(new Product[] { swap }, valueDate); var floatLegValue = coordinator.Value(new Product[] { floatLeg }, valueDate); Assert.AreEqual(swapValue, floatLegValue, 0.01); }
public void TestMultiHWAndFXToyCCIRS() { var valueDate = new Date(2016, 9, 17); var zarRatesSim = new HullWhite1F(Currency.ZAR, 0.05, 0.01, 0.07, 0.07, valueDate); zarRatesSim.AddForecast(FloatingIndex.JIBAR3M); var usdRatesSim = new HullWhite1F(Currency.USD, 0.05, 0.01, 0.01, 0.01, valueDate); usdRatesSim.AddForecast(FloatingIndex.LIBOR3M); var eurRatesSim = new HullWhite1F(Currency.EUR, 0.05, 0.01, 0.005, 0.005, valueDate); eurRatesSim.AddForecast(FloatingIndex.EURIBOR3M); var currencyPairs = new[] { new CurrencyPair(Currency.USD, Currency.ZAR), new CurrencyPair(Currency.EUR, Currency.ZAR) }; var spots = new[] { 13.6, 15.0 }; var vols = new[] { 0.15, 0.15 }; var correlations = new[, ] { { 1.0, 0.0 }, { 0.0, 1.0 } }; var model = new MultiHWAndFXToy(valueDate, Currency.ZAR, new[] { zarRatesSim, usdRatesSim, eurRatesSim }, currencyPairs, spots, vols, correlations); var portfolio = new List <Product>(); portfolio.Add(CreateFloatingLeg(Currency.ZAR, valueDate, -15e6, FloatingIndex.JIBAR3M, 7)); portfolio.Add(CreateFloatingLeg(Currency.EUR, valueDate, +1e6, FloatingIndex.EURIBOR3M, 7)); portfolio.Add(CreateFloatingLeg(Currency.ZAR, valueDate, 13e6, FloatingIndex.JIBAR3M, 13)); portfolio.Add(CreateFloatingLeg(Currency.USD, valueDate, -1e6, FloatingIndex.EURIBOR3M, 13)); portfolio.Add(IRSwap.CreateZARSwap(0.07, true, 20e6, valueDate, Tenor.Years(4))); var stepInMonths = 1; var fwdValueDates = Enumerable.Range(1, 13 * 12 / stepInMonths) .Select(i => valueDate.AddMonths(stepInMonths * i)).ToArray(); var coord = new Coordinator(model, new List <Simulator>(), 1000); //coord.SetThreadedness(false); var epe = coord.EPE(portfolio.ToArray(), valueDate, fwdValueDates); Assert.AreEqual(1555002, epe[0], 5000); Assert.AreEqual(2170370, epe[87], 5000); Assert.AreEqual(0, epe[155], 5); //Debug.WriteToFile("c:\\dev\\quantsa\\temp\\epeTest_singlethread_10000.csv", epe); }
public void TestCurveStripSeparateForecastAndDiscount() { var valueDate = new Date(2017, 1, 13); var zar = Currency.ZAR; var N = 1.0; var r1 = 0.12; var r2 = 0.08; var date1 = valueDate.AddMonths(6); var date2 = valueDate.AddMonths(12); var zarDisc = new ZeroRatesCurveForStripping(valueDate, zar); var jibar3mForecast = new ForwardRatesCurveForStripping(valueDate, FloatingIndex.JIBAR3M, zarDisc); Product depo1 = new CashLeg(new[] { valueDate, date1 }, new[] { -N, N * (1 + r1 * 0.5) }, new[] { zar, zar }); Product depo2 = new CashLeg(new[] { valueDate, date2 }, new[] { -N, N * (1 + r2 * 1) }, new[] { zar, zar }); Product swap = IRSwap.CreateZARSwap(0.08, true, 1.0, valueDate, Tenor.Months(9)); var modelZARDisc = new DeterminsiticCurves(zarDisc); modelZARDisc.AddRateForecast(jibar3mForecast); var coordZARDisc = new Coordinator(modelZARDisc, new List <Simulator>(), 1); coordZARDisc.SetThreadedness(false); var mcs = new MultiCurveStripper(valueDate); mcs.AddDiscounting(depo1, () => coordZARDisc.Value(depo1, valueDate), N, 1.0, zarDisc); mcs.AddDiscounting(depo2, () => coordZARDisc.Value(depo2, valueDate), N, 1.0, zarDisc); mcs.AddForecast(swap, () => coordZARDisc.Value(swap, valueDate), 0.0, 1.0, jibar3mForecast, FloatingIndex.JIBAR3M); mcs.Strip(); Assert.AreEqual(N, coordZARDisc.Value(depo1, valueDate), 1e-6); Assert.AreEqual(N, coordZARDisc.Value(depo2, valueDate), 1e-6); Assert.AreEqual(0, coordZARDisc.Value(swap, valueDate), 1e-6); /*double[] fwdRates = new double[180]; * for (int i=0; i<180; i++ ) * { * fwdRates[i] = jibar3mForecast.GetForwardRate(valueDate.AddTenor(Tenor.Days(i))); * } * Debug.WriteToFile("c:\\dev\\quantsa\\temp\\fwdRates.csv", fwdRates); */ }
public void TestCoordinatorEPESwap() { // Make the swap var rate = 0.07; var payFixed = true; double notional = 1000000; var startDate = new Date(2016, 9, 17); var tenor = Tenor.Years(5); var swap = IRSwap.CreateZARSwap(rate, payFixed, notional, startDate, tenor); // Set up the model var valueDate = new Date(2016, 9, 17); var a = 0.05; var vol = 0.005; var flatCurveRate = 0.07; var hullWiteSim = new HullWhite1F(Currency.ZAR, a, vol, flatCurveRate, flatCurveRate, valueDate); hullWiteSim.AddForecast(FloatingIndex.JIBAR3M); var coordinator = new Coordinator(hullWiteSim, new List <Simulator>(), 5000); var date = valueDate; var endDate = valueDate.AddTenor(tenor); var fwdValueDates = new List <Date>(); while (date < endDate) { fwdValueDates.Add(date); date = date.AddTenor(Tenor.Days(10)); } var epe = coordinator.EPE(new Product[] { swap }, valueDate, fwdValueDates.ToArray()); //Debug.WriteToFile(@"c:\dev\temp\epe_rate08_vol005.csv", epe); Assert.AreEqual(2560, epe[0], 100.0); Assert.AreEqual(6630, epe[90], 100.0); Assert.AreEqual(734, epe[182], 30); }
public void TestMultiHWAndFXToyCCIRSAltConstructor() { var valueDate = new Date(2016, 9, 17); var spots = new List <double> { 13.6, 15.0 }; var vols = new List <double> { 0.15, 0.15 }; var correlations = new[, ] { { 1.0, 0.0 }, { 0.0, 1.0 } }; // ZAR HW specs IDiscountingSource zarCurve = new DatesAndRates(Currency.ZAR, valueDate, new[] { valueDate, valueDate.AddMonths(240) }, new[] { 0.07, 0.07 }); var zarHWParams = new HWParams { vol = 0.01, meanReversionSpeed = 0.05 }; var zarRequiredIndices = new List <FloatingIndex> { FloatingIndex.JIBAR3M }; // Lists to be populated for other currencies var otherCcys = new List <Currency>(); var otherCcyCurves = new List <IDiscountingSource>(); var otherCcyHwParams = new List <HWParams>(); var otherCcyRequiredIndices = new List <List <FloatingIndex> >(); // USD HW specs otherCcys.Add(Currency.USD); otherCcyCurves.Add(new DatesAndRates(Currency.USD, valueDate, new[] { valueDate, valueDate.AddMonths(240) }, new[] { 0.01, 0.01 })); otherCcyHwParams.Add(new HWParams { vol = 0.01, meanReversionSpeed = 0.05 }); otherCcyRequiredIndices.Add(new List <FloatingIndex> { FloatingIndex.LIBOR3M }); // EUR HW specs otherCcys.Add(Currency.EUR); otherCcyCurves.Add(new DatesAndRates(Currency.EUR, valueDate, new[] { valueDate, valueDate.AddMonths(240) }, new[] { 0.005, 0.005 })); otherCcyHwParams.Add(new HWParams { vol = 0.01, meanReversionSpeed = 0.05 }); otherCcyRequiredIndices.Add(new List <FloatingIndex> { FloatingIndex.EURIBOR3M }); // Construct the model var model = new MultiHWAndFXToy(valueDate, zarCurve, zarRequiredIndices, zarHWParams, otherCcys, spots, vols, otherCcyCurves, otherCcyRequiredIndices, otherCcyHwParams, correlations); var portfolio = new List <Product>(); portfolio.Add(CreateFloatingLeg(Currency.ZAR, valueDate, -15e6, FloatingIndex.JIBAR3M, 7)); portfolio.Add(CreateFloatingLeg(Currency.EUR, valueDate, +1e6, FloatingIndex.EURIBOR3M, 7)); portfolio.Add(CreateFloatingLeg(Currency.ZAR, valueDate, 13e6, FloatingIndex.JIBAR3M, 13)); portfolio.Add(CreateFloatingLeg(Currency.USD, valueDate, -1e6, FloatingIndex.EURIBOR3M, 13)); portfolio.Add(IRSwap.CreateZARSwap(0.07, true, 20e6, valueDate, Tenor.Years(4))); var stepInMonths = 1; var fwdValueDates = Enumerable.Range(1, 13 * 12 / stepInMonths) .Select(i => valueDate.AddMonths(stepInMonths * i)).ToArray(); var coord = new Coordinator(model, new List <Simulator>(), 1000); //coord.SetThreadedness(false); var epe = coord.EPE(portfolio.ToArray(), valueDate, fwdValueDates); Assert.AreEqual(1555002, epe[0], 5000); Assert.AreEqual(2170370, epe[87], 5000); Assert.AreEqual(0, epe[155], 5); //Debug.WriteToFile("c:\\dev\\quantsa\\temp\\epeTest_2.csv", epe); }