Re: Small bug in inflationPeriod()

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Small bug in inflationPeriod()

Chris Kenyon-2

Hi Niall, and all,

thanks for catching this.  Fix+test now in SVN on R1000 branch, and trunk.

Best regards,
Chris

------------------------------

Message: 5
Date: Wed, 10 Nov 2010 15:59:12 +0000
From: Niall O'Sullivan <[hidden email]>
Subject: [Quantlib-dev] Small bug in inflationPeriod() in
    termstructures/inflationtermstructure.cpp
To: [hidden email]
Message-ID:
    <[hidden email]>
   
Content-Type: text/plain; charset="us-ascii"



Hi all,

I believe there is a small bug in the function inflationPeriod() in
infaltiontermstructure.cpp.

The following example program fails in QL 1.0.1.

#include<ql/termstructures/inflationtermstructure.hpp>
using namespace QuantLib;
int main () {
  Date d = Date(1,Dec,2009);
  Frequency f = Quarterly;
  std::pair<Date,Date> res = inflationPeriod (d,f);
  return 0;
}

with the error message:
terminate called after throwing an instance of 'Quantlib::Error'
  what (): month 14 outside January-December range [1,12]

The problem occurs when the month is December and the frequency is either
Quarterly or Semiannual (for some other months you don't get an exception,
but the function returns the incorrect period). We have the following in
inflationtermstructure.cpp:

  case Quarterly:
    startMonth = Month(3*(month-1)/3 + 1);
    endMonth = Month(startMonth + 2);

When month is 12 then startMonth is (incorrectly) set to
  3*(12-1)/3+1 = 12
and endMonth is set to 14 which ultimately causes the above exception.

The bug is the order of evaluation which should be something like
3*((month-1)/3)+1
giving us startMonth = 3*((12-1)/3)+1 = 10 and thus endMonth = 12.

The Semiannual block of code has the same problem, to fix both I propose
adding 2 pairs of brackets as below:

<  startMonth = Month(6*(month-1)/6 + 1);
<  startMonth = Month(3*(month-1)/3 + 1);
--
>  startMonth = Month(6*((month-1)/6) + 1);
>  startMonth = Month(3*((month-1)/3) + 1);

Regards,
Niall.


------------------------------------------------------------------------------
Centralized Desktop Delivery: Dell and VMware Reference Architecture
Simplifying enterprise desktop deployment and management using
Dell EqualLogic storage and VMware View: A highly scalable, end-to-end
client virtualization framework. Read more!
http://p.sf.net/sfu/dell-eql-dev2dev
_______________________________________________
QuantLib-dev mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-dev