finmath / finmath-lib

Mathematical Finance Library: Algorithms and methodologies related to mathematical finance.
Apache License 2.0
488 stars 168 forks source link

TimeDiscretization inconsistency #58

Closed delreluca closed 6 years ago

delreluca commented 6 years ago

TimeDiscretization states that it will round times to a certain tick size in the Javadoc. This is done inconsistently between the different constructors:

The ellipsis/double array constructor will not round the given times to tick size. The other constructors (boxed Double array, Set and ArrayList) will do that.

All constructors fail to remove duplicates though.

A unit test for the missing rounding can look like this:

public class TimeDiscretizationTest {

    private static double getHalfTickMore(double a) {
        return a + 0.5 / (365.0 * 24.0);
    }

    @Test
    public void testTickRoundingOfUnboxedArrayConstruction() {

        double a = 4.2;
        double identicalToA = getHalfTickMore(a);

        TimeDiscretization discretization = new TimeDiscretization(a, identicalToA);

        Assert.assertEquals(discretization.getTime(0), discretization.getTime(1), 0.0);
    }

    @Test
    public void testTickRoundingOfBoxedArrayConstruction() {

        double a = 4.2;
        double identicalToA = getHalfTickMore(a);

        TimeDiscretization discretization = new TimeDiscretization(new Double[] { a, identicalToA});

        Assert.assertEquals(discretization.getTime(0), discretization.getTime(1), 0.0);
    }
}

There is another inconsistency: The interface Set<> is accepted but List<> is not, although its implementation ArrayList<> is.

I will try and perform a pull request with the following fixes and unit tests soon:

Here are some other features that might be useful:

delreluca commented 6 years ago

Fixed by PR #60