linear algebra - Accelerate the calculation of inv(X'*X)*Q*inv(X'*X) in Matlab? -
i have calculate newey-west standard errors large multiple regression models.
the final step of calculation obtain
nwse = sqrt(diag(n.*inv(x'*x)*q*inv(x'*x)));
this file exchange contribution implements
nwse = sqrt(diag(n.*((x'*x)\q/(x'*x))));
this looks reasonable, in case (5000x5000 sparse q , x'*x) it's far slow needs (about 30secs, have repeat 1 million different models). ideas how make line faster?
please note need diagonal, not entire matrix , both q , (x'*x) positive-definite.
i believe can save lot of computation time explicitly doing lu factorization, [l, u, p, q] = lu(x'*x);
, use factors when doing calculations. also, since x
constant 100 models, pre-calculating x'*x
save time.
note in case, time demanding operation might sqrt
-function.
% constant every 100 models or so: m = x'*x; [l, u, p, q] = lu(m); % now, guess should quite bit faster (i might have messed order): nwse = sqrt(diag(n.*(q * ( u \ (l \ (p * q))) * q * (u \ (l \ p)))));
the first 2 terms commonly used:
l
lower triangular matrix
u
upper triangular matrix
now, p
, q
bit more uncommon.
p
row permutation matrix used obtain numerical stability. there not performance gain in doing [l, u, p] = lu(m)
compared [l, u] = lu(m)
sparse matrices.
q
offers significant performance gain. q
column permutation matrix used reduce amount of fill when doing factorization.
note [l, u, p, q] = lu(m)
valid syntax sparse matrices.
as why using full pivoting described above should faster:
try following see purpose of column permutation matrix q
. easier work elements aligned around diagonal.
s = sprand(100,100,0.01); [l, u, p] = lu(s); spy(l) figure spy(u)
now, compare this:
[ll, uu, pp, qq] = lu(s); spy(ll); figure spy(uu);
unfortunately, don't have matlab here right now, can't guarantee put arguments in correct order, think it's correct.
Comments
Post a Comment