Priors on transformed parameters
In Stan a sampling statement can be written on anything the model computes: put the product of two parameters on the left of a ~ and it reads like a prior on the product. It is not quite one. The statement multiplies the density over the parameters that are actually sampled, and how that density carries over to the product depends on how the product stretches the space. To give the product the distribution written, the model also has to add the log of the transform's Jacobian. And two statements that each give their quantity the right distribution can still make the wrong joint distribution.
1 With and without the Jacobian
Stan samples parameters, here a and b, from the density the model block writes. A statement c ~ p with c = f(a, b) multiplies that density by p(f(a, b)). But changing variables from (a, b) to (a, c) divides a density by |∂c/∂b|, so for a and c to come out as stated the model has to multiply by |∂c/∂b| as well: target += log(abs(…)). Where that factor is a constant, as for sums, leaving it out changes nothing. Choose one of five transforms and compare it with and without its adjustment.
What Stan samples
Each quantity
The example uses k = 1.
2 Two marginals are not a joint
Suppose a and b are meant to be independent, a ~ N(0, 1) and b ~ N(0, s²), so that c = a + b ~ N(0, 1 + s²). Separate statements for the intended marginals of a and c give each quantity the right distribution and the Jacobian is 1, yet b comes out wrong: statements multiply, so these two make a and c independent, which they were never meant to be. One statement for (a, c) together, with their covariance of 1, gives back what was meant. With a fixed as data, the difference is in b's posterior.
a and b, with a as data
a and c
The example uses 1, so c ~ N(0, √2).
| Meant | As written |
|---|
How it works
The change of variables
If (a, b) has density p(a, b) and c = f(a, b) is one-to-one in b, then (a, c) has density p(a, b) / |∂c/∂b|. A model whose density over (a, b) is pa(a) pc(f(a, b)) |∂c/∂b| therefore gives (a, c) the density pa(a) pc(c): both as stated, and independent. Without the last factor, (a, c) carries 1/|∂c/∂b| as well. For c = ab that is 1/a, which tilts a log-normal a down by one on the log scale and makes a normal a's integral diverge at 0. Scripts and agents can get every number here through the API.
Computed exactly
Every distribution on this page is a closed form: normals, a Cauchy (the ratio of two independent standard normals), the chi distribution and the χ². The contours of panel 1 hold exactly 50% and 90% of the mass: for a bivariate normal at its Mahalanobis radii, and for the normal product by integrating the standard normal pair (a, ab) over each level set. Panel 2's joint distributions are bivariate normals; with a as data, b's posterior is the stated prior's conditional distribution given a, from which each dot is one seeded draw.
When a target is not a distribution
A missing Jacobian can make a target improper: its density cannot be normalised, so it does not define a probability distribution. In the square transform with one degree of freedom and a > 0, omitting 2a makes the integral diverge at 0; in the normal product, omitting |a| makes the integral diverge there. Sampling cannot resolve an undefined distribution.