Rewrite lpdf in transformed parameters for multivariate normal distribution

Hi,

I have the following code that uses the Stan function of the multivariate normal distribution Cholesky parameterization:

data {
  int<lower=0> N;
  vector[N] x;
  vector[N] y;
  real mu_x;
  real my_y;
}

parameters {
  real<lower=0> sigma_x;
  real<lower=0> sigma_y;
  real<lower=-1, upper=1> rho;
}

model {
  sigma_x ~ normal(0, 20);
  sigma_y ~ normal(0, 20);

  vector[2] mu = [mu_x, mu_y]';
  // cholesky decomposition of covariance matrix
  matrix[2, 2] L_Sigma = [[sigma_x, 0], [rho * sigma_y, sigma_y * sqrt(1 - rho * rho)]];
  target += multi_normal_cholesky_lpdf([x, y]' | mu, L_sigma);
}

I want to rewrite this code and make the variable of interest be a transformed parameter. This is a suggestion of Ben Goodrich in the following topic.
To sample from the X ~ N(mu, Sigma) we can use the following:

X = mu + chol(L) * z

I use the above to write the following code

data {
  int<lower=0> N;
  vector[N] x;
  vector[N] y;
  real mu_x;
  real my_y;
}

parameters {
  vector[N] z;
  real<lower=0> sigma_x;
  real<lower=0> sigma_y;
  real<lower=-1, upper=1> rho;
}

transformed_parameters {
  vector[2] mu = [mu_x, mu_y]';
  // cholesky decomposition of covariance matrix
  matrix[2, 2] L_Sigma = [[sigma_x, 0], [rho * sigma_y, sigma_y * sqrt(1 - rho * rho)]];
  [x, y]' = mu + (l_Sigma * z);
}

model {
  target += normal_lpdf(z | 0, 1);
  target += normal_lpdf(sigma_x | 0, 20);
  target += normal_lpdf(sigma_y | 0, 20);
}

Is this done correctly? In the end I want to write code for non-centered parameterization of the multivariate student-t and multivariate normal-skew distributions.
Thanks in advance

1 Like