Announcement

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

  • Construct a Symmetric Matrix and Compute Eigenvalue

    Dear All,
    I previously posted on the General forum (http://www.statalist.org/forums/foru...-two-variables). But this question is more appropriate for mata or other matrix related forum. Taking the advice from a regular stata forum user, I hope to clarify my problem and get help from you all. Thanks very much !
    I have a stata dataset with over 70,000 observations, below is a pretend data :

    Industry_row Industry_column xvar
    11A0 1121 2
    11A0 11A0 1
    11A0 3122 4
    11B0 1129 2
    11B0 2111 3

    Industry_row is the input industry, and industry column is the output industry. My real data has 450 unique Industry_row and 473 unique Industry column.

    Task 1: develop a matrix that has rows from input industry and columns from output industry and the elements of the matrix contain xvar value. If the row and column pair has no xvar value, so the expected output in matrix form:
    1121 11A0 3122 1129 2111
    11A0 2 1 4
    11B0 2 3
    My research question is to compute the eigenvalue and construct the eigenvector using the adjacency matrix above. My matrix needs to be square and symmetric. Therefore,


    Task 2 is to expand the matrix , I thought of using all combinations of input and output industries, that is the matrix will be 6*6 dimensions with (11A0, 11B0, 1121, 3122, 1129, 2111) as rows and columns. the xvars will be zeros for the row and column pairs that were not in the dataset.

    Task 3: I will compute eigenvector centrality measure (a network measure), this can only be applied to symmetric adjacency matrices. Therefore, I need to make the matrix from step 2 symmetric . Following the literature, I plan to take the maximum value of the upper and lower triangles

    the resulting matrix should look like
    11A0 11B0 1121 3122 1129 2111
    11A0 1 0 2 4 0 0
    11B0 0 0 0 0 2 3
    1121 2 0 0 0 0 0
    3122 4 0 0 0 0 0
    1129 0 2 0 0 0 0
    2111 0 3 0 0 0 0
    basically, if the original data has 11A0 1121 with xvar =2, we will assign 1121 11A0 pair's xvar a value of 2 (replace 0).

    I got help from Nick, Roberto and a few others sample programs to do task 1 and 2 from my previous posting (http://www.statalist.org/forums/foru...-two-variables). Based on their suggestion, I post it to this new forum related to matrix.

    If you could help with task 3, to make it symmetrical, and task 4, compute eigenvalue and develop eigenvector for this matrix, that will be greatly appreciated.

    Best,

    Rochelle



  • #2
    Use the makesym function:

    Code:
    clear all
    
    mata
    
    void function makesym(matrix M) {
        if (rows(M)!=cols(M)) {
        exit(error(1))
        }
        for(i=1;i<=rows(M);i++) {
          for(j=1;j<i;j++) {
            mx=max(M[i,j] \ M[j,i])
            M[i,j]=mx
            M[j,i]=mx      
          }
        }    
    }
    
    
    //A=1,2,3\4,5,6\7,8,9
    A=runiform(3,3)
    A
    makesym(A)
    A
    end

    Example:
    Code:
    : A
                     1             2             3
        +-------------------------------------------+
      1 |  .2648020918   .9477426449   .2769154108  |
      2 |  .1180158504   .4079702524   .7219491643  |
      3 |  .8716910495   .4611428848   .4216726252  |
        +-------------------------------------------+
    
    : makesym(A)
    
    : A
    [symmetric]
                     1             2             3
        +-------------------------------------------+
      1 |  .2648020918                              |
      2 |  .9477426449   .4079702524                |
      3 |  .8716910495   .7219491643   .4216726252  |
        +-------------------------------------------+


    Best, Sergiy Radyakin

    Comment


    • #3
      Just to flag that what you are doing is, in effect, changing data. I didn't pick up a reason from your thread on why that made sense and I don't want to be cited as endorsing or encouraging it.

      Comment


      • #4
        To Sergiy,

        Thank you very much for the code. I can see how it works. But I need to learn how to use my matrix A (473*473 dimension)(stored as column, row pairs) to be readable into your code. I never used mata, so I need to learn this part.

        To Nick,

        Please find attached paper in which the authors discuss how to apply eigenvector to asymmetric matrix. On page 197, paragraph 1 the author discuss that the alpha-centrality approach can be used when position in a network do not receive any nominations ( it seems to apply to my research project, input industry feeds into output industry, but not vice versa). They arue that the solution in 6(a) is a reasonable extrapolation of the eigenvector approach .

        My plan to take the maximum value of the upper and lower triangles to make the matrix symmetric, and use the eigenvector corresponding to the largest eigenvalue of this symmetric matrix as the eigenvector centrality - this follows this paper's discussion on page 194 below equation (7).

        Of course, I understand your concern and I respect your decision not to be cited.

        Sincerely,

        Rochelle

        Comment


        • #5
          Sorry, please see attachment.
          Attached Files

          Comment


          • #6
            Originally posted by Rochelle Zhang View Post
            I need to learn how to use my matrix A (473*473 dimension)(stored as column, row pairs) to be readable into your code. I never used mata, so I need to learn this part.
            In your previous thread (connecting with this one) there's code that creates a matrix in Stata and then takes it to Mata in order to do computations there. st_matrix is used.
            You should:

            1. Read the FAQ carefully.

            2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

            3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

            4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

            Comment


            • #7
              Rochelle, you don't have to use matrices to solve this issue. If your data is in pairs of coordinates (R,C, and value) then all you need to do is swap R and C for cases where R>C, then collapse by (R,C) retaining the maximum:

              Code:
              clear all
              set obs 25
              generate r=floor((_n+4)/5)
              generate c=mod(_n-1,5)+1
              generate v=runiform()
              format v %6.4f
              list, sepby(r)
              
              gen r1=cond(r<=c,r,c)
              gen c1=cond(r<=c,c,r)
              drop r c
              
              list, sepby(r1)
              collapse (max) v, by(r1 c1)
              
              list, sepby(r1)
              (list statements are only to illustrate what's going on, you omit them in your final program).

              Best, Sergiy Radyakin

              Comment


              • #8
                Thank you for your great suggestion, Sergiy.

                Comment


                • #9
                  Many Thanks to Roberto !!!

                  Comment

                  Working...
                  X