Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: cross-products in Mata


From   Kit Baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: cross-products in Mata
Date   Tue, 7 Jul 2009 09:51:15 -0400

<>
Edwin wrote


i have a view X on k variables and a (k x k) matrix W

i need a vector where row i equals X[i,.]*W*X[i,.]'

at the moment i do the following

xwx = J(st_nobs(), 1, .)
for (i=1; i<=st_nobs(); ++i)
   xwx[i] = X[i,.]*W*X[i,.]'

which does the trick, but i was wondering whether it is possible to speed
this up?


This brings about a 20% speed improvement:

webuse auto,clear
drop make
drop if mi(rep78)
expand 10000

set rmsg on
mata:
st_view(X=.,.,.)
W = runiform(11,11)
end

mata:
xwx = J(st_nobs(), 1, .)
for (i=1; i<=st_nobs(); i++) {
   xwx[i] = X[i,.]*W*X[i,.]'
}
end

mata:
XW = X*W
xwx2 = J(st_nobs(), 1, .)
for (i=1; i<=st_nobs(); i++) {
   xwx2[i] = XW[i,.] * X[i,.]'
}
end


Kit
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index