CreditDefaultSwap throws RuntimeError

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

CreditDefaultSwap throws RuntimeError

eckuipers-web
Hello all
I started playing with the CreditDefaultSwap instrument in Python today. It seems to do a good job. I compared it to the ISDA CDS Converter and it is always within 0.5 of the expected price for their test runs (quoted in points upfront, par = 100, so 0.5 is pretty close to the bid-ask). However when i tried to create some timeseries of CDS prices i noticed that for some dates it would not calculate and it throws a RuntimeError.

I am on windows. My Python tabs are 4 spaces. I tried to recreate the problem in C++ but my compiling and linking skills are limited so no luck there so far.

***************** python code*****************
#! /usr/bin/python

import QuantLib as ql

calendar=ql.TARGET()
date=ql.Date(1,1,2006)
date=calendar.adjust(date,ql.Following)
maturity=ql.Date(20,12,2014)
spread=0.05
recovery_rate=0.4
hazard_rate=0.2
rate=0.03

schedule=ql.Schedule(
    date, # effective date
    maturity, # termination date
    ql.Period(ql.Quarterly), # period
    calendar, # calendar
    ql.Following, # business day convention
    ql.Following, # business day convention termination date
    ql.DateGeneration.TwentiethIMM, # date generation rule
    False # not end of month
    )

cds=ql.CreditDefaultSwap(
    ql.Protection.Buyer, # side
    1, # nominal
    spread, # spread or coupon
    schedule, # schedule
    ql.Following,  # payment convention (for fixed leg)
    ql.Actual360() # day counter (for fixed leg)
    )

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
    )

engine=ql.MidPointCdsEngine(
    issuer, # issuer
    ql.RelinkableYieldTermStructureHandle(
        yield_term_structure
        ) # relinkable handle to yield term struct
    )

cds.setPricingEngine(engine)

while date<ql.Date(24,9,2009):
    ql.Settings.instance().evaluationDate=date
    print date,cds.NPV()
    date=calendar.advance(date,1,ql.Days)

***************** program output *****************

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
   
IDLE 1.2.4      ==== No Subprocess ====
>>>
January 2nd, 2006 0.263658107802
January 3rd, 2006 0.263491636412
January 4th, 2006 0.263332772338
January 5th, 2006 0.2632096056
January 6th, 2006 0.263050559541
January 9th, 2006 0.262515408333
January 10th, 2006 0.262356076546
January 11th, 2006 0.262189769632
January 12th, 2006 0.262073358828
January 13th, 2006 0.261907066107
January 16th, 2006 0.261378348767
January 17th, 2006 0.261212166923
January 18th, 2006 0.261052210651
January 19th, 2006 0.26092895973
January 20th, 2006 0.260768820861
January 23rd, 2006 0.260233536448
January 24th, 2006 0.260073111165
January 25th, 2006 0.259907098378
January 26th, 2006 0.259789218202
January 27th, 2006 0.259623220806
January 30th, 2006 0.259092986143
January 31st, 2006 0.258927101884
February 1st, 2006 0.258766050391
February 2nd, 2006 0.258642722424
February 3rd, 2006 0.258481487725
February 6th, 2006 0.257946084166
February 7th, 2006 0.257784562374
February 8th, 2006 0.257618851815
February 9th, 2006 0.25749949834
February 10th, 2006 0.257333804389
February 13th, 2006 0.256802055441
February 14th, 2006 0.256636476915
February 15th, 2006 0.256474327203
February 16th, 2006 0.25635092944
February 17th, 2006 0.256188595913
February 20th, 2006 0.255653087506
February 21st, 2006 0.255490466215
February 22nd, 2006 0.255325066095
February 23rd, 2006 0.255204235423
February 24th, 2006 0.255038853145
February 27th, 2006 0.254505593102
February 28th, 2006 0.254340328569
March 1st, 2006 0.25417707766
March 2nd, 2006 0.254053617469
March 3rd, 2006 0.253890182141
March 6th, 2006 0.25335458342
March 7th, 2006 0.253190859664
March 8th, 2006 0.253025778305
March 9th, 2006 0.25290346657
March 10th, 2006 0.252738404303
March 13th, 2006 0.252203636509
March 14th, 2006 0.252038694338
March 15th, 2006 0.25187433928
March 16th, 2006 0.261130819848
March 17th, 2006 0.261295419891
March 20th, 2006 0.26174681239
March 21st, 2006 0.261587593079
March 22nd, 2006 0.261419247638
March 23rd, 2006 0.261302821423
March 24th, 2006 0.261134485964
March 27th, 2006 0.260604068102
March 28th, 2006 0.260435836088
March 29th, 2006 0.260275987339
March 30th, 2006 0.260150554409
March 31st, 2006 0.259990521833
April 3rd, 2006 0.259451350546
April 4th, 2006 0.259291029036
April 5th, 2006 0.25912295047
April 6th, 2006 0.259005043915
April 7th, 2006 0.258836976483
April 10th, 2006 0.258304998685
April 11th, 2006 0.258137036874
April 12th, 2006 0.258060912109
April 13th, 2006 0.25789293432
April 18th, 2006 0.256988628348
April 19th, 2006 0.256820824453
April 20th, 2006 0.256701433431
April 21st, 2006 0.256533641837
April 24th, 2006 0.256000106557
April 25th, 2006 0.255832422791
April 26th, 2006 0.255670362851
April 27th, 2006 0.25556572789
April 28th, 2006 0.255403469522
May 2nd, 2006 0.254680423781
May 3rd, 2006 0.25451290246
May 4th, 2006 0.254392022867
May 5th, 2006 0.254224515031
May 8th, 2006 0.253689424865
May 9th, 2006 0.253522027092
May 10th, 2006 0.253358856837
May 11th, 2006 0.253233124517
May 12th, 2006 0.25306976854
May 15th, 2006 0.252530096058
May 16th, 2006 0.252366448998
May 17th, 2006 0.25219921826
May 18th, 2006 0.252076846022
May 19th, 2006 0.251909629971
May 22nd, 2006 0.251372987662
May 23rd, 2006 0.251205883939
May 24th, 2006 0.251041600251
May 25th, 2006 0.25091578224
May 26th, 2006 0.250751312203
May 29th, 2006 0.250211500395
May 30th, 2006 0.250046738573
May 31st, 2006 0.249879806539
June 1st, 2006 0.249755937609
June 2nd, 2006 0.249589021479
June 5th, 2006 0.249050829919
June 6th, 2006 0.248884028412
June 7th, 2006 0.248718628197
June 8th, 2006 0.248592731738
June 9th, 2006 0.248427144549
June 12th, 2006 0.247887207677
June 13th, 2006 0.247721328008
June 14th, 2006 0.247554702906
June 15th, 2006
Traceback (most recent call last):
  File "C:\Devel\src\koert\simple_ql\issue.py", line 64, in <module>
    print date,cds.NPV()
  File "build\lib.win32-2.5\QuantLib\QuantLib.py", line 3336, in NPV
RuntimeError: negative time (-0.00547945) given
>>>

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; 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&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CreditDefaultSwap throws RuntimeError

japari
Quoting [hidden email]:

>
> 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&reg; 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&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: CreditDefaultSwap throws RuntimeError

japari
Sorry I was too quick with the send button,

What the debugger shows: (the dates are different)

38719 : accrual start
38792 : effective start date AND "Today" (Thursday)
38796 : coupon date, AND Yield TS ref date. (Monday)

--> mid point default date : 38794

So if we accrue defaults at default dates we are going to request a DF on that
date.

Regards
Pepe

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; 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&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users