# Random method for Interpolated RVs

**URL:** <https://discourse.pymc.io/t/random-method-for-interpolated-rvs/3205>\
**Category:** Questions\
**Created:** [May 4, 2019, 12:24am UTC](https://discourse.pymc.io/t/random-method-for-interpolated-rvs/3205 "2019-05-04T00:24:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tanmoy.7989](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/tanmoy.7989/32/1825_2.png) [@Tanmoy.7989](https://discourse.pymc.io/u/Tanmoy.7989)\
**Post date:** [May 4, 2019, 12:24am UTC](https://discourse.pymc.io/t/random-method-for-interpolated-rvs/3205/1 "2019-05-04T00:24:30Z")

</div>

I’m trying an approach for prediction using pymc3 models:  
In short: after training:  
a) collect all the RVs that don’t have parent nodes i.e. are priors  
b) form kde s out of their samples in the trace, after training  
c) then recreate the model, removing any observed data that was given during training, re-defining these RVs (in (a)) as Interpolated distributions using kde s computed in (b).  
d) run sample\_prior\_predictive using the updated model (and new input data if presented with)

However, in the process the random() method for the Interpolated distribution keeps throwing up an error that I don’t fully undestand. As an example: this snippet right here:

import numpy as np, pymc3 as pm  
x = np.arange(10) ; y = np.random.random(10)  
with pm.Model() as m:  
v = pm.Interpolated(‘v’, x,y)

print(v.random(size = 10))

Traceback (most recent call last):  
File “/home/tsanyal/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/distributions/distribution.py”, line 617, in generate\_samples  
ValueError: Need at least 1 and at most 32 array objects.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):  
File “”, line 1, in   
File “/home/tsanyal/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/model.py”, line 43, in **call**  
File “/home/tsanyal/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/distributions/continuous.py”, line 3993, in random  
File “/home/tsanyal/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/distributions/distribution.py”, line 619, in generate\_samples  
ValueError: max() arg is an empty sequence

Any ideas on how to solve this?

---

<div class="post-metadata">

**Author:** ![chartl](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/chartl/32/1515_2.png) [@chartl](https://discourse.pymc.io/u/chartl)\
**Post date:** [May 4, 2019, 4:13am UTC](https://discourse.pymc.io/t/random-method-for-interpolated-rvs/3205/2 "2019-05-04T04:13:31Z")

</div>

It’s strange, but just try `pm.sample`. Sampling based on the likelihood surface works; but anything involving the RNG appears to be broken.

```auto
import numpy as np, pymc3 as pm
from matplotlib import pyplot as plt
x = np.arange(200)/20. - 5. ; y = np.exp(-x**2/2)/np.sqrt(2*np.pi)
plt.plot(x,y)
with pm.Model() as m:
    v = pm.Interpolated('v', x.reshape((200,)), y.reshape((200,)))
    tr = pm.sample(1000, tune=2000, chains=4, cores=4)

```

works for me.

However, there is weirdness the second you touch the prior predictive:

```auto
   ...
   pm.sample_prior_predictive(500)

TypeError Traceback (most recent call last)
<ipython-input-28-f06baca22414> in <module>
      5 with pm.Model() as m:
      6 v = pm.Interpolated('v', x.reshape((200,)), y.reshape((200,)))
----> 7 tr = pm.sample_prior_predictive(10)

~/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/sampling.py in sample_prior_predictive(samples, model, vars, random_seed)
   1298 names = get_default_varnames(model.named_vars, include_transformed=False)
   1299 # draw_values fails with auto-transformed variables. transform them later!
-> 1300 values = draw_values([model[name] for name in names], size=samples)
   1301 
   1302 data = {k: v for k, v in zip(names, values)}

~/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/distributions/distribution.py in draw_values(params, point, size)
    390 point=point,
    391 givens=temp_givens,
--> 392 size=size)
    393 givens[next_.name] = (next_, value)
    394 drawn[(next_, size)] = value

~/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/distributions/distribution.py in _draw_value(param, point, givens, size)
    484 return point[param.name]
    485 elif hasattr(param, 'random') and param.random is not None:
--> 486 return param.random(point=point, size=size)
    487 elif (hasattr(param, 'distribution') and
    488 hasattr(param.distribution, 'random') and

~/anaconda3/lib/python3.7/site-packages/pymc3-3.6-py3.7.egg/pymc3/model.py in __call__ (self, *args, **kwargs)
     40 
     41 def __call__ (self, *args, **kwargs):
---> 42 return getattr(self.obj, self.method_name)(*args, **kwargs)
     43 
     44 

TypeError: random() got an unexpected keyword argument 'point'

```

---

<div class="post-metadata">

**Author:** ![lucianopaz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/lucianopaz/32/2186_2.png) [@lucianopaz](https://discourse.pymc.io/u/lucianopaz)\
**Post date:** [May 5, 2019, 5:00pm UTC](https://discourse.pymc.io/t/random-method-for-interpolated-rvs/3205/3 "2019-05-05T17:00:46Z")

</div>

Seems to be a bug in `Interpolated.random` and possibly in `generate_samples`, could you submit this as an issue on GitHub? We’ll work on a fix when we get the chance.
