--
data {
int<lower=1> K;
cov_matrix[K] S; // scale matrix
real<lower=K> nu; // degrees of freedom
}
transformed data {
matrix[K,K] L_of_S;
L_of_S <- cholesky_decompose(S);
}
parameters {
real subdiagonals[0.5 * K * (K - 1)];
real<lower=0> diagonals[K];
}
model {
matrix[K,K] L;
int count;
count <- 1;
for(j in 1:K) {
L[j,j] <- sqrt(diagonals[K]);
for(i in 1:(j - 1)) {
L[i,j] <- subdiagonals[count];
count <- count + 1;
}
for(i in (j+1):K) {
L[i,j] <- 0.0;
}
}
// priors imply L * L_of_S * L_of_S' * L' ~ Wishart(nu, S);
subdiagonals ~ normal(0.0, 1.0);
for(k in 1:K) {
diagonals[k] ~ chi_squared(nu - k + 1);
}
// rest of model
}
generated quantities {
cov_matrix[K] Sigma;
Sigma <- multiply_lower_tri_self_transpose(L * L_of_S);
}
On Thursday, January 17, 2013 11:38:13 PM UTC-5, Eric Brown wrote:
Instead of defining a covariance matrix as a parameter and calculating its Cholesky factor as a local variable (which involves a lot of auto-diff stuff), I think it would be better to do the reverse and define the (free elements of the) Cholesky factor as a parameter and (if necessary) calculate the covariance matrix as a local variable or generated quantity. There is an example in section 13.1 of the manual that implies a Wishart prior on the covariance matrix. In other words, something like this
data {
int<lower=1> K;
cov_matrix[K] S; // scale matrix
real<lower=K> nu; // degrees of freedom
}
transformed data {
matrix[K,K] L_of_S;
L_of_S <- cholesky_decompose(S);
}
parameters {
real subdiagonals[0.5 * K * (K - 1)];
real<lower=0> diagonals[K];
}
model {
matrix[K,K] L;
int count;
count <- 1;
for(j in 1:K) {
L[j,j] <- sqrt(diagonals[K]);
for(i in 1:(j - 1)) {
L[i,j] <- subdiagonals[count];
count <- count + 1;
}
for(i in (j+1):K) {
L[i,j] <- 0.0;
}
}
// priors imply L * L' ~ Wishart(nu, S);
subdiagonals ~ normal(0.0, 1.0);
for(k in 1:K) {
diagonals[k] ~ chi_squared(nu - k + 1);
}
// rest of model
}
generated quantities {
cov_matrix[K] Sigma;
Sigma <- multiply_lower_tri_self_transpose(L);
}
Ben
On Thursday, January 17, 2013 11:38:13 PM UTC-5, Eric Brown wrote:
--
Ben, Per your guidance in the attached noted, would the geometry / mixing improve to sample a covariance matrix from the parameters of it's cholesky decomposition with the off-diagonals drawn from standard normals and the diagonals from inverse gammas or folded normals, on the one hand, versus decomposing the covariance matrix into a correlation matrix and vector of scales for sampling, on the other hand?
http://www.uow.edu.au/~mwand/wispap.pdf
Asim
Here is another prior on Covariance matrices that you may find useful.
--
On Thursday, January 17, 2013 11:38:13 PM UTC-5, Eric Brown wrote:
--
Ben
- Bob
--