Hi all,
I'm currently comparing more closely the different ways of how to
compute principal components, leading to some remarks or suggestions
(and questions) about the respective documentation.
1) eigensym:
(i) The resulting eigenvalues are ascending, and there's a relatively
complicated code example about how to get them in descending order, namely:
<help-now>
matrix U
e = eigensym(A, &U)
Tmp = msortby((-e' | U)',1)'
e = -Tmp[1,]'
U = Tmp[2:,]
# now largest to smallest eigenvalues
print e U
</help-now>
I guess that this help predates the introduction of the mreverse()
function? Because the same effect can be achieved simply by doing:
<help-simple>
# now largest to smallest eigenvalues
e = mreverse(e)
U = mreverse(U')'
</help-simple>
So, OK to change the help example there? Or am I missing something?
(ii) The mreverse() calls in hansl take a little time. Certainly not
dramatic, but if you need the descending ordering, I found that this
little overhead is what makes the eigensym-based solution slightly
slower than using svd(). So a follow-up question or suggestion: Why not
introduce an optional boolean 3rd arg to eigensym, switching to
descending order? I'm guessing that the reordering at the C level (or
even BLAS?) would be noticeably faster than in hansl.
2) princomp:
There's a verbal explanation of how they are computed, which I find
difficult to follow. I have verified that the following computations
coincide:
<hansl>
clear
A = mnormal(30,10)
matrix v = empty # to hold eigenvectors etc.
# eigensym-based
m = cdemean(A)
eigensym(m'm, &v)
pe = m * mreverse(v')'
# plain princomp
pp = princomp(A, cols(A), TRUE)
set assert warn
assert(sum(abs(pe - pp)) == 0)
</hansl>
I think it would be helpful to include an example like this one in the
princomp help. Or maybe a variant based on the correlation matrix
(default in princomp) instead of the covariance like here. OK?
thanks
sven