Skip to content

Instantly share code, notes, and snippets.

@khakieconomics
Created November 27, 2018 01:02
Show Gist options
  • Save khakieconomics/d0ef578262f575654a2dbd9b4ce4cc40 to your computer and use it in GitHub Desktop.
Save khakieconomics/d0ef578262f575654a2dbd9b4ce4cc40 to your computer and use it in GitHub Desktop.
A simple matrix completion model for fixed K in Stan
data {
int N; // number of individuals
int T; // number of time periods
matrix[N, T] Y; // outcome matrix; missing entries set to -9.0
int K; // rank of matrix
}
parameters {
matrix[N, K] M; // individual loadings
matrix[T, K] U; // time factors
real<lower = 0> sigma; // error scale
positive_ordered[K] gamma; // vector of individual prior scales on time shocks
real<lower = 0> gamma_scale; // hyperscale
}
transformed parameters {
matrix[N, T] Theta = M * U';
}
model {
sigma ~ inv_gamma(1, 1);
gamma_scale ~ exponential(1);
gamma ~ normal(0, gamma_scale);
for(t in 1:T) {
U[t] ~ normal(0, gamma);
}
// likelihood
for(i in 1:N) {
M[i] ~ normal(0, 1);
for(t in 1:T){
if(Y[i,t]!= -9.0) {
Y[i,t] ~ normal(Theta[i,t], sigma);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment