# Getting shared parameters with MCMC

**URL:** <https://discourse.pymc.io/t/getting-shared-parameters-with-mcmc/1567>\
**Category:** Questions\
**Created:** [July 24, 2018, 1:56pm UTC](https://discourse.pymc.io/t/getting-shared-parameters-with-mcmc/1567 "2018-07-24T13:56:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ru111](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@ru111](https://discourse.pymc.io/u/ru111)\
**Post date:** [July 24, 2018, 1:56pm UTC](https://discourse.pymc.io/t/getting-shared-parameters-with-mcmc/1567/1 "2018-07-24T13:56:36Z")

</div>

Sorry for a newbie question… I have a 4x4 `data` matrix and would like to obtain parameters `alpha` (4-vector), `beta` (4-vector) and `c` using MCMC such that the posterior distribution in `data[i,j]` is given by `1/(1 + exp(alpha[i] + beta[j] + c))` (sigmoid). So for 16 data cells I have 9 parameters. I’ve followed the basic pymc3 [tutorial](http://docs.pymc.io/notebooks/getting_started) and tried the following:

```
basic_model = pm.Model()
with basic_model:

    # Priors for unknown model parameters
    a = pm.Normal('a', mu=10, sd=10, shape=4)
    b = pm.Normal('b', mu=10, sd=10, shape=4)
    c = pm.Normal('c', mu=0, sd=10)

    alpha = np.tile(a, (4, 1)).T
    beta = np.tile(b, (4, 1))
    offset = np.tile(c, (4, 4))

    # Expected values of outcome 
    # (I'm trying to vectorise the operation here, to get a 4x4)
    mu = pm.math.sigmoid(alpha + beta + offset) # ERROR!

    # Likelihood (sampling distribution) of observations
    # (data also 4x4 matrix)
    Y_obs = pm.Bernoulli('Y_obs', p=mu, observed=data)

    # draw 500 posterior samples
    trace = pm.sample(50000)

```

The error is:

> AsTensorError: (‘Cannot convert [[Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0\n Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0]\n [Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0\n Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0]\n [Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0\n Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0]\n [Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0\n Elemwise{true\_div,no\_inplace}.0  
> Elemwise{true\_div,no\_inplace}.0]] to TensorType’, \<class  
> ‘numpy.ndarray’\>)

The error element is probably referring to `mu` which has too many elements (?). In the tutorial\* with the following simple distribution, `mu` when printed is simply `Elemwise{add,no_inplace}.0` but in my `mu` I have 4x4 elements of `Elemwise{true_div,no_inplace}.0`. Looks like I’m not allowed to tile the parameters and pass it through. Perhaps it is better to do a matrix factorisation method, if so, how can I go about doing this?

\*Tutorial example uses:

```
alpha = pm.Normal('alpha', mu=0, sd=10) 
beta = pm.Normal('beta', mu=0, sd=10, shape=2) 
sigma = pm.HalfNormal('sigma', sd=1) 
mu = alpha + beta[0] * X1 + beta[1] * X2 # mu = Elemwise{add,no_inplace}.0

```

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [July 24, 2018, 4:35pm UTC](https://discourse.pymc.io/t/getting-shared-parameters-with-mcmc/1567/2 "2018-07-24T16:35:23Z")

</div>

You can not use numpy function on theano tensors. You can either use the reshaping function in theano.tensor module, or create the shape of RV so that it will broadcast to the shape you want:

```python
basic_model = pm.Model()
with basic_model:
    # Priors for unknown model parameters
    a = pm.Normal('a', mu=10, sd=10, shape=(4, 1))
    b = pm.Normal('b', mu=10, sd=10, shape=(1, 4))
    c = pm.Normal('c', mu=0, sd=10)
    test = a+b <-- this will broadcast to (4 x 4)
    mu = test + c <-- scalar c will add to the (4 x 4) elementwise

```

---

<div class="post-metadata">

**Author:** ![ru111](https://avatars.discourse-cdn.com/v4/letter/r/fbc32d/32.png) [@ru111](https://discourse.pymc.io/u/ru111)\
**Post date:** [July 24, 2018, 5:22pm UTC](https://discourse.pymc.io/t/getting-shared-parameters-with-mcmc/1567/3 "2018-07-24T17:22:32Z")

</div>

This worked perfectly - thanks so much!
