When to adjust for jacobian and when to skip in pymc3

When do you adjust for the jacobian?

option1: You can either not worry about the jacobian

with pm.Model() as transform_model:
    y = pm.Gamma('y', 2, 4)
    y_inv = pm.Deterministic('y_inv', 1 / y)
    mydata = pm.Normal('mydata', mu=0, tau=y_inv, observed=obs)

option2: or adjust for it like following:

with pm.Model() as transform_model:
    y = pm.Gamma('y', 2, 4)
    y_inv = pm.Deterministic('y_inv', 1 / y)
    constrain = pm.Potential('constrain', -2 * tt.log(y_inv))
    mydata = pm.Normal('mydata', mu=0, tau=y_inv, observed=obs)

The above examples are borrowed from the stan manual.
I am confused because in stan, it seems that you don’t have to correct for the jacobian if the parameter of interest in the parameter block like following:

parameters {
  real<lower=0> y;
}

transformed parameters {
  real<lower=0> y_inv;
  y_inv = 1 / y; 
}

model {
  y ~ gamma(2,4);
}

whereas if the transformed variable is in the parameter block but you sample the untransformed variable, you want to correct for the jacobian

parameters {
  real<lower=0> y_inv;
}
transformed parameters {
  real<lower=0> y;
  y = 1 / y_inv;  // change variables
}
model {
  y ~ gamma(2,4);
  target +=  -2 * log(y_inv);  //  Jacobian adjustment;
}

parameter, transformed parameter concepts are not present in the pymc3 context… so which one is the correct way?

Transformed parameter are also presented in pymc3. You can have a look at the related session in our doc: https://docs.pymc.io/developer_guide.html#random-variable, https://docs.pymc.io/Probability_Distributions.html?highlight=probability#auto-transformation, and https://docs.pymc.io/notebooks/api_quickstart.html#Transformed-distributions-and-changes-of-variables.

There is no correct way as it is depending on your application and your intention. For example, you might not want to adjust for jacobian when you are doing non-center parameterization.