Updating Priors with Posterior for Vector-Valued Distributions

Hello,

I’m looking to build on the technique demonstrated in the “Updating Priors” notebook in the PyMC3 documentation. I have an initial model parameter which has a shape argument passed to indicate that it’s vector-valued.

Once I do an initial run of the model, is it possible to create, in a new model, an Interpolated parameter from the trace of my vector-valued parameter such that the shape is preserved from the original parameter? I tried doing a crude edit of the from_posterior function from the above notebook like so:

def from_posterior_multi(param, samples):
    smin, smax = np.min(samples, axis=0), np.max(samples, axis=0)
    width = smax - smin
    x = []
    y = []
    for i in range(width.shape[0]):
        x_i = np.linspace(smin[i], smax[i], 100)
        y_i = stats.gaussian_kde(samples[:,i])(x_i)
        # what was never sampled should have a small probability but not 0,
        # so we'll extend the domain and use linear approximation of density on it
        x.append(np.concatenate([[x_i[0] - 3 * width[i]], x_i, [x_i[-1] + 3 * width[i]]]))
        y.append(np.concatenate([[0], y_i, [0]]))
        
    return pm.Interpolated(param, x, y, shape=width.shape[0])

but I got the following error when I tried to create an Interpolated parameter this way:

error: failed in converting 2nd argument `y' of dfitpack.fpcurf0 to C/Fortran array

Am I trying to force pm.Interpolated to do something it doesn’t want to in terms of shape?
Apologies if I’ve missed the answer lurking somewhere here or in the docs!

Thanks,
Johannes

1 Like

pm.Interpolated is only meant for 1D random variables. So if you have a vector random variable, you need to apply pm.Interpolated separately for each element.

Hey thanks for the reply! If I want to iterate within a model context and create an Interpolated RV for each element, is there a good way to concatenate them back together if other variables in the model downstream depend on (advanced, in this case) indexing the vector-valued random variable?

I think it might help, but I am not sure how much as the interpolation RV is not particularly fast I think.

@jmharkins were you able to figure out a solution?