# Implicit uniform prior for computing the log marginal likelihood

**URL:** https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119
**Category:** Modeling
**Tags:** specification, rstan
**Created:** [April 19, 2023, 7:38am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119 "2023-04-19T07:38:17Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![DexterW](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dexterw/32/16242_2.png) [@DexterW](https://discourse.mc-stan.org/u/DexterW)
#### Post date: [April 19, 2023, 7:38am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/1 "2023-04-19T07:38:18Z")

</div>

I am trying to compute the Bayes factor via _bridge sampling_ from Stan models.

According to the vignettes by [Gronau (2021)](https://cran.r-project.org/web/packages/bridgesampling/vignettes/bridgesampling_stan_ttest.html),

> … to compute the (log) marginal likelihood for a Stan model, we need to specify the model in a certain way. Instad of using “~” signs for specifying distributions, we need to directly use the (log) density functions. The reason for this is that when using the “~” sign, constant terms are dropped which are not needed for sampling from the posterior. However, for computing the marginal likelihood, these constants need to be retained. For instance, instead of writing `y ~ normal(mu, sigma)` we would need to write `target += normal_lpdf(y | mu, sigma)`

In a model block, the Jeffreys prior for the **error standard deviation** \sigma\_\epsilon is coded as

```stan
model {
  target += -log(sigma); // Jeffreys prior
} 

```

Moreover, the Jeffreys prior for the **grand mean** \mu implicitly follows a uniform distribution. My question (to compute the log marginal likelihood) is: Do I need to write it out in some `_lpdf` syntax or leave it undefined?

```stan
model {
 // mu ~ implicit uniform prior // Jeffreys prior
} 

```

A concrete example can be view at [RPubs - Bayes Factor for One-Way ANOVA](https://rpubs.com/sherloconan/1022575)

Thanks for any comments.

---

<div class="post-metadata">

### Author: ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)
#### Post date: [April 19, 2023, 9:01am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/2 "2023-04-19T09:01:59Z")

</div>

`brms` models are compatible with `bridgesampling` by default and they generate code which uses implicit uniform priors, so I’d think you were fine. But the bridgesampling devs are also on discourse, so I’ll ping them to clarify just in case: @Quentin @Henrik_Singmann

In general though, I’d argue that it’s best practice to make priors explicit in models, to guard against any changes to default behaviours in the future

---

<div class="post-metadata">

### Author: ![DexterW](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dexterw/32/16242_2.png) [@DexterW](https://discourse.mc-stan.org/u/DexterW)
#### Post date: [April 19, 2023, 9:32am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/3 "2023-04-19T09:32:17Z")

</div>

Thank you, Andrew.

The grand mean \mu, in this case, has been assigned an improper uniform prior (unbounded) [(Stan User’s Guide, version 2.31)](https://mc-stan.org/docs/stan-users-guide/some-differences-in-the-statistical-models-that-are-allowed.html). I doubt we can write it explicit in Stan, can we?

---

<div class="post-metadata">

### Author: ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)
#### Post date: [April 19, 2023, 9:41am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/4 "2023-04-19T09:41:23Z")

</div>

If you’re referring to the implicit uniform, then note that:

```stan
parameters {
  real mu;
}
model {}

```

Is the same as:

```stan
parameters {
  real mu;
}
model {
  mu ~ uniform(negative_infinity(), positive_infinity());
}

```

@avehtari what’s your perspective on whether or not to explicitly specify the implicit uniform prior?

---

<div class="post-metadata">

### Author: ![DexterW](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dexterw/32/16242_2.png) [@DexterW](https://discourse.mc-stan.org/u/DexterW)
#### Post date: [April 19, 2023, 9:43am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/5 "2023-04-19T09:43:52Z")

</div>

Terrific! Thanks again, Andrew.

---

<div class="post-metadata">

### Author: ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)
#### Post date: [April 19, 2023, 10:34am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/6 "2023-04-19T10:34:44Z")

</div>

> [@andrjohns](#):
>
> @avehtari what’s your perspective on whether or not to explicitly specify the implicit uniform prior?

Marginal likelihood is not defined when prior is improper, and `uniform(negative_infinity(), positive_infinity())` is improper, so it is irrelevant in this thread whether it is defined implicitly or explicitly.

> [@andrjohns](#):
>
> `brms` models are compatible with `bridgesampling` by default and they generate code which uses implicit uniform priors, so I’d think you were fine.

They are not fine with improper priors. The brms documentation example explicitly defines proper priors ([Log Marginal Likelihood via Bridge Sampling — bridge\_sampler.brmsfit • brms](https://paul-buerkner.github.io/brms/reference/bridge_sampler.brmsfit.html))

@DexterW you need to define proper priors to have valid (log) marginal likelihood computation.

---

<div class="post-metadata">

### Author: ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)
#### Post date: [April 19, 2023, 10:41am UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/7 "2023-04-19T10:41:16Z")

</div>

Ah of course, thanks Aki!

---

<div class="post-metadata">

### Author: ![DexterW](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/dexterw/32/16242_2.png) [@DexterW](https://discourse.mc-stan.org/u/DexterW)
#### Post date: [April 19, 2023, 2:48pm UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/8 "2023-04-19T14:48:08Z")

</div>

Thank you, Aki.

I have tested: (1) undefined, (2) large-range flat, (3) large-spread normal.  
`// mu ~ implicit uniform prior`  
`target += uniform_lpdf(mu | -1000, 1000);`  
`target += normal_lpdf(mu | 0, 100);`

The resulting Bayes factor values are very close. I guess it is because the same Jeffreys prior is assumed on \mu for both competing models of the Bayes factor.

---

<div class="post-metadata">

### Author: ![avehtari](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/avehtari/32/5935_2.png) [@avehtari](https://discourse.mc-stan.org/u/avehtari)
#### Post date: [April 19, 2023, 6:33pm UTC](https://discourse.mc-stan.org/t/implicit-uniform-prior-for-computing-the-log-marginal-likelihood/31119/9 "2023-04-19T18:33:07Z")

</div>

> [@DexterW](#):
>
> The resulting Bayes factor values are very close. The resulting Bayes factor values are very close. I guess it is because the same Jeffreys prior is assumed on  
> μ for both competing models of the Bayes factor.

1. The title of this thread says marginal likelihood and not Bayes factor, and bridgesampling package first estimates the marginal likelihoods
2. When computing the Bayes factor directly, it is possible that some terms cancel out, but bridgesampling package estimates the marginal likelihoods first separately and the cancellation of improper prior is not happening.
3. With improprer prior, the marginal likelihood is not defined, but the algorithm used by the bridgesampling package is still returning a finite estimate (which is infinitely wrong). The priors you tested are all flat in the interesting part of the posterior, and as bridgesampling package uses an algorithm that focuses around the posterior draws, and thus provides similar estimates, but the estimates also for the proper priors are likely to be far from the true marginal likelihood. Unfortunately, bridgesampling package doesn’t provide good diagnostic when the estimate is likely to be unreliable.
