예제 #1
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TreeSwaptionEngine obj)
 {
     return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr);
 }
예제 #2
0
        public void testCachedValues()
        {
            //("Testing Bermudan swaption against cached values...");

            CommonVars vars = new CommonVars();

            vars.today = new Date(15, Month.February, 2002);

            Settings.setEvaluationDate(vars.today);

            vars.settlement = new Date(19, Month.February, 2002);
            // flat yield term structure impling 1x5 swap at 5%
            vars.termStructure.linkTo(Utilities.flatRate(vars.settlement,
                                                  0.04875825,
                                                  new Actual365Fixed()));

            double atmRate = vars.makeSwap(0.0).fairRate();

            VanillaSwap itmSwap = vars.makeSwap(0.8*atmRate);
            VanillaSwap atmSwap = vars.makeSwap(atmRate);
            VanillaSwap otmSwap = vars.makeSwap(1.2*atmRate);

            double a = 0.048696, sigma = 0.0058904;
            ShortRateModel model=new HullWhite(vars.termStructure,a, sigma);
            List<Date> exerciseDates= new List<Date>();
            List<CashFlow> leg = atmSwap.fixedLeg();

            for (int i=0; i<leg.Count; i++) {
                Coupon coupon = (Coupon)(leg[i]);
                exerciseDates.Add(coupon.accrualStartDate());
            }

            Exercise exercise = new BermudanExercise(exerciseDates);
            IPricingEngine engine = new TreeSwaptionEngine(model, 50);

            #if QL_USE_INDEXED_COUPON
            Real itmValue = 42.2413, atmValue = 12.8789, otmValue = 2.4759;
            #else
            double itmValue = 42.2470, atmValue = 12.8826, otmValue = 2.4769;
            #endif

            double tolerance = 1.0e-4;

            Swaption swaption = new Swaption(itmSwap, exercise);
            swaption.setPricingEngine(engine);
            if (Math.Abs(swaption.NPV()-itmValue) > tolerance)
                Assert.Fail("failed to reproduce cached in-the-money swaption value:\n"
                            + "calculated: " + swaption.NPV() + "\n"
                            + "expected:   " + itmValue);

            swaption = new Swaption(atmSwap, exercise);
            swaption.setPricingEngine(engine);
            if (Math.Abs(swaption.NPV()-atmValue) > tolerance)
                Assert.Fail("failed to reproduce cached at-the-money swaption value:\n"
                            + "calculated: " + swaption.NPV() + "\n"
                            + "expected:   " + atmValue);

            swaption = new Swaption(otmSwap, exercise);
            swaption.setPricingEngine(engine);
            if (Math.Abs(swaption.NPV()-otmValue) > tolerance)
                Assert.Fail("failed to reproduce cached out-of-the-money "
                            + "swaption value:\n"
                            + "calculated: " + swaption.NPV() + "\n"
                            + "expected:   " + otmValue);

            for (int j=0; j<exerciseDates.Count; j++)
                exerciseDates[j] = vars.calendar.adjust(exerciseDates[j]-10);
            exercise = new BermudanExercise(exerciseDates);

            #if QL_USE_INDEXED_COUPON
            itmValue = 42.1917; atmValue = 12.7788; otmValue = 2.4388;
            #else
            itmValue = 42.1974; atmValue = 12.7825; otmValue = 2.4399;
            #endif

            swaption = new Swaption(itmSwap, exercise);
            swaption.setPricingEngine(engine);
            if (Math.Abs(swaption.NPV()-itmValue) > tolerance)
                Assert.Fail("failed to reproduce cached in-the-money swaption value:\n"
                            + "calculated: " + swaption.NPV() + "\n"
                            + "expected:   " + itmValue);

            swaption = new Swaption(atmSwap, exercise);
            swaption.setPricingEngine(engine);
            if (Math.Abs(swaption.NPV()-atmValue) > tolerance)
                Assert.Fail("failed to reproduce cached at-the-money swaption value:\n"
                            + "calculated: " + swaption.NPV() + "\n"
                            + "expected:   " + atmValue);

            swaption = new Swaption(otmSwap, exercise);
            swaption.setPricingEngine(engine);
            if (Math.Abs(swaption.NPV()-otmValue) > tolerance)
                Assert.Fail("failed to reproduce cached out-of-the-money "
                            + "swaption value:\n"
                            + "calculated: " + swaption.NPV() + "\n"
                            + "expected:   " + otmValue);
        }
예제 #3
0
        public void testCachedValues()
        {
            //("Testing Bermudan swaption against cached values...");

            CommonVars vars = new CommonVars();

            vars.today = new Date(15, Month.February, 2002);

            Settings.setEvaluationDate(vars.today);

            vars.settlement = new Date(19, Month.February, 2002);
            // flat yield term structure impling 1x5 swap at 5%
            vars.termStructure.linkTo(Utilities.flatRate(vars.settlement,
                                                         0.04875825,
                                                         new Actual365Fixed()));

            double atmRate = vars.makeSwap(0.0).fairRate();

            VanillaSwap itmSwap = vars.makeSwap(0.8 * atmRate);
            VanillaSwap atmSwap = vars.makeSwap(atmRate);
            VanillaSwap otmSwap = vars.makeSwap(1.2 * atmRate);

            double          a = 0.048696, sigma = 0.0058904;
            ShortRateModel  model         = new HullWhite(vars.termStructure, a, sigma);
            List <Date>     exerciseDates = new List <Date>();
            List <CashFlow> leg           = atmSwap.fixedLeg();

            for (int i = 0; i < leg.Count; i++)
            {
                Coupon coupon = (Coupon)(leg[i]);
                exerciseDates.Add(coupon.accrualStartDate());
            }

            Exercise       exercise   = new BermudanExercise(exerciseDates);
            IPricingEngine treeEngine = new TreeSwaptionEngine(model, 50);
            IPricingEngine fdmEngine  = new FdHullWhiteSwaptionEngine(model as HullWhite);

#if QL_USE_INDEXED_COUPON
            double itmValue = 42.2413, atmValue = 12.8789, otmValue = 2.4759;
            double itmValueFdm = 42.2111, atmValueFdm = 12.8879, otmValueFdm = 2.44443;
#else
            double itmValue = 42.2470, atmValue = 12.8826, otmValue = 2.4769;
            double itmValueFdm = 42.2091, atmValueFdm = 12.8864, otmValueFdm = 2.4437;
#endif

            double tolerance = 1.0e-4;

            Swaption swaption = new Swaption(itmSwap, exercise);
            swaption.setPricingEngine(treeEngine);
            if (Math.Abs(swaption.NPV() - itmValue) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached in-the-money swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + itmValue);
            }

            swaption.setPricingEngine(fdmEngine);
            if (Math.Abs(swaption.NPV() - itmValueFdm) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached in-the-money swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + itmValueFdm);
            }

            swaption = new Swaption(atmSwap, exercise);
            swaption.setPricingEngine(treeEngine);
            if (Math.Abs(swaption.NPV() - atmValue) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached at-the-money swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + atmValue);
            }
            swaption.setPricingEngine(fdmEngine);
            if (Math.Abs(swaption.NPV() - atmValueFdm) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached at-the-money swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + atmValueFdm);
            }

            swaption = new Swaption(otmSwap, exercise);
            swaption.setPricingEngine(treeEngine);
            if (Math.Abs(swaption.NPV() - otmValue) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached out-of-the-money "
                             + "swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + otmValue);
            }
            swaption.setPricingEngine(fdmEngine);
            if (Math.Abs(swaption.NPV() - otmValueFdm) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached out-of-the-money "
                             + "swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + otmValueFdm);
            }

            for (int j = 0; j < exerciseDates.Count; j++)
            {
                exerciseDates[j] = vars.calendar.adjust(exerciseDates[j] - 10);
            }
            exercise = new BermudanExercise(exerciseDates);

#if QL_USE_INDEXED_COUPON
            itmValue = 42.1917; atmValue = 12.7788; otmValue = 2.4388;
#else
            itmValue = 42.1974; atmValue = 12.7825; otmValue = 2.4399;
#endif

            swaption = new Swaption(itmSwap, exercise);
            swaption.setPricingEngine(treeEngine);
            if (Math.Abs(swaption.NPV() - itmValue) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached in-the-money swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + itmValue);
            }

            swaption = new Swaption(atmSwap, exercise);
            swaption.setPricingEngine(treeEngine);
            if (Math.Abs(swaption.NPV() - atmValue) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached at-the-money swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + atmValue);
            }

            swaption = new Swaption(otmSwap, exercise);
            swaption.setPricingEngine(treeEngine);
            if (Math.Abs(swaption.NPV() - otmValue) > tolerance)
            {
                QAssert.Fail("failed to reproduce cached out-of-the-money "
                             + "swaption value:\n"
                             + "calculated: " + swaption.NPV() + "\n"
                             + "expected:   " + otmValue);
            }
        }
예제 #4
0
 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(TreeSwaptionEngine obj) {
   return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
 }