[SciPy-User] UnivariateSpline broken?

Tony S Yu tsyu80@gmail....
Fri Jun 4 09:36:41 CDT 2010


On Jun 4, 2010, at 8:30 AM, Robert Elsner wrote:

> 
> Hello,
> 
> 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?
> 
> Cheers
> 
> #!/usr/bin/python
> 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?

-Tony




More information about the SciPy-User mailing list