# Using input data as index for other model parameters

**URL:** <https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453>\
**Category:** v5\
**Tags:** modeling, prediction\
**Created:** [November 24, 2025, 11:47am UTC](https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453 "2025-11-24T11:47:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gasper256](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gasper256/32/8954_2.png) [@gasper256](https://discourse.pymc.io/u/gasper256)\
**Post date:** [November 24, 2025, 11:47am UTC](https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453/1 "2025-11-24T11:47:07Z")

</div>

Hello,

I have a dataset, in a form of pandas DataFrame `c`, with columns `cat1, cat2, value`. As names suggest, `cat1` and `cat2` are categorical variables with 3 and 24 distinct values, respectively, and `value` is continuous. I am trying to model `value` with Beta distribution whose `alpha` and `beta` parameters depend on a combination of `cat1 & cat2`, i.e. for each pair of `(cat1, cat2) = (c1, c2)`, I assume there are parameters `alpha_c1` and `beta_c1` so that `c.loc[(c[“cat1”]=c1) & (c[“cat2”]=c2), “value”]` is `Beta(alpha_c1, beta_c1)`-distributed.

I have created & run my model as follows:  
`with pm.Model(`  
` coords={`  
` “obs_idx”: c.index, # I don't know if that is necessary; also tried without and avoiding dims setting in the variables of the model`  
` “cat1”: c[“cat1”].unique(), # 3 distinct values`  
` “cat2”: c[“cat2”].unique(), # 24 distinct values`  
` }`  
`) as m:`  
` # idx = pm.Data(“idx”, c.index, dims=“obs_idx”) # I don’t know whether that is important`  
` cat1 = pm.Data(“cat1”, c[“cat1”], dims=“obs_idx”)`  
` cat2 = pm.Data(“cat2”, c[“cat2”], dims=“obs_idx”)`  
` output_data = pm.Data(`  
` “value”,`  
` c[“value”],`  
` dims=“obs_idx”`  
` )`  
` # Alpha & beta priors`  
` alpha_prior = pm.HalfNormal(f"alpha_prior", sigma=4, dims=[“cat1”, “cat2”])`  
` beta_prior = pm.HalfNormal(f"beta_prior", sigma=4, dims=[“cat1”, “cat2”])`  
` # Every (cat1, cat2) pair has its own (alpha, beta) parameters`  
` alpha = pm.Deterministic(“alpha”, alpha_prior[cat1, cat2], dims=“obs_idx”)`  
` beta = pm.Deterministic(“beta”, beta_prior[cat1, cat2], dims=“obs_idx”)`  
` val = pm.Beta(`  
` "value",`  
` alpha=alpha,`  
` beta=beta,`  
` observed=output_data,`  
` shape=cat1.shape[0], # or idx.shape[0], if used,`  
` dims=“obs_idx”`  
` )`  
` pdata = pm.sample(`  
` draws=1600,`  
` chains=6,`  
` cores=6,`  
` )`

The model runs as expected; if I check the posterior distributions of `alpha_prior` and `beta_prior` variables, they are distinct, depending on `cat1` and `cat2` indices; see PDFs of beta distributions attached (stupid names, because priors become posteriors ☹ ):

 ![q1](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/8/855b1df6d8bba1873314c6d05217fb1a2968d3be.jpeg)

However, if I try to generate posterior predictive samples, they seem to be independent of `cat1` and `cat2` data setup:  
`with m:`  
` m.set_dim("obs_idx", new_length=168, coord_values=list(range(100000, 100168)))`  
` # if I avoid `obs_idx` coordinate in model definition, I may skip the line above`  
` pm.set_data(`  
` {`  
` "cat1": 24*[2] + 24*[0] + 5*24*[2],`  
` "cat2": 7*list(range(24))`  
` }`  
` )`  
` post_data = pm.sample_posterior_predictive(`  
` pdata, var_names=["value"], random_seed=256, predictions=True`  
` )`  
I would expect that the first 24 and last 120 values generated in `post_data` would follow the distributions as in the third subplot above (`cat1 = 2`) and values from 24 to 48 would follow the distributions as in the first subplot (`cat1 = 0`), but if I plot  
`plt.plot(post_data.predictions.value.mean(dim=["draw", "chain"]).data)`

I get the following:

 ![q2](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/4/485313e37ad73f753ea88807a2302418f5b6abaf.png)

If I manually implement sampling from inference data, named `pdata` in my case, I got reasonable output:  
`cat1_seq = 24*[2] + 24*[0] + 5*24*[2]`  
`cat2_seq = 7*list(range(24))`  
`values = []`  
`np.random.seed(314)`  
`for c1id, c2id in zip(cat1_seq, cat2_seq):`  
` _chain_id = np.random.randint(6)`  
` _draw_id = np.random.randint(1600)`  
` values.append(`  
` scipy.stats.beta(`  
` a=pdata.posterior.alpha_prior[_chain_id, _draw_id, c1id, c2id],`  
` b=pdata.posterior.beta_prior[_chain_id, _draw_id, c1id, c2id]`  
` ).rvs()`  
` )`  
`plt.plot(range(168), values)`

 ![q3](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/1/19b2798c42d8d1cac4c473d8d3a308ec1169af3d.png)

So, it seems from the first graph with three subplots that the model learns the differences between `(cat1, cat2)` combinations. But I can’t enforce the posterior predictive sampling to use `(cat1, cat2)` combinations sequence by my choice.

How can I achieve that the model use the provided input data for `cat1` and `cat2` while posterior predictive sampling?

Thanks,  
Gasper

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [November 24, 2025, 12:02pm UTC](https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453/2 "2025-11-24T12:02:11Z")

</div>

Tip: You can use triple backtick ````` to create a multiline code snippet if you do ` ```python` you even get syntax highlight.

It seems like your setup should work. When you call `pm.sample_posterior_predictive`, PyMC has to do some logic to know if variables from the trace are still valid for posterior predictive. It shows this as `Sampling: [list_of_vars]` log. What shows up in your case?

Finall, can you provide a fully reproducible snippet with data? It will lower the barrier for someone here to jump and try to find the issue. Fake data with the right data type and shapes is sufficient (if it reproduces the issue)

---

<div class="post-metadata">

**Author:** ![gasper256](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gasper256/32/8954_2.png) [@gasper256](https://discourse.pymc.io/u/gasper256)\
**Post date:** [November 24, 2025, 1:19pm UTC](https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453/3 "2025-11-24T13:19:50Z")

</div>

Hello,

thanks for reply & for the tip.

When I execute `sample_posterior_predictive`, the output is  
`2025-11-24 12:29:12,196 - pymc.sampling.forward - INFO - Sampling: [alpha_prior, beta_prior, value]`

Please find demo data attached.

And the whole code (```trick seems not to work for me :() is in attachment.

[demo.py](https://discourse.pymc.io/uploads/short-url/iB4ipQCl64iPMNBwuiYHBHGbFqm.py) (2.1 KB)

[demo.csv](https://discourse.pymc.io/uploads/short-url/wKQF1md3UGsWFBPDxcBwK55iPCT.csv) (233.2 KB)

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [November 24, 2025, 2:06pm UTC](https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453/4 "2025-11-24T14:06:41Z")

</div>

The problem is PyMC is sampling `alpha_prior` and `beta_prior`, which you don’t want to. I think the problem arises because the dims of those variables have the same name as the `pm.Data` variables. Try to change that?

---

<div class="post-metadata">

**Author:** ![ricardoV94](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ricardov94/32/5775_2.png) [@ricardoV94](https://discourse.pymc.io/u/ricardoV94)\
**Post date:** [November 24, 2025, 2:10pm UTC](https://discourse.pymc.io/t/using-input-data-as-index-for-other-model-parameters/17453/5 "2025-11-24T14:10:05Z")

</div>

Issue [BUG: Missing check for collision between coord name and data variable name · Issue #7788 · pymc-devs/pymc · GitHub](https://github.com/pymc-devs/pymc/issues/7788)
