Assembla home | Assembla project page
 

Changeset 207

Show
Ignore:
Timestamp:
06/15/08 22:53:10 (5 months ago)
Author:
snovik
Message:

Fix: ConvexMonotone? interpolation + test of Bootstrapping with ConvexMonotone?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/QLNet/QLNet/Math/Interpolations/convexmonotoneinterpolation.cs

    r202 r207  
    4949        } 
    5050        public double primitive(double x) { 
     51            double result = (quadraticity_ * quadraticHelper_.primitive(x) + (1.0 - quadraticity_) * convMonoHelper_.primitive(x)); 
    5152            return (quadraticity_ * quadraticHelper_.primitive(x) + (1.0 - quadraticity_) * convMonoHelper_.primitive(x)); 
    5253        } 
     
    618619                return extrapolationHelper_.value(x); 
    619620            } 
    620             return sectionHelpers_[sectionHelpers_.Keys.First(y => x < y)].value(x); 
     621 
     622            double i; 
     623            if (x > sectionHelpers_.Keys.Last()) 
     624                i = sectionHelpers_.Keys.Last(); 
     625            else if (x < sectionHelpers_.Keys.First()) 
     626                i = sectionHelpers_.Keys.First(); 
     627            else 
     628                i = sectionHelpers_.Keys.First(y => x < y); 
     629            return sectionHelpers_[i].value(x); 
    621630        } 
    622631 
     
    625634                return extrapolationHelper_.primitive(x); 
    626635            } 
    627             return sectionHelpers_[sectionHelpers_.Keys.First(y => x < y)].primitive(x); 
     636 
     637            double i; 
     638            if (x >= sectionHelpers_.Keys.Last()) 
     639                i = sectionHelpers_.Keys.Last(); 
     640            else if (x <= sectionHelpers_.Keys.First()) 
     641                i = sectionHelpers_.Keys.First(); 
     642            else 
     643                i = sectionHelpers_.Keys.First(y => x < y); 
     644            return sectionHelpers_[i].primitive(x); 
    628645        } 
    629646 
     
    718735        public bool global { get { return true; } } 
    719736        public int requiredPoints { get { return 2; } } 
    720  
    721         const int dataSizeAdjustment = 1; 
     737        public int dataSizeAdjustment { get { return 1; } } 
    722738    } 
    723739} 
  • trunk/QLNet/QLNet/Termstructures/Iterativebootstrap.cs

    r202 r207  
    4242                       (ts_.interpolator_.requiredPoints-1) + " required"); 
    4343 
    44             for (int i = 0; i < n; ++i) { 
    45                 ts_.instruments_[i].registerWith(ts_.update); 
    46             } 
     44            ts_.instruments_.ForEach(x => x.registerWith(ts_.update)); 
    4745        } 
    4846 
     
    5553 
    5654            //prepare instruments 
    57             int n = ts_.instruments_.Count
     55            int n = ts_.instruments_.Count, i
    5856 
    5957            // ensure rate helpers are sorted 
     
    6159 
    6260            // check that there is no instruments with the same maturity 
    63             for (int i = 1; i < n; ++i) { 
     61            for (i = 1; i < n; ++i) { 
    6462                Date m1 = ts_.instruments_[i - 1].latestDate(), 
    6563                     m2 = ts_.instruments_[i].latestDate(); 
     
    6866 
    6967            // check that there is no instruments with invalid quote 
    70             for (int i = 0; i < n; ++i) 
    71                 if (!ts_.instruments_[i].quoteIsValid()
    72                     throw new ArgumentException("instrument " + i + " (maturity: " + ts_.instruments_[i].latestDate() + 
    73                            ") has an invalid quote"); 
     68            i = ts_.instruments_.FindIndex(x => !x.quoteIsValid()); 
     69            if (i != -1
     70                throw new ArgumentException("instrument " + i + " (maturity: " + ts_.instruments_[i].latestDate() + 
     71                       ") has an invalid quote"); 
    7472 
    7573            // setup instruments and register with them 
    76             for (int i = 0; i < n; ++i) { 
    77                 // There is a significant interaction with observability. 
    78                 ts_.instruments_[i].setTermStructure(ts_); 
    79             } 
     74            ts_.instruments_.ForEach(x => x.setTermStructure(ts_)); 
    8075 
    8176            // calculate dates and times 
     
    8479            ts_.dates_[0] = ts_.initialDate(ts_); 
    8580            ts_.times_[0] = ts_.timeFromReference(ts_.dates_[0]); 
    86             for (int i = 0; i < n; ++i) { 
     81            for (i = 0; i < n; ++i) { 
    8782                ts_.dates_[i + 1] = ts_.instruments_[i].latestDate(); 
    8883                ts_.times_[i + 1] = ts_.timeFromReference(ts_.dates_[i + 1]); 
     
    107102                    ts_.interpolation_ = ts_.interpolator_.interpolate(ts_.times_, ts_.times_.Count, ts_.data_); 
    108103                } 
    109                 for (int i=1; i<n+1; ++i) { 
     104                for (i=1; i<n+1; ++i) { 
    110105                    // calculate guess before extending interpolation to ensure that any extrapolation is performed 
    111106                    // using the curve bootstrapped so far and no more 
     
    165160                // exit conditions 
    166161                double improvement = 0.0; 
    167                 for (int i=1; i<n+1; ++i) 
     162                for (i=1; i<n+1; ++i) 
    168163                    improvement = Math.Max(improvement, Math.Abs(ts_.data_[i]-previousData[i])); 
    169164                if (improvement<=ts_.accuracy_)  // convergence reached 
  • trunk/QLNet/Test2008/T_Piecewiseyieldcurve.cs

    r203 r207  
    467467        } 
    468468 
    469  
    470 //void PiecewiseYieldCurveTest::testLiborFixing() { 
    471  
    472 //    BOOST_MESSAGE( 
    473 //        "Testing use of today's LIBOR fixings in swap curve..."); 
    474  
    475 //    CommonVars vars; 
    476  
    477 //    std::vector<boost::shared_ptr<RateHelper> > swapHelpers(vars.swaps); 
    478 //    boost::shared_ptr<IborIndex> euribor6m(new Euribor6M); 
    479  
    480 //    for (Size i=0; i<vars.swaps; i++) { 
    481 //        Handle<Quote> r(vars.rates[i+vars.deposits]); 
    482 //        swapHelpers[i] = boost::shared_ptr<RateHelper>(new 
    483 //            SwapRateHelper(r, Period(swapData[i].n, swapData[i].units), 
    484 //                           vars.calendar, 
    485 //                           vars.fixedLegFrequency, vars.fixedLegConvention, 
    486 //                           vars.fixedLegDayCounter, euribor6m)); 
    487 //    } 
    488  
    489 //    vars.termStructure = boost::shared_ptr<YieldTermStructure>(new 
    490 //        PiecewiseYieldCurve<Discount,LogLinear>(vars.settlement, 
    491 //                                                swapHelpers, 
    492 //                                                Actual360())); 
    493  
    494 //    Handle<YieldTermStructure> curveHandle = 
    495 //        Handle<YieldTermStructure>(vars.termStructure); 
    496  
    497 //    boost::shared_ptr<IborIndex> index(new Euribor6M(curveHandle)); 
    498 //    for (Size i=0; i<vars.swaps; i++) { 
    499 //        Period tenor = swapData[i].n*swapData[i].units; 
    500  
    501 //        VanillaSwap swap = MakeVanillaSwap(tenor, index, 0.0) 
    502 //            .withEffectiveDate(vars.settlement) 
    503 //            .withFixedLegDayCount(vars.fixedLegDayCounter) 
    504 //            .withFixedLegTenor(Period(vars.fixedLegFrequency)) 
    505 //            .withFixedLegConvention(vars.fixedLegConvention) 
    506 //            .withFixedLegTerminationDateConvention(vars.fixedLegConvention); 
    507  
    508 //        Rate expectedRate = swapData[i].rate/100, 
    509 //             estimatedRate = swap.fairRate(); 
    510 //        Real tolerance = 1.0e-9; 
    511 //        if (std::fabs(expectedRate-estimatedRate) > tolerance) { 
    512 //            BOOST_ERROR("before LIBOR fixing:\n" 
    513 //                        + swapData[i].n + " year(s) swap:\n" 
    514 //                        + std::setprecision(8) 
    515 //                        + "    estimated rate: " 
    516 //                        + io::rate(estimatedRate) + "\n" 
    517 //                        + "    expected rate:  " 
    518 //                        + io::rate(expectedRate)); 
    519 //        } 
    520 //    } 
    521  
    522 //    Flag f; 
    523 //    f.registerWith(vars.termStructure); 
    524 //    f.lower(); 
    525  
    526 //    index->addFixing(vars.today, 0.0425); 
    527  
    528 //    if (!f.isUp()) 
    529 //        BOOST_ERROR("Observer was not notified of rate fixing"); 
    530  
    531 //    for (Size i=0; i<vars.swaps; i++) { 
    532 //        Period tenor = swapData[i].n*swapData[i].units; 
    533  
    534 //        VanillaSwap swap = MakeVanillaSwap(tenor, index, 0.0) 
    535 //            .withEffectiveDate(vars.settlement) 
    536 //            .withFixedLegDayCount(vars.fixedLegDayCounter) 
    537 //            .withFixedLegTenor(Period(vars.fixedLegFrequency)) 
    538 //            .withFixedLegConvention(vars.fixedLegConvention) 
    539 //            .withFixedLegTerminationDateConvention(vars.fixedLegConvention); 
    540  
    541 //        Rate expectedRate = swapData[i].rate/100, 
    542 //             estimatedRate = swap.fairRate(); 
    543 //        Real tolerance = 1.0e-9; 
    544 //        if (std::fabs(expectedRate-estimatedRate) > tolerance) { 
    545 //            BOOST_ERROR("after LIBOR fixing:\n" 
    546 //                        + swapData[i].n + " year(s) swap:\n" 
    547 //                        + std::setprecision(8) 
    548 //                        + "    estimated rate: " 
    549 //                        + io::rate(estimatedRate) + "\n" 
    550 //                        + "    expected rate:  " 
    551 //                        + io::rate(expectedRate)); 
    552 //        } 
    553 //    } 
    554 //} 
    555  
    556469        [TestMethod()] 
    557470        public void testForwardRateDayCounter() {