# How to implement the product of multiple likelihood functions

**URL:** https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177
**Category:** Questions
**Created:** [November 9, 2018, 11:17am UTC](https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177 "2018-11-09T11:17:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tboutelier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/tboutelier/32/1222_2.png) [@tboutelier](https://discourse.pymc.io/u/tboutelier)
#### Post date: [November 9, 2018, 11:17am UTC](https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177/1 "2018-11-09T11:17:14Z")

</div>

In my problem, I have M observations y\_i (y\_i is a vector of N elements), and a set of parameters \theta\_i (a vector of 3 elements) for each observation. There is a deterministic model that link the parameter \theta\_i to the i-th observation y\_i. My objective is to estimate the parameters \theta\_i for every observations.

My model is built as follow:

- Each set of observation is independent and have an independent likelihood function p(y\_i | \theta\_i).
- For each observation i, the parameters \theta\_i has a multivariate Gaussian prior with parameter \mu and \Sigma\_\mu. \mu and \Sigma\_\mu are the same for each observation i
- \mu and \Sigma\_\mu follow some prior p(\mu, \Sigma\_\mu)

Hence, the posterior for my model is:  
p(\theta\_{1:M}, \mu, \Sigma\_\mu | y\_{1:M}) \propto p(\mu, \Sigma\_\mu) \prod\_{i=1}^Mp(y\_i|\theta\_i)p(\theta\_i|\mu, \Sigma\_\mu)

The likelihood p(y\_i | \theta\_i) for each observation is ad-hoc, so it is implemented through its logp with `DensityDist` function.

What do you think would be the best strategy to implement the product of the likelihoods functions:

1. Implement it with one `DensityDist` and one big `logp` function, inside which I will do the sum of \log p(y\_i|\theta\_i) for i=1..M

or

1. Should I define a generic function for the `logp` of each observation i and `DensityDist`, and then do the sum with the `Potential` function?

The second solution raises an other question: how to do the sum M likelihood function within the `Potential` function?

Thanks!

---

<div class="post-metadata">

### Author: ![twiecki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/twiecki/32/6930_2.png) [@twiecki](https://discourse.pymc.io/u/twiecki)
#### Post date: [November 9, 2018, 11:26am UTC](https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177/2 "2018-11-09T11:26:27Z")

</div>

There is a third solution: Do like in 2. but don’t sum the in the potential as PyMC3 already does that for you if you define multiple observed variables.

---

<div class="post-metadata">

### Author: ![tfg](https://avatars.discourse-cdn.com/v4/letter/t/e68b1a/32.png) [@tfg](https://discourse.pymc.io/u/tfg)
#### Post date: [November 10, 2018, 4:37am UTC](https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177/3 "2018-11-10T04:37:57Z")

</div>

I think the line [model.py:713](https://github.com/pymc-devs/pymc3/blob/master/pymc3/model.py#L713) is what you’re looking for. It shows how the `Model` class multiplies all of the terms in your posterior equation. Those include

- the likelihoods (`observed_RVs` \subset `basic_RVs`)
- the series of terms from however you’ve factored your joint prior (`free_RVs` \subset `basic_RVs`)
- any `potentials`

---

<div class="post-metadata">

### Author: ![tboutelier](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/tboutelier/32/1222_2.png) [@tboutelier](https://discourse.pymc.io/u/tboutelier)
#### Post date: [November 12, 2018, 7:48am UTC](https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177/4 "2018-11-12T07:48:14Z")

</div>

Thanks @twiecki and @tfg. I will try what you propose and let you know if I managed to make it work!  
Best regards

---

<div class="post-metadata">

### Author: ![Dr\_Z](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/dr_z/32/2445_2.png) [@Dr\_Z](https://discourse.pymc.io/u/Dr_Z)
#### Post date: [January 29, 2020, 4:49pm UTC](https://discourse.pymc.io/t/how-to-implement-the-product-of-multiple-likelihood-functions/2177/5 "2020-01-29T16:49:21Z")

</div>

@tboutelier So can you tell us which approach you ended up using? It will benefit others reading this question.
