3.4 Loading matrix for factor analysis
Rick Farouni, on the Stan users group, inquired as to how to build a Cholesky factor for a covariance matrix with a unit diagonal, as used in Bayesian factor analysis (Aguilar and West 2000). This can be accomplished by declaring the below-diagonal elements as parameters, then filling the full matrix as a transformed parameter.
data {
int<lower=2> K;
}transformed data {
int<lower=1> K_choose_2;
1)) / 2;
K_choose_2 = (K * (K -
}parameters {
vector[K_choose_2] L_lower;
}transformed parameters {
cholesky_factor_cov[K] L;
for (k in 1:K) {
1;
L[k, k] =
}
{int i;
for (m in 2:K) {
for (n in 1:(m - 1)) {
L[m, n] = L_lower[i];0;
L[n, m] = 1;
i +=
}
}
} }
It is most convenient to place a prior directly on L_lower
.
An alternative would be a prior for the full Cholesky factor L
,
because the transform from L_lower
to L
is just the
identity and thus does not require a Jacobian adjustment (despite the
warning from the parser, which is not smart enough to do the code
analysis to infer that the transform is linear). It would not be at
all convenient to place a prior on the full covariance matrix L * L'
, because that would require a Jacobian adjustment; the exact
adjustment is detailed in the reference manual.