Announcement

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

  • Hi all

    I am very new to mata and ran to a problem which I could not figure out.

    I am trying to implement matrix function.

    I have a matrix b (a 1X9 matrix) and matrix v (a 9X9 matrix). I am required to:
    Randomly draw (5000 draws) from standard normal distribution a vector x with 9 independent elements (x is a 5000 X9 matrix).
    Calculate Z such that Z = b' + c'x, (where c = cholesky (v))
    Finally, generate matrix "a" such that a = Z/0.0311

    I am failing to get past the Z matrix. I am only able to implement this calculation if I keep the dimensions of x to 9X9.

    Here the part of my code that is at least running.

    mat b = [1.0599, 0.1580, 0.5303, 0.0518, -0.0144, -0.0787, -0.1104, -0.1413, 0.0489]

    mat v = [0.0034, 0.0056, -0.0003, -0.0004, -0.0003, -0.0004, -0.0001, -0.0003, 0 ///
    \0.0056, 0.0111, -0.0003, -0.0006, -0.0005, -0.0008, -0.0001, -0.0006, 0.0001 ///
    \-0.0003, -0.0003, 0.018, 0.0008, 0, 0, 0, 0, 0 ///
    \-0.0004, -0.0006, 0.0008, 0.0147, 0, 0, 0, 0, 0 ///
    \-0.0003, -0.0005, 0, 0, 0.0068, 0.0002, 0, 0, 0 ///
    \-0.0004, -0.0008, 0, 0, 0.0002, 0.0063, 0, 0, 0.0001 ///
    \-0.0001, -0.0001, 0, 0, 0, 0, 0.0041, 0.0002, 0 ///
    \-0.0003, -0.0006, 0, 0, 0, 0, 0.0002, 0.0044, 0 ///
    \0, 0.0001, 0, 0, 0, 0.0001, 0, 0, 0.0065]

    mat c = cholesky(v)

    set obs 5000
    forvalues i=1/9 {
    gen x`i'=rnormal(0,1)


    }

  • #2
    What you are doing is using Stata's old matrix functions, which is NOT mata.

    To use mata, you need to do something like

    Code:
    // Stata code goes here
    // ...
    
    mata:
    // Your Mata code goes here
    b = 1.0599, 0.1580, 0.5303, 0.0518, -0.0144, -0.0787, -0.1104, -0.1413, 0.0489
    v = ...
    c = cholesky(v)
    
    X = rnormal(5000,9, 0, 1)
    Z = ...
    a = ...
    end
    
    // Stata code again

    Comment


    • #3
      Thank you Sergio. I think my understanding of the mata side of stata is less than elementary. I am still failing to get around my challenge.


      In my particular case matrix c is a 9X9 and matrix x is a 5040X9. Is there a way to tell stata to do the multiplaction of the two matrices in steps taking only 9 rows of matrix x at a time (that will amount to 560 "iterations")? Alternatively, build the product of the two matrices in small blocks of nine rows at a time.

      The next challenge will be to add a row vecotr b (1X9) through all the 5040 rows of the poduct of c and x.

      Thank you again for you assistance

      Comment


      • #4
        Thabo: Please use informative titles for your posts. http://www.statalist.org/forums/help#topiclines

        Just imagine how difficult the forum would be to work with if thread titles were all on the level of "Hi all" "Good morning" "I have a question".

        Comment

        Working...
        X