Ejemplo n.º 1
0
        public void testCmsSwap()
        {
            // Testing Hagan-pricer flat-vol equivalence for swaps
            CommonVars vars = new CommonVars();

            SwapIndex swapIndex = new SwapIndex("EuriborSwapIsdaFixA",
                                                new Period(10, TimeUnit.Years),
                                                vars.iborIndex.fixingDays(),
                                                vars.iborIndex.currency(),
                                                vars.iborIndex.fixingCalendar(),
                                                new Period(1, TimeUnit.Years),
                                                BusinessDayConvention.Unadjusted,
                                                vars.iborIndex.dayCounter(),//??
                                                vars.iborIndex);
            // FIXME
            //shared_ptr<SwapIndex> swapIndex(new
            //    EuriborSwapIsdaFixA(10*Years, vars.iborIndex->termStructure()));
            double     spread      = 0.0;
            List <int> swapLengths = new List <int>();

            swapLengths.Add(1);
            swapLengths.Add(5);
            swapLengths.Add(6);
            swapLengths.Add(10);
            int         n   = swapLengths.Count;
            List <Swap> cms = new List <Swap>(n);

            for (int i = 0; i < n; ++i)
            {
                // no cap, floor
                // no gearing, spread
                cms.Add(new MakeCms(new Period(swapLengths[i], TimeUnit.Years),
                                    swapIndex,
                                    vars.iborIndex, spread,
                                    new Period(10, TimeUnit.Days)).value());
            }

            for (int j = 0; j < vars.yieldCurveModels.Count; ++j)
            {
                vars.numericalPricers[j].setSwaptionVolatility(vars.atmVol);
                vars.analyticPricers[j].setSwaptionVolatility(vars.atmVol);
                for (int sl = 0; sl < n; ++sl)
                {
                    Utils.setCouponPricer(cms[sl].leg(0), vars.numericalPricers[j]);
                    double priceNum = cms[sl].NPV();
                    Utils.setCouponPricer(cms[sl].leg(0), vars.analyticPricers[j]);
                    double priceAn = cms[sl].NPV();

                    double difference = Math.Abs(priceNum - priceAn);
                    double tol        = 2.0e-4;
                    bool   linearTsr  = j == vars.yieldCurveModels.Count - 1;
                    if (difference > tol)
                    {
                        QAssert.Fail("\nLength in Years:  " + swapLengths[sl] +
                                     "\nswap index:       " + swapIndex.name() +
                                     "\nibor index:       " + vars.iborIndex.name() +
                                     "\nspread:           " + (spread) +
                                     "\nYieldCurve Model: " + vars.yieldCurveModels[j] +
                                     "\nNumerical Pricer: " + (priceNum) + (linearTsr ? " (Linear TSR Model)" : "") +
                                     "\nAnalytic Pricer:  " + (priceAn) +
                                     "\ndifference:       " + (difference) +
                                     "\ntolerance:        " + (tol));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void testFairRate()
        {
            // Testing Hagan-pricer flat-vol equivalence for coupons
            CommonVars vars = new CommonVars();

            SwapIndex swapIndex = new SwapIndex("EuriborSwapIsdaFixA",
                                                new Period(10, TimeUnit.Years),
                                                vars.iborIndex.fixingDays(),
                                                vars.iborIndex.currency(),
                                                vars.iborIndex.fixingCalendar(),
                                                new Period(1, TimeUnit.Years),
                                                BusinessDayConvention.Unadjusted,
                                                vars.iborIndex.dayCounter(),//??
                                                vars.iborIndex);

            // FIXME
            //shared_ptr<SwapIndex> swapIndex(new
            //    EuriborSwapIsdaFixA(10*Years, vars.iborIndex->termStructure()));
            Date   startDate              = vars.termStructure.link.referenceDate() + new Period(20, TimeUnit.Years);
            Date   paymentDate            = startDate + new Period(1, TimeUnit.Years);
            Date   endDate                = paymentDate;
            double nominal                = 1.0;
            double?infiniteCap            = null;
            double?infiniteFloor          = null;
            double gearing                = 1.0;
            double spread                 = 0.0;
            CappedFlooredCmsCoupon coupon = new CappedFlooredCmsCoupon(nominal, paymentDate,
                                                                       startDate, endDate,
                                                                       swapIndex.fixingDays(), swapIndex,
                                                                       gearing, spread,
                                                                       infiniteCap, infiniteFloor,
                                                                       startDate, endDate,
                                                                       vars.iborIndex.dayCounter());

            for (int j = 0; j < vars.yieldCurveModels.Count; ++j)
            {
                vars.numericalPricers[j].setSwaptionVolatility(vars.atmVol);
                coupon.setPricer(vars.numericalPricers[j]);
                double rate0 = coupon.rate();

                vars.analyticPricers[j].setSwaptionVolatility(vars.atmVol);
                coupon.setPricer(vars.analyticPricers[j]);
                double rate1 = coupon.rate();

                double difference = Math.Abs(rate1 - rate0);
                double tol        = 2.0e-4;
                bool   linearTsr  = j == vars.yieldCurveModels.Count - 1;

                if (difference > tol)
                {
                    QAssert.Fail("\nCoupon payment date: " + paymentDate +
                                 "\nCoupon start date:   " + startDate +
                                 "\nCoupon floor:        " + (infiniteFloor) +
                                 "\nCoupon gearing:      " + (gearing) +
                                 "\nCoupon swap index:   " + swapIndex.name() +
                                 "\nCoupon spread:       " + (spread) +
                                 "\nCoupon cap:          " + (infiniteCap) +
                                 "\nCoupon DayCounter:   " + vars.iborIndex.dayCounter() +
                                 "\nYieldCurve Model:    " + vars.yieldCurveModels[j] +
                                 "\nNumerical Pricer:    " + (rate0) + (linearTsr ? " (Linear TSR Model)" : "") +
                                 "\nAnalytic Pricer:     " + (rate1) +
                                 "\ndifference:          " + (difference) +
                                 "\ntolerance:           " + (tol));
                }
            }
        }