Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khakieconomics/d01b43a50c7904bd73e8c190e0a8ecf8 to your computer and use it in GitHub Desktop.
Save khakieconomics/d01b43a50c7904bd73e8c190e0a8ecf8 to your computer and use it in GitHub Desktop.
data {
int N; // number of rows
int T; // number of inidvidual-choice sets/task combinations
int I; // number of Individuals
int P; // number of covariates
vector<lower = 0, upper = 1>[N] choice; // binary indicator for choice
matrix[N, P] X; // product attributes
int task[T]; // index for tasks
int task_individual[T]; // index for individual
int start[T]; // the starting observation for each task
int end[T]; // the ending observation for each task
}
parameters {
vector[P] beta; // hypermeans of the part-worths
vector<lower = 0>[P] tau; // diagonal of the part-worth covariance matrix
matrix[I, P] z; // individual random effects (unscaled)
}
transformed parameters {
// here we use the reparameterization discussed on slide 30
matrix[I, P] beta_individual = rep_matrix(beta', I) + z*diag_matrix(tau);;
}
model {
// create a temporary holding vector
vector[N] log_prob;
// priors on the parameters
tau ~ normal(0, .5);
beta ~ normal(0, .5);
to_vector(z) ~ normal(0, 1);
// log probabilities of each choice in the dataset
for(t in 1:T) {
log_prob[start[t]:end[t]] = log(softmax(X[start[t]:end[t]]*beta_individual[task_individual[t]]'));
}
// use the likelihood derivation on slide 29
target += log_prob' * choice;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment