# How to do MCMC sampling with Multiple likelihood functions？

**URL:** <https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239>\
**Category:** Questions\
**Created:** [November 11, 2020, 4:01pm UTC](https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239 "2020-11-11T16:01:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jym0502](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jym0502/32/3066_2.png) [@Jym0502](https://discourse.pymc.io/u/Jym0502)\
**Post date:** [November 11, 2020, 4:01pm UTC](https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239/1 "2020-11-11T16:01:16Z")

</div>

I am new to MCMC and theano, and now I have three equations similar to the following equations：  
r1 = ……  
r2 = ……  
r3 = ……  
R\_a = r1 + r2  
R\_b = r1 - r3  
R\_c = - r2 - r3;  
Due to the fact that experimental data of R\_a, R\_b and R\_c is available, so I want to use Likehood of R\_a, R\_b and R\_c to do sampling as follow:

> R\_a\_likehood = pm.Normal(‘R\_a\_likehood’,mu = R\_a\_obs, sd = sigma, observed = R\_a\_data)  
> R\_b\_likehood = pm.Normal(‘R\_b\_likehood’,mu = R\_b\_obs, sd = sigma, observed = R\_b\_data)  
> R\_c\_likehood = pm.Normal(‘R\_c\_likehood’,mu = R\_c\_obs, sd = sigma, observed = R\_c\_data)

But it seems that only one likehood can be used.  
I tried to use np.vstack to combine the array, but I encountered the problem of “ValueError: setting an array element with a sequence.”  
So how can I do MCMC sampling with Multiple likelihood functions？

---

<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:** [November 12, 2020, 8:11am UTC](https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239/2 "2020-11-12T08:11:03Z")

</div>

The model code is valid and multiple likelihood will be accounted for - I dont see np.vstack in your code, where and how do you use it? The error seems to raise from there.

---

<div class="post-metadata">

**Author:** ![ar-valdez](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ar-valdez/32/2848_2.png) [@ar-valdez](https://discourse.pymc.io/u/ar-valdez)\
**Post date:** [November 16, 2020, 6:14pm UTC](https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239/3 "2020-11-16T18:14:15Z")

</div>

Before I use to solve this “Adding” but, now I am interested in knowing how to solve this kind of scenario…

---

<div class="post-metadata">

**Author:** ![Jym0502](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/jym0502/32/3066_2.png) [@Jym0502](https://discourse.pymc.io/u/Jym0502)\
**Post date:** [November 23, 2020, 12:47am UTC](https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239/4 "2020-11-23T00:47:35Z")

</div>

Sorry for the late reply!

 ![Q1](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/b/bdc63b4736c3c260d7fc0dc614b72dc103a82a98.jpeg)

This is the original model with three likelihoods (where ‘p’, ‘keq’, ‘R’, ‘T\_Bottom’ and ‘r’ are the experimental data), and the equations in the red block is the model I want to simulated, but my professor said that the MCMC model should have only one likelihood.

As a result of which, I try to use np.hstack to combine three ‘r’ into one array as follow:

```
with XF_model_PL:    
    r_CH4_obs = - r_2 - r_3
    r_CO2_obs = r_1 + r_2
    r_CO_obs = r_3 - r_1
    rrr_obs = np.hstack((r_CH4_obs,r_CO_obs,r_CO2_obs))
    rrr_likelihood = pm.Normal('rrr_likelihood',mu = rrr_obs, sd = sigma, observed = rrr0)

```

As a result of which, Python encounted the error of ‘setting an array element with a sequence.’

 ![QQ截图20201123094111](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/e/e6bf1cb01d6ae11010e8de8f046f95cb61c9a4e0.png)

So I want to ask for help that how to construct a likelihood function with a vector or matrix to complete the MCMC sampling. Also I would like to find some reference material or literature on this to simulate different multiple likelihood models with MCMC methods. Thanks!

---

<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:** [November 23, 2020, 1:38am UTC](https://discourse.pymc.io/t/how-to-do-mcmc-sampling-with-multiple-likelihood-functions/6239/5 "2020-11-23T01:38:00Z")

</div>

You don’t need to do anything to your code to get the combined likelihood - behind the scenes, PyMC3 is adding together the contributions from the log likelihoods from each of the 3 lines. Creating a single vector variable is admittedly cleaner and easier to follow, but it won’t fundamentally change the results you get.

As for the `ValueError` you’re getting, the shapes of the variables you’ve provided in `rrr_likelihood` aren’t compatible. Can you print off the shapes of these using `x.shape` or `x.tag.test_value.shape`?
