So maybe I am misunderstanding what the author is staying, but I am reading Chapter 14 of Kruschke's Doing Bayesian Analysis. I am reading about the software Stan and how it uses the Hamiltonian MCMC (HMC) to sample from the posterior distribution. To recap, HMC generates a proposal (jumping) distribution based on the gradient of the posterior distribution. To do HMC in Stan, we specify a model by defining both a distribution (or likelihood) for the data and the prior, for example:

model {
    theta ~ beta(1,1) ;  // prior
    y ~ bernoulli(theta) // likelihood

But then I read a blurb that says: enter image description here

In other words, in Stan, we are not directly randomly sampling from the posterior (like we would do in Gibbs where we sample from the conditional posterior). Instead, we determine a proposal point via the gradient of the posterior. What I don't get is why are we multiplying the posterior by the likelihood? I don't understand.


  • 4
    $\begingroup$ Just speculation, but I wonder if the author is attempting to explain the conceptually how the code relates to Bayesian inference (prior $\times$ likelihood), not necessarily the exact mechanism that Stan uses to compute a posterior (HMC). $\endgroup$
    – Sycorax
    Commented Nov 7, 2019 at 17:58
  • $\begingroup$ I understand prior * likelihood, but he says the posterior * likelihood, which has me confused. $\endgroup$
    – confused
    Commented Nov 7, 2019 at 18:49
  • 1
    $\begingroup$ The sampling satement y ~ bernoulli(...); is purely notational -- what's actually happening is that the log_probability is incremented by that amount. I think that's what the author means by "current posterior probability." I don't know what the author writes after "In fact, in Stan,..." because it's cut off, but that might be what the author explains ntext. $\endgroup$
    – Sycorax
    Commented Nov 7, 2019 at 18:52
  • $\begingroup$ I included a longer quote. Thanks $\endgroup$
    – confused
    Commented Nov 7, 2019 at 20:21

1 Answer 1


Stan computes a log-posterior density and uses its gradient to do sampling. It does this by incrementing a variable storing the log probability (really, the log kernel. Ben Goodrich points out that Stan only needs to care about the log probability up to constant terms, which are neglected). At each iteration, each sampling statement in the model block increments the log probability variable, so when you have several sampling statements, the value of the log posterior is incremented several times. When the author writes "current posterior" in the quote, the author means "the current value of the log posterior accumulator." That's what the "running total" in the last sentence refers to.

So to answer your question

[W]hy are we multiplying the posterior by the likelihood?

Instead of a "running total" on the log scale, we can do equivalent computations using a "running product" on the original scale, where we compute the posterior density in stages, one product at a time.

Stan still does the computations "under the hood" by incrementing a log probability, but as a matter of convenience, users can write y ~ distribution(arguments) instead of writing increment_log_prob(distribution_log(arguments)) .

  • 1
    $\begingroup$ I would have said "log kernel" instead of "log probability" because the thing being incremented only needs to be within a constant of the log probability density in order for the MCMC algorithm(s) in Stan to work. $\endgroup$ Commented Nov 8, 2019 at 0:13
  • $\begingroup$ @BenGoodrich Yes, I've made an edit to include this detail. Originally, I made the decision to omit that detail because the question seemed focused why the posterior density is updated in steps. Do you think this answer gives a good accounting of how this part of Stan works? $\endgroup$
    – Sycorax
    Commented Nov 8, 2019 at 13:27
  • $\begingroup$ Yes, it is accurate now. $\endgroup$ Commented Nov 8, 2019 at 16:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.