public void testInArrears() { //("Testing in-arrears swap calculation..."); CommonVars vars = new CommonVars(); /* See Hull, 4th ed., page 550 Note: the calculation in the book is wrong (work out the adjustment and you'll get 0.05 + 0.000115 T1) */ Date maturity = vars.today + new Period(5, TimeUnit.Years); Calendar calendar = new NullCalendar(); Schedule schedule = new Schedule(vars.today, maturity, new Period(Frequency.Annual), calendar, BusinessDayConvention.Following, BusinessDayConvention.Following, DateGeneration.Rule.Forward, false); DayCounter dayCounter = new SimpleDayCounter(); List<double> nominals = new List<double>() { 100000000.0 }; IborIndex index = new IborIndex("dummy", new Period(1, TimeUnit.Years), 0, new EURCurrency(), calendar, BusinessDayConvention.Following, false, dayCounter, vars.termStructure); double oneYear = 0.05; double r = Math.Log(1.0 + oneYear); vars.termStructure.linkTo(Utilities.flatRate(vars.today, r, dayCounter)); List<double> coupons = new List<double>() { oneYear }; List<CashFlow> fixedLeg = new FixedRateLeg(schedule) .withCouponRates(coupons, dayCounter) .withNotionals(nominals); List<double> gearings = new List<double>(); List<double> spreads = new List<double>(); int fixingDays = 0; double capletVolatility = 0.22; var vol = new Handle<OptionletVolatilityStructure>( new ConstantOptionletVolatility(vars.today, new NullCalendar(), BusinessDayConvention.Following, capletVolatility, dayCounter)); IborCouponPricer pricer = new BlackIborCouponPricer(vol); List<CashFlow> floatingLeg = new IborLeg(schedule, index) .withPaymentDayCounter(dayCounter) .withFixingDays(fixingDays) .withGearings(gearings) .withSpreads(spreads) .inArrears() .withNotionals(nominals); Utils.setCouponPricer(floatingLeg, pricer); Swap swap = new Swap(floatingLeg, fixedLeg); swap.setPricingEngine(new DiscountingSwapEngine(vars.termStructure)); double storedValue = -144813.0; double tolerance = 1.0; if (Math.Abs(swap.NPV() - storedValue) > tolerance) Assert.Fail("Wrong NPV calculation:\n" + " expected: " + storedValue + "\n" + " calculated: " + swap.NPV()); }
public void testSimple() { Period[] p = { new Period(3, TimeUnit.Months), new Period(6, TimeUnit.Months), new Period(1, TimeUnit.Years) }; double[] expected = { 0.25, 0.5, 1.0 }; int n = p.Length; // 4 years should be enough Date first= new Date(1,Month.January,2002), last = new Date(31,Month.December,2005); DayCounter dayCounter = new SimpleDayCounter(); for (Date start = first; start <= last; start++) { for (int i=0; i<n; i++) { Date end = start + p[i]; double calculated = dayCounter.yearFraction(start,end,null ,null ); if (Math.Abs(calculated-expected[i]) > 1.0e-12) { Assert.Fail ("from " + start + " to " + end + "Calculated: " + calculated + "Expected: " + expected[i]); } } } }
public void irData(string name, SquareRootProcess process, double rand, DateTime preDate, DateTime nextDate) { DayCounter dc = new SimpleDayCounter(); clsHDAT_MARKETDATA_TB clstb = new clsHDAT_MARKETDATA_TB(); clstb.INDEX_CD = name; clstb.REF_DT = preDate.ToString("yyyyMMdd"); clstb.SelectOwn(); clstb.REF_DT = nextDate.ToString("yyyyMMdd"); double x0 = clstb.LAST; double t0 = 0.0; double dt = dc.yearFraction(preDate, nextDate); double v = Math.Round(process.evolve(t0, x0, dt, rand), 8); clstb.LAST = v; clstb.LOW = v; clstb.HIGH = v; clstb.Insert(); }