Announcement

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

  • Generating Momentum Returns

    From the appended data example, I want to calculate momentum of stocks as the 11-month return of the stock during the period beginning 12 months prior to and ending one month prior to the measurement date. By this definition, momentum of stock i measured at the end of month t, which is denoted as Momi,t, is therefore taken to be the return of the stock during the 11-month period covering month t-11 through t-1.

    The stock_id is the unique firm id, mdate represents monthly date and pr is the closing price of the stock observed at the end of given month.

    Kindly suggest a code that measures the given momentum variable Momi,t,in this case.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int stock_id str56 stock float(mdate pr)
    1 "3M India Ltd."                   484    500
    1 "3M India Ltd."                   485 620.05
    1 "3M India Ltd."                   486    664
    1 "3M India Ltd."                   487  639.4
    1 "3M India Ltd."                   488    602
    1 "3M India Ltd."                   489  525.2
    1 "3M India Ltd."                   490 602.15
    1 "3M India Ltd."                   491  659.9
    1 "3M India Ltd."                   492    627
    1 "3M India Ltd."                   493    610
    1 "3M India Ltd."                   494  438.8
    1 "3M India Ltd."                   495  424.5
    1 "3M India Ltd."                   496  424.9
    1 "3M India Ltd."                   497  380.1
    1 "3M India Ltd."                   498  325.1
    1 "3M India Ltd."                   499    323
    1 "3M India Ltd."                   500    266
    1 "3M India Ltd."                   501    270
    1 "3M India Ltd."                   502    293
    1 "3M India Ltd."                   503    267
    1 "3M India Ltd."                   504 259.95
    1 "3M India Ltd."                   505    271
    1 "3M India Ltd."                   506 311.45
    1 "3M India Ltd."                   507 314.25
    1 "3M India Ltd."                   508  282.7
    1 "3M India Ltd."                   509 285.15
    2 "A B B India Ltd."                484  37.99
    2 "A B B India Ltd."                485   41.2
    2 "A B B India Ltd."                486   40.6
    2 "A B B India Ltd."                487  49.15
    2 "A B B India Ltd."                488  42.32
    2 "A B B India Ltd."                489  43.31
    2 "A B B India Ltd."                490     52
    2 "A B B India Ltd."                491  52.59
    2 "A B B India Ltd."                492  53.06
    2 "A B B India Ltd."                493  63.18
    2 "A B B India Ltd."                494  47.52
    2 "A B B India Ltd."                495  49.24
    2 "A B B India Ltd."                496  55.74
    2 "A B B India Ltd."                497  49.96
    2 "A B B India Ltd."                498   50.4
    2 "A B B India Ltd."                499  45.71
    2 "A B B India Ltd."                500  39.51
    2 "A B B India Ltd."                501  38.32
    2 "A B B India Ltd."                502  40.97
    2 "A B B India Ltd."                503   40.8
    2 "A B B India Ltd."                504   44.5
    2 "A B B India Ltd."                505  51.37
    2 "A B B India Ltd."                506  53.11
    2 "A B B India Ltd."                507  53.26
    2 "A B B India Ltd."                508  49.19
    2 "A B B India Ltd."                509  53.75
    3 "A C C Ltd."                      484  113.5
    3 "A C C Ltd."                      485 117.95
    3 "A C C Ltd."                      486    108
    3 "A C C Ltd."                      487  124.5
    3 "A C C Ltd."                      488  92.25
    3 "A C C Ltd."                      489  93.45
    3 "A C C Ltd."                      490 142.65
    3 "A C C Ltd."                      491    159
    3 "A C C Ltd."                      492 178.95
    3 "A C C Ltd."                      493 177.85
    3 "A C C Ltd."                      494  129.8
    3 "A C C Ltd."                      495  149.8
    3 "A C C Ltd."                      496  139.5
    3 "A C C Ltd."                      497  136.4
    3 "A C C Ltd."                      498    139
    3 "A C C Ltd."                      499 127.95
    3 "A C C Ltd."                      500  124.5
    3 "A C C Ltd."                      501  133.2
    3 "A C C Ltd."                      502 170.65
    3 "A C C Ltd."                      503  151.8
    3 "A C C Ltd."                      504    160
    3 "A C C Ltd."                      505  163.5
    3 "A C C Ltd."                      506 153.95
    3 "A C C Ltd."                      507 150.65
    3 "A C C Ltd."                      508 151.25
    3 "A C C Ltd."                      509  158.9
    4 "A D C India Communications Ltd." 484  331.4
    4 "A D C India Communications Ltd." 485  402.7
    4 "A D C India Communications Ltd." 486 462.15
    4 "A D C India Communications Ltd." 487 389.65
    4 "A D C India Communications Ltd." 488  355.7
    4 "A D C India Communications Ltd." 489  229.7
    4 "A D C India Communications Ltd." 490  238.8
    4 "A D C India Communications Ltd." 491 198.05
    4 "A D C India Communications Ltd." 492 215.15
    4 "A D C India Communications Ltd." 493  178.8
    4 "A D C India Communications Ltd." 494  114.8
    4 "A D C India Communications Ltd." 495  111.1
    4 "A D C India Communications Ltd." 496  128.5
    4 "A D C India Communications Ltd." 497  127.5
    4 "A D C India Communications Ltd." 498  91.55
    4 "A D C India Communications Ltd." 499  110.9
    4 "A D C India Communications Ltd." 500  77.95
    4 "A D C India Communications Ltd." 501   75.5
    4 "A D C India Communications Ltd." 502  92.15
    4 "A D C India Communications Ltd." 503     87
    4 "A D C India Communications Ltd." 504   85.5
    4 "A D C India Communications Ltd." 505  82.45
    end
    format %tm mdate
    Last edited by Sartaj Hussain; 15 Nov 2021, 11:27.

  • #2
    Expressed as a percent:
    Code:
    xtset stock_id mdate
    gen wanted = ((L1.pr - L12.pr)/L12.pr)*100

    Comment


    • #3
      Thanks indeed. This is fine. But there is one more thing. For each monthly Momi,t value, I require count of return observations used. This is to replace Momi,t value with missing if number of return observations is less than 9 for any given month for a stock. By definition, Momi,t is return for 11 months period covering month t-11 through t-1.
      Last edited by Sartaj Hussain; 15 Nov 2021, 12:18.

      Comment


      • #4
        Add the following two lines to the code at the end:
        Code:
        xtset stock_id mdate
        gen wanted = ((L1.pr - L11.pr)/L11.pr)*100
        rangestat (count) obs_used = pr, by(stock_id) interval(mdate -11 -1)
        replace obs_used = . if missing(wanted)
        -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer, and is available from SSC.

        Note: In your example data, there are no missing values or missing observations, so this additional variable is not necessary there. But I suppose your real data are not so pretty.

        Also note the change in the earlier code: I had misunderstood the original request as covering a 12 month period, rather than an 11 month period. So the code in #2 is incorrect.

        All of that said, I don't understand why it matters how many observations are available out of the 11 month period. The return over the 11 months is calculated just from the price at t-1 and the price at t-11. What happens in between makes no difference to the 11 month return. Even if all of them were missing, you would still have a correct 11 month return.

        Comment


        • #5
          Well Thanks, But my point is still not taken well. From some earlier post on STATALIST, someone used this code but i think it is not measuring momentum correct. Could you verify it in the light of definition and rule given in #1 of this thread.

          Code:
          bysort stock_id:gen rt =((pr[_n]-pr[_n-1])/pr[_n-1])
          rangestat (sum) rt (count) rt, interval(mdate -11 -1) by(stock_id)
          rename rt_sum mom
          replace mom=. if rt_count<9
          And yes,
          All of that said, I don't understand why it matters how many observations are available out of the 11 month period. The return over the 11 months is calculated just from the price at t-1 and the price at t-11. What happens in between makes no difference to the 11 month return. Even if all of them were missing, you would still have a correct 11 month return.
          This condition is imposed to overcome issues related to measurement errors concerned with variable Momi,t, given below model for measurement of Momi,t,. By this, Momi,t, appears to be calculated by accumulating individual monthly returns instead of one period return. So this is why condition matters.
          Click image for larger version

Name:	Mom.PNG
Views:	1
Size:	2.4 KB
ID:	1636699



          Last edited by Sartaj Hussain; 15 Nov 2021, 19:20.

          Comment


          • #6
            The code you show in #5 is not correct. Periodic returns cannot be combined with addition. Doing so is, at best, an approximation which ignores higher-order terms, is valid only when the returns themselves are small and is a worse and worse approximation as the number of periods grows. At worst it is not even in the ballpark: if there are any missing values of pr in the period from t-11 to t-1, then it is just dead wrong.

            The method relying only on pr at t-1 and t-11 is bullet-proof. It is exact, and gives the correct answer whenever it is computable.

            The argument about measurement errors makes no sense to me. In fact, the formula shown at the end of #5 has 11 possible sources of measurment error, with little reason to think they will average each other out. By contrast, the only sources of measurement error in the method used in #4 are at times t-1 and t-11. And, by the way, the formula you in #5 is not what is calculated by the code in #5.
            Last edited by Clyde Schechter; 15 Nov 2021, 19:30.

            Comment


            • #7
              Perhaps, I am not able to convey it appropriately. Therefore, i am attaching herewith excerpts from the book: Emprirical Asset Pricing by Bali, Engle and Murray that lays description for calculation of momentum returns. I want the code according to this definition
              Click image for larger version

Name:	momen.PNG
Views:	1
Size:	31.0 KB
ID:	1636709
              Click image for larger version

Name:	momen1.PNG
Views:	1
Size:	30.1 KB
ID:	1636710
              .

              Comment


              • #8
                Thank you for the reference. It doesn't change what I said in #6. If you implement that product formula directly, as can be done with code such as:
                Code:
                by stock_id (mdate), sort: gen retplus1 = pr/L1.pr
                by stock_id (mdate): gen product = 1 if _n == 1
                by stock_id (mdate): replace product = L1.product*(retplus1) if _n > 1
                by stock_id (mdate): gen wanted2 = (L1.product/L11.product - 1)*100
                you will find that wanted2 agrees with wanted to within 4 decimal places in your data from #1. Now, crucially, in that data, there are no missing values of pr within the 11 month intervals under consideration. If there were, then the results would disagree, and the direct implementation of the product formula would give incorrect results. Moreover, the discrepancies between wanted and wanted2 in the complete data situation are entirely due to the repeated rounding errors generated by unnecessarily iterating through what is, in fact, a "telescoping"* product: the errors accumulate--they do not average out. This is proved by revising both codes to use double precision instead of float: doing that reduces the discrepancy between wanted and wanted2 so that they agree to 14 decimal places.


                *telescoping product. The term Ri,m + 1 is nothing other than pri, m/pri, m-1. So when you have this product spelled out it becomes (omitting subscript i for brevity) product = (prt-1/prt-2) * (prt-1/prt-2) *(prt-2/prt-3) * ... * (prt-9/prt-10) * (prt-10/prt-11). It is clear that all of the pr factors cancel except for the first and last, so the expression is exactly the same as prt-1/prt-11, which is what my code relies on. So algebraically the formulas are equivalent. The difference when it comes to calculation is that the direct application of the product formula introduces rounding/truncation error at each step, whereas the shortened version eliminates that, and the shortened version also handles missing values gracefully, whereas the full product formula does not.
                Last edited by Clyde Schechter; 16 Nov 2021, 10:58.

                Comment


                • #9
                  So, code in #2 is enough for the purpose considering requirements in #7. Right?

                  Comment


                  • #10
                    Yes, right.

                    Comment


                    • #11
                      Thanks. I will follow it.

                      Comment

                      Working...
                      X