Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: Why is Mata much slower than MATLAB at matrix inversion?

 From Patrick Roland To statalist@hsphsun2.harvard.edu Subject Re: st: Why is Mata much slower than MATLAB at matrix inversion? Date Sat, 21 Jul 2012 13:28:33 -0700

```As a brief followup, and since StataCorp have responded, I tested
matrix multiplication too. Stata 12.0 takes fully 100 times longer to
multiply two 2000x2000 matrices than MATLAB R2011b.

This code took ~21 seconds in Mata:

mata
j = rnormal(2000,2000,0,1)
timer_on(1)
g =j*j'
timer_off(1)
timer()
end

This code took ~0.21 seconds in MATLAB:

j = randn(2000,2000);
tic;
g = j*j';
toc;

Happy to stand corrected if I've made a mistake.

On Fri, Jul 20, 2012 at 6:19 PM, Richard Herron
<richard.c.herron@gmail.com> wrote:
> Snap. Yes, your m from -runiform()- will certainly be invertible.
>
> Richard Herron
>
>
> On Fri, Jul 20, 2012 at 7:14 PM, Patrick Roland
> <patrick.rolande@gmail.com> wrote:
>> To be clear, my point was that all Mata matrix inverse functions are
>> slower than MATLAB. It does seem though that this is not true for
>> small matrices (e.g. 100x100), but the difference is easily an order
>> of magnitude when it comes to larger matrices (2000x2000).
>>
>> The fact that I compared cholinv() and a general inverse function
>> should be to Mata's favor, since cholinv should presumably be faster
>> if it exploits the special structure of the matrix.
>>
>> X'X is positive definite if X is invertible (as in my example),
>> because a'X'Xa  = (Xa)'(Xa) > 0.
>>
>> On Fri, Jul 20, 2012 at 2:48 PM, David M. Drukker <ddrukker@stata.com> wrote:
>>> Patrick Roland <patrick.rolande@gmail.com> posted that the Mata function
>>> -cholinv()- is slower than a Matlab function for large matrices.
>>>
>>> Others have discussed some issues with Patrick's example.  Despite these
>>> issues, we took Patrick's post seriously, looked at the code, and found
>>> something that could be sped up.
>>>
>>> We will release a faster version of -cholinv()- in an upcoming executable
>>> update.
>>>
>>> Note that any speed difference related to -cholinv()- is only noticeable for
>>> large matrices.  For small matrices, such as variance-covariance matrices
>>> for models with 100 or fewer parameters, the difference is much harder to
>>> find.  For example, the computation takes about .001 seconds on my machine.
>>>
>>> Best,
>>> David
>>> ddrukker@stata.com
>>>
>>>
>>> *
>>> *   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/
>> *
>> *   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/
> *
> *   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/
*
*   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/
```