Slightly different result using InterpolatedUnivariateSpline and interp1d
<p>I was using <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html" rel="nofollow">interp1d</a> to fit a cubic spline but ran into some memmory issues, so as per the following <a href="http://stackoverflow.com/questions/21435648/cubic-spline-memory-error/21440722#21440722">question</a> I have switched to using <a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.InterpolatedUnivariateSpline.html" rel="nofollow">InterpolatedUnivariateSpline</a>. However, I have noticed that there are some (very) small differences between the resulting functions. My questions therefore are; </p>

<p><strong>A.</strong> What causes the difference, as far as I can tell it has to do with the underlying methodology (using FITPACK or not) as per this <a href="http://stackoverflow.com/a/6236482/1093485">answer</a>. However, should the underlying math not be the same?</p>

<p><strong>B.</strong> Is it possible to reproduce the interp1d results using InterpolatedUnivariateSpline (altering the smoothing spline degree or boundaries only made the two graphs even more different)?</p>

<p>Minimal code to reproduce the slight difference:</p>

<pre><code>from scipy.interpolate import interp1d
from scipy.interpolate import InterpolatedUnivariateSpline
import matplotlib.pyplot as plt
import matplotlib
import numpy

x = [916.03189697265634, 916.0718969726563, 916.11189697265627, 916.15189697265623, 916.1918969726562, 916.23189697265627, 916.27189697265624, 916.31189697265631, 916.35189697265628, 916.39189697265624, 916.4318969726562, 916.47189697265628, 916.51189697265625, 916.55189697265632, 916.59189697265629, 916.63189697265625, 916.67189697265621, 916.71189697265618]
y = [893483.0, 2185234.0, 3903053.0, 4264327.0, 3128900.0, 1374942.0, 554350.0, 442512.0, 414232.0, 403098.0, 413778.0, 264185.0, 363063.0, 473762.0, 452284.0, 526806.0, 461402.0, 424270.0]
newX = numpy.linspace(x[0],x[-1],2500*(x[-1]-x[0]))

f_interp1d = interp1d(x,y, kind='cubic')
f_Univariate = InterpolatedUnivariateSpline(x,y)
yINTER = f_interp1d(newX)
yUNIVAR = f_Univariate(newX)
fig =  plt.figure()
ax = fig.add_subplot(111)
plt.plot(x,y,'b*')
plt.plot(newX,yINTER,'r--')
plt.plot(newX,yUNIVAR,'g--')
plt.legend(['Raw Data','Interp1d','Univariate Spline'],loc='best')
plt.show()
</code></pre>

<p>Yielding the following graph (seemingly fine):</p>

<p><a href="http://i.stack.imgur.com/UaCUe.png" rel="nofollow"><img src="http://i.stack.imgur.com/UaCUe.png" alt="enter image description here"></a></p>

<p>However, a close in view shows that there is a difference:</p>

<p><a href="http://i.stack.imgur.com/ZVAZz.png" rel="nofollow"><img src="http://i.stack.imgur.com/ZVAZz.png" alt="enter image description here"></a></p>

<p>I have found that the main difference is that InterpolatedUnivariateSpline attempts to perform a continuous fit while cubic interp1d applies a piecewise fit. </p>

<p>The only solution that I have come up with (for now) is to ensure that both functions only use 4 data points (around the highest data point), as both functions will yield a single solution (instead of a single solution versus two partial solutions if using 5 data points).</p>

<p>snippet:</p>

<pre><code># Strip top point
maxInt = 0
for index,i in enumerate(y):
    if i &gt; maxInt:
        maxInt = i
x_sub = x[y.index(maxInt)-2:y.index(maxInt)+2]
y_sub = y[y.index(maxInt)-2:y.index(maxInt)+2]
newX = numpy.linspace(x_sub[0],x_sub[-1],2500*(x_sub[-1]-x_sub[0]))

f_interp1d = interp1d(x_sub,y_sub, kind='cubic')
f_Univariate = InterpolatedUnivariateSpline(x_sub,y_sub)
yINTER = f_interp1d(newX)
yUNIVAR = f_Univariate(newX)
fig =  plt.figure()
ax = fig.add_subplot(111)
plt.plot(x,y,'b*')
plt.plot(newX,yINTER,'r--')
plt.plot(newX,yUNIVAR,'g--')
plt.legend(['Raw Data','Interp1d','Univariate Spline'],loc='best')
plt.show()
</code></pre>

<p>This yields the following graphs (zoomed out):</p>

<p><a href="http://i.stack.imgur.com/wPy1l.png" rel="nofollow"><img src="http://i.stack.imgur.com/wPy1l.png" alt="enter image description here"></a></p>

<p>The close-up shows that both functions truly are 'identical':</p>

<p><a href="http://i.stack.imgur.com/VaZV2.png" rel="nofollow"><img src="http://i.stack.imgur.com/VaZV2.png" alt="enter image description here"></a></p>

<p>However, I am still hoping that there is a better method to force both functions to produce similar behaviour.</p>

python scipy curve-fitting