pythonscipyinterpolationcurve-fittingcdf

Applying Horizontal Shifts to interpolated function in Python


advance notice: Hi, I don't say that I have A problem, and I kinda can solve the thing I'm dealing with 'and I will show my silly way of doing that', but I want to know if there is an easy way or other solutions "maybe curve fitting" that requires another methods, packages or modules.

I'm looking for other options and solutions because my original code that I'm working on is big and I'm looking for every trick to simplify it.

Description:

I have a set of points with x & y coordinates that I preformed interpolation using scipy.interpolate.interp1d method on them. after that the result would be of a Function that I want to shift along the x axis.

Edit: I know now that I can apply the shift to the resulting array after I specify the points, but I want to keep the result of type interp1d because I'm doing more function related stuff (ex: transformations, integrating). and sometimes I need specific points and I don't care about the whole curve, and in others I just need the whole curve.

so having array from the start gets in the way of the intended flow of the code.

example:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

# some set of x and y points
x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 4, 7, 8])

# interpolation function
func = interpolate.interp1d(x, y, kind='quadratic', bounds_error=False, fill_value=(y[0], y[-1]))

x_samp = np.linspace(x[0] - x.mean(), x[-1] + x.mean())
y_samp = func(x_samp)

plt.plot(x, y, 'o', label='points')
plt.plot(x_samp, y_samp, '-', label='function')
plt.legend()
plt.show()

Points & interpolated function

now after that I had my function, I want to apply a horizontal shift. I searched everywhere for how to apply it, but the only way that I found referred to applying the shift on the x array, and then reapply interp1d(x_new, y)

but before that I do that, is there some easy trick that could be in Scipy or other packages that I could not see or find? I need to find such thing for curiosity.

applying the shift on the x array, and then reapply interp1d(x_new, y):

def shifting_func(x, y, shift):
    x_shift = x + shift
    function = interpolate.interp1d(x_shift, y, kind='quadratic', bounds_error=False, fill_value=(y[0], y[-1]))
    return function

func_shift = shifting_func(x, y, 2)

y_new = func_shift(x_samp)

plt.plot(x, y, 'o', label='points')
plt.plot(x_samp, y_samp, '-', label='function')
plt.plot(x_samp, y_new, '--', label='applying shift')
plt.legend()
plt.show()

Points & interpolated function & shifted function

as you can see the shift is applied, but I still feel it kinda hassle, is there any other way?

my original points do have the look of cumulative density function and mayby they follow GEV_CDF, so How can I benefit of that?


Solution

  • You don't need to shift the data and interpolate the function again. If all you want to do is evaluate the function, then you can wrap the function in a lambda function (or a regular function) that will shift the x value before evaluation.

    func_shift = lambda x: func(x-shift)
    

    Where a negative shift will be to the left and a positive shift will be to the right.