# Concatenate constant to random variable vector

**URL:** <https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365>\
**Category:** v5\
**Created:** [February 9, 2023, 10:41pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365 "2023-02-09T22:41:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![VadimSokolov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vadimsokolov/32/5425_2.png) [@VadimSokolov](https://discourse.pymc.io/u/VadimSokolov)\
**Post date:** [February 9, 2023, 10:41pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365/1 "2023-02-09T22:41:05Z")

</div>

In my model I have a vector that has constant last element and remaining of the elements are RVs. I tried to define it using

```python
a = [pm.Flat("a",shape=n-1),1]

```

OR

```python
a = at.concatenate([pm.Flat("a",shape=n-1),[1]],axis=0)

```

But neither works. Any suggestion of how to correctly do the concatenation?  
Thank you!

---

<div class="post-metadata">

**Author:** ![cluhmann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/cluhmann/32/3083_2.png) [@cluhmann](https://discourse.pymc.io/u/cluhmann)\
**Post date:** [February 10, 2023, 1:31pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365/2 "2023-02-10T13:31:32Z")

</div>

The first approach produces a Python list (which is probably not what you want). I’m not sure exact what the second does, but the Python list (`[1]`) probably doesn’t play well with the tensor. @ricardoV94 would know more, but you might pack the constant value into a `pm.ConstantData` object? See [here](https://www.pymc.io/projects/docs/en/latest/api/generated/pymc.ConstantData.html) and [here](https://www.pymc.io/projects/docs/en/latest/api/generated/pymc.Data.html). Just a guess.

---

<div class="post-metadata">

**Author:** ![VadimSokolov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vadimsokolov/32/5425_2.png) [@VadimSokolov](https://discourse.pymc.io/u/VadimSokolov)\
**Post date:** [February 10, 2023, 5:45pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365/3 "2023-02-10T17:45:35Z")

</div>

Thank you @cluhmann. Will pack. Still not sure how to concatenate two together. @ricardoV94 any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![ckrapu](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ckrapu/32/1366_2.png) [@ckrapu](https://discourse.pymc.io/u/ckrapu)\
**Post date:** [February 10, 2023, 5:54pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365/4 "2023-02-10T17:54:48Z")

</div>

I think the issue isn’t with concatenating; it’s the flat prior. Can you tell me what error you get? Also, in practice, a very wide normal or a uniform prior typically works just as well. Outside of some unusual circumstances, it’s generally a good idea to avoid using flat priors.

I believe your code works well for concatenating with other priors. For reference, here’s [an example colab](https://colab.research.google.com/drive/1iMFwdGEG4hr-X2VOWgkk_yxTMXK94ZMh_?usp=sharing) and [gist](https://gist.github.com/ckrapu/11c414ca02100e4899b3867918a21242) showing the syntax for doing the concatenation in 1 and 2 dimensions.

---

<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:** [February 10, 2023, 6:08pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365/5 "2023-02-10T18:08:24Z")

</div>

There is nothing wrong with your code. You can convince yourself the operation works as expected like this:

```python
import pymc as pm
n = 5
a = pm.math.concatenate([pm.Flat.dist(shape=n-1),[1]],axis=0)
a.shape.eval() # array([5])

```

Just note that you can’t do prior or posterior predictive from Flat (or HalfFlat) variables. That may be why you are getting errors.

---

<div class="post-metadata">

**Author:** ![VadimSokolov](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/vadimsokolov/32/5425_2.png) [@VadimSokolov](https://discourse.pymc.io/u/VadimSokolov)\
**Post date:** [February 10, 2023, 6:24pm UTC](https://discourse.pymc.io/t/concatenate-constant-to-random-variable-vector/11365/6 "2023-02-10T18:24:31Z")

</div>

Thank you all. Not sure why I was getting an error, it all works now!
