Announcement

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

  • Generating idioskew

    I want to use below code to generate monthly idiosyncratic skewness which is equal to skewness of residuals obtained each month by regressing rt on mkt, smb and hml using twelve months daily rt, mkt, smb and hml data from months beginning t-11 and ending month t. However, before i use the code, I want to confirm that whether it is correctly specified. The code and data set example is appended below for a cross-validation.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int stock_id str52 stock str10 month float(pr mk rf smb hml)
    1 "3M India Ltd." "29-06-2001"  380.1 1050.43 .000190118  .006906632   -.02635773
    1 "3M India Ltd." "02-07-2001"    380  1044.7 .000190118    .0243053   .012395788
    1 "3M India Ltd." "03-07-2001"    382 1016.12 .000190118  .009193858   .011030175
    1 "3M India Ltd." "04-07-2001"    373 1012.19 .000189043 -.003779778   .002102887
    1 "3M India Ltd." "05-07-2001"    370 1016.32 .000189043 -.002112604  -.009983404
    1 "3M India Ltd." "06-07-2001"    363 1008.44 .000189043 -.004746309   .004176213
    1 "3M India Ltd." "09-07-2001"  370.1 1005.61 .000189043  .004967289  -.000901557
    1 "3M India Ltd." "10-07-2001"    365 1015.86 .000189043 -.004156345  -.005973499
    1 "3M India Ltd." "11-07-2001"    365  1023.1 .000189043 -.009570085 -.0000385042
    1 "3M India Ltd." "12-07-2001"      . 1045.89 .000189043 -.008317466  -.014657998
    1 "3M India Ltd." "13-07-2001"  379.8 1047.82 .000189043 -.009258101   .003756839
    1 "3M India Ltd." "16-07-2001"  374.5 1043.17 .000189043  .008141486   .001153436
    1 "3M India Ltd." "17-07-2001"    384 1038.48 .000189043  .002734654  -.006272981
    1 "3M India Ltd." "18-07-2001"      . 1035.52 .000191194  .002666798  -.007612133
    1 "3M India Ltd." "19-07-2001"    380 1029.28 .000191194 -.010764934   .002164563
    1 "3M India Ltd." "20-07-2001"    367 1021.48 .000191194 -.003531434   .006274182
    1 "3M India Ltd." "23-07-2001"      . 1017.91 .000191194  .003957119    .00298848
    1 "3M India Ltd." "24-07-2001"    364 1007.34 .000191194  .003522038  -.002557836
    1 "3M India Ltd." "25-07-2001"  362.1  992.87 .000190118 -.005112115  -.003178912
    1 "3M India Ltd." "26-07-2001"    345  983.87 .000190118  .008406641  -.007936762
    1 "3M India Ltd." "27-07-2001"    335  985.08 .000190118  .007523834  -.007662709
    1 "3M India Ltd." "30-07-2001"      .  997.06 .000190118 -.001052119   .003796941
    1 "3M India Ltd." "31-07-2001"  325.1 1007.38 .000190118 -.006281636  -.006013084
    1 "3M India Ltd." "01-08-2001" 321.35 1002.07 .000187969 -.003827866   .007759471
    1 "3M India Ltd." "02-08-2001"    318 1007.21 .000187969 -.002262957  -.006467539
    1 "3M India Ltd." "03-08-2001"    323 1016.31 .000187969  .002335253  -.005333087
    1 "3M India Ltd." "06-08-2001"  332.5 1016.84 .000187969  .013860058  -.002403292
    1 "3M India Ltd." "07-08-2001"    332  1009.8 .000187969 .0000310135   .008616665
    1 "3M India Ltd." "08-08-2001"    330 1005.47 .000186894  .002830373   .006939544
    1 "3M India Ltd." "09-08-2001"    330 1005.16 .000186894  .000680501 -.0000935619
    1 "3M India Ltd." "10-08-2001"  325.9 1001.29 .000186894 -.003242821   .004484232
    1 "3M India Ltd." "13-08-2001"    330  991.02 .000186894  .016223654  -.003393324
    1 "3M India Ltd." "14-08-2001"    325 1004.51 .000184742 -.009961346  -.007970894
    1 "3M India Ltd." "16-08-2001"    320 1006.83 .000184742 -.001116778    .00184716
    1 "3M India Ltd." "17-08-2001"    320  998.73 .000184742  .003973659     .0040762
    1 "3M India Ltd." "20-08-2001"  301.3  993.36 .000184742    .0070569  -.002350738
    1 "3M India Ltd." "21-08-2001" 309.55  998.08 .000184742  .003531019  -.011190037
    1 "3M India Ltd." "23-08-2001"    305  998.48 .000184742 -.004440534   .004637646
    1 "3M India Ltd." "24-08-2001"  302.5 1001.45 .000184742  .002634362  -.011495728
    1 "3M India Ltd." "27-08-2001"    315 1005.27 .000184742  .017009705  -.007403183
    1 "3M India Ltd." "28-08-2001"    315 1003.13 .000184742 -.002180334   .002409128
    1 "3M India Ltd." "29-08-2001"      . 1001.18 .000183666  .006914516   .001008481
    1 "3M India Ltd." "30-08-2001"    323  999.71 .000183666  .007821609  -.001851162
    1 "3M India Ltd." "31-08-2001"      .  986.25 .000183666  .005843469   .008782397
    1 "3M India Ltd." "03-09-2001" 311.05  976.87 .000183666  .018127613  -.008090637
    1 "3M India Ltd." "04-09-2001"    324  980.81 .000183666  .000421051  -.007871736
    1 "3M India Ltd." "05-09-2001"    321  976.21 .000183666  .001085125    .00383226
    1 "3M India Ltd." "06-09-2001"  316.9  967.46 .000183666  .001295311  -.002122955
    1 "3M India Ltd." "07-09-2001"    318  968.13 .000183666 -.005481656  -.000656931
    1 "3M India Ltd." "10-09-2001"    317  965.93 .000183666  .008949677   .004532822
    1 "3M India Ltd." "11-09-2001"    310  959.32 .000183666  .004254708   .002196711
    1 "3M India Ltd." "12-09-2001" 303.85  924.96 .000187969  -.00078408   .005701145
    1 "3M India Ltd." "13-09-2001"  305.4  915.84 .000187969 -.002050521   .014715027
    1 "3M India Ltd." "14-09-2001"    290  861.02 .000187969  .006616689   .013136523
    1 "3M India Ltd." "17-09-2001"  257.3  804.92 .000187969  .006880768   .001179116
    1 "3M India Ltd." "18-09-2001"    280   836.2 .000187969 -.008791488   .004700812
    1 "3M India Ltd." "19-09-2001"    290  840.08 .000193344  .007614173   .017786305
    1 "3M India Ltd." "20-09-2001" 278.95  826.37 .000193344 -.004123228   .008266608
    1 "3M India Ltd." "21-09-2001"    272  792.18 .000193344   .01756763    .02199402
    1 "3M India Ltd." "24-09-2001"    271  815.18 .000193344 -.006995669  -.017225754
    1 "3M India Ltd." "25-09-2001"    270  807.94 .000193344  .004815123   .009335892
    1 "3M India Ltd." "26-09-2001"  261.5  821.67 .000187969 -.008984162  -.012087896
    1 "3M India Ltd." "27-09-2001"    260  825.81 .000187969 -.004142652   .006669711
    1 "3M India Ltd." "28-09-2001"    266  850.56 .000187969 -.017421316   .000376217
    1 "3M India Ltd." "01-10-2001"  276.5   844.2 .000187969  .005426327   .001641476
    1 "3M India Ltd." "03-10-2001"    270  832.46 .000184742  .003100564   .014084384
    1 "3M India Ltd." "04-10-2001"  263.8  842.15 .000184742  -.00491618  -.009198776
    1 "3M India Ltd." "05-10-2001"      .  844.87 .000184742  .002996849   -.00724678
    1 "3M India Ltd." "08-10-2001"  273.5  831.16 .000184742  .003219688   .010580573
    1 "3M India Ltd." "09-10-2001"    270  842.27 .000184742 -.012359478   .002310888
    1 "3M India Ltd." "10-10-2001"    278  861.97 .000185818  -.00611104  -.014635766
    1 "3M India Ltd." "11-10-2001"    270  875.14 .000185818 -.008688306  -.003316985
    1 "3M India Ltd." "12-10-2001"  265.5  878.54 .000185818  .000993069  -.005601964
    1 "3M India Ltd." "15-10-2001"  262.2  880.39 .000185818   .01196225   .001652932
    1 "3M India Ltd." "16-10-2001" 259.85  891.59 .000185818  .001527667  -.007085373
    1 "3M India Ltd." "17-10-2001"    260  906.58 .000187969 -.003437033  -.001419346
    1 "3M India Ltd." "18-10-2001"  261.9  890.81 .000187969  .001909316    .01943403
    1 "3M India Ltd." "19-10-2001"    260   899.6 .000187969 -.002249387  -.003969661
    1 "3M India Ltd." "22-10-2001"    265  903.64 .000187969  .015999708   .009118058
    1 "3M India Ltd." "23-10-2001"    265   916.9 .000187969 -.008250358   .000710333
    1 "3M India Ltd." "24-10-2001"  261.6  912.92 .000178282  -.00415184   .008784475
    1 "3M India Ltd." "25-10-2001" 263.25  909.88 .000178282  .002199966    .00303661
    1 "3M India Ltd." "29-10-2001" 265.85  911.35 .000178282  .001279933   .007193609
    1 "3M India Ltd." "30-10-2001"    265  895.86 .000178282  .007337891   .006994818
    1 "3M India Ltd." "31-10-2001"    270  902.84 .000180436 -.002044746   -.00972682
    1 "3M India Ltd." "01-11-2001"  274.9  916.37 .000180436 -.005455928  -.012044747
    1 "3M India Ltd." "02-11-2001" 265.95  918.84 .000180436 -.006226132   .000252876
    1 "3M India Ltd." "05-11-2001"    270  919.12 .000180436  .005960923   .006250963
    1 "3M India Ltd." "06-11-2001"    275  932.31 .000180436  .000496976   -.00934026
    1 "3M India Ltd." "07-11-2001" 270.25  920.84 .000180436  .008103575   .013195202
    1 "3M India Ltd." "08-11-2001" 274.75  931.26 .000180436 -.001850496  -.001523932
    1 "3M India Ltd." "09-11-2001"  271.5  934.92 .000180436  .005705178  -.002968989
    1 "3M India Ltd." "12-11-2001"    273  942.81 .000180436   .02629301  -.002724774
    1 "3M India Ltd." "13-11-2001"    285  937.24 .000180436   .00258975  .0000337117
    1 "3M India Ltd." "14-11-2001"  289.9  950.69 .000180436 -.002274552    .00513802
    1 "3M India Ltd." "15-11-2001"    290  968.45 .000180436 -.002998726  -.024889307
    1 "3M India Ltd." "19-11-2001"  297.2   997.2 .000180436  .016279232  -.012132827
    1 "3M India Ltd." "20-11-2001"    288  986.23 .000180436  .004437539    .00697962
    1 "3M India Ltd." "21-11-2001"    290  994.23 .000180436  .002880188   .006559707
    1 "3M India Ltd." "22-11-2001"    288 1003.35 .000180436 -.006428535   .012129813
    end
    Code:
    * Change date to STATA format
    gen date1=date(month,"DMY")
    format date1 %td
    drop month
    rename date1 date
    gen mdate= mofd(date)
    format mdate %tm
    * Calculate Raw Returns Returns
    bysort stock_id (date) : gen rt_r =((pr[_n]-pr[_n-1])/pr[_n-1])
    bysort stock_id (date) : gen mkt_r =((mk[_n]-mk[_n-1])/mk[_n-1])
    gen rt=rt_r-rf
    gen mkt=mkt_r-rf
    gen mkt2 = mkt^2
    * Generate monthly idiosyncratic skewness
    capture program drop one_regression
    program define one_regression
        regress rt mkt mkt2
        tempvar resid    //  no need to keep resid around after the program
        predict `resid', resid
        summ `resid', detail
        gen double skewness = r(skewness)
        gen obs  = r(N)    
    end
    
    bysort stock_id mdate (date): gen high = cond(_n==1, mdate, -99)
    rangerun one_regression, by(stock_id) interval(mdate -11 high)
    
    keep if mdate==high      // <-- just to help reduce to 1 summary observation per mdate-stock combination
    keep stock* mdate skewness obs  // <-- keep summary variables
    
    drop if mdate<ym(2002,6)
    drop if mdate>ym(2003,5)
    replace skewness =. if obs<200
    drop obs
    rename skewness idioskew
    Last edited by Sartaj Hussain; 24 Nov 2021, 08:41.

  • #2
    In your explanation, you say that you want to regress returns on mkt, smb, and hml. But the actual regression in your one_regression command has mkt and mkt2, and there is no mention of smb and hml. Other than that, it looks right.

    Comment


    • #3
      Yes, absolutely. Thanks for pointing out. It may be read as:

      Code:
      regress rt mkt smb hml

      Comment


      • #4
        Moreover, on the same data example, i want to generate systematic or co-skewness which is equal to beta coeffcient of mkt2 obtained each month by regressing rt on mkt, and mkt2 using twelve months daily rt, mkt, and mkt2 data from months beginning t-11 and ending month t. Does the following code represent this specification or needs some modification.

        Code:
        *Setting date to STATA readable format
        gen date2 = date(date, "DM20Y")
        format date2 %td
        gen mdate = mofd(date2)
        drop date
        rename date2 date
        format mdate %tm mon
        gen month = month(date)
        gen year = year(date)
        
        *Generating Stock and Market Returns, etc.
        bysort stock_id (date) : gen rt_r =((pr[_n]-pr[_n-1])/pr[_n-1])
        bysort stock_id (date) : gen mkt_r =((mk[_n]-mk[_n-1])/mk[_n-1])
        gen rt=rt_r-rf
        gen mkt=mkt_r-rf
        gen mkt2 = mkt^2
        
        *Regressing for generating coskewness measure
        rangestat (reg) rt mkt mkt2, interval(mdate -11 0) by(stock_id)
        collapse(mean) mdate reg_nobs stock_id b_mkt2, by(stock year month)
        order stock year month mdate reg_nobs stock_id b_mkt2
        drop if mdate<ym(2002,6)
        drop if mdate>ym(2003,5)
        replace b_mkt2 =. if reg_nobs<200
        rename b_mkt2 coskew
        keep stock_id stock mdate coskew

        Comment


        • #5
          It looks basically correct. A few little things:

          1. The example data does not actually have a variable called "date" so the code doesn't get past the first line. I assume, however, that you are referring there to the data that is called (inappropriately, since it is a daily date) month. Renaming month as date before starting the code resolves this, and also prevents an error later when you try to generate a variable named month (which would already exist, though it would not be what you want.)

          2. Given the actual format of that daily date variable, you can use "DMY" instead of "DM20Y" in your -gen date2 = date(date, "DM20Y")- command. In the example data, the code works as is. But if your real data extends back before year 2000, then "DM20Y" will cause Stata generate a missing value for the date variable--which, in turn, will result in the exclusion of those observations from all of the regressions.

          3. In the example data, the end result is an empty data set because there aren't even 200 observations in the entire example, let alone 200 for any stock_id and mdate. I assume this isn't a problem in your full data set.

          Comment


          • #6
            Thanks indeed for your observations. I have taken note of them for implementation. About your point 3. Yes absolutely.

            Comment

            Working...
            X