Closed tomwhoiscontrary closed 6 years ago
The (non-monotonic) Akima approximation also produces NaNs in this case, although only half as many. Monotonic Akima is fine.
Definitely not expected. Thanks for the heads-up.
If you (or anybody else) have time to debug the issue, I'd be grateful.
I'll try to make time to dig into this - it should be simple to track down. However, like everyone, i have lots of other priorities, so i can't make any promises about when i'll get to it!
Of course. Thanks!
Hi, I took a quick look last night.
I’m not an expert on interpolation but first should we make sure that it’s assumed f(x) is monotone?
Seems like Fritsch-Butland interpolation should have at least non-strict monotone assumption. See MONOTONE PIECEWISE CUBIC INTERPOLATION.
So we should make data below work:
x | y |
---|---|
0 | 10 |
2 | 10 |
3 | 10 |
5 | 10 |
6 | 10 |
8 | 10 |
9 | 10.5 |
11 | 15 |
12 | 50 |
14 | 60 |
15 | 85 |
And the root cause is when calling update() in impl, tmp_ array's calculation involves DIV/0 issue.
So questions:
In quantlib, is Fritsch-Butland designed only for monotone data?
If so, it should only be strict monotone or equality is acceptable in the sequence?
@Grant6899 raises a point here: what does "monotonic" mean? Hyman's 1983 paper Accurate Monotonicity Preserving Cubic Interpolation talks about local monotonicity - if there is some range of the input data which is monotonic, then it should also be monotonic in the interpolation. In other words, Hyman's algorithm works on my original data. Or am i confused?
So, i had assumed that this is what we mean by "monotonic" for all the interpolation methods. Is that not the case? Does Fritsch-Butland require the input data to be globally monotonic?
I note that the comment on the CubicInterpolation class mentions:
While some non-linear schemes (Modified Parabolic, Fritsch-Butland, Kruger) are guaranteed to be locally monotonic in their original approximation, all other schemes must be filtered according to the Hyman criteria at the expense of their linearity.
Which certainly suggests to me that Fritsch-Butland is locally, rather than globally, monotonic.
If so, i would suggest that the first change is to clarify the documentation, and to add asserts checking that the input data is indeed suitable.
I note that if you jitter the y values in my example very slightly:
std::vector<double> knot_ys = {1, 2, 1.000002, 1.000001, 1.000003};
Then Fritsch-Butland interpolates this quite happily. Well, i say happily; the values are not NaN, but are very silly indeed.
I think this is a classic numerical stability issue. I bet that somewhere in the algorithm, we are taking the difference between successive y values, and then dividing something by it. When they are the same, that makes a NaN; when they are very close, that makes some oversized values. If we can find where that is happening, we can see if we can reformulate that step in more stable terms.
We can track down the troublesome step by breaking the algorithm into a sequence of elementary arithmetic operations, and checking each one for NaNity. This is something so simple even i can do it - but it's tedious enough that i haven't found time to do it yet!
I also had assumed that the interpolation preserves local monotonicity, but doesn't require it globally.
I am trying to do some cubic interpolation. In some cases, the function i am interpolating has a constant value across its domain except at one point. Like this:
I would expect any monotone interpolation of these values to produce a value that rises between 0 and 1, falls between 1 and 2, and is constant between 2 and 4. Indeed, this is what i see using Kruger, harmonic, monotone spline, or monotone Akima derivative approximations. However, using Fritsch-Butland approximation, the value rises between 0 and 1, falls between 1 and 2, and is NaN between 2 and 4!
Should Fritsch-Butland approximation work in this situation, or does it have limitations i'm not aware of?
Here's the test code i'm using:
This produces this output: