Announcement

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

  • Matrix Multiplication Bug

    Dear Statlists,

    My aim is simply to multiply the transpose of ret_m with W_L (both are Stata's variables). However, after I stored the result E into Stata memory, the values of E are completely missing. Is there anyone who might be able to give me some ideas? I will really appreciate that help!!!

    Here are the Mata codes:

    Code:
    mata
    C = st_data(. , ("ret_m"))
    D = st_data(. , ("W_L)"))
    E = C' * D
    st_addvar("double","E1")
    end
    Data is here:

    Code:
    input float(ret_m W_L)
       -.005025199  0
        -.08080798  1
         -.6153846  5
        -.05714292  -2
        -.24242426  0
         .05999997  1
         -.3773585  0
        -.21212125  2
      2.357986e-08  -2
         -.3846154  0
        -.06250003  0
        -.06666666  0
                 0  0
       -.013237344  0
       -.010204043  1
          .0721649  0
       -.003974303  6
         .03921594  9
         .05660392  0
        .016154103  -2
        -.03571403  0
        -.07407409  0
         .03748569  2
         -.0392157  0
       -.071428575  0
         .05268749  0
         .02127646  0
         .08333311  0
    Last edited by Jae Li; 24 Oct 2017, 13:04.

  • #2
    The call to st_addvar("double", "E1" ) creates a variable called E1, but doesn't store anything on it. You would need to add a call to st_store(1, "E1", E). But C'D is 1x1, I get -3.0461, so you should probably store it in a Stata scalar, not a variable.

    Comment


    • #3
      Jae Li Aside: Try to use cross() for computing cross-products (X'*Z), it is slightly more efficient

      Code:
      mata
      C = st_data(. , ("ret_m"))
      D = st_data(. , ("W_L"))
      st_numscalar("dot_prod",cross(C,D))
      end
      scalar list dot_prod

      Comment


      • #4
        German Rodriguez Thank you for your post! It's working now!

        May I ask you one more question? I'd like to calculate an equation: k = 2 / l' * |z-z_bar|, where k is a normalizing constant, z is the rank of beta and z_bar is the average rank, l' is the transpose of (n*1) vector of ones. Here are my Mata codes, but for some reason, it shows error in the command window. Would you mind take a few minutes to have a look for me?

        Z= st_data(. , ("z"))
        Z_bar = st_data(. , ("z_bar"))
        A = 1
        gen k = 2 / (A' * abs(z - z_bar))

        Then Stata command said
        /: 3204 matrix found where scalar required
        <istmt>: - function returned error
        Here is the data;

        Code:
        input float(Z  Z_bar)
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
               . 1218531.5
        Many thanks to you indeed in advance!!!

        Comment


        • #5
          @Christophe Kolodziejczyk Hi Christophe, I just saw your post. Many thanks for your codes, it works very well with my request after trying!

          Would you mind take another look in my question at below? I will really appreciate that!

          Here is the formula k = 2 / l' * |z-z_bar|, where l' is the transpose of (n*1) vector of ones, but it led to a error as below:

          Code:
           Z= st_data(. , ("z"))    //z is the rank of beta and,
          Z_bar = st_data(. , ("z_bar"))    //z_bar is the average rank
          A = 1
          gen k = 2 / (A' * abs(z - z_bar))
          It showed:

          /: 3204 matrix found where scalar required
          <istmt>: - function returned error
          Do you possibly know how to fix the error? Thank you very much in advance!!
          Last edited by Jae Li; 25 Oct 2017, 05:17.

          Comment


          • #6
            Jae Li You should drop the gen in the last line, since it is Stata code.
            Then since

            Code:
            A'*abs(z-z_bar)
            is just taking the sum of the absolute difference between z and z_bar, you can use the sum() function instead of computing the dot-product.
            Although this line of code is not necessary it is worth mentionning that in your code A is a scalar instead of a vector which results in a error when you compute the division. Now to produce a vector of ones you should use the J() function.


            Code:
             Z= st_data(. , ("z"))    //z is the rank of beta and,
            Z_bar = st_data(. , ("z_bar"))    //z_bar is the average rank
            // A = J(1,length(z),1) // assuming z is a vector, but this line is unnecessary if you are using sum()
            // k = 2/ ( A*sum( abs(z - z_bar)) )
            // or 
            k = 2 / sum( abs(z - z_bar))

            Comment


            • #7
              @Christophe Kolodziejczyk Many thanks for your reply! I tried it but it indicates an error:

              . mata
              ------------------------------------------------- mata (type end to exit) ------------------------------------------------------------------------------------------------------------
              : Z= st_data(. , ("z"))

              : Z_bar= st_data(. , ("z_bar"))

              : A = J(1, length(Z), 1)

              : k = 2 / (A * sum( abs(Z - Z_bar)))
              /: 3204 matrix found where scalar required
              <istmt>: - function returned error
              r(3204);
              Do you know why did it happen? But if I tried without "A" vector involved, then it works very well for computing the normalizing constant k.

              Also, if I wanna write some Stata codes at below in Mata environment, such as -replace- and -if-, how can I achieve that?

              Code:
              replace W_H= 0 if W_H <= 0
               or
              if P== 1
              Thank you very much for your help indeed! Hope to hear from you soon!

              Comment


              • #8
                Sorry, we should drop the sum() and it should be then.


                Code:
                k = 2 / (A * abs(Z - Z_bar))
                I would prefer the other formulation though. A*abs(z-z_bar) is equivalent to sum(abs(z-z_bar)). In the last formulation you don't need to compute the A matrix.
                BTW there is some problem with your data example, z contains missing values and z_bar should be defined as double

                to execute Stata code from Mata use the stata() function (Note the use of double quotes in this example)

                Code:
                mata: stata(`"di "Hello" "')
                mata: stata("replace W_H = 0) if W_H <= 0
                These examples are a bit silly and it only makes sense to use this function within a Mata function. In interactive mode you can juse write end to pause Mata code, write Stata code and come back later to Mata. Mata objects will still be in memory unless you invoke clear mata.

                Comment


                • #9
                  @Christophe Kolodziejczyk Thank you for your clarification, Christophe! Your post is very much helpful!!

                  Yes, you are right that z contains missing values, why does the z_bar define as double by using the below code?

                  Code:
                   mata: stata(`"di "Hello" "')
                  Many thanks for your time and help!! Hope to hear from you soon!!
                  Last edited by Jae Li; 26 Oct 2017, 17:10.

                  Comment


                  • #10
                    Jae Li It doesn't. What I meant was to input your variables as doubles. The problem was that the decimal part was not showing up i. But actually it is not strictly necessary, because it seems to be more a problem of format displaying. The default format for float is apparently %9.0g, but if you change the format to %10.0g, you get the correct display of numbers.

                    Code:
                    input float(z z_bar) 
                    ...
                    end
                    format z_bar %10.0g
                    
                    // or
                    
                    input double(z z_bar)
                    ...
                    end
                    My comment about double quotes was related to the combined use of stata() and the display command with a string. In order to use stata() you have in this case to put the Stata statement di "Hello" between quotes. In order to work you have to use double quotes, i.e.
                    Code:
                    `"  "'
                    . It was just a general comment on how to input string containing quotes in a Mata function. I hope it clarifies.

                    See help quotes for details



                    Comment


                    • #11
                      @Christophe Kolodziejczyk May I ask that will these following codes be right?

                      Code:
                      ************************Stata mode************************
                      input float(z z_bar)  
                      egen z= rank(lag_full_b)
                      egen z_bar= mean(z)
                      end
                      format z_bar %10.0g
                      Thank you for your reply and explanations! I've learnt a lot from it! It's truly helpful!

                      May I ask you one more question? My apologies for bothering you for so long! Since I wanna test whether A* L = 1, so when I use the
                      Code:
                       A = J(1, length(Z), 1)
                      , the variable A surprisingly generates all missing values and produces an error message at below. Could you possible spot the issue? I will be very much appreciated that help!


                      Code:
                      . mata
                      ------------------------------------------------- mata (type end to exit) -------------------------
                      :
                      : A = J(1, length(Z), 1)
                      
                      : L = st_data(. , ("W_L"))
                      
                      : B_L = A * L
                                             *:  3200  conformability error
                                       <istmt>:     -  function returned error
                      r(3200);
                      Code:
                      This is data:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input float W_L double A
                      -6.424788e-08 .
                      -5.729101e-08 .
                      -6.7769044e-08 .
                      -6.166563e-08 .
                      -6.901432e-08 .
                      -6.966144e-08 .
                      -6.247115e-08 .
                      -7.19936e-08 .
                      -6.184903e-08 .
                      -6.96927e-08 .
                      -8.231949e-08 .
                      -8.023117e-08 .
                      -8.873448e-08 .
                      -7.704035e-08 .
                      -8.884285e-08 .
                      -8.063862e-08 .
                      -8.881889e-08 .
                      -8.608345e-08 .
                      -8.666076e-08 .
                      -9.506298e-08 .
                      -8.037185e-08 .
                      -9.780467e-08 .
                      -8.959523e-08 .
                      -8.497156e-08 .
                      -9.257973e-08 .
                      -7.93225e-08 .
                      -8.315315e-08 .
                      -9.15241e-08 .
                      -9.147721e-08 .
                      Last edited by Jae Li; 28 Oct 2017, 13:43.

                      Comment

                      Working...
                      X