Random method for Interpolated RVs

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?

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

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:

   ...
   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'

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.