# Feed posterior to a shared variable

**URL:** <https://discourse.pymc.io/t/feed-posterior-to-a-shared-variable/8051>\
**Category:** General\
**Created:** [September 20, 2021, 1:21pm UTC](https://discourse.pymc.io/t/feed-posterior-to-a-shared-variable/8051 "2021-09-20T13:21:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmelchio](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rmelchio](https://discourse.pymc.io/u/rmelchio)\
**Post date:** [September 20, 2021, 1:21pm UTC](https://discourse.pymc.io/t/feed-posterior-to-a-shared-variable/8051/1 "2021-09-20T13:21:26Z")

</div>

Hi,  
I am trying to fit a linear regression model (using PyMC3) where one of the predictors has a posterior distribution. I normally use pm.Data to set the values of predictors but I don’t think I can do that when the predictor is not a column but a 2 dimensional array where rows are posterior samples and columns are time points.  
Is there any way to do that in PyMC3?

To give a concrete example I am trying to predict revenue ~ B\_trend \* trend + B\_seas \* seas + B\_spend \* spend. Spend is a time series. Trend and seasonality are instead computed using an external library that return posteriors in the form of 2d arrays.

How can I feed that to the regression?

---

<div class="post-metadata">

**Author:** ![falk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/falk/32/4421_2.png) [@falk](https://discourse.pymc.io/u/falk)\
**Post date:** [September 21, 2021, 8:30am UTC](https://discourse.pymc.io/t/feed-posterior-to-a-shared-variable/8051/2 "2021-09-21T08:30:00Z")

</div>

You can definitely use multi-column matrices. But instead of using the multiplication operator, think of matrix multiplication (“dot” operator). You can find all kinds of operations in the `theano.tensor` library (`tt`), for example you can also stack column vectors. So try something like:  
[…] + `tt.dot(spend, B_spend)`

The tricky part is getting the dimensionality right for `B_spend` (assuming `spend` is the `n` by `2` `pm.Data` tensor). I’m sure there are examples in the docs (search for `theano.tensor`). I also recently put one [here](https://discourse.pymc.io/t/prediction-setting-data-fails-with-multivariate-observed/8011).

Cheers!

---

<div class="post-metadata">

**Author:** ![rmelchio](https://avatars.discourse-cdn.com/v4/letter/r/aca169/32.png) [@rmelchio](https://discourse.pymc.io/u/rmelchio)\
**Post date:** [September 22, 2021, 10:43am UTC](https://discourse.pymc.io/t/feed-posterior-to-a-shared-variable/8051/3 "2021-09-22T10:43:04Z")

</div>

Can I do that even if the X used for training was unidimensional? It only becomes bi-dimensional at prediction (since I am feeding the whole posterior predictive instead of just the mean/median).

---

<div class="post-metadata">

**Author:** ![falk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/falk/32/4421_2.png) [@falk](https://discourse.pymc.io/u/falk)\
**Post date:** [September 22, 2021, 3:40pm UTC](https://discourse.pymc.io/t/feed-posterior-to-a-shared-variable/8051/4 "2021-09-22T15:40:07Z")

</div>

I think changing dimensionality will most likely break the maths… Unless my brain is too inflexible by all the shape error treatment it had to overcome 🙂  
So you either have to initialize bi-dimensionally from the start (I always do that, most data vectors are `n x 1` column vectors and slopes are `1 x o` shaped tensors with `o` being the number of observables).  
Or you can solve the extra dimension by using a “loop”.

Just try!
