« Return to Thread: rebuild a YieldTermStructure from discrete data

Re: rebuild a YieldTermStructure from discrete data

by Khanh Nguyen :: Rate this Message:

Reply to Author | View in Thread

Hi Luigi,

I have another question regarding this.

I am following Example\CallableBond\CallableBond.cpp to implement a
callable bond function for rquantlib. My problem is this

If I built the term structure directly like this

Date today = Date(16,October,2007);
boost::shared_ptr<SimpleQuote> rRate(new SimpleQuote(0.055));
Handle<YieldTermStructure> termStructure(flatRate(today,rRate,Actual360()));

then my codes work fine.

However, if I built the term structure curve from discrete date using
InterpolateZeroCurve, calling bond-NPV() returns an 'year outside
valid range' exception. In particular, the codes are

Handle<YieldTermStructure> termStructure(rebuildCurveFromZeroRates(

  hwTermDateSexp,

  hwTermZeroSexp));

with hwTermDateSexp and hwTermZeroSexp are date and zero rates
vectors. They are generated by


        int n = termStructure->maxDate() - settlementDate;
        for (int i = 0; i<n;i++){
        std::vector<ColDatum> row(numCol);
            Date d = current;
            row[0].setDateValue(RcppDate(d.month(),
                                         d.dayOfMonth(),
                                         d.year()));

            double zrate = termStructure->zeroRate(current, ActualActual(),
                                            Continuous);
            row[1].setDoubleValue(zrate);
            frame.addRow(row);
            current++;
        }


The rebuiltCurveFromZeroRates is implemented as

boost::shared_ptr<YieldTermStructure> rebuildCurveFromZeroRates(
                                                                SEXP dateSexp,
                                                                SEXP zeroSexp){
    RcppDateVector rcppdates  = RcppDateVector(dateSexp);
    int n = rcppdates.size();
    std::vector<QuantLib::Date> dates(rcppdates.size());
    for (int i = 0;i<n;i++){
        QuantLib::Date day(dateFromR(rcppdates(i)) );
        dates[i] = day;

    }
    //extract coupon rates vector
    RcppVector<double> RcppVec(zeroSexp);
    std::vector<double> zeros(RcppVec.stlVector());

    boost::shared_ptr<YieldTermStructure>
        rebuilt_curve(new
                      InterpolatedZeroCurve<LogLinear>(
                                                       dates,
                                                       zeros,
                                                       ActualActual()));
    return rebuilt_curve;
}

Any ideas? This has been bugging me for a few days. Thanks

-k


On Mon, Jun 29, 2009 at 10:35 PM, Luigi
Ballabio<luigi.ballabio@...> wrote:

> On Sun, 2009-06-21 at 09:58 -0400, Khanh Nguyen wrote:
>> This question relates to my current project RQuantLib. Is it possible
>> to reconstruct a YieldTermStructure object from the following data
>>
>> > data.frame(curves$time, curves$zero)
>>     curves.time curves.zero
>> 1           0.0  0.03907353
>> 2           0.1  0.03763962
>> 3           0.2  0.03792946
>> ....
>> 100         9.9  0.05163158
>> 101        10.0  0.05173125
>>
>> the 'curves' object is built from this data using quantlib
>>
>> tsQuotes <- list(d1w  =0.0382,
>>                  d1m  =0.0372,
>>                  fut1=96.2875,
>>                  fut2=96.7875,
>>                  fut3=96.9875,
>>                  fut4=96.6875,
>>                  fut5=96.4875,
>>                  fut6=96.3875,
>>                  fut7=96.2875,
>>                  fut8=96.0875,
>>                  s3y  =0.0398,
>>                  s5y  =0.0443,
>>                  s10y =0.05165,
>>                  s15y =0.055175)
>
> I'm not familiar with RQuantLib, so I'll have to make some guesses.
> Since you list a set of deposits, futures, and swaps, I assume that you
> built a piecewise yield curve.  If you want to store it and reconstruct
> it later, your best choice would be to store the nodes of the curve (for
> instance, with your set of inputs you'd have a node at 1 week, one at 1
> month etc.)  From here though, I can only guess. For instance, when you
> build the piecewise curve you can interpolate on discount factors, zero
> yields, or forwards.  Depending on what you chose, the nodes will
> contain different values and you'll need to know what you have stored in
> order to rebuild the curve (using an InterpolatedDiscountCurve, or an
> InterpolatedZeroCurve...)  Also, the data you're listing above are at
> t=0.1, 0.2 etc, and seem to be some kind of regular sampling of the
> curve rather than the values at the nodes. With those data, you won't be
> able to reconstruct the curve exactly.
>
> Luigi
>
>
> --
>
> This gubblick contains many nonsklarkish English flutzpahs, but the
> overall pluggandisp can be glorked from context.
> -- David Moser
>
>
>

------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time,
vendors submitting new applications to BlackBerry App World(TM) will have
the opportunity to enter the BlackBerry Developer Challenge. See full prize  
details at: http://p.sf.net/sfu/Challenge
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users

 « Return to Thread: rebuild a YieldTermStructure from discrete data