# Linear interpolation as a Theano Op?

**URL:** <https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581>\
**Category:** Questions\
**Created:** [February 28, 2020, 7:46pm UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581 "2020-02-28T19:46:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sammosummo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sammosummo/32/96_2.png) [@sammosummo](https://discourse.pymc.io/u/sammosummo)\
**Post date:** [February 28, 2020, 7:46pm UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581/1 "2020-02-28T19:46:55Z")

</div>

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,

```auto
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?

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 2, 2020, 7:31am UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581/2 "2020-03-02T07:31:43Z")

</div>

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](https://github.com/pymc-devs/pymc3/blob/fb156af4cf32fd1b2af6a073a2cac9707c933152/pymc3/distributions/dist_math.py#L251)

---

<div class="post-metadata">

**Author:** ![sammosummo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/sammosummo/32/96_2.png) [@sammosummo](https://discourse.pymc.io/u/sammosummo)\
**Post date:** [March 3, 2020, 2:43pm UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581/3 "2020-03-03T14:43:28Z")

</div>

Great, thanks. I’ll try it out.

---

<div class="post-metadata">

**Author:** ![joe](https://avatars.discourse-cdn.com/v4/letter/j/b9e5f3/32.png) [@joe](https://discourse.pymc.io/u/joe)\
**Post date:** [March 15, 2020, 2:37pm UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581/4 "2020-03-15T14:37:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bobrepoman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/bobrepoman/32/2255_2.png) [@Bobrepoman](https://discourse.pymc.io/u/Bobrepoman)\
**Post date:** [April 24, 2020, 11:43am UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581/5 "2020-04-24T11:43:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [April 26, 2020, 5:36am UTC](https://discourse.pymc.io/t/linear-interpolation-as-a-theano-op/4581/6 "2020-04-26T05:36:12Z")

</div>

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