Differences between jacobian += and target+=?

Hi!
Thanks for another release :)

I’m a bit confused by jacobian +=, and I don’t find much about it. When should I use it? How is it different from target +=?

There is a bit about this in the documentation, but the short answer is that in many situations, they are more-or-less equivalent. The jacobian += statement is useful for both conveying intent and it can be used in transformed parameters, which target += cannot.

When are they not equivalent, you might be asking? Well, the C++ that jacobian += ... gets compiled down to looks something like

if (use_jacobian) {
  target += ...
}

Where the condition value there is usually True, but in some circumstances (like optimization, by default), it is not. This lets you implement your own transforms that work the same way as the built-in transforms, which also do not apply the change of variables within optimization (by default)

Thanks! Very clear answer!

And by the way: Why is it that the jacobian adjustment shouldn’t be used in optimization?

It’s not necessarily that it shouldn’t, you just get two different answers depending on whether you do or not. I think the docs state it more precisely than I could:

Without the Jacobian adjustment, optimization returns the (regularized) maximum likelihood estimate (MLE), the value which maximizes the likelihood of the data given the parameters, (including prior terms).

Applying the Jacobian adjustment produces the maximum a posteriori estimate (MAP), the maximum value of the posterior distribution.

For a long time, Stan only provided optimization for the MLE (no jacobian adjustment). Nowadays, you can request the jacobian adjustments be included to estimate the MAP, but the default is still to exclude them.

AFAICT the docs are wrong, and that the difference between using the Jacobian adjustment or not has been accurately described here: https://users.aalto.fi/~ave/casestudies/Jacobian/jacobian.html#Parameter_transformation_and_Jacobian_adjustment

I had fixed doc elsewhere, but it seems not in this specific part in CmdStan doc. The doc is partially correct, but misses to mention that the mentioned MAP is in the unconstrained space. The Laplace sampling part does mention

The laplace method produces a sample from a normal approximation centered at the mode of a distribution in the unconstrained space. If the mode is a maximum a posteriori (MAP) estimate, the samples provide an estimate of the mean and standard deviation of the posterior distribution. If the mode is a maximum likelihood estimate (MLE), the sample provides an estimate of the standard error of the likelihood. In general, the posterior mode in the unconstrained space doesn’t correspond to the mean (nor mode) in the constrained space, and thus the sample is needed to infer the mean as well as the standard deviation. (See this case study for a visual illustration.)

I’ll add to my TODO list to clarify the optimization section.

Sorry to hijack an old thread, but I have a question. I’m using transformed parameters to do a bunch of sum_to_zero constraints for non-centered parameterisations, so all of the constrained parameters get std_normal() priors. Can I “hack” the jacobian += syntax by incrementing the priors here? The advantage for me is that I don’t need to store a bunch of standard normal parameters so the final model fit object will be quite a bit smaller.

yes, I wouldn’t even call it a hack. The jacobian keyword isn’t magically different from target as it still adds the log adjustment to the target log posterior. The benefit of using jacobian is to easily remove whatever is added here when calling optimization. If you used jacobian for all priors in your model and you turned off jacobian in the optimization call, it would be equivalent to an MLE.

Nope. Turning off Jacobian doesn’t remove priors so if there are priors you don’t get MLE. Whether the Jacobian is included determines whether the mode corresponds to the mode in unconstrained or constrained space. See Laplace method and Jacobian of parameter transformation – Aki Vehtari. This used to be wrong in Stan Reference Manual, but was fixed quite long time ago.

So if I put all my priors in ‘jacobian +=’ that doesn’t equal mle (which is what I said)? It’s the same log prob as a frequentist would use when turning those priors off.

Sorry, it seems I didn’t read the post to which you were replying. So I correct: even if your all priors are included with jacobian, but you have also other parameters with constraints (but no priors) then it’s still about in which space the mode is, so just saying “MLE” is still confusing

I suppose it’s a hack in the sense that you can’t increment the target in transformed parameters, and the only way to avoid having a bunch of standard normal transformed parameters in the returned fit object is to increment the jacobian.

There’s a slightly simpler explanation here. Until we added jacobian +=, there was no way to write a transform in the Stan language that behaved the same way as the built in transforms.

All the built in transforms use jacobian += for their Jacobian adjustments so that we can turn them off when doing penalized maximum likelihood. Turning off the Jacobian lets us get the same answer as you’d get by optimizing within the constrained space.

Now if @spinkney wants to code the priors using jacobian +=, then they will indeed get dropped when you drop the Jacobian and you’ll get a proper MLE (as opposed to a penalized MLE). This is not what we intended with jacobian +=, but users are clever!