Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


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

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


From   Patrick Roland <patrick.rolande@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Why is Mata much slower than MATLAB at matrix inversion?
Date   Fri, 20 Jul 2012 13:11:35 -0700

j is necessarily positive definite. The speed difference seems to hold
for matrices of all sizes. Would very much appreciate any insight
here.

On Fri, Jul 20, 2012 at 12:47 PM, Richard Herron
<richard.c.herron@gmail.com> wrote:
> Your matrix j isn't necessarily positive definite, although it is symmetric.
>
> Does this significant speed advantage stand for real use cases? I
> don't know of a use case where you need to invert a 2000 x 2000
> matrix.
>
> I think this is about all the the help that I can provide; I spend
> most of my time outside of Mata. Hopefully one of the experts can shed
> some light here.
>
> Richard Herron
>
>
> On Fri, Jul 20, 2012 at 3:18 PM, Patrick Roland
> <patrick.rolande@gmail.com> wrote:
>> The matrix formation takes a negligible amount of time. luinv() seems
>> to be (much) faster than cholinv() even for positive definite
>> matrices. Here's the code I used to check:
>>
>> timer_clear()
>> mata
>> m = runiform(2000,2000)
>> j = m*m'
>> timer_on(1)
>> h = cholinv(j)
>> timer_off(1)
>> timer_on(2)
>> p = luinv(j)
>> timer_off(2)
>> timer()
>>
>>
>> On Fri, Jul 20, 2012 at 11:57 AM, Richard Herron
>> <richard.c.herron@gmail.com> wrote:
>>> -cholinv()- would be faster if you met the symmetric, positive
>>> definite requirement. I am also surprised that Mata isn't competitive
>>> with Matlab on matrix inversion (I thought they all used the same
>>> algorithms).
>>>
>>> Have you tried taking the matrix generation step out of the timing
>>> block? Is there a chance that the matrix formation process if
>>> different between the two (Sorry, I don't have Stata and Matlab
>>> installs handy).
>>>
>>> Richard Herron
>>>
>>>
>>> On Fri, Jul 20, 2012 at 2:50 PM, Patrick Roland
>>> <patrick.rolande@gmail.com> wrote:
>>>> luinv() takes about 15 times longer than MATLAB's inverse function.
>>>> Maybe I'm being naive, but why wouldn't all serious matrix languages
>>>> invert matrices at roughly the same speed? Don't they use the same
>>>> linear algebra routines?
>>>>
>>>> Another curiosity is that the luinv() is more general and faster. Why
>>>> would anyone want to use cholinv()?
>>>>
>>>> On Fri, Jul 20, 2012 at 9:17 AM, Richard Herron
>>>> <richard.c.herron@gmail.com> wrote:
>>>>> Patrick, -cholinv()- requires a symmetric positive definite matrix,
>>>>> but I don't think you M matrix is PD. Try -luinv()-.
>>>>>
>>>>> Although for most use cases your matrix will be symmetric, right? Then
>>>>> you should use -invsym()- or a solve function.
>>>>>
>>>>> Richard Herron
>>>>>
>>>>>
>>>>> On Thu, Jul 19, 2012 at 9:22 PM, Patrick Roland
>>>>> <patrick.rolande@gmail.com>
>>>>> wrote:
>>>>>>
>>>>>> I'm comparing two code snippets. In Mata:
>>>>>>
>>>>>> mata
>>>>>> timer_on(1)
>>>>>> M = rnormal(2000,2000,0,1)
>>>>>> J = cholinv(M)
>>>>>> timer_off(1)
>>>>>> timer()
>>>>>> end
>>>>>>
>>>>>> In MATLAB:
>>>>>>
>>>>>> tic;
>>>>>> M = normrnd(0,1,2000,2000);
>>>>>> J = inv(M);
>>>>>> toc;
>>>>>>
>>>>>> I find that MATLAB is about 20 times faster (1.5 seconds vs 30
>>>>>> seconds). Is there something I'm missing here, or is MATLAB just much
>>>>>> faster at matrix inversion? I'd much prefer to use Mata because of
>>>>>> integration with Stata, but if the speed difference is going to be on
>>>>>> this order then MATLAB is more attractive.
>>>>>>
>>>>>>
>>>>>> Any input here is appreciated.
>>>>>> *
>>>>>> *   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/
>> *
>> *   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/


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