# Understanding the likelihood function

**URL:** <https://discourse.pymc.io/t/understanding-the-likelihood-function/9329>\
**Category:** Questions\
**Created:** [May 3, 2022, 5:56am UTC](https://discourse.pymc.io/t/understanding-the-likelihood-function/9329 "2022-05-03T05:56:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BjornHartmann](https://avatars.discourse-cdn.com/v4/letter/b/a87d85/32.png) [@BjornHartmann](https://discourse.pymc.io/u/BjornHartmann)\
**Post date:** [May 3, 2022, 5:56am UTC](https://discourse.pymc.io/t/understanding-the-likelihood-function/9329/1 "2022-05-03T05:56:15Z")

</div>

I am struggling with understanding a key element of an inference model in PYMC3.

The likelihood distribution can be understood as “how you think your data is distributed”(?), I am however confused.

I have a simple example case to showcase my confusion:

```auto
model = pm.Model()
data = np.random.exponential(scale=2,size=1000)
with model:
    # prior on mu
    lam = pm.Normal("lam",sd=1)
    pm.Exponential("L",lam, observed = data)
    trace = pm.sample(draws=1000)

```

In this case, I am infering the lambda value of the data i believe is exponentially distributed, hence my exponential likelihood function(?).

Sample\_posterior\_predictive seems to sanity-check that my model is somewhat correct (original data in blue):  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/7/7608dedc5d903f2919296ed0abe3229d94c81458.png)

Confusions:

1. The likelihood function cannot change shape? so if my likelihood function is normally distributed, but my data is exponentially distributed, the sample\_posterior\_predictive would still be normally distributed? In that case, choosing the right likelihood function is crucial?  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/pymc3/original/2X/2/29b1fc9d9e7d376b7c4ebf302bf5c2c43c899fa8.png)

2. If above is correct, how come more complex models can have a normally distribued likelihood function? I.e. the _Getting started with PyMC3_(John Salvatier) showcases a motivational example of a linear regression ( [Getting started with PyMC3](https://docs.pymc.io/en/v3/pymc-examples/examples/getting_started.html#:~:text=%23%20Likelihood%20(sampling%20distribution)%20of%20observations%0A%20%20%20%20Y_obs%20%3D%20pm.Normal(%22Y_obs%22%2C%20mu%3Dmu%2C%20sigma%3Dsigma%2C%20observed%3DY))) where there are multiple prior random variables. The likelihood is still normally distributed, how come? We are not saying the data comes from a normal distribution? What is normally distributed in this likelihood function?

Thank you so much to anyone taking the time to answer this, I believe this is the last peace of my pussle understanding how these inference works in broad terms.

---

<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:** [May 3, 2022, 6:19am UTC](https://discourse.pymc.io/t/understanding-the-likelihood-function/9329/2 "2022-05-03T06:19:12Z")

</div>

If you have not seen it, I gave a talk few years back on likelihood should be a good place to start/consolidate your understanding: [GitHub - junpenglao/All-that-likelihood-with-PyMC3](https://github.com/junpenglao/All-that-likelihood-with-PyMC3)

As for your question:

> [@BjornHartmann](#):
>
> 1. The likelihood function cannot change shape? so if my likelihood function is normally distributed, but my data is exponentially distributed, the sample\_posterior\_predictive would still be normally distributed? In that case, choosing the right likelihood function is crucial?

Choosing the likelihood function is always crucial, and while likelihood function cannot change shape (technically it change shape according to the parameterization, e.g., the Gaussian becomes wider with larger \sigma), you **do** see shape changes using `sample_posterior_predictive` or `sample_prior_predictive` because they can depends on other information. You can see it simply with:

```python
with pm.Model():
    sigma = pm.HalfNormal("sigma", 1)
    observed = pm.Normal('obs', np.arange(10), sigma, observed=np.arange(10) + np.random.randn(10))

```

> [@BjornHartmann](#):
>
> 1. If above is correct, how come more complex models can have a normally distribued likelihood function? I.e. the _Getting started with PyMC3_ (John Salvatier) showcases a motivational example of a linear regression ( [Getting started with PyMC3](https://docs.pymc.io/en/v3/pymc-examples/examples/getting_started.html#:~:text=%23%20Likelihood%20(sampling%20distribution)%20of%20observations%0A%20%20%20%20Y_obs%20%3D%20pm.Normal(%22Y_obs%22%2C%20mu%3Dmu%2C%20sigma%3Dsigma%2C%20observed%3DY))) where there are multiple prior random variables. The likelihood is still normally distributed, how come? We are not saying the data comes from a normal distribution? What is normally distributed in this likelihood function?

Hopefully with the above, you can now answer (2). The key is to understand whether the random variable representing the observation is a scalar -like variable and you repeatedly draw from this variable

```python
obs = np.random.normal(3, 2.5, size=(num_obs))

```

or a vector -like variable that depends on other information

```python
x = np.arange(num_obs)
obs = np.random.normal(x, 2.5)

```

in both case you have `obs.shape=(10,)`, but the interpretation and how you would build a Bayesian model is very different
