Linear interpolation as a Theano Op?

I want to transform one variable into another within my model, but the transformation function is not analytic. Linear interpolation based on a lookup table will do as an approximation. I can create such a function very easily in scipy,

f = scipy.interpolate.interp1d(x, y) 

where x and y are from the lookup table. I guess I can wrap f inside a Theano Op, but I also need the gradient for NUTS to sample, right? Is there an easy way to calculate this?

There is a SplineWrapper(theano.Op) which creates a theano operation from scipy.interpolate.UnivariateSpline: https://github.com/pymc-devs/pymc3/blob/fb156af4cf32fd1b2af6a073a2cac9707c933152/pymc3/distributions/dist_math.py#L251

2 Likes

Great, thanks. I’ll try it out.

do you have some example for how to use this SplineWrapper(theano.Op)? thanks~

Bumping this as i am unsure how to use the SplineWrapper.

Do we first have to use scipy.interpolate.UnivariateSpline to define the model with our x,y data and then pass it to the wrapper with the new array xs to be interpolated over? if so we would have something like the following

f = sci.interpolate.UnivariateSpline(x,y)
spl = SplineWrapper(f)
spl(xs)

or do we input the data directly into the wrapper? Sorry for my confusion.

1 Like

The only example i know of is https://docs.pymc.io/notebooks/updating_priors.html?highlight=updating%20prior