[SciPy-User] UnivariateSpline broken?
Fri Jun 4 09:54:18 CDT 2010
On Fri, Jun 4, 2010 at 10:36 AM, Tony S Yu <firstname.lastname@example.org> wrote:
> On Jun 4, 2010, at 8:30 AM, Robert Elsner wrote:
>> the UnivariateSpline implementation in scipy.interpolate seems to be
>> broken (tested on 0.7). It just produces garbage for some use cases
>> especially with logarithmic spacing of the x values.
>> A sample script to illustrate the problem is here. Am I misusing the
>> code or is it a bug?
>> import numpy as np
>> from scipy.interpolate import UnivariateSpline, splrep, splev
>> # Works as expected
>> x = np.logspace(-4, 1)
>> y = x**2
>> sp_1 = UnivariateSpline(x,y,k=3)
>> print np.all((sp_1(x) - y) < 1e-10 )
>> # Doesn't work as expected
>> y = np.sin(x)
>> sp_2 = UnivariateSpline(x,y,k=3)
>> print np.all((sp_2(x) - y) < 1e-10 )
>> # Works if using low-level routines
>> tck = splrep(x,y,k=3)
>> print np.all((splev(x, tck) - y) < 1e-10 )
> It appears that the default behavior for UnivariateSpline is to smooth the input data. You can fix your example above with the following:
> sp_2 = UnivariateSpline(x,y,k=3,s=0)
> This default behavior isn't obvious since the default value for the smoothing factor, `s`, is set to None, and the docstring doesn't mention what happens when `s = None`. This behavior is particularly weird because `splrep` also uses `s = None` as a default, but this None value gets changed to `s=0` (I don't know what UnivariateSpline does with `s=None` since some magic happens within Fortran code).
> Do any devs know if this difference in default behaviors is intentional?
I think the behavior of UnivariateSpline overall is a bit weird (?),
because it does confusing delegation to the subclasses. But, I think,
nobody has done a systematic review of the spline classes recently.
Hopefully, the docmarathon gets around to clean up the documentation
for the spline classes.
> SciPy-User mailing list
More information about the SciPy-User