Facing app crash with zeroRate function

classic Classic list List threaded Threaded
4 messages Options
v17
Reply | Threaded
Open this post in threaded view
|

Facing app crash with zeroRate function

v17
Hi,

Request members help on the below issue -

I have following code in my project -

boost::shared_ptr<YieldTermStructure> CYieldCurveUtils::MakeTermStructure(const vector<Rate> &liborRates, const vector<Period> &liborTenors, YieldCurveDef &_ycDef, bool zeroRates ){

        ycDef = _ycDef;
        ycDef.evalDate = ycDef.cal.advance(ycDef.evalDate, -2, Days);

        const int size = liborRates.size();
        std::vector<shared_ptr<RateHelper>> instruments;
        Period oneYear(1, Years);
        shared_ptr<Euribor> swapIndex(new Euribor(6 * Months));
        vector<Date> liborDates(liborTenors.size());
        Date curDate;
        if (!zeroRates){
                for (int idx  = 0; idx  < size; idx++){
                        if (liborTenors[idx] < oneYear){
                                shared_ptr<RateHelper> rateHelper(new DepositRateHelper(liborRates[idx],liborTenors[idx],ycDef.fixingDays,ycDef.cal, ycDef.bdc, ycDef.endOfMonth, ycDef.dc));
                                instruments.push_back(rateHelper);
                        }
                        else {
                                        shared_ptr<RateHelper> rateHelper(new SwapRateHelper(liborRates[idx],liborTenors[idx], ycDef.cal, ycDef.freq, ycDef.bdc, ycDef.dc, swapIndex));
                                        instruments.push_back(rateHelper);
                        }
                }
  yieldCurve = shared_ptr<YieldTermStructure>(new PiecewiseYieldCurve<Discount, LogLinear, IterativeBootstrap>(ycDef.evalDate, instruments, ycDef.dc));
                        yieldCurve->enableExtrapolation();
        }
        else{
                for (int i = 0; i < size; i++){
                        curDate = ycDef.cal.advance(ycDef.evalDate, liborTenors[i]);
                        liborDates[i] = curDate;
                }
                yieldCurve = shared_ptr<YieldTermStructure>(new InterpolatedZeroCurve<LogLinear>(liborDates, liborRates, ycDef.dc, ycDef.cal));
                yieldCurve->enableExtrapolation();
        }
        return yieldCurve;
}


void GetZeroRates(const vector<Real> &liborRates, const vector<Period> &liborTenors, vector <Real> &zeroRates, YieldCurveDef &_ycDef){
        boost::shared_ptr<YieldTermStructure> ptrYiedCurve = MakeTermStructure(liborRates, liborTenors, _ycDef, true);
        int size = liborTenors.size();
        zeroRates.clear();
        zeroRates.resize(size);
        for (int i = 0; i < size; i++){
                zeroRates[i] = ptrYiedCurve->zeroRate(_ycDef.evalDate + liborTenors[i], _ycDef.dc, Compounded, Annual);
                //std::cout << "Rate = " << yieldCurve->forwardRate(_ycDef.evalDate + Period(1*Years), _ycDef.evalDate + Period(1*Years) + periods[i] , _ycDef.dc, Compounded, Annual) << std::endl;
        }

}


Program crashes when it hits zeroRate(...) function. Is it looking for discountImpl function implementation? I am using InterpolatedZeroCurve which provides the implementation for discountImpl and zeroYieldImpl functions.
Reply | Threaded
Open this post in threaded view
|

Re: Facing app crash with zeroRate function

Luigi Ballabio
Hello,
    I'll run your code when I have some time, but in the meantime you
can try catching the exception and printing out its message to see
what the library is telling you, as in:

try {
    GetZeroRates(...);
} catch (QuantLib::Error& e) {
    cout << e.what() << endl;
    throw;
}

Luigi


On Thu, Jan 16, 2014 at 6:48 AM, v17 <[hidden email]> wrote:

> Hi,
>
> Request members help on the below issue -
>
> I have following code in my project -
>
> boost::shared_ptr<YieldTermStructure>
> CYieldCurveUtils::MakeTermStructure(const vector<Rate> &liborRates, const
> vector<Period> &liborTenors, YieldCurveDef &_ycDef, bool zeroRates ){
>
>         ycDef = _ycDef;
>         ycDef.evalDate = ycDef.cal.advance(ycDef.evalDate, -2, Days);
>
>         const int size = liborRates.size();
>         std::vector<shared_ptr<RateHelper>> instruments;
>         Period oneYear(1, Years);
>         shared_ptr<Euribor> swapIndex(new Euribor(6 * Months));
>         vector<Date> liborDates(liborTenors.size());
>         Date curDate;
>         if (!zeroRates){
>                 for (int idx  = 0; idx  < size; idx++){
>                         if (liborTenors[idx] < oneYear){
>                                 shared_ptr<RateHelper> rateHelper(new
> DepositRateHelper(liborRates[idx],liborTenors[idx],ycDef.fixingDays,ycDef.cal,
> ycDef.bdc, ycDef.endOfMonth, ycDef.dc));
>                                 instruments.push_back(rateHelper);
>                         }
>                         else {
>                                         shared_ptr<RateHelper> rateHelper(new
> SwapRateHelper(liborRates[idx],liborTenors[idx], ycDef.cal, ycDef.freq,
> ycDef.bdc, ycDef.dc, swapIndex));
>                                         instruments.push_back(rateHelper);
>                         }
>                 }
>                         yieldCurve = shared_ptr<YieldTermStructure>(new
> PiecewiseYieldCurve<Discount, LogLinear, IterativeBootstrap>(ycDef.evalDate,
> instruments, ycDef.dc));
>                         yieldCurve->enableExtrapolation();
>         }
>         else{
>                 for (int i = 0; i < size; i++){
>                         curDate = ycDef.cal.advance(ycDef.evalDate, liborTenors[i]);
>                         liborDates[i] = curDate;
>                 }
>                 yieldCurve = shared_ptr<YieldTermStructure>(new
> InterpolatedZeroCurve<LogLinear>(liborDates, liborRates, ycDef.dc,
> ycDef.cal));
>                 yieldCurve->enableExtrapolation();
>         }
>         return yieldCurve;
> }
>
>
> void GetZeroRates(const vector<Real> &liborRates, const vector<Period>
> &liborTenors, vector <Real> &zeroRates, YieldCurveDef &_ycDef){
>         boost::shared_ptr<YieldTermStructure> ptrYiedCurve =
> MakeTermStructure(liborRates, liborTenors, _ycDef, true);
>         int size = liborTenors.size();
>         zeroRates.clear();
>         zeroRates.resize(size);
>         for (int i = 0; i < size; i++){
>                 zeroRates[i] = ptrYiedCurve->zeroRate(_ycDef.evalDate + liborTenors[i],
> _ycDef.dc, Compounded, Annual);
>                 //std::cout << "Rate = " << yieldCurve->forwardRate(_ycDef.evalDate +
> Period(1*Years), _ycDef.evalDate + Period(1*Years) + periods[i] , _ycDef.dc,
> Compounded, Annual) << std::endl;
>         }
>
> }
>
>
> Program crashes when it hits zeroRate(...) function. Is it looking for
> discountImpl function implementation? I am using InterpolatedZeroCurve which
> provides the implementation for discountImpl and zeroYieldImpl functions.
>
>
>
>
> --
> View this message in context: http://quantlib.10058.n7.nabble.com/Facing-app-crash-with-zeroRate-function-tp14853.html
> Sent from the quantlib-dev mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> CenturyLink Cloud: The Leader in Enterprise Cloud Services.
> Learn Why More Businesses Are Choosing CenturyLink Cloud For
> Critical Workloads, Development Environments & Everything In Between.
> Get a Quote or Start a Free Trial Today.
> http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
> _______________________________________________
> QuantLib-dev mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev



--
<https://implementingquantlib.blogspot.com>
<https://twitter.com/lballabio>

------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev
v17
Reply | Threaded
Open this post in threaded view
|

Re: Facing app crash with zeroRate function

v17
Hi Luigi,

I found the issue.

Code was incorrectly using PiecewiseYieldCurve, DepositeRate/SwapRateHelper instead of InterpolatedZeroCurve for zero rates

My bad. 

Sorry for that and thanks for looking :)

Thanks,
Varun Yadav


On Thursday, January 16, 2014 2:50 PM, Luigi Ballabio [via QuantLib] <[hidden email]> wrote:
Hello,
    I'll run your code when I have some time, but in the meantime you
can try catching the exception and printing out its message to see
what the library is telling you, as in:

try {
    GetZeroRates(...);
} catch (QuantLib::Error& e) {
    cout << e.what() << endl;
    throw;
}

Luigi


On Thu, Jan 16, 2014 at 6:48 AM, v17 <[hidden email]> wrote:

> Hi,
>
> Request members help on the below issue -
>
> I have following code in my project -
>
> boost::shared_ptr<YieldTermStructure>
> CYieldCurveUtils::MakeTermStructure(const vector<Rate> &liborRates, const
> vector<Period> &liborTenors, YieldCurveDef &_ycDef, bool zeroRates ){
>
>         ycDef = _ycDef;
>         ycDef.evalDate = ycDef.cal.advance(ycDef.evalDate, -2, Days);
>
>         const int size = liborRates.size();
>         std::vector<shared_ptr<RateHelper>> instruments;
>         Period oneYear(1, Years);
>         shared_ptr<Euribor> swapIndex(new Euribor(6 * Months));
>         vector<Date> liborDates(liborTenors.size());
>         Date curDate;
>         if (!zeroRates){
>                 for (int idx  = 0; idx  < size; idx++){
>                         if (liborTenors[idx] < oneYear){
>                                 shared_ptr<RateHelper> rateHelper(new
> DepositRateHelper(liborRates[idx],liborTenors[idx],ycDef.fixingDays,ycDef.cal,
> ycDef.bdc, ycDef.endOfMonth, ycDef.dc));
>                                 instruments.push_back(rateHelper);
>                         }
>                         else {
>                                         shared_ptr<RateHelper> rateHelper(new
> SwapRateHelper(liborRates[idx],liborTenors[idx], ycDef.cal, ycDef.freq,
> ycDef.bdc, ycDef.dc, swapIndex));
>                                         instruments.push_back(rateHelper);
>                         }
>                 }
>                         yieldCurve = shared_ptr<YieldTermStructure>(new
> PiecewiseYieldCurve<Discount, LogLinear, IterativeBootstrap>(ycDef.evalDate,
> instruments, ycDef.dc));
>                         yieldCurve->enableExtrapolation();
>         }
>         else{
>                 for (int i = 0; i < size; i++){
>                         curDate = ycDef.cal.advance(ycDef.evalDate, liborTenors[i]);
>                         liborDates[i] = curDate;
>                 }
>                 yieldCurve = shared_ptr<YieldTermStructure>(new
> InterpolatedZeroCurve<LogLinear>(liborDates, liborRates, ycDef.dc,
> ycDef.cal));
>                 yieldCurve->enableExtrapolation();
>         }
>         return yieldCurve;
> }
>
>
> void GetZeroRates(const vector<Real> &liborRates, const vector<Period>
> &liborTenors, vector <Real> &zeroRates, YieldCurveDef &_ycDef){
>         boost::shared_ptr<YieldTermStructure> ptrYiedCurve =
> MakeTermStructure(liborRates, liborTenors, _ycDef, true);
>         int size = liborTenors.size();
>         zeroRates.clear();
>         zeroRates.resize(size);
>         for (int i = 0; i < size; i++){
>                 zeroRates[i] = ptrYiedCurve->zeroRate(_ycDef.evalDate + liborTenors[i],
> _ycDef.dc, Compounded, Annual);
>                 //std::cout << "Rate = " << yieldCurve->forwardRate(_ycDef.evalDate +
> Period(1*Years), _ycDef.evalDate + Period(1*Years) + periods[i] , _ycDef.dc,
> Compounded, Annual) << std::endl;
>         }
>
> }
>
>
> Program crashes when it hits zeroRate(...) function. Is it looking for
> discountImpl function implementation? I am using InterpolatedZeroCurve which
> provides the implementation for discountImpl and zeroYieldImpl functions.
>
>
>
>
> --
> View this message in context: http://quantlib.10058.n7.nabble.com/Facing-app-crash-with-zeroRate-function-tp14853.html
> Sent from the quantlib-dev mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> CenturyLink Cloud: The Leader in Enterprise Cloud Services.
> Learn Why More Businesses Are Choosing CenturyLink Cloud For
> Critical Workloads, Development Environments & Everything In Between.
> Get a Quote or Start a Free Trial Today.
> http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
> _______________________________________________
> QuantLib-dev mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev


--
<https://implementingquantlib.blogspot.com>
<https://twitter.com/lballabio>

------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
QuantLib-dev mailing list
[hidden email]



If you reply to this email, your message will be added to the discussion below:
http://quantlib.10058.n7.nabble.com/Facing-app-crash-with-zeroRate-function-tp14853p14854.html
To unsubscribe from Facing app crash with zeroRate function, click here.
NAML


Reply | Threaded
Open this post in threaded view
|

Re: Facing app crash with zeroRate function

cheng li
In reply to this post by v17
Make sure that the zero rates dates are all later than the earliest date in your curve.

发自我的 iPad

> 在 2014年1月16日,13:48,v17 <[hidden email]> 写道:
>
> Hi,
>
> Request members help on the below issue -
>
> I have following code in my project -
>
> boost::shared_ptr<YieldTermStructure>
> CYieldCurveUtils::MakeTermStructure(const vector<Rate> &liborRates, const
> vector<Period> &liborTenors, YieldCurveDef &_ycDef, bool zeroRates ){
>
>    ycDef = _ycDef;
>    ycDef.evalDate = ycDef.cal.advance(ycDef.evalDate, -2, Days);
>
>    const int size = liborRates.size();
>    std::vector<shared_ptr&lt;RateHelper>> instruments;
>    Period oneYear(1, Years);
>    shared_ptr<Euribor> swapIndex(new Euribor(6 * Months));
>    vector<Date> liborDates(liborTenors.size());
>    Date curDate;
>    if (!zeroRates){
>        for (int idx  = 0; idx  < size; idx++){
>            if (liborTenors[idx] < oneYear){
>                shared_ptr<RateHelper> rateHelper(new
> DepositRateHelper(liborRates[idx],liborTenors[idx],ycDef.fixingDays,ycDef.cal,
> ycDef.bdc, ycDef.endOfMonth, ycDef.dc));
>                instruments.push_back(rateHelper);
>            }
>            else {
>                    shared_ptr<RateHelper> rateHelper(new
> SwapRateHelper(liborRates[idx],liborTenors[idx], ycDef.cal, ycDef.freq,
> ycDef.bdc, ycDef.dc, swapIndex));
>                    instruments.push_back(rateHelper);
>            }
>        }
>              yieldCurve = shared_ptr<YieldTermStructure>(new
> PiecewiseYieldCurve<Discount, LogLinear, IterativeBootstrap>(ycDef.evalDate,
> instruments, ycDef.dc));
>            yieldCurve->enableExtrapolation();
>    }
>    else{
>        for (int i = 0; i < size; i++){
>            curDate = ycDef.cal.advance(ycDef.evalDate, liborTenors[i]);
>            liborDates[i] = curDate;
>        }        
>        yieldCurve = shared_ptr<YieldTermStructure>(new
> InterpolatedZeroCurve<LogLinear>(liborDates, liborRates, ycDef.dc,
> ycDef.cal));
>        yieldCurve->enableExtrapolation();
>    }
>    return yieldCurve;
> }
>
>
> void GetZeroRates(const vector<Real> &liborRates, const vector<Period>
> &liborTenors, vector <Real> &zeroRates, YieldCurveDef &_ycDef){
>    boost::shared_ptr<YieldTermStructure> ptrYiedCurve =
> MakeTermStructure(liborRates, liborTenors, _ycDef, true);
>    int size = liborTenors.size();
>    zeroRates.clear();
>    zeroRates.resize(size);
>    for (int i = 0; i < size; i++){
>        zeroRates[i] = ptrYiedCurve->zeroRate(_ycDef.evalDate + liborTenors[i],
> _ycDef.dc, Compounded, Annual);
>        //std::cout << "Rate = " << yieldCurve->forwardRate(_ycDef.evalDate +
> Period(1*Years), _ycDef.evalDate + Period(1*Years) + periods[i] , _ycDef.dc,
> Compounded, Annual) << std::endl;
>    }
>
> }
>
>
> Program crashes when it hits zeroRate(...) function. Is it looking for
> discountImpl function implementation? I am using InterpolatedZeroCurve which
> provides the implementation for discountImpl and zeroYieldImpl functions.
>
>
>
>
> --
> View this message in context: http://quantlib.10058.n7.nabble.com/Facing-app-crash-with-zeroRate-function-tp14853.html
> Sent from the quantlib-dev mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> CenturyLink Cloud: The Leader in Enterprise Cloud Services.
> Learn Why More Businesses Are Choosing CenturyLink Cloud For
> Critical Workloads, Development Environments & Everything In Between.
> Get a Quote or Start a Free Trial Today.
> http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
> _______________________________________________
> QuantLib-dev mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev

------------------------------------------------------------------------------
CenturyLink Cloud: The Leader in Enterprise Cloud Services.
Learn Why More Businesses Are Choosing CenturyLink Cloud For
Critical Workloads, Development Environments & Everything In Between.
Get a Quote or Start a Free Trial Today.
http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev