Frage

I have a 2D regular grid of parameters that at each point have a model for a 1D model and I would like to interpolate that 1D model array to any point of parameters space (not just the grid nodes).

For 1D grid of 1 parameter (say A) with grid values a I can do the trick using interpolate.RectBivariateSpline, which generates a function that I can call at any given value of A. ex:

from scipy import interpolate
ip = interpolate.RectBivariateSpline(a, x, model, s = 0)
new_model = ip([aa], [x])

where a is the 1D "grid" or array of values for the parameter A, x is the 1D array of discrete pixels at which the model is expressed f(x) = model(x), and aa is the new value of A at which I want to interpolate.

For the 2D case one solution would be to compute at each pixel of x (each x_i) the interpolation of the 2 parameters using RectBivariateSpline or another method, (like slicing a 3D volume in 2D planes) but that seems cumbersome and does not account for correlation between pixels on the model.

The solution here Multivariate spline interpolation in python/scipy? does not quite apply to my case as I need a complete array to be returned and I need to evaluate at different points on different moments.

From the solutions on scipy interpolation: Both LinearNDinterp and NearestNDInterp might work, but I don't know how to do a similar thing as with RectBivariateSpline, i.e. feed it the 2D (or nD) parameters grid, the pixels array for the model and the model values, and then get back a interpolated model for the same pixels but different parameter values grid(a1, a2, ... an; x) -> model(A1', A2', ... An'; x)

The solution might be here: Interpolation in vector-valued multi-variate function but I don't know how to reinterpret it to my case...

Ultimately what I would like to do is have the function f(A1, A2, ... An; x) (or something equivalent) and with some Montecarlo explore in a finer grid the parameters space when comparing the model with data.

Thanks in advance!

War es hilfreich?

Lösung

Well since nobody has come with any help I'll post my own solution. As hinted starting from the solution on Interpolation in vector-valued multi-variate function and with little more research on LinearNDinterpolation from Extrapolate with LinearNDInterpolator and basically here Python 4D linear interpolation on a rectangular grid (I missed this in my first search) I adapted it to my case in a quite straightforward way.

points = np.array((a1, a2)).T
values = np.array((models))

ip = interpolate.LinearNDInterpolator(points, values)
p = [A1_new, A2_new]
new_model = ip([point])[0]

Where a1, a2 are the arrays with the grid points of each of the parameters, for example in a 2x3 grid:

a1= [1.1, 1.1, 1.1, 2.2, 2.2, 2.2]
a2= [3.3, 4.4, 5.5, 3.3, 4.4, 5.5]

and a plot as example (of my models): 2 models of the grid and the interpolated one from an out of grid point p = (A1', A2')

With that I can call the "function" ip at any point of the grid that I need, this is also easily extended for higher dimensions new_model = ip(A1', A2', ...). Although I have not check the scalability of it with time (if it's linear with N or most probably goes with the power of N.)

I hope this can be of some help to others.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top