Announcement

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

  • First of all, i need to have ew_mean_rt also. So, how should i place this line in the code:

    Code:
     
     by mdate idiovol_decile, sort: egen ew_mean_rt = mean(rt)
    Moreover, while i run the code #105, following error is encountered which needs to be debugged:

    reshape wide @stock_count vw_mean_@rt, i(mdate) j(group) string
    (j = _idiovol_d._ _idiovol_d10_ _idiovol_d1_ _idiovol_d2_ _idiovol_d3_ _idiovol_d4_ _idiovol_d5_ _idiovol_d6_ _idiovol_d7_ _idiovol_d8_ _idiovol_d9_)
    _idiovol_d._stock_count invalid variable name
    r(198);


    Comment


    • Actually, the best place to create the ew_mean_rt variable is inside program one_weighted_return. Because that program is used under -runby, by(mdate idiovol_decile)-, the by prefix to the -egen- command is not needed there.

      I cannot replicate the error you are getting with -reshape-. It does not arise with the example data. But I also do not see how it can arise in the full data either. It can only occur if the -reshape- is being run when there are missing values of the variable idiovol_decile, but those are removed by an earlier command (see comment in code below) and nothing in between re-creates them. So I don't know what to say about that. If you are sure you are not somehow skipping over that -drop if...- command and you still get that error, please post back with a new example of data that reproduces the problem.

      Here is the code, creating an ew_mean_rt variable.

      Code:
      xtset stock_id mdate
      gen lagged_idiovol = L1.idiovol
      gen lagged_mcap = L1.mcap
      by mdate, sort: egen idiovol_decile = xtile (lagged_idiovol), nq(10)
      
      capture program drop one_weighted_return
      program define one_weighted_return
          if !missing(idiovol_decile){
              egen numerator = total(lagged_mcap*rt)
              egen denominator = total(lagged_mcap)
              gen vw_mean_rt = numerator/denominator
              egen ew_mean_rt = mean(rt)
          }
          exit
      end
      
      runby one_weighted_return, by(mdate idiovol_decile)
      
      keep mdate idiovol_decile *_mean_rt
      drop if missing(idiovol_decile, vw_mean_rt, ew_mean_rt) // THIS COMMAND SHOULD PREVENT THE RESHAPE ERROR
      
      by mdate *_decile, sort: gen stock_count = _N
      by mdate idiovol_decile, sort: keep if _n == 1
      
      gen str32 group = "idiovol_d" + string(idiovol_d) + "_"
      drop *_decile
      reshape wide  @stock_count  ew_mean_@rt vw_mean_@rt, i(mdate) j(group) string

      Comment


      • With the given changes, #107 code works now well.

        Now there is one more thing. I want to do this entire analysis (run the code #107) first, only for January month and second for all other months separately excluding January.

        What is the condition that should be put in the code #107
        Last edited by Sartaj Hussain; 17 Jun 2022, 13:04.

        Comment


        • Lately, i could do #108 myself. Thanks indeed!

          Comment


          • In #107, deciles are formed every month from July to June based on lagged month idiovol followed by weighting returns (rt's) by lagged month market cap. I just want some changes to #107 to create separate codes as per below details:

            1. Deciles are formed each month from: ​​​​​July to September based on June (begin) idiovol values, then from: October to December based on September idiovol values, from: January to March using December idiovol values and finally from April to June using idiovol values observed at March month.

            2. Deciles need to be formed each month from July to December following idiovol values observed in the month of June (begin) and then from January to June following idiovol values of December month.

            3. Deciles are made each month from July to June following idiovol values observed at June (begin).

            However, in all these cases, weighted average portfolio returns procedure will remain similar to #107. (no change). Data example is same as in #104.
            Last edited by Sartaj Hussain; 21 Jun 2022, 03:37.

            Comment


            • So these analyses all differ by the choice of a reference month for identifying the lagged mcap and idiovol that determine the weighting of the weighted average return and the decile, respectively.

              1. In this case, the breakdown is quarterly instead of monthly.
              Code:
              xtset stock_id mdate
              gen int ref_month = mofd(dofq(qofd(date))-1)
              format ref_month %tm
              rangestat (max) lagged_idiovol = idiovol, by(stock_id) interval(mdate ref_month ref_month)
              rangestat (max) lagged_mcap = mcap, by(stock_id) interval(mdate ref_month ref_month)
              by mdate, sort: egen idiovol_decile = xtile (lagged_idiovol), nq(10)
              
              capture program drop one_weighted_return
              program define one_weighted_return
                  if !missing(idiovol_decile){
                      egen numerator = total(lagged_mcap*rt)
                      egen denominator = total(lagged_mcap)
                      gen vw_mean_rt = numerator/denominator
                      egen ew_mean_rt = mean(rt)
                  }
                  exit
              end
              
              runby one_weighted_return, by(mdate idiovol_decile)
              
              keep mdate idiovol_decile *_mean_rt
              drop if missing(idiovol_decile, vw_mean_rt, ew_mean_rt) // THIS COMMAND SHOULD PREVENT THE RESHAPE ERROR
              
              by mdate *_decile, sort: gen stock_count = _N
              by mdate idiovol_decile, sort: keep if _n == 1
              
              gen str32 group = "idiovol_d" + string(idiovol_d) + "_"
              drop *_decile
              reshape wide  @stock_count  ew_mean_@rt vw_mean_@rt, i(mdate) j(group) string
              The code is the same as #107, except for the italicized commands.

              For 2, it is half-years. Use the same code as for 1, but change the italicized commands to:
              Code:
              gen int ref_month = mofd(dofh(hofd(date))-1)
              format ref_month %tm
              rangestat (max) lagged_idiovol = idiovol, by(stock_id) interval(mdate ref_month ref_month)
              rangestat (max) lagged_mcap = mcap, by(stock_id) interval(mdate ref_month ref_month)
              For 3, it is years. Use the same code as for 1, but for "years" that begin in July and end the following June. Change the italicized commands to:
              Code:
              gen int ref_month = mofd(dofy(yofd(dofm(mdate-6))))+5
              format ref_month %tm
              rangestat (max) lagged_idiovol = idiovol, by(stock_id) interval(mdate ref_month ref_month)
              rangestat (max) lagged_mcap = mcap, by(stock_id) interval(mdate ref_month ref_month)
              All of the above require the use of -rangestat-, which is written by Robert Picard, Nick Cox, and Roberto Ferrer, available from SSC>

              Comment


              • What does this line do in the code:

                Code:
                 
                 rangestat (max) lagged_mcap = mcap, by(stock_id) interval(mdate ref_month ref_month)
                Basically, in all the three versions, i want that each month weighted average returns of deciles should be calculated following lag or previous month's mcap of each return (rt).

                Comment


                • I'm confused now. Do you mean that you want the portfolios to be defined by the idiovol of the months you refer to in #110 but you still want the weighting of the return to be by the immediately previous month's mcap?

                  If so, then the italicized code should look like this for analysis 1:
                  Code:
                  gen int ref_month = mofd(dofq(qofd(date))-1)
                  format ref_month %tm
                  rangestat (max) lagged_idiovol = idiovol, by(stock_id) interval(mdate ref_month ref_month)
                  gen lagged_mcap = L1.mcap
                  This is the same as the italicized code for analysis 1 in #111, except for the bold-faced command. Use this bold-faced line to replace the -rangestat (max) lagged_mcap = mcap...- command in the code for analyses 2 and 3 as well.

                  Comment


                  • Do you mean that you want the portfolios to be defined by the idiovol of the months you refer to in #110 but you still want the weighting of the return to be by the immediately previous month's mcap?
                    Yes. Forming of portfolios according to #110. And weighting of return by immediately preceding month's mcap and so on for all months.

                    Having said above, may i understand why the line i mentioned in #112 was kept earlier.
                    Last edited by Sartaj Hussain; 21 Jun 2022, 14:11.

                    Comment


                    • I had that line in the code to calculate the mcap as of the "reference" month because I thought that you wanted the weighting to be by that month, rather than the immediately preceding calendar month. By reference month I mean, the month immediately preceding the quarter (1), half-year (2), or July-June year (3) of the current observation.

                      Comment

                      Working...
                      X