How to use paramters in generated quantities?


data{
  int <lower=0> J;  // number of components in the pool
  int <lower=0> K; // K interaction
  matrix<lower=0,upper=1>[K, J] prior_activity;
  matrix<lower=0,upper=1>[K, J] prior_qed;
  matrix<lower=0,upper=1>[K, J] prior_sa;
  
  matrix<lower=0,upper=1>[K, J] observed_activity; // observed activity scores
  matrix<lower=0,upper=1>[K, J] observed_qed; // observed qed scores
  matrix<lower=0,upper=1>[K, J] observed_sa; // observed sa scores
  
  
  int <lower=0> advice [K];
  int <lower=0, upper=1> decision[K]; 
  
}
parameters{
  simplex[3] w;  // weights for activity, qed, sa
  real<lower=1,upper=4> beta1; // bias parameter in prior knowledge
  real<lower=10,upper=40> beta2; // bias parameter in prior knowledge
}


transformed parameters{

  real <lower=0, upper=1> prob[K];
  
  // for (i in 1:K){
  //   if(prior_choice[i]!=advice[i]){
  //     prob[i]=switch_prob[i];
  //   }
  //   else{
  //     prob[i]=1;
  // }
  
}


model {  
  w ~ normal(.5, .15);
  beta1~uniform(1,4);
  beta2~uniform(10,40);
  decision~bernoulli(prob);
}

generated quantities{
  matrix<lower=0,upper=1>[K, J] prior_scores=w[1]* prior_activity+ w[2]*prior_qed + w[3]* prior_sa;
  matrix<lower=0,upper=1>[K, J] observed_scores= w[1]*observed_activity+w[2]*observed_qed+ w[3]*observed_sa;
  vector<lower=0,upper=1>[J] prior_prob;
  vector<lower=0,upper=1>[K] switch_prob;
  // real <lower=0, upper=1> prob[K];

  real logits;
  int prior_choice[K];

  for (i in 1:K){
    prior_prob=softmax(to_vector(prior_scores[i]*beta1));//socres in i-th interaction
    prior_choice[i]=categorical_rng(prior_prob);
    logits=observed_scores[i,advice[i]]- observed_scores[i,prior_choice[i]];
    switch_prob[i]=exp(logits)/(1+exp(logits));
    if(prior_choice[i]!=advice[i]){
      prob[i]=switch_prob[i];
    }
    else{
      prob[i]=1;
    }
  }

I want to model decisions based on some prior and observations. There are 3 parameters w transformed priors and observations to prior score and observation scores. Based on prior score, there is a prior choice using softmax function. Then, either or not to switch prior choice to advice depends on observation score. Now, the final decision to take prior choice or advice will depends on switch probability. The decision is assumed under a bernoulli distribution with a probability prob.
Now, my problem is I don’t know where should I define parameter prob. If I define prob in transformed parameters, then it is not allow to generate a categorical variate and define int variate. If I define prob in generated quantities, then I can’t use prob in the model. How should I organise the code to make it right?

data{
  int <lower=0> J;  // number of components in the pool
  int <lower=0> K; // K interaction
  matrix[K, J] prior_activity;
  matrix[K, J] prior_qed;
  matrix[K, J] prior_sa;
  
  matrix<lower=0,upper=1>[K, J] observed_activity; // observed activity scores
  matrix<lower=0,upper=1>[K, J] observed_qed; // observed qed scores
  matrix<lower=0,upper=1>[K, J] observed_sa; // observed sa scores
  
  
  int <lower=0> advice [K];
  int <lower=0, upper=1> decision[K]; 
  
}
parameters{
  simplex[3] w;  // weights for activity, qed, sa
  real<lower=1,upper=4> beta1; // bias parameter in prior knowledge
  real<lower=10,upper=40> beta2; // bias parameter in prior knowledge
}


transformed parameters{
  matrix<lower=0,upper=1>[K, J] prior_prob;
  
  for (i in 1:K){
    row_vector[J] prior_scores=w[1]* prior_activity[i]+ w[2]*prior_qed[i] + w[3]* prior_sa[i];
    prior_prob[i]=to_row_vector(softmax(to_vector(prior_scores*beta1)));
  }
}



model{
  int prior_choice[K];
  real advantage[K];
  row_vector[J] observed_scores;
  w ~ normal(.5, .15);
  beta1~uniform(1,4);
  beta2~uniform(10,40);
  

  
  for (i in 1:K){
    prior_choice[i] ~ categorical(to_vector(prior_prob[i]));
    observed_scores= w[1]*observed_activity[i]+w[2]*observed_qed[i]+ w[3]*observed_sa[i];
    advantage[i]=observed_scores[advice[i]]- observed_scores[prior_choice[i]];
    decision~bernoulli_logit(beta2*advantage[i]);
  }
}

Now, I change the code to be looked like this. But there is still a problem for prior choices. They should be generated from generated quantities. However, variables from generated quantities are not able to be referred in model block.

I need prior choices for the advantage scores, which would be a parameter for bernoulli distribution. What should I do?

You are trying to find a way to create a discrete parameter, but Stan does not allow you to do this; you must marginalize over discrete parameters. You can generate random numbers from discrete distributions in generated quantities, but there is no way to use those to inform the sampling.

1 Like