예제 #1
0
        /// <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();
        }
예제 #2
0
        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();
        }
예제 #3
0
        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);
        }
예제 #4
0
        /// <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();
        }
예제 #5
0
        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 }));
                }
            }
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }