# st: re: speeding up mkmat

 From Kit Baum To statalist@hsphsun2.harvard.edu Subject st: re: speeding up mkmat Date Sun, 6 Feb 2005 17:14:10 -0500

I have posted a revised version of mkmat2.ado in the same site, following some improvements suggested by Nick Cox. I have run the following program for various n to determine how these two versions of mkmat behave as n increases.

local x 25
sysuse auto,clear
expand `x'
mat b = J(3,3,0)
mat bb = J(3,3,0)
count
local nobs = r(N)
forv i=1/`nobs' {
mkmat price mpg rep78 if _n==`i', mat(a)
mat a = a'*a
mat b = nullmat(b)+a
}
forv i=1/`nobs' {
mkmat2 price mpg rep78 if _n==`i', mat(aa)
mat aa = aa'*aa
mat bb = nullmat(bb)+aa
}

mat c = a-aa
mat list c

The timing statistics reveal that the ratio in favor of mkmat2 is declining:
+----------------------------------+
| n mkmat mkmat2 ratio |
|----------------------------------|
1. | 74 1.66 .27 6.148148 |
2. | 148 3.39 .65 5.215385 |
3. | 222 5.22 1.04 5.019231 |
4. | 296 6.8 1.52 4.473684 |
5. | 370 8.36 2.28 3.666667 |
|----------------------------------|
6. | 444 11.9 2.85 4.175438 |
7. | 518 12.26 3.73 3.286863 |
8. | 592 13.92 4.55 3.05934 |
9. | 666 15.83 5.45 2.904587 |
10. | 740 17.62 6.53 2.698316 |
|----------------------------------|
11. | 1110 28.52 13.74 2.075691 |
12. | 1480 38.59 23.68 1.629645 |
13. | 1850 48.75 34.42 1.416328 |
+----------------------------------+

But a graph of the mkmat and mkmat2 series against n show something very similar to parallel lines as n increases, with mkmat2 maintaining a similar advantage in terms of elapsed time (about 14 sec. for the larger sample sizes).

Kit Baum, Boston College Economics
http://ideas.repec.org/e/pba1.html

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/