# Jacobian of transform

I want to know what should I do according to the following sentence :

DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable.
If it does, you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
AA[i,j] ~ normal(...)


where, AA[i,j] is defined by the following;

 AA[i,j] =Phi( (mu[i,j]/v[i,j])/sqrt((1/v[i,j])^2+1) ).
equivalently,
A_{i,j}:= \Phi (\frac{\mu_{i,j}/\sigma_{i,j}}{\sqrt{(1/\sigma_{i,j})^2+1}}),

where mu and v are also parameter which should be estimated, and Phi are the cumulative
distribution function of the standard Gaussian distribution.

In the model block, I write as follows:

 AA[i,j] ~ normal(A[i],hyper_v[j]);
equivalently
A_{i,j} \sim Normal (A_{i},\sigma_{j}^2)
where hyper_v[j] and A[i]is parameter should be estimated.

Chapter 35 of the manual probably describes this better than I can, but I can try to explain the way I think about it, with an example.

Let’s say you have a parameter \sigma that’s the standard deviation of a Gaussian, and you’re trying to estimate it. Say you put some prior on it like say a lognormal. So in Stan we might have

parameters {
real<lower=0> sigma;
}
model {
sigma ~ lognormal(1.0, 0.2);
}


Well, if you wanted to think instead in terms of \sigma^2, you might naively try

parameters {
real<lower=0> sigma;
}
model {
real<lower=0> sigma_sq = sigma^2;
sigma_sq ~ lognormal(1.0, 0.2);
}


but this would NOT be equivalent, and you would get the same warning you got. You can even try this in R by taking 1000 samples of a lognormal then taking another 1000 samples and taking their square root. The histograms will look different.

To actually make it equivalent, you would need to include the so-called log absolute determinant of the Jacobian, and chapter 35 of the Stan manual describes that in more detail.

In summary, it’s just a warning to make sure you know you’re doing what you think you’re doing. I hope that helps.

1 Like

Thank you for your kind reply, so I read Chap. 35 from 403 to 404 (later I have to more read), but I can not com up anything for my code. If I use target += formulation, then the error of my title disappears. The estimates with title error are not precise value? or wrong value?

can you post how do you fix the problem, I encounter the same problem.

The error says that you need to include a target += statement with the log absolute determinant of the Jacobian of the transform.

Thus I used the target formulation, and in my case, the error vanished.

As an example ( which is from the code ?rstan),
use the former code rather than lator codes.
I tried to make a reproducible example to show the efficacy of the target formulation but I cannot.

target formulation

stanmodelcode <- "
data {
int<lower=0> N;
real y[N];
}

parameters {
real mu;
}

model {
target += normal_lpdf(mu | 0, 10);
target += normal_lpdf(y  | mu, 1);
}


Non-target formulation

stanmodelcode <- "
data {
int<lower=0> N;
real y[N];
}

parameters {
real mu;
}

model {
mu ~ normal(0, 10);
y ~ normal(mu, 1);
}


To tell the truth, I do not understand the statement with the log absolute determinant of the Jacobian of the transform. I do not use the Jacobian, thus I am not sure that this treatment is correct.

I am a very beginner, so you need to ask someone to get an obvious answer.

I guess the warning means the follwoig

Let f(y|\theta) be a likelihood for data y and model parameter \theta.
Let \theta' = \varphi(\theta) be a non-linear parameter transformation.

Then for any function \psi(y,\theta),

\int \psi(y,\theta')f(y,\theta')dy d\theta' =\int \psi(y, \varphi(\theta))f(y, \varphi(\theta)) |\det (\frac{\partial \varphi(\theta)}{\partial \theta} )|dy d\theta.

Thus, we obtain

f(y,\theta') is written by f(y, \varphi(\theta)) |\det (\frac{\partial \varphi(\theta)}{\partial \theta} )|,
from which,
\log f(y,\theta') is written by \log f(y, \varphi(\theta)) + \log |\det (\frac{\partial \varphi(\theta)}{\partial \theta} )|.
In HMC calculation, Stan codes with Non-target transformations drop the constant term in the likelihood function. To include the canstant term for precise calculation of likelihood, we use target formulations. Furthermore , Stan cannot calculate the Jacobian, namely \log |\det (\frac{\partial \varphi(\theta)}{\partial \theta} )|. Thus we have to calculate it by hands and add it in the mode block. This is my guess, but I am not sure that it is correct. But I am not sure why the worning restrict the case in non-linear. Can Stan calculates the Jacobian in case of linear transformation?

I’m embarrassing because my English in the previous posts was crazy :’-D

1 Like