Announcement

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

  • Matrix, standard deviation of each row

    I have a matrix that is 12 rows by 500 columns. I would like to take the average and standard deviation of the rows. I have devised a method to take the mean but I have limited understanding of how to take the sd. For example,

    Code:
    clear all
    clear mata
    
    global num_rows = 3
    
    matrix A = (1,2,9\2,7,5\2,4,18)
    matrix B = J($num_rows, 1, 1)
    matrix sum = A*B
    matrix M = sum/$num_rows
    mat all = A,M
    How can I add the standard deviation of each row?

    Thank you very much for your help.
    Last edited by Jack Reimer; 15 Aug 2019, 08:28.

  • #2
    Here is one way to do it

    Code:
    clear all
    clear mata
    
    global num_rows = 3
    
    matrix A = (1,2,9\2,7,5\2,4,18)
    matrix B = J($num_rows, 1, 1)
    matrix sum = A*B
    matrix M = sum/$num_rows
    mat all = A,M
    mata
    A = st_matrix("A")
    st_matrix("sd", diagonal(sqrt(variance(A'))))
    end
    mat all = A,M,sd
    matlist all
    
                 |        c1         c2         c3         c1         c1
    -------------+-------------------------------------------------------
              r1 |         1          2          9          4   4.358899
              r2 |         2          7          5   4.666667   2.516611
              r3 |         2          4         18          8   8.717798
    Or even better
    Code:
    matrix A = (1,2,9\2,7,5\2,4,18)
    mata
    A = st_matrix("A")
    sd =  diagonal(sqrt(variance(A')))
    mean = mean(A')'
    all = A, mean, sd
    st_matrix("all", all)
    end
    matlist all
                 |        c1         c2         c3         c4         c5 
    -------------+-------------------------------------------------------
              r1 |         1          2          9          4   4.358899 
              r2 |         2          7          5   4.666667   2.516611 
              r3 |         2          4         18          8   8.717798
    Last edited by Attaullah Shah; 15 Aug 2019, 16:20.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Work in Mata is surely the theme here. Consider also

      Code:
      clear 
      input A1 A2 A3 
      1 2 2 
      2 7 4
      9 5 18
      end 
      
      su A? 
      
      matrix A = (1,2,9\2,7,5\2,4,18)
      
      mata 
      work = meanvariance(st_matrix("A")')
      work 
      means = work[1, .]'
      sds = sqrt(diagonal(work[2::rows(work),]))
      means 
      sds 
      end 
      
      
      
      
      : means 
                       1
          +---------------+
        1 |            4  |
        2 |  4.666666667  |
        3 |            8  |
          +---------------+
      
      : sds 
                       1
          +---------------+
        1 |  4.358898944  |
        2 |  2.516611478  |
        3 |  8.717797887  |
          +---------------+

      Comment

      Working...
      X