示例#1
0
        public void testSpreadTreatment()
        {
            //"Testing swaption treatment of spread...";

            CommonVars vars = new CommonVars();

            double[] spreads = { -0.002, -0.001, 0.0, 0.001, 0.002 };

            for (int i=0; i<exercises.Length; i++) {
                for (int j=0; j<lengths.Length ; j++) {
                    for (int k=0; k<type.Length ; k++) {
                        Date exerciseDate = vars.calendar.advance(vars.today,
                                                                  exercises[i]);
                        Date startDate =
                            vars.calendar.advance(exerciseDate,
                                                  vars.settlementDays,TimeUnit.Days);
                        for (int l=0; l<spreads.Length ; l++) {
                            VanillaSwap swap =
                                new MakeVanillaSwap(lengths[j], vars.index, 0.06)
                                        .withEffectiveDate(startDate)
                                        .withFloatingLegSpread(spreads[l])
                                        .withType(type[k]);
                            // FLOATING_POINT_EXCEPTION
                            double correction = spreads[l] *
                                                swap.floatingLegBPS() /
                                                swap.fixedLegBPS();
                            VanillaSwap equivalentSwap =
                                new MakeVanillaSwap(lengths[j], vars.index, 0.06+correction)
                                        .withEffectiveDate(startDate)
                                        .withFloatingLegSpread(0.0)
                                        .withType(type[k]);
                            Swaption swaption1 =
                                vars.makeSwaption(swap,exerciseDate,0.20);
                            Swaption swaption2 =
                                vars.makeSwaption(equivalentSwap,exerciseDate,0.20);
                            Swaption swaption1_cash =
                                vars.makeSwaption(swap,exerciseDate,0.20,
                                                  Settlement.Type.Cash);
                            Swaption swaption2_cash =
                                vars.makeSwaption(equivalentSwap,exerciseDate,0.20,
                                                  Settlement.Type.Cash);
                            if (Math.Abs(swaption1.NPV()-swaption2.NPV()) > 1.0e-6)
                                Assert.Fail("wrong spread treatment:" +
                                    "\nexercise: " + exerciseDate +
                                    "\nlength:   " + lengths[j] +
                                    "\ntype      " + type[k] +
                                    "\nspread:   " + spreads[l] +
                                    "\noriginal swaption value:   " + swaption1.NPV() +
                                    "\nequivalent swaption value: " + swaption2.NPV());

                            if (Math.Abs(swaption1_cash.NPV()-swaption2_cash.NPV()) > 1.0e-6)
                                Assert.Fail("wrong spread treatment:" +
                                    "\nexercise date: " + exerciseDate +
                                    "\nlength: " + lengths[j] +
                                    //"\npay " + (type[k] ? "fixed" : "floating") +
                                    "\nspread: " + spreads[l] +
                                    "\nvalue of original swaption:   "  + swaption1_cash.NPV() +
                                    "\nvalue of equivalent swaption: "  + swaption2_cash.NPV());
                        }
                    }
                }
            }
        }