Constraints and target log density output

While working with truncated distributions, I noticed that the target
log density output (lp__) differs depending on whether a lower bound constraint
is specified for a parameter or not, even if the shape of the
distribution remains more or less unchanged.

For example, if a parameter is specified as coming from a
scaled_inv_chi_square(5, 1), the resulting lp__ differs depending on
whether <lower=0> is specified for the parameter.

Also, when there is a lower bound, the lp__ differs from ‘manually’
generated lp (specified in the generated quantities block), but this
is not the case when there is no lower bound.

I was wondering why this discrepency occurs and whether it is intended
behaviour?

Stan code:

    parameters {
      real sigma;
      // OR real sigma<lower=0>;
    }
    model {
      target += scaled_inv_chi_square_lpdf(sigma | 5, 1);
    }
    generated quantities {
      real gen_lp;
      gen_lp = scaled_inv_chi_square_lpdf(sigma | 5, 1);
    }

Draws summary:

    # no lower bound
                          mean    se_mean       sd    n_eff     Rhat
     sigma            1.678592 0.04436190 2.345639 2795.773 1.003143
     gen_lp          -1.243732 0.02225399 1.246196 3135.859 1.002770
     lp__            -1.243732 0.02225399 1.246196 3135.859 1.002770
    # <lower=0> on sigma
                          mean     se_mean        sd    n_eff     Rhat
     sigma            1.696977 0.022947537 2.3487140 10475.82 1.000356
     gen_lp          -1.256868 0.011323521 1.2504616 12194.89 1.000206
     lp__            -1.033168 0.007030302 0.7664612 11885.91 1.000221
1 Like

Sorry, short on time, but this is likely intended: with the lower bound constraint you are sampling a different posterior. AFAIK, target is the log prob as a function of the unconstrained parameters and hence contains the Jacobian adjustment when you use constraints.

I’d however ask @jonah to double check, if I am making sense.

1 Like