/// <summary> /// Add extra dates to make sure that the minimum spacing is not too large to make the Monte Carlo errors bad. /// <para/> /// At this point the dates are all copied. /// </summary> public override void Prepare(Date anchorDate) { _anchorDate = anchorDate; fM = date => _inputRate; PM = date => Math.Exp(-_inputRate * (date - anchorDate) / 365.0); double minStepSize = 20; allDates.Insert(0, anchorDate); allDates = allDates.Distinct().ToList(); allDates.Sort(); var newDates = new List <Date>(); newDates.Add(new Date(allDates[0])); for (var i = 1; i < allDates.Count; i++) { var nSteps = (int)Math.Floor((allDates[i] - allDates[i - 1]) / minStepSize); var days = (allDates[i] - allDates[i - 1]) / (nSteps + 1); for (var j = 0; j < nSteps; j++) { newDates.Add(new Date(allDates[i - 1].AddTenor(Tenor.FromDays((j + 1) * days)))); } newDates.Add(new Date(allDates[i])); } allDates = newDates; allDatesDouble = allDates.Select(date => (double)date).ToArray(); }
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.FromYears(5); var swap = TestHelpers.CreateZARSwap(rate, payFixed, notional, startDate, tenor, TestHelpers.Jibar3M); // 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(TestHelpers.ZAR, a, vol, flatCurveRate, flatCurveRate); hullWiteSim.AddForecast(TestHelpers.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.FromDays(10)); } var allDetails = coordinator.GetValuePaths(new Product[] { swap }, valueDate, fwdValueDates.ToArray()); allDetails.GetNames(); }
public void RateCurveCalibrator_CanCalibrateTwoCurves() { var instruments = new List <IRateCurveInstrument>(); var zarCsaCurveDescription = new DiscountingSourceDescription(_zar, new BankAccountNumeraire(_zar)); var jibarDiscountDescription = new DiscountingSourceDescription(_zar, _jibar3M); instruments.Add(new DepoCurveInstrument(Tenor.FromMonths(3), 0.071, jibarDiscountDescription)); instruments.Add(new FixedFloatSwapCurveInstrument(Tenor.FromMonths(6), _jibar3M, 0.0, 0.07, zarCsaCurveDescription, FixedFloatSwapCurveInstrument.CurveToStrip.Forecast)); instruments.Add(new FixedFloatSwapCurveInstrument(Tenor.FromYears(2), _jibar3M, 0.0, 0.07, zarCsaCurveDescription, FixedFloatSwapCurveInstrument.CurveToStrip.Forecast)); instruments.Add(new DepoCurveInstrument(Tenor.FromDays(1), 0.06, zarCsaCurveDescription)); instruments.Add(new BasisSwapCurveInstrument(Tenor.FromYears(1), _jibar3M, _jibar1D, 0.0, 0.01, zarCsaCurveDescription, BasisSwapCurveInstrument.CurveToStrip.DiscountCurve)); instruments.Add(new BasisSwapCurveInstrument(Tenor.FromYears(2), _jibar3M, _jibar1D, 0.0, 0.01, zarCsaCurveDescription, BasisSwapCurveInstrument.CurveToStrip.DiscountCurve)); var calib = new RateCurveCalibrator(instruments, new MultiDimNewton(1e-8, 100), zarCsaCurveDescription, new FloatRateIndex[] { _jibar1D }, jibarDiscountDescription, new FloatRateIndex[] { _jibar3M }); var mdc = new MarketDataContainer(); mdc.Set(calib); calib.TryCalibrate(_calibrationDate, mdc); var testValues = instruments.Select(inst => Math.Abs(inst.Objective())); var maxTestValue = testValues.Max(); Assert.AreEqual(0.0, maxTestValue, 1e-8); }
/// <summary> /// Add extra dates to make sure that the minimum spacing is not too large to make the Monte Carlo errors bad. /// <para/> /// At this point the dates are all copied. /// </summary> public override void Prepare(Date anchorDate) { _anchorDate = anchorDate; _fM = date => _inputRate; _pm = date => Math.Exp(-_inputRate * (date - anchorDate) / 365.0); double minStepSize = 20; _allDates.Insert(0, anchorDate); _allDates = _allDates.Distinct().ToList(); _allDates.Sort(); var newDates = new List<Date>(); newDates.Add(new Date(_allDates[0])); for (var i = 1; i < _allDates.Count; i++) { var nSteps = (int) Math.Floor((_allDates[i] - _allDates[i - 1]) / minStepSize); var days = (_allDates[i] - _allDates[i - 1]) / (nSteps + 1); for (var j = 0; j < nSteps; j++) newDates.Add(new Date(_allDates[i - 1].AddTenor(Tenor.FromDays((j + 1) * days)))); newDates.Add(new Date(_allDates[i])); } _allDates = newDates; _allDatesDouble = _allDates.Select(date => (double) date).ToArray(); _dist = new NormalDistribution(); Generator.Seed = -1585814591; // This magic number is: "HW1FSimulator".GetHashCode(); }
private void SetDates() { _exDates = new List <Date>(); foreach (var couponDate in _couponDates) { if (couponDate > _firstCouponDate) { _exDates.Add(couponDate); } } _exProducts = new List <IProduct>(); foreach (var couponDate in _couponDates) { if (couponDate > _firstCouponDate) { _exProducts.Add(new CashLeg(new[] { couponDate.AddTenor(Tenor.FromDays(1)) }, new[] { -_notional }, new[] { _currency })); } } }
public void TestPhysicalSwaptionEPE() { var coordinator = new Coordinator(hullWiteSim, new List <Simulator>(), 5000); var exDate = new List <Date> { new Date(2018, 9, 17) }; // Couterparty has option to enter into a receive fixed swap var physicalSwaption = new BermudanSwaption(swapPay, exDate, false); var date = valueDate; var endDate = valueDate.AddTenor(new Tenor(0, 0, 3, 5)); var fwdValueDates = new List <Date>(); while (date <= endDate) { fwdValueDates.Add(date); date = date.AddTenor(Tenor.FromDays(10)); } var epe = coordinator.EPE(new Product[] { physicalSwaption }, valueDate, fwdValueDates.ToArray()); //Debug.WriteToFile(@"c:\dev\temp\ene_physicalswaption_HW.csv", epe); }
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.FromYears(5); var swap = TestHelpers.CreateZARSwap(rate, payFixed, notional, startDate, tenor, TestHelpers.Jibar3M); // 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(TestHelpers.ZAR, a, vol, flatCurveRate, flatCurveRate); hullWiteSim.AddForecast(TestHelpers.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.FromDays(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); }