splrep/splev versus interp1d

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

splrep/splev versus interp1d

Johann Rohwer
Hi,

What is the difference (in terms of the underlying algorithms) between
splines generated by sp.interpolate.interp1d and sp.interpolate.splrep/splev?

As an example, when I do a simple spline interpolation of a sparsely sampled
sin function, the two methods give me close but not identical results
(especially close to the bounds of the x data the interpolated values differ).

Code:
import scipy as sp
sp.pkgload('interpolate')
x=sp.linspace(0,10,11)
y=sp.sin(x)
x2=sp.linspace(0,10,201)
tck=sp.interpolate.splrep(x,y,k=3)
y2=sp.interpolate.splev(x2,tck)
f=sp.interpolate.interp1d(x,y,kind=3)
y3=f(x2)

y2 and y3 differ!

Regards
Johann
_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: splrep/splev versus interp1d

Stéfan van der Walt
Hi Johann

If I interpolate on points including the originals, i.e.

x2 = sp.linspace(0,10,200,endpoint=False)

then I see that, for both methods, they at least go through those points:

n [147]: for nr,i in enumerate(x):
    print y[nr] - y2[x2 == i]
   .....:
   .....:
[  3.01232352e-18]
[ -1.11022302e-16]
[ -1.11022302e-16]
[  2.77555756e-17]
[ 0.]
[ -3.33066907e-16]
[ 0.]
[ 0.]
[  1.11022302e-16]
[  2.22044605e-16]
[]

In [149]: for nr,i in enumerate(x):
    print y[nr] - y3[x2 == i]
   .....:
   .....:
[ -7.49400542e-16]
[  6.66133815e-16]
[  6.66133815e-16]
[ -1.38777878e-16]
[  3.33066907e-16]
[  8.88178420e-16]
[ -1.11022302e-16]
[  4.44089210e-16]
[  2.22044605e-16]
[ -3.88578059e-16]
[]

Maybe the two algorithms use different smoothness measures?  I'm
interested to know why this happens, but I don't have more time to
look at it right now, unfortunately.

Thanks
Stéfan

2008/5/21 Johann Rohwer <[hidden email]>:

> Hi,
>
> What is the difference (in terms of the underlying algorithms) between
> splines generated by sp.interpolate.interp1d and sp.interpolate.splrep/splev?
>
> As an example, when I do a simple spline interpolation of a sparsely sampled
> sin function, the two methods give me close but not identical results
> (especially close to the bounds of the x data the interpolated values differ).
>
> Code:
> import scipy as sp
> sp.pkgload('interpolate')
> x=sp.linspace(0,10,11)
> y=sp.sin(x)
> x2=sp.linspace(0,10,201)
> tck=sp.interpolate.splrep(x,y,k=3)
> y2=sp.interpolate.splev(x2,tck)
> f=sp.interpolate.interp1d(x,y,kind=3)
> y3=f(x2)
>
> y2 and y3 differ!
>
> Regards
> Johann
> _______________________________________________
> SciPy-user mailing list
> [hidden email]
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user
Reply | Threaded
Open this post in threaded view
|

Re: splrep/splev versus interp1d

Travis Oliphant-5
In reply to this post by Johann Rohwer
Johann Rohwer wrote:
> Hi,
>
> What is the difference (in terms of the underlying algorithms) between
> splines generated by sp.interpolate.interp1d and sp.interpolate.splrep/splev?
>  
The former does not use any "smoothing" while the latter does use
"smoothing".     Interpolation using splines for order greater than 1
actually requires additional constraints to be made as there are more
degrees of freedom left after specifying continuity up to the (k-1)st
derivative (for order k).

The splrep functions use a smoothing constraint which I find useless
unless you are interpolating noisy "data" (you can set the smoothing
constraint to 0 and then I'm not sure what additional constraint it is
using to define a unique spline).

The interp1d function which uses low-level spline interpolation
functions by default uses a constraint that enforces "minimial"
discontinuity in the kth-derivative.  So, yes, they will return
non-identical results and this is to be expected.

Interpolations for k>1 depends on the additional assumptions you add and
there is a large number of possibilities.   I'd like to flesh this out a
bit better so it is clear what additional constraints are available.

-Travis

_______________________________________________
SciPy-user mailing list
[hidden email]
http://projects.scipy.org/mailman/listinfo/scipy-user