Skip to content

Instantly share code, notes, and snippets.

@soonraah
Created October 5, 2014 16:22
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 soonraah/3813bd0533654b4dc038 to your computer and use it in GitHub Desktop.
Save soonraah/3813bd0533654b4dc038 to your computer and use it in GitHub Desktop.
Stan code to train multi dimensional GMM (Gaussian Mixture Model) with diagonal covariance.
data {
int<lower=1> D; // number of dimensions
int<lower=1> N; // number of samples
int<lower=1> M; // number of mixture components
vector[D] X[N]; // data to train
}
parameters {
simplex[M] weights; // mixture weights
vector[D] mu[M]; // means
vector<lower=0.0>[D] sigma[M]; // standard deviation
}
model {
real ps[M];
for(n in 1:N){
for(m in 1:M){
ps[m] <- log(weights[m]);
for(d in 1:D){
ps[m] <- ps[m] + normal_log(X[n, d], mu[m, d], sigma[m, d]);
}
}
increment_log_prob(log_sum_exp(ps));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment