Fwd: Re: CreditDefaultSwap throws RuntimeError

View: New views
5 Messages — Rating Filter:   Alert me  

Fwd: Re: CreditDefaultSwap throws RuntimeError

by Jose Aparicio-Navarro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Was there a reason why we are not forcing the refDate to be before the val date
in the CDS engines?

Regards
Pepe


----- Forwarded message from Jose Aparicio-Navarro <japari@...> -----
    Date: Tue, 29 Sep 2009 12:47:51 +0200
    From: Jose Aparicio-Navarro <japari@...>
Reply-To: Jose Aparicio-Navarro <japari@...>
 Subject: Re: [Quantlib-users] CreditDefaultSwap throws RuntimeError
      To: eckuipers-web@...

Quoting eckuipers-web@...:

>
> hazard_rate_structure=ql.FlatHazardRate(
>     ql.QuoteHandle(ql.SimpleQuote(hazard_rate)), # quote handle
>     ql.ActualActual() # day counter
>     )
>
> issuer=ql.Issuer(
>     ql.RelinkableDefaultProbabilityTermStructureHandle(
>         hazard_rate_structure
>         ), # relinkable handle to default prob term struct
>     recovery_rate # recovery rate
>     )
>
> yield_term_structure=ql.FlatForward(
>     2, # settlement days
>     calendar, # calendar
>     rate, # rate
>     ql.ActualActual() # day counter
>     )
>

The FlatHR is (I assuming what the Py binds are doing) constructing a relative
date curve linked to the instance date with a 0 settlement delay.
The Yield term structure is doing the same with a 2 days settlement delay.

The engine requests DFs and Probabilities on coupon days and default days.

16th June 06 is a thursday, next coupon goes over a weekend, your TS jumps the
weekend, the prob not. The coupon is on the following Tuesday. The the engine is
asking on a past date, is my guess this is the first date this situation takes
place.
I was surprised it crashed on the 15th so I coded it and in C++ it crashes on
the 16th (Friday). It might be your output buffer not being flushed.


Date date(1,January, 2006);
date = TARGET().adjust(date, Following);
Date maturity(20,December, 2014);
Rate spread = 0.05;
Real recoveryRate = 0.4;
Rate hazardRate = 0.2;
Rate yieldRate = 0.03;

Schedule schedCds =
    MakeSchedule().from(date)
                  .to(maturity)
                  .withFrequency(Quarterly)
                  .withConvention(Following)
                  .withTerminationDateConvention(Following)
                  .withCalendar(TARGET())
                  .withRule(DateGeneration::TwentiethIMM)
   ;

CreditDefaultSwap our_cds(Protection::Buyer,
                      1., spread, schedCds,
                      Following,
                      Actual360()
                      );
Handle<DefaultProbabilityTermStructure> probability(
    boost::shared_ptr<DefaultProbabilityTermStructure>(new
        FlatHazardRate(0,  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            TARGET(),
            hazardRate, ActualActual())));

Handle<YieldTermStructure> yield_term_structure(
      boost::shared_ptr<FlatForward>(
              new FlatForward(0, //2,  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                TARGET(), yieldRate,
                ActualActual())));

boost::shared_ptr<CreditDefaultSwap::engine>
    HRengine_tst(boost::shared_ptr<MidPointCdsEngine>
        (new MidPointCdsEngine(probability,
                        recoveryRate, yield_term_structure, true
         )));

our_cds.setPricingEngine(HRengine_tst);

while(date < Date(24, September, 2009)) {
    Settings::instance().evaluationDate() = date;
    cout << date << " , " << our_cds.NPV() << endl;
    date = TARGET().advance(date, 1, Days);
}



Regards
Pepe


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users

----- End forwarded message -----



------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users

Re: [Quantlib-dev] Fwd: Re: CreditDefaultSwap throws RuntimeError

by Luigi Ballabio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 2009-09-29 at 13:38 +0200, Jose Aparicio-Navarro wrote:
> Was there a reason why we are not forcing the refDate to be before the val date
> in the CDS engines?

Before the evaluation date or the default date? If it's the evaluation
date, I wouldn't add a strict requirement as the engine worked for most
dates.  If it's the mid-period default date, we can think about it.  Do
we require it to be after the curve reference, or we adjust it a day or
two so that it falls on the reference date?

Luigi


> ----- Forwarded message from Jose Aparicio-Navarro <japari@...> -----
>     Date: Tue, 29 Sep 2009 12:47:51 +0200
>     From: Jose Aparicio-Navarro <japari@...>
> Reply-To: Jose Aparicio-Navarro <japari@...>
>  Subject: Re: [Quantlib-users] CreditDefaultSwap throws RuntimeError
>       To: eckuipers-web@...
>
> Quoting eckuipers-web@...:
>
> >
> > hazard_rate_structure=ql.FlatHazardRate(
> >     ql.QuoteHandle(ql.SimpleQuote(hazard_rate)), # quote handle
> >     ql.ActualActual() # day counter
> >     )
> >
> > issuer=ql.Issuer(
> >     ql.RelinkableDefaultProbabilityTermStructureHandle(
> >         hazard_rate_structure
> >         ), # relinkable handle to default prob term struct
> >     recovery_rate # recovery rate
> >     )
> >
> > yield_term_structure=ql.FlatForward(
> >     2, # settlement days
> >     calendar, # calendar
> >     rate, # rate
> >     ql.ActualActual() # day counter
> >     )
> >
>
> The FlatHR is (I assuming what the Py binds are doing) constructing a relative
> date curve linked to the instance date with a 0 settlement delay.
> The Yield term structure is doing the same with a 2 days settlement delay.
>
> The engine requests DFs and Probabilities on coupon days and default days.
>
> 16th June 06 is a thursday, next coupon goes over a weekend, your TS jumps the
> weekend, the prob not. The coupon is on the following Tuesday. The the engine is
> asking on a past date, is my guess this is the first date this situation takes
> place.
> I was surprised it crashed on the 15th so I coded it and in C++ it crashes on
> the 16th (Friday). It might be your output buffer not being flushed.
>
>
> Date date(1,January, 2006);
> date = TARGET().adjust(date, Following);
> Date maturity(20,December, 2014);
> Rate spread = 0.05;
> Real recoveryRate = 0.4;
> Rate hazardRate = 0.2;
> Rate yieldRate = 0.03;
>
> Schedule schedCds =
>     MakeSchedule().from(date)
>                   .to(maturity)
>                   .withFrequency(Quarterly)
>                   .withConvention(Following)
>                   .withTerminationDateConvention(Following)
>                   .withCalendar(TARGET())
>                   .withRule(DateGeneration::TwentiethIMM)
>    ;
>
> CreditDefaultSwap our_cds(Protection::Buyer,
>                       1., spread, schedCds,
>                       Following,
>                       Actual360()
>                       );
> Handle<DefaultProbabilityTermStructure> probability(
>     boost::shared_ptr<DefaultProbabilityTermStructure>(new
>         FlatHazardRate(0,  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>             TARGET(),
>             hazardRate, ActualActual())));
>
> Handle<YieldTermStructure> yield_term_structure(
>       boost::shared_ptr<FlatForward>(
>               new FlatForward(0, //2,  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>                 TARGET(), yieldRate,
>                 ActualActual())));
>
> boost::shared_ptr<CreditDefaultSwap::engine>
>     HRengine_tst(boost::shared_ptr<MidPointCdsEngine>
>         (new MidPointCdsEngine(probability,
>                         recoveryRate, yield_term_structure, true
>          )));
>
> our_cds.setPricingEngine(HRengine_tst);
>
> while(date < Date(24, September, 2009)) {
>     Settings::instance().evaluationDate() = date;
>     cout << date << " , " << our_cds.NPV() << endl;
>     date = TARGET().advance(date, 1, Days);
> }
>
>
>
> Regards
> Pepe
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry® Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9-12, 2009. Register now!
> http://p.sf.net/sfu/devconf
> _______________________________________________
> QuantLib-users mailing list
> QuantLib-users@...
> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>
> ----- End forwarded message -----
>
>
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry® Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9-12, 2009. Register now!
> http://p.sf.net/sfu/devconf
> _______________________________________________
> QuantLib-dev mailing list
> QuantLib-dev@...
> https://lists.sourceforge.net/lists/listinfo/quantlib-dev

--

The First Rule of Optimization: Don't do it.
The Second Rule of Optimization (For experts only): Don't do it yet.
-- Michael Jackson



------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users

Re: [Quantlib-dev] Fwd: Re: CreditDefaultSwap throws RuntimeError

by Jose Aparicio-Navarro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Quoting Luigi Ballabio <luigi.ballabio@...>:

> Before the evaluation date or the default date? If it's the evaluation
> date, I wouldn't add a strict requirement as the engine worked for most
> dates.  If it's the mid-period default date, we can think about it.  Do
> we require it to be after the curve reference, or we adjust it a day or
> two so that it falls on the reference date?
>

Yep, lets make it the TS ref date. Moving this

Date effectiveStartDate =
   (startDate <= today && today <= endDate) ? today : startDate;

into:

Date effectiveStartDate =
    (startDate <= settlementDate && settlementDate <= endDate) ?
        settlementDate : startDate;

works for both cds engines.

There will still be problems if:
 probability_->referenceDate() > discountCurve_->referenceDate()

I havent tested it much though.
Regards
Pepe


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users

Re: [Quantlib-dev] Fwd: Re: CreditDefaultSwap throws RuntimeError

by Luigi Ballabio :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, 2009-10-05 at 11:31 +0200, Jose Aparicio-Navarro wrote:

> Quoting Luigi Ballabio <luigi.ballabio@...>:
> Yep, lets make it the TS ref date. Moving this
>
> Date effectiveStartDate =
>    (startDate <= today && today <= endDate) ? today : startDate;
>
> into:
>
> Date effectiveStartDate =
>     (startDate <= settlementDate && settlementDate <= endDate) ?
>         settlementDate : startDate;
>
> works for both cds engines.

Except I'd leave the start date alone and correct the default date
instead, if possible. No?

Luigi


--

There is no opinion so absurd that some philosopher will not
express it.
-- Marcus Tullius Cicero, "Ad familiares"



------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users

Re: [Quantlib-dev] Fwd: Re: CreditDefaultSwap throws RuntimeError

by Jose Aparicio-Navarro :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Quoting Luigi Ballabio <luigi.ballabio@...>:

> On Mon, 2009-10-05 at 11:31 +0200, Jose Aparicio-Navarro wrote:
> > Quoting Luigi Ballabio <luigi.ballabio@...>:
> > Yep, lets make it the TS ref date. Moving this
> >
> > Date effectiveStartDate =
> >    (startDate <= today && today <= endDate) ? today : startDate;
> >
> > into:
> >
> > Date effectiveStartDate =
> >     (startDate <= settlementDate && settlementDate <= endDate) ?
> >         settlementDate : startDate;
> >
> > works for both cds engines.
>
> Except I'd leave the start date alone and correct the default date
> instead, if possible. No?
>
> Luigi
>
Right, not to mix things. But theres a potential problem with

Probability P = probability_->defaultProbability(
                    effectiveStartDate, ///<<<<<
                    endDate);
if "effectiveStartDate" lies before the probability curve reference date (I am
thinking: startAccrual < refProb < refTS < endAccrual). Now negative times come
from the probability TS instead.
We can add :

defaultEffectiveStartPeriod =
    effectiveStartDate < refProb ? refProb : effectiveStartDate;

Regards
Pepe

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
QuantLib-users@...
https://lists.sourceforge.net/lists/listinfo/quantlib-users