Announcement

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

  • Inverse of a matrix

    Dear All,

    I have the following symmetric matrix:

    Code:
    symmetric V_diff[11,11]
                    pc_culture  urb_rate1850        school  countryname1  countryname2  countryname3  countryname4  countryname5
      pc_culture     .06447299
    urb_rate1850    -.05278748     .01628029
          school    -.01874854     .01102653     .00363522
    countryname1     4.5903346    -3.0965529    -1.1290617     301.73363
    countryname2     2.1064027     -.7624796    -.36318082      117.9168     26.429972
    countryname3    -.85005066     1.2145158     .40800901    -80.140974    -52.851368    -4.1350514
    countryname4     2.3099423    -1.3619878    -.51357007     144.96865     50.376108    -45.701961     67.589369
    countryname5    -.07604234     .67402019      .2123714     -28.60747    -32.117323    -17.134995    -20.746305    -21.352163
    countryname6     3.1871472     -1.809098    -.69833247     198.38592     67.085994    -64.338675     91.943415    -30.135964
    countryname7     1.8894841    -1.0291854    -.39089213     115.11489     36.858158    -40.093335     52.603569     -20.17471
    countryname8     4.1191875    -2.9437497    -1.0622745     276.82325     113.61946    -67.176435     134.80566    -20.069622
    
                  countryname6  countryname7  countryname8
    countryname6     124.86398
    countryname7     71.298498     40.350407
    countryname8     184.94091     107.99118     252.38147
    I want its inverse. So I use:

    Code:
    mat B = syminv(V_diff)
    THe following is the result:

    Code:
    symmetric B[11,11]
                    pc_culture  urb_rate1850        school  countryname1  countryname2  countryname3  countryname4  countryname5
      pc_culture             0
    urb_rate1850             0             0
          school             0             0             0
    countryname1             0             0             0     .00331418
    countryname2             0             0             0             0             0
    countryname3             0             0             0             0             0             0
    countryname4             0             0             0             0             0             0             0
    countryname5             0             0             0             0             0             0             0             0
    countryname6             0             0             0             0             0             0             0             0
    countryname7             0             0             0             0             0             0             0             0
    countryname8             0             0             0             0             0             0             0             0
    
                  countryname6  countryname7  countryname8
    countryname6             0
    countryname7             0             0
    countryname8             0             0             0
    It looks to me that something is strange. Nonetheless, if my matrix algebra is correct, if I multiply the original matrix by its inverse, I should get the identity matrix. This is what I got:

    Code:
    . mat C = V_diff*B
    
    . mat list C
    
    C[11,11]
                    pc_culture  urb_rate1850        school  countryname1  countryname2  countryname3  countryname4  countryname5
      pc_culture             0             0             0      .0152132             0             0             0             0
    urb_rate1850             0             0             0    -.01026254             0             0             0             0
          school             0             0             0    -.00374192             0             0             0             0
    countryname1             0             0             0             1             0             0             0             0
    countryname2             0             0             0     .39079768             0             0             0             0
    countryname3             0             0             0    -.26560173             0             0             0             0
    countryname4             0             0             0     .48045242             0             0             0             0
    countryname5             0             0             0    -.09481035             0             0             0             0
    countryname6             0             0             0     .65748695             0             0             0             0
    countryname7             0             0             0     .38151162             0             0             0             0
    countryname8             0             0             0     .91744247             0             0             0             0
    
                  countryname6  countryname7  countryname8
      pc_culture             0             0             0
    urb_rate1850             0             0             0
          school             0             0             0
    countryname1             0             0             0
    countryname2             0             0             0
    countryname3             0             0             0
    countryname4             0             0             0
    countryname5             0             0             0
    countryname6             0             0             0
    countryname7             0             0             0
    countryname8             0             0             0
    I cannot understand what is going on. I assume that there is something wrong with my commands. I would be grateful for any help you may provide.

    Best,

    Dario

  • #2
    Is it positive definite?

    For example, what does
    Code:
    mata: cholesky(V_diff)
    
    // or
    
    mata: symeigenvalues(V_diff)
    give?

    Comment


    • #3
      Sorry, that should be
      Code:
      mata: cholesky(st_matrix("V_diff"))
      
      // or
      
      mata: symeigenvalues(st_matrix("V_diff"))

      Comment


      • #4
        Joseph Coveney Thanks for your reply. This is what I got:

        Code:
        . mata: cholesky(st_matrix("V_diff"))
                 1    2    3    4    5    6    7    8    9   10   11
             +--------------------------------------------------------+
           1 |   .    0    0    0    0    0    0    0    0    0    0  |
           2 |   .    .    0    0    0    0    0    0    0    0    0  |
           3 |   .    .    .    0    0    0    0    0    0    0    0  |
           4 |   .    .    .    .    0    0    0    0    0    0    0  |
           5 |   .    .    .    .    .    0    0    0    0    0    0  |
           6 |   .    .    .    .    .    .    0    0    0    0    0  |
           7 |   .    .    .    .    .    .    .    0    0    0    0  |
           8 |   .    .    .    .    .    .    .    .    0    0    0  |
           9 |   .    .    .    .    .    .    .    .    .    0    0  |
          10 |   .    .    .    .    .    .    .    .    .    .    0  |
          11 |   .    .    .    .    .    .    .    .    .    .    .  |
             +--------------------------------------------------------+
        
        . mata: symeigenvalues(st_matrix("V_diff"))
                          1              2              3              4              5
            +----------------------------------------------------------------------------
          1 |   868.8093179    1.08236e-13    8.19047e-14    5.15725e-14    6.39471e-15
            +----------------------------------------------------------------------------
                          6              7              8              9             10
             ----------------------------------------------------------------------------
          1    -4.96898e-17   -1.45734e-14   -6.98127e-14   -7.60789e-14   -.9766598434
             ----------------------------------------------------------------------------
                         11
             ----------------+
          1    -79.88665422  |
             ----------------+
        
        .
        So I guess that the result in the first matrix suggests that it is not positive definite. So basically, what I am doing is to replicate the Hausman test manually (I need to do it in that way in my research). Therefore, I have tried to compare the results that I get from the usage of the hausman command and the one I try to do manually.

        What I did was:

        Code:
        ivreg Y (X1=Z1 Z2) X2 X3 countryname*,  nocon
        
        matrix b_iv = e(b)
        matrix V_iv = e(V)
        
        reg Y X1 X2 X3 countryname*, nocon
        
        matrix b_ols = e(b)
        matrix V_ols = e(V)
        
        matrix b_diff = b_iv - b_ols
        matrix V_diff = V_iv - V_ols
        matrix H = b_diff' * syminv(V_diff) * b_diff
        scalar hausman_stat = H[1,1]
        scalar df = colsof(b_diff)
        scalar p_value = chi2tail(df, hausman_stat)
        display "Hausman chi2(" df ") = " hausman_stat ", p = " p_value
        If the matrix V_diff is not positive definite, the test done with hausman should ndisplay a warning, am I correct? Differently, this is what I get:

        Code:
           chi2(10) = (b-B)'[(V_b-V_B)^(-1)](b-B)
                    =   4.17
        Prob > chi2 = 0.9392
        When I try to do do it manually using the Hausman test procedure "H=(β^​IV​−β^​OLS​)′[Var(β^​IV​)−Var(β^​OLS​)]−1(β^​IV​−β^​OLS​)", I get

        Code:
        . matrix H = b_diff' * syminv(V_diff) * b_diff
        conformability error
        Hence, I was trying to understand why this happens.

        Dario
        Last edited by Dario Maimone Ansaldo Patti; 06 Aug 2025, 07:48.

        Comment


        • #5
          Please, ignore my previous post. I repeated the procedure and it works. I got exactly the same statistic and p-value

          Comment

          Working...
          X