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);
}
}