Finish translation for InterpolatedDiscountCurve

Bug #1209521 reported by Richard Gomes
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
JQuantLib
In Progress
High
Richard Gomes

Bug Description

A potential user wrote a test case for class InterpolatedDiscountCurve and reported an ArrayIndexOutOfBoundsException.

We need to write a similar test case and make the needed fixes.

Below we can see the user report:
----------------------------------------------------------

Hi, I have written a simple example to use InterpolatedDiscountCurve to interpolate a point using the Linear interpolation method.
It gives me an ArrayBoundException. Here are the Dates and Discount Factors I am using:

jdate is 2008-09-04 // settle date
jdate is 2008-10-06
jdate is 2009-10-07
jdate is 2010-10-01
jdate is 2011-10-02

rate[0]=1.0 // reference rate
rate[1]=2.0999999999999996
rate[2]=3.0999999999999996
rate[3]=4.1
rate[4]=5.1

// COnstructor

InterpolatedDiscountCurve<Linear> curve=
new InterpolatedDiscountCurve<Linear>(jDates,
discounts,
dct,
(org.jquantlib.time.Calendar)UnitedStates.getCalendar(
UnitedStates.Market.GOVERNMENTBOND),
(Linear)ipol);

// trying to interpolate for date 2009-07-31:

curve.getDiscount(newDate)

// exception:

Exception:-4 java.lang.ArrayIndexOutOfBoundsException: -4
java.lang.ArrayIndexOutOfBoundsException: -4
at org.jquantlib.math.interpolation.LinearInterpolation.evaluateImpl(LinearInterpolation.java:108)
at org.jquantlib.math.interpolation.AbstractInterpolation.evaluate(AbstractInterpolation.java:129)
at org.jquantlib.termstructures.yieldcurves.InterpolatedDiscountCurve.discountImpl(InterpolatedDiscountCurve.java:143)
at org.jquantlib.termstructures.YieldTermStructure.getDiscount(YieldTermStructure.java:310)
at org.jquantlib.termstructures.YieldTermStructure.getDiscount(YieldTermStructure.java:302)
at com.knight.vv.quant.QuantTest.interpolateDC(QuantTest.java:77)
at com.knight.vv.quant.QuantTest.main(QuantTest.java:22)

I think its in the locate() method that is trying to locate the point to interpolate for but I don't see why it should be as the
date I am trying to interpolate for is WITHIN bounds of the dates I have passed.

Please help, I really would like to use JQuantLib for a project but so far I haven't been able to get a simple example as above to
work.

Thanks a lot,
-Sid.

=============
Relationships
=============
related to http://bugs.launchpad.net/bugs/jquantlib-129

Tags: translation
Revision history for this message
Dominik Holenstein (holdom) wrote :

This is the C++ code for locate() in interpolation.hpp:
protected:
            Size locate(Real x) const {
                if (x < *xBegin_)
                    return 0;
                else if (x > *(xEnd_-1))
                    return xEnd_-xBegin_-2;
                else
                    return std::upper_bound(xBegin_,xEnd_-1,x)-xBegin_-1;
            }

This is our code of locate() in JQuantLib (AbstractInterpolation.java):
protected int locate(double x) /* @ReadOnly */ {
        if (x <= vx[0])
            return 0;
        else if (x > vx[vx.length-1])
            return vx.length-2;
        else
            return Sorting.binarySearchFromTo(vx, x, 0, vx.length-1)-1;
    }

Question:
We have to change the line 'if (x <= vx[0])' to 'if(x < vx[0]', right?

Dominik

Revision history for this message
Richard Gomes (frgomes) wrote :

Hi Dominik,

I'm not sure whether my translation is correct or not. Maybe your observation is correct. I dont know how std::upper_bound behave exactly. Maybe test cases could point out some mistakes. At the moment, this method is being called by test cases but I'm not plenty confident on this code yet. Test coverage at: http://www.jquantlib.org/maven2/sites/jquantlib/cobertura/org.jquantlib.math.interpolations.AbstractInterpolation.html.

Maybe Gary could help us. He has better expertise on C++ and QuantLib.

In addition, this issue involves a little bit more effort on translation and code review. When I started the translation, we didnt have TypeToken and TypeReference yet. These tricky classes are needed in order to mimic a weird class hierarchy dictated by the way C++ templates are used. In a nutshell, a certain class "C" can extend class "A" or class "B" (!), depending on the way certain templates are used. Obviously this cannot be done easily in Java. I suppose that TypeToken or TypeReferennce could be used to mimic this behavior but I havent focused on this issue yet.

Cheers :)

Revision history for this message
Richard Gomes (frgomes) wrote :

Previous notes are mostly relevant to issue 129, which is already solved.

What is really relevant to this issue is:

When I started the translation, we didnt have TypeToken and TypeReference yet. These tricky classes are needed in order to mimic a weird class hierarchy dictated by the way C++ templates are used. In a nutshell, a certain class "C" can extend class "A" or class "B" (!), depending on the way certain templates are used. Obviously this cannot be done easily in Java. I suppose that TypeToken or TypeReferennce could be used to mimic this behavior but I havent focused on this issue yet.

Revision history for this message
Gary Kennedy (gkennedy) wrote :

The problem is the use if Sorting.binarySearchFromTo, it is not quite equivelent of std::upper_bound, specifically Sorting.binarySearchfromTo will return a negative number when the value is not found on the sorted vector. The javadoc on the return value says it more exactly...

"index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the list: the index of the first element greater than the key, or list.length, if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found."

for interest, there is also a simailar locate method on interpolation methods in Numerical Recipes in C.

Revision history for this message
Richard Gomes (frgomes) wrote :

Kicked to v0.1.4

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.