Created
March 7, 2018 13:05
-
-
Save khakieconomics/0333a54dff4fabf6204080ca5bf92cb6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
cholesky_factor_corr[P] L_Omega; // the cholesky factor of the correlation matrix of tastes/part-worths | |
} | |
transformed parameters { | |
// here we use the reparameterization discussed on slide 30 | |
matrix[I, P] beta_individual = rep_matrix(beta', I) + z*diag_pre_multiply(tau, L_Omega);; | |
} | |
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); | |
L_Omega ~ lkj_corr_cholesky(4); | |
// 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