Bugs in TermStructure::zeroCoupon() discussed on www.Wilmott.com

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

Bugs in TermStructure::zeroCoupon() discussed on www.Wilmott.com

Liguo Song
Hi, Nando,

I noticed the discussion about the 0/0 bug in the TermStructure::zeroCoupon() on
the forum at www.Wilmott.com. So, I checked the source code and find that the
following code is problematic.

     inline Rate TermStructure::zeroCoupon(Time t, int f,
         bool extrapolate) const {
             DiscountFactor df = discountImpl(t, extrapolate);
             if (f > 0 && t <= (1.0/(double)f))
                return Rate((1.0/df-1.0)/t);
             if (t == 0.0)
                t = 1.0;
             return Rate((QL_POW(1.0/df,1.0/(t*f))-1.0)*f);
     }

The "if (t == 0.0) " will never be processed as as t <= 1.0/(double)f is always
true when f > 0 and t=0.0. Thus, 0/0 or x/0 will happen as long as f > 0 and t =
0.0.

I am just quite curious about why it didn't happen to any compiler other than
Borland.

I am not knowledgable enough to offer any remedy here. Anyone up for a patch?


Later.


Liguo



Reply | Threaded
Open this post in threaded view
|

Re: Bugs in TermStructure::zeroCoupon() discussed on www.Wilmott.com

Luigi Ballabio-2
At 03:00 PM 9/9/03 -0500, Liguo Song wrote:

>I noticed the discussion about the 0/0 bug in the
>TermStructure::zeroCoupon() on the forum at www.Wilmott.com. So, I checked
>the source code and find that the following code is problematic.
>
>     inline Rate TermStructure::zeroCoupon(Time t, int f,
>         bool extrapolate) const {
>             DiscountFactor df = discountImpl(t, extrapolate);
>             if (f > 0 && t <= (1.0/(double)f))
>                return Rate((1.0/df-1.0)/t);
>             if (t == 0.0)
>                t = 1.0;
>             return Rate((QL_POW(1.0/df,1.0/(t*f))-1.0)*f);
>     }
>
>The "if (t == 0.0) " will never be processed as as t <= 1.0/(double)f is
>always true when f > 0 and t=0.0. Thus, 0/0 or x/0 will happen as long as
>f > 0 and t = 0.0.

Hmm. The "if (t == 0.0)" part doesn't make much sense to me either. Why
change t to 1.0 if 0.0 was requested? Andre', any insight?


>I am just quite curious about why it didn't happen to any compiler other
>than Borland.

I'm just as puzzled.


>I am not knowledgable enough to offer any remedy here. Anyone up for a patch?

I'd try something like:

     inline Rate TermStructure::zeroCoupon(Time t, int f,
                                          bool extrapolate) const {
         if (t == 0.0)
             return forwardImpl(0.0);
        DiscountFactor df = discountImpl(t, extrapolate);
        if (f > 0 && t <= (1.0/f))
             return Rate((1.0/df-1.0)/t);
         else
            return Rate((QL_POW(1.0/df,1.0/(t*f))-1.0)*f);
     }

Later,
         Luigi