Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Eigenvectors in Stata (mata) vs. Matlab

    Hi everyone,
    maybe this is a very silly question and I am overlooking something very, very obvious. I am interested obtaining the eigenvectors from a symmetric matrix. Comparing results from mata and Matlab I note that the eigenvectors are the same in absolute value, but the sign is different for some columns. Does anyone know why this is the case? The sign in my case matters as I need the eigenvectors for further steps in some calculations and I need to ensure that my Stata/mata code reproduces some Matlab code.

    Let me show the following example:

    Code:
    clear all
    set seed 123
    
    mata a1 = rnormal(100,10,0,1)
    mata a = quadcross(a1,a1)
    mata eigensystem(a,evec=.,ev=.)
    mata evec
    Mata returns the following eigenvectors:

    Code:
    . mata evec
                      1             2             3             4             5             6             7             8             9            10
         +---------------------------------------------------------------------------------------------------------------------------------------------+
       1 |   .154773751    .430540368    .157377265    .736356707   -.190048099   -.039730741   -.266316045    .330046069    .043541535    .065145637  |
       2 |   .560311284   -.070572586      .5335531   -.183320564   -.103330895   -.392098006    .262833648    .164038255   -.127964573     -.2932615  |
       3 |  -.107331677    .315302011   -.101299235   -.278079616   -.329385204    .128312068    .028175163    .171943027    .629334735   -.500095683  |
       4 |   -.11680293    .628603515    .203390955   -.227595329    .022151179    .416304179    .370359529    .040548746   -.416397393    .109773256  |
       5 |     .3366534    .227323037   -.535369916    .223776312    .515636807   -.093825465    .321012737   -.085672388    -.01291286   -.336231658  |
       6 |  -.396290022   -.307470176   -.185830684    .299810497   -.378426197   -.072974618     .50877657    .276063847   -.285346408   -.242853522  |
       7 |  -.225810941   -.253774484    .384405925    .109023332    .582850665    .358419676   -.082034656    .376075698     .07693423   -.320457292  |
       8 |  -.089692161     -.0261965    .377944666    .372175253    -.05496082    .141525952    .269537163    -.73266162    .234119525   -.150355911  |
       9 |  -.321274722    .161023461    .142640628   -.009946111    .281437301   -.488882304    .386767574    .170962276    .408914718    .431471158  |
      10 |   .451994837   -.279046218   -.082544808    .067696836   -.114842988    .503268107    .362163387    .204810529    .320544564    .405106383  |
         +---------------------------------------------------------------------------------------------------------------------------------------------+
    
    mata colsum(evec)
                     1             2             3             4             5             6             7             8             9            10
        +---------------------------------------------------------------------------------------------------------------------------------------------+
      1 |   .246530821    .825732429    .894267896    1.10989732     .23108175    .460318847    2.16127507    .916154439    .870768073   -.831759133  |
        +---------------------------------------------------------------------------------------------------------------------------------------------+
    While if I do the same in Matlab:
    Code:
    [V,D] = eig(a);
    V =
        0.0651   -0.0435    0.3300   -0.2663   -0.0397    0.1900    0.7364   -0.1574    0.4305    0.1548
       -0.2933    0.1280    0.1640    0.2628   -0.3921    0.1033   -0.1833   -0.5336   -0.0706    0.5603
       -0.5001   -0.6293    0.1719    0.0282    0.1283    0.3294   -0.2781    0.1013    0.3153   -0.1073
        0.1098    0.4164    0.0405    0.3704    0.4163   -0.0222   -0.2276   -0.2034    0.6286   -0.1168
       -0.3362    0.0129   -0.0857    0.3210   -0.0938   -0.5156    0.2238    0.5354    0.2273    0.3367
       -0.2429    0.2853    0.2761    0.5088   -0.0730    0.3784    0.2998    0.1858   -0.3075   -0.3963
       -0.3205   -0.0769    0.3761   -0.0820    0.3584   -0.5829    0.1090   -0.3844   -0.2538   -0.2258
       -0.1504   -0.2341   -0.7327    0.2695    0.1415    0.0550    0.3722   -0.3779   -0.0262   -0.0897
        0.4315   -0.4089    0.1710    0.3868   -0.4889   -0.2814   -0.0099   -0.1426    0.1610   -0.3213
        0.4051   -0.3205    0.2048    0.3622    0.5033    0.1148    0.0677    0.0825   -0.2790    0.4520
    
    sum(V)
    ans =
    
       -0.8318   -0.8708    0.9162    2.1613    0.4603   -0.2311    1.1099   -0.8943    0.8257    0.2465
    To make the readability easier, I added the column sums which nicely display the problem. First you note that the order is reversed, Matlab displays the eigenvalues in ascending order, mata in descending order. Secondly I noted that columns (Stata perspective) 1, 2, 4, 6, 8 and 10 have the same sign, the others have reversed signs.

    My question is: why is this the case? Any thoughts would be highly appreciated!

    Thanks!

    Jan

  • #2
    Jan, the following two sources may help:

    https://stackoverflow.com/questions/...igns-sometimes
    https://stats.stackexchange.com/ques...igenvectors-wi

    Comment


    • #3
      Thanks for this. This helped to understand the issue.

      My next question is then, how do I make sure that Stata produces the same results as Matlab?

      Comment


      • #4
        I solved it! I using the first k eigenvectors as regressors in a regression and I am interested in the residuals of that regressions. The estimated coefficients will account for the sign so that the residuals will be correct.

        Thanks a lot!

        Comment


        • #5
          Fairly interesting. As eigenvalues may be arbitrarily signed, any valid method relying on them should not be influenced by the signs.

          Comment


          • #6
            #5 muddies the waters. The issue is not with eigenvalues, but with eigenvectors. The point as I see is not to expect eigenvector results to be identical, because signs alone are at choice, but to make sure that differences do not bite.

            Comment

            Working...
            X