I am a Stan user for two or three years, but I am not clear how to state model block properly using Jacobian. So, I prepare several basic examples to code a model block with Jacobian.
Let (\Omega, \mathcal{A}) be a mesurable space throughout this post.
For frequentist, the following example is maybe sufficient.
Question.1 ~ transformation of data ~
Let X:\Omega \to [-\pi/2,\pi/2] be a random variable from an unknow distribution.
Consider the model defined by
\tan(X) \sim \text{Norml}(\mu, \sigma^2),
whose priors are
\mu \sim \text{Uniform}(-\infty ,\infty),
\sigma \sim \text{Uniform}(0,\infty).
Then the question is âCode this model with Jacobianâ.
Answer.1
Let f(y|\mu, \sigma) be the heat kernel.
Because
f(y|\mu, \sigma)dy=f(\tan(x)|\mu, \sigma)dy=f(\tan(x)|\mu, \sigma)\frac{d(\tan(x))}{dx}dx=f(\tan(x)|\mu, \sigma)\frac{1}{cos^2(x)}dx,
we obtain
\log(f(y|\mu, \sigma)) = \log f(\tan(x)|\mu, \sigma) -2 \log \cos(x).
Thus in the model block, I guess, I should add the last term -2 \log \cos(x) in the model block.
The code is shown in the last part of this post. Is it correctly coded?
I guess the sign is wrong maybe âŚ
Question.2 Transformation on parameter space \Theta
Let X:\Omega \to \mathbb{R} be a random variable from an unknow distribution.
Consider the model,
X \sim \text{Norml}(\mu, \exp(\theta)),
whose priors are
\mu \sim \text{Uniform}(-\infty ,\infty),
\theta \sim \text{Uniform}(-\infty,\infty).
Then the question is âDose any Jacobian adjustment is required?â. If the answer is not, then there is a further question that âIs there a transformation of paramter spaces for which any Jacobian adjustment is required?â. and if so, then please show a simple example.
Answer.2
Let \pi(\theta|x) be posterior with a model paramter \theta and a realization of X is denoted by the letter x.
Because for \pi(\theta|x) ⌠I guess some Jacobian adjustment is required but I am not sure.
Question.3 Linear Transformation of data for precise log likelihoods
Let X:\Omega \to \mathbb{R}^d be a random variable from an unknow distribution.
Let A be an invertible, orientation-preserving (\det A >0) matrix of size d \times d.
Consider the model,
AX \sim \text{Norml}(\mu, v),
whose priors are
\mu \sim \text{Uniform}(-\infty ,\infty),
v\sim \text{Uniform}(0,\infty).
Then the question is âDose Stan calculates likelihoods by taking account the matrix A ?â.
Explanation of this question 3
Let f(y|\mu, \sigma) be the heat kernel.
Because
f(y|\mu, \sigma)dy=f(Ax|\mu, \sigma)dy=f(Ax|\mu, \sigma)\frac{dAx}{dx}dx=f(Ax|\mu, \sigma)(\det A )dx,
we obtain
\log(f(y|\mu, \sigma)) = \log f(\tan(x)|\mu, \sigma) + \log \det A.
OrâŚ
\log f(\tan(x)|\mu, \sigma) =\log(f(y|\mu, \sigma)) - \log \det A ?
Thus in the model block, I guess, I should add (or minus? please tell me with the reason) the last term \log \det A in the model block. But, in the past, Stan only warns if the transfromation is non-linear, so from this fact, I guess Stan takes account the Jacobian if transfomation is linear. Is my guess correct? I also gess as same as ~
statement (e.g., x ~ noraml(a,b)
) the linear transformation gives a constant in log likelihoods and hence I also guess Stan drops the constrant term \log \det A. So, I am not sure what Stan dose for linear transfom and whether Stan takes account the \log \det A in log likelihoods (denoted by lp__
in stanfit
objects).
The code of Answer 1
stanmodelcode <- "
data {
int<lower=0> N;
real x[N];
}
parameters {
real mu;
}
model {
target += normal_lpdf(mu | 0, 10);
target += normal_lpdf(tan(x) | mu, 1);
for(i in 1:N)target += -2*log(cos(x[i]));
}
"
x <- runif(20, min = -3.14/2, max = 3.14/2)
dat <- list(N = 20, x = x);
f <- stan(model_code = stanmodelcode, model_name = âexampleâ,
data = dat, iter = 1111, chains = 1)
rstan::check_hmc_diagnostics(f)
If my guess is correct, then also please let me know. Sorry for bothering,
I would appreciate it if you could reply