Error when sampling a transformed parameter - Left-hand side of sampling statement (~) may contain a non-linear transform of a parameter or local variable

Hi,

I am trying to change my code but now I always get an error when running my code:

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:
a_mu ~ von_mises(...)

I guess it has something to do with the fact, that mu a_mu is a transformed parameter, because in my previous version of this code, when I didn’t use the unit_vector, everything went fine.
So is it not possible to sample a transformed parameter or where is the mistake? And what could I do?

functions{
 real d_mu_c_helper_fct(real d_mu, real d_sigma, real kappa,
 real rho_1, real rho_2, real angle, vector a_mu) {

 real d_mu_c = d_mu + d_sigma * sqrt(kappa) 
  * (rho_1 *(cos(angle) - cos(a_mu[1])) + rho_2*(sin(angle) - sin(a_mu[2])));

return d_mu_c;
  }
}

data {
    int         K;  // number classes
    int         N;  // number of all data points
    vector[N]    angle; //theta
    vector[N]    dist; //x
}


parameters{
  // variables for von mises distribution, mu and kappa
  unit_vector[2] v_a_mu[K];
  vector<lower=0, upper=8>[K]  kappa;
  // variables for normal distribution, mu and sigma
  vector<lower=0, upper=18> [K] d_mu;
  vector<lower=0, upper=6> [K] d_sigma;	

  real<lower=0, upper=1> rho_1;
  real<lower=0, upper=1-rho_1> rho_2;
}

transformed parameters{

  real rho = sqrt(rho_1 * rho_1 + rho_2 * rho_2); //eq 1.3

  vector[K] a_mu;
  vector[K] d_sigma_c;

  for (k in 1:K){
    a_mu[K] = atan2(v_a_mu[K,2], v_a_mu[K,1]);
    d_sigma_c[k] =  pow(d_sigma[k],2) * (1 - pow(rho,2)); 
  }
}

model{
  d_mu ~ normal (5, 5);
  kappa ~ normal (5, 5);
  rho_1 ~ normal (1, 1);
  rho_2 ~ normal (1, 1);
  d_sigma ~ normal  (2,2);
  a_mu ~ von_mises (4, 2);

  for (n in 1:N) {
    vector [K] pb;
    vector[K] d_mu_c;
    for (k in 1:K) {
  
      d_mu_c[k] = d_mu_c_helper_fct(d_mu[k], d_sigma[k], kappa[k], rho_1, rho_2, angle[n], v_a_mu[k]);
  
     
      pb[k] = von_mises_lpdf(angle[n] | a_mu[k], kappa[k]) + normal_lpdf(dist[n] | d_mu_c[k] , d_sigma_c[k]);
    }
    target += log_sum_exp(pb);
  }
}

That is not an error, but rather a warning. If your code does not execute, then there is some other problem.

First, sampling statements do not imply something is being sampled. Rather a_mu ~ von_mises (4, 2); gets mapped to

increment the posterior kernel (in log units) by the logarithm of the Von Mises PDF for a_mu

except a_mu is a transformed parameter rather than a parameter so the warning above is telling you that if you continue you will not be drawing from the intended posterior distribution. The question is: If a_mu is distributed according to the Von Mises distribution, then what is the PDF of the parameter v_a_mu. The answer is the Von Mises PDF — written in terms of v_a_mu — multiplied by the absolute value of the determinant of the Jacobian matrix for the transformation from v_a_mu to a_mu. This change-of-variables process is talked about a lot in the Stan User Manual and is also discussed at

So, you would need to increment target by the logarithm of that absolute determinant. Usually it is easier to reformulate your problem so that the priors can be applied directly to the things declared in the parameters block.

1 Like