Specify different prior distribution only for some variables in the variables dim

Hey, I have a model where i set prior distributions for multiple variables using coords and dims. I don’t want to specify different prior for each variable but only for some. Is there a way to alter the prior values for only one variable from all variables dim?

You can pass arrays for inputs. Let’s say you wanted a vector of 10 normals, all with unit standard deviation, except the last one which should have half of it.

import numpy as np
import pymc as pm

with pm.Model() as m:
  sigmas = np.ones(10)
  sigmas[-1] = 0.5
  x = pm.Normal("x", sigma=sigmas)

Does this answer your question?

1 Like

I think that my formulation of the problem without code is very inaccurate :slight_smile:
Here i define some coords where I will set mutltiple priors for “delay_vars”. There will be priors on some non-linear transformations as well as on model coefficients. It would be very inefficient to specify som 15 priors (3 for each variable). Only priors for “alpha” parameter would be different between the “delay_vars”. So far, I can set priors which are common for all variables in “delay_vars” but I would like to change just some of them.

delay_vars = ['tv_S', 'ooh_S', 'print_S', 'facebook_I', 'search_clicks_P', 'newsletter']

coords = {
    'date': df_scaled[date_var].values,
    'delay_vars': delay_vars,
    'control_vars': control_vars,

with pm.Model(coords=coords) as custom_model:
    beta_delay_vars = pm.HalfNormal(
    )  # the same for all delay_vars

    alpha = pm.Beta(name='alpha', alpha=1, beta=3,
                    dims='delay_vars')  # to be defined for some vars separately 

If you set dims="delay_vars" you will be sampling as many variables as there are delay_vars.

You can always sample a smaller subset of variables and then broadcast them to the final shape (or use them to fill an array with arbitrary indexing routines).

The traditional case is in linear models, where coefficients can be reused for multiple observations:

with pm.Model(coords=coords) as m:
  group_coeffs = pm.Normal("group_coefs", 0, 1, dims="unique_groups")
  # Deterministic is optional
  trial_mu = pm.Deterministic("trial_mu", group_coeffs[group_idxs] * x, dims="trials")
  pm.Normal("llike", trial_mu, observed=data, dims="trials")

Where indexing with [group_idxs], expands the small number of group_coeffs variables into a vector of size trials

1 Like

Wouldn’t be sufficient to do just this?:

    alpha = pm.Beta(
        name = 'alpha',
        alpha = [1, 1, 1, 1, 1, 1],
        beta = [3, 3, 3, 3, 6, 6],
        dims = 'delay_vars'

@Oliver131313 depends if you want to sample 6 distinct variables with the same prior or 2 distinct variabls that are used in 6 places. It’s a completely different model.

It’s the difference between np.random.beta([1, 1, 1, 1, 1, 1], [3, 3, 3, 3, 6, 6], size=(6,)) and np.random.beta([1, 1], [3, 6], size=(2,))[0, 0, 0, 0, 1, 1]

The tip box in this section touches on this: Distribution Dimensionality — PyMC 5.1.2 documentation

1 Like

I’m really greatful that you’re willing to asnwer my questions. Thank you.
My model structure is like this: Screenshot by Lightshot
I take the data, apply transformation and then calculate contributions of the delay_vars.

Therefore my question was how could i specify different parameters for each element of delay_vars dim.
Here I’m illustrating it on the alpha parameter. The prior distributions are the same for all elements in delay_vars. However, i want parameter of the distribution to differ for each element of delay_vars (as shown in previous example). Is my previous example correct way to achieve this or do i have to do that in different way?

Yes, your example of alpha was good. It says you have 6 independent beta variables parametrized with the specified alpha, beta pairs.

1 Like