Announcement

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

  • Rolling average using a variable as weight with tssmooth or other

    I would like to calculate the rolling average of the estimated beta coefficient via statsby (say "_b_LogSize") weighted by their t-statistics (say "t_LogSize"). A 5 months rolling window would be fine to start with. I am trying to get more stable coefficient, the intuitions is that we want to give more importance to less noisy beta estimates (hence higher t-stat) in the rolling window average.

    Would you be able to help me code that? Thanks.


    Code:
     
     * Example generated by -dataex-. To install: ssc install dataex clear input float(date _b_LogSize t_LogSize abs_t_LogSize tsum_LogSize tw_LogSize) 480   -8.266647   -4.911039   4.911039         .          . 481    2.581961    2.566609   2.566609         .          . 482    1.255753    1.632749   1.632749         .          . 483    .8442108   1.1369855  1.1369855         .          . 484  -3.3915694  -3.5551646  3.5551646 13.802547  .25757307 485  -1.0104964  -1.3168218  1.3168218  10.20833  .12899484 486   -1.903375  -2.2898364  2.2898364  9.931558  .23056166 487    .4738106    .6090372   .6090372 8.9078455  .06837087 488   -.0876909  -.11545384  .11545384  7.886314 .014639772 489   1.1190147    1.424378   1.424378  5.755527     .24748 490  -1.2875426   -1.538554   1.538554  5.977259  .25740126 491   -4.066994   -3.895815   3.895815  7.583238   .5137403 492   -.3479997    -.489352    .489352  7.463553  .06556556 493     .777527   1.1554649  1.1554649  8.503564  .13588007 494   -3.302649  -3.8024635  3.8024635  10.88165   .3494382 495  -1.8951005   -3.518439   3.518439 12.861534   .2735629 496    -3.67513   -5.062582   5.062582   14.0283   .3608835 497   .07620832   .13785669  .13785669 13.676805 .010079597 498   -.4320115   -.9034046   .9034046 13.424746  .06729398 499    2.646034    3.619975   3.619975 13.242257   .2733654 500   -6.142633   -7.235136   7.235136 16.958954   .4266263 501   -2.587543   -3.311952   3.311952 15.208324  .21777233 502  -2.1349971  -4.4263663  4.4263663 19.496834     .22703 503    .4620627     .861635    .861635 19.455065  .04428846 504   1.2540562    2.224489   2.224489 18.059578  .12317502 505   -2.555257   -5.383336   5.383336 16.207779   .3321452 506   .13377367   .23725756  .23725756 13.133084 .018065639 507    1.979651   4.3510914  4.3510914  13.05781   .3332176 508    .6884593    1.247736   1.247736  13.44391   .0928105 509    .4770622    .6950083   .6950083  11.91443  .05833333 510   -.4007422   -.7961677   .7961677  7.327261   .1086583 511   -.9754319  -1.9351906  1.9351906  9.025194  .21442093 512    .6744914    .8957349   .8957349  5.569838  .16081886 513   -5.285298   -5.344624   5.344624  9.666725   .5528888 514    .4946819    .9643504   .9643504  9.936068  .09705554 515   -.3796268   -.7855311   .7855311  9.925431  .07914327 516 -.033686493   -.0726955   .0726955  8.062936 .009016009 517  -1.7550406   -3.186371   3.186371 10.353572   .3077557 518   -.3205542   -.6093335   .6093335  5.618281   .1084555 519   -5.138365   -7.682914   7.682914 12.336844   .6227617 520   -.3349997   -.8906261   .8906261  12.44194  .07158258 521   -2.908237   -5.094873   5.094873 17.464117  .29173377 522   -.7331108   -1.782251   1.782251 16.059998  .11097455 523   -1.350431   -3.309204   3.309204 18.759869  .17639804 524  -1.0057278  -2.1181622  2.1181622 13.195116  .16052623 525  -1.0435315  -2.6591425  2.6591425 14.963633    .177707 526    .6656214   1.9491335  1.9491335 11.817893   .1649307 527  -1.0886611    -2.61192    2.61192 12.647563   .2065157 528   -.6431454  -1.8611777  1.8611777 11.199536  .16618346 529  -1.2877115   -4.164419   4.164419 13.245793   .3143956 530  -.07760277  -.18290086  .18290086  10.76955 .016983146 531   -.7118146  -1.9242047  1.9242047 10.744623  .17908536 532   -.6422755  -2.0677242  2.0677242 10.200427  .20270957 533   .19157185    .5342802   .5342802  8.873529  .06021056 534   -.2652633   -.7714291   .7714291  5.480539  .14075789 535   -1.318615  -3.7864094  3.7864094  9.084047   .4168196 536   -.8856038  -2.0179932  2.0179932  9.177836   .2198768 537  -1.7783096   -5.388499   5.388499 12.498611  .43112785 538   -.9958586   -3.248513   3.248513 15.212844   .2135375 539    .6375813   1.9973515  1.9973515 16.438766  .12150252 540    .1909554    .5236455   .5236455 13.176003  .03974237 541  -.17884855    -.613762    .613762 11.771771  .05213846 542  -.55945045  -1.5065902  1.5065902  7.889862  .19095267 543   -1.655147   -4.741406   4.741406  9.382755  .50533193 544  -1.4834945    -4.56018    4.56018 11.945584   .3817461 545   -.9255059   -2.598146   2.598146 14.020084    .185316 546 -.022075985 -.072398596 .072398596  13.47872 .005371326 547    .1892124    .5711942   .5711942 12.543324   .0455377 548   -.3425543   -.9594861   .9594861  8.761405   .1095128 549   -.9611792   -2.651947   2.651947  6.853171  .38696635 550   -.6658006  -2.2426708  2.2426708  6.497696   .3451486 551   -2.314715   -5.489883   5.489883  11.91518   .4607469 552   -.6898829  -2.0486205  2.0486205 13.392607   .1529665 553   -1.823041   -5.690817   5.690817 18.123938   .3139945 554    .9535037    3.016883   3.016883 18.488874   .1631729 555   -.1609003   -.4754425   .4754425 16.721645 .028432757 556   .18754247    .6115062   .6115062  11.84327  .05163323 557   1.0243611    2.611286   2.611286 12.405935  .21048684 558   -.7195773  -2.1321523  2.1321523   8.84727   .2409955 559   -.8113478  -2.3689744  2.3689744  8.199362   .2889218 560  -1.6538635   -5.002859   5.002859 12.726778   .3930971 561  -1.0327011  -3.5210226  3.5210226 15.636294  .22518267 562   .54082316   2.1440485  2.1440485 15.169057  .14134356 563   -1.244109  -4.0540304  4.0540304 17.090935  .23720355 564  -1.0785816   -3.872051   3.872051 18.594011  .20824185 565    .1194961    .4390831   .4390831 14.030236  .03129549 566   .20267415    .6495556   .6495556  11.15877  .05821033 567  -.23517534   -.7762432   .7762432  9.790963   .0792816 568    .1605866   .56066215  .56066215  6.297596  .08902797 569    .2715707    .7454796   .7454796  3.171024  .23509115 570   -.6019359   -1.850141   1.850141  4.582082   .4037774 571     .744113   2.2486794  2.2486794  6.181205  .36379305 572   -.7933149   -1.911649   1.911649  7.316611   .2612752 573    .7634989   1.8988053  1.8988053  8.654755  .21939446 574   -.3443597   -.9029207   .9029207  8.812196  .10246262 575    -.299389   -.6598354   .6598354   7.62189   .0865711 576   -.8753999  -2.0526454  2.0526454  7.425856  .27641872 577   .08494118   .22103527  .22103527  5.735242  .03853983 578   -.8450174  -2.0233812  2.0233812  5.859818   .3452976 579   -1.264442  -3.2563105  3.2563105  8.213208   .3964724 end format %tm date

  • #2
    Hi Nick Cox ,

    As requested, I have reposted the problem. Would you be able to help me with it? Thanks a lot in advance.

    Comment


    • #3
      So you want a numerator which is a sum of (coefficient * |t|) and a denominator that is a sum of |t|. and it might be simplest to calculate those terms separately, divide and then smooth. If there is weighting in terms of position in the window that can be applied in the tssmooth step.

      There is some bemusement at the common circumstance of people asking for estimates from short windows in nonstationary set-ups and then fighting the unstable results that ensue.

      Comment


      • #4
        Nick Cox ,

        I have followed your advise and calculated the moving average step by step. That said, I found that "mvsumm" outputs a missing value as soon as there is 1 missing observation in the rolling window. I would like the sum to be calculated even if there are missing observations (and the number of missing observation is large, it is fine). Is there any way to do that? Thanks


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(date _b_cons _se_cons)
        420   5.235463  .4037871
        421   2.637793  .4058051
        422  2.1695664  .3941343
        423   3.394307  .3744062
        424   3.718754  .4345176
        425   5.040031  .4324667
        426  1.6427498  .3967432
        427     2.9233  .4361332
        428  -1.251731 .51226795
        429   4.908429   .481409
        430  2.3579211 .54432464
        431   2.436128  .5687554
        432  1.8171914  .3997969
        433  1.4277178  .4025899
        434   3.591775  .5172937
        435  2.3184636  .4067847
        436  -.7598963  .4264558
        437  -6.013553  .4280576
        438   4.366294  .4037682
        439   5.383612  .4108178
        440  1.1763055  .4288642
        441   6.780958  .4751916
        442  .15786995  .4212696
        443  4.1936893 .58589226
        444   .6976556   .478064
        445 -3.4035025  .4244522
        446   3.047014  .4904323
        447   8.286017  .6000704
        448  4.7289295  .4038502
        449   8.061139 .53246087
        450  -.7699274  .3822343
        451   6.956202  .5361638
        452  -3.689183  .4537393
        453   2.490534  .4182274
        454          .  .5026994
        455          . .49219525
        456          .  .4454123
        457   5.493586  .4599263
        458  1.7082976 .45441365
        459  -3.163379  .3834031
        460   1.908874  .5838114
        461  -3.350941  .5250871
        462 -15.846566  .5937477
        463          .  .7439138
        464          .  .6647503
        465          .  .6519957
        466          .   .679787
        467          .  .8157451
        468          .  .5238271
        469          .  .6967819
        470          .  .8545355
        471          .  .4537542
        472          .  .6030177
        473          . .56334877
        474 -1.7396554 .53681165
        475          . .53161365
        476  3.2035575  .6939787
        477   2.803353  .6828648
        478   5.779533  .8593852
        479  -2.635584  .7307889
        480  1.4875647  1.409249
        481  10.364455  .8611727
        482   .5120128  .6650609
        483          .   .633361
        484          .  .8127021
        485  2.7445076  .6752296
        486   8.071743  .6957738
        487  -.3374953  .6946788
        488   1.919629   .660174
        489          .  .6769049
        490          .  .7454608
        491          .  .8464224
        492          .  .6304375
        493 -4.6182528  .6146526
        494          .  .7939447
        495          .  .4954495
        496          .  .6671479
        497   .3104184 .50857276
        498 -2.6642945  .4362705
        499 -10.788202  .6733522
        500          .  .7731413
        501          .  .7157365
        502          .  .4391771
        503   .4236472  .4900339
        504  -.2026007 .50882864
        505   6.528147 .42923555
        506 -2.4020104   .516851
        507 -1.3701775   .408389
        508  -6.106886  .4996288
        509  -9.192336  .6342255
        510          .  .4650242
        511          .  .4648311
        512          .   .699941
        513          .   .900997
        514          .  .4820879
        515          .  .4522624
        516          .  .4417731
        517          . .52349055
        518          . .50444263
        519          .  .6400056
        end
        format %tm date
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(date _b_cons _se_cons t_cons abs_t_cons tsum_cons bw_cons)
        420   5.235463  .4037871    12.9659   12.9659         .           .
        421   2.637793  .4058051   6.500147  6.500147         .           .
        422  2.1695664  .3941343   5.504637  5.504637         .           .
        423   3.394307  .3744062   9.065841  9.065841         .           .
        424   3.718754  .4345176   8.558351  8.558351         .           .
        425   5.040031  .4324667  11.654148 11.654148         .           .
        426  1.6427498  .3967432   4.140587  4.140587         .           .
        427     2.9233  .4361332   6.702769  6.702769         .           .
        428  -1.251731 .51226795  -2.443508  2.443508         .           .
        429   4.908429   .481409  10.195964 10.195964         .           .
        430  2.3579211 .54432464  4.3318286 4.3318286         .           .
        431   2.436128  .5687554   4.283262  4.283262         .           .
        432  1.8171914  .3997969  4.5452867 4.5452867         .           .
        433  1.4277178  .4025899   3.546333  3.546333         .           .
        434   3.591775  .5172937   6.943396  6.943396         .           .
        435  2.3184636  .4067847   5.699486  5.699486         .           .
        436  -.7598963  .4264558 -1.7818875 1.7818875         .           .
        437  -6.013553  .4280576 -14.048466 14.048466         .           .
        438   4.366294  .4037682  10.813863 10.813863         .           .
        439   5.383612  .4108178   13.10462  13.10462 146.83028     .480488
        440  1.1763055  .4288642   2.742839  2.742839 136.60722    .0236182
        441   6.780958  .4751916  14.269943 14.269943 144.37701    .6702167
        442  .15786995  .4212696    .374748   .374748 139.24713 .0004248666
        443  4.1936893 .58589226   7.157783  7.157783 137.33907   .21856503
        444   .6976556   .478064  1.4593352 1.4593352 130.24005  .007817207
        445 -3.4035025  .4244522  -8.018578  8.018578 126.60448  -.21556304
        446   3.047014  .4904323   6.212915  6.212915  128.6768    .1471193
        447   8.286017  .6000704   13.80841  13.80841 135.78246    .8426473
        448  4.7289295  .4038502  11.709612 11.709612 145.04855    .3817613
        449   8.061139 .53246087  15.139402 15.139402 149.99199     .813649
        450  -.7699274  .3822343  -2.014281  2.014281 147.67444  -.01050182
        451   6.956202  .5361638  12.974023 12.974023  156.3652   .57717395
        452  -3.689183  .4537393  -8.130622  8.130622 159.95055   -.1875289
        453   2.490534  .4182274   5.954975  5.954975 162.35918   .09134726
        454          .  .5026994          .         .         .           .
        455          . .49219525          .         .         .           .
        456          .  .4454123          .         .         .           .
        457   5.493586  .4599263  11.944492 11.944492         .           .
        458  1.7082976 .45441365   3.759345  3.759345         .           .
        459  -3.163379  .3834031  -8.250791  8.250791         .           .
        460   1.908874  .5838114   3.269676  3.269676         .           .
        461  -3.350941  .5250871  -6.381685  6.381685         .           .
        462 -15.846566  .5937477  -26.68906  26.68906         .           .
        463          .  .7439138          .         .         .           .
        464          .  .6647503          .         .         .           .
        465          .  .6519957          .         .         .           .
        466          .   .679787          .         .         .           .
        467          .  .8157451          .         .         .           .
        468          .  .5238271          .         .         .           .
        469          .  .6967819          .         .         .           .
        470          .  .8545355          .         .         .           .
        471          .  .4537542          .         .         .           .
        472          .  .6030177          .         .         .           .
        473          . .56334877          .         .         .           .
        474 -1.7396554 .53681165 -3.2407184 3.2407184         .           .
        475          . .53161365          .         .         .           .
        476  3.2035575  .6939787   4.616219  4.616219         .           .
        477   2.803353  .6828648  4.1052833 4.1052833         .           .
        478   5.779533  .8593852   6.725195  6.725195         .           .
        479  -2.635584  .7307889  -3.606492  3.606492         .           .
        480  1.4875647  1.409249  1.0555726 1.0555726         .           .
        481  10.364455  .8611727   12.03528  12.03528         .           .
        482   .5120128  .6650609   .7698735  .7698735         .           .
        483          .   .633361          .         .         .           .
        484          .  .8127021          .         .         .           .
        485  2.7445076  .6752296  4.0645547 4.0645547         .           .
        486   8.071743  .6957738  11.601103 11.601103         .           .
        487  -.3374953  .6946788  -.4858292  .4858292         .           .
        488   1.919629   .660174   2.907762  2.907762         .           .
        489          .  .6769049          .         .         .           .
        490          .  .7454608          .         .         .           .
        491          .  .8464224          .         .         .           .
        492          .  .6304375          .         .         .           .
        493 -4.6182528  .6146526  -7.513598  7.513598         .           .
        494          .  .7939447          .         .         .           .
        495          .  .4954495          .         .         .           .
        496          .  .6671479          .         .         .           .
        497   .3104184 .50857276   .6103716  .6103716         .           .
        498 -2.6642945  .4362705  -6.106978  6.106978         .           .
        499 -10.788202  .6733522 -16.021635 16.021635         .           .
        500          .  .7731413          .         .         .           .
        501          .  .7157365          .         .         .           .
        502          .  .4391771          .         .         .           .
        503   .4236472  .4900339   .8645263  .8645263         .           .
        504  -.2026007 .50882864  -.3981708  .3981708         .           .
        505   6.528147 .42923555  15.208776 15.208776         .           .
        506 -2.4020104   .516851  -4.647394  4.647394         .           .
        507 -1.3701775   .408389 -3.3550794 3.3550794         .           .
        508  -6.106886  .4996288 -12.222846 12.222846         .           .
        509  -9.192336  .6342255 -14.493798 14.493798         .           .
        510          .  .4650242          .         .         .           .
        511          .  .4648311          .         .         .           .
        512          .   .699941          .         .         .           .
        513          .   .900997          .         .         .           .
        514          .  .4820879          .         .         .           .
        515          .  .4522624          .         .         .           .
        516          .  .4417731          .         .         .           .
        517          . .52349055          .         .         .           .
        518          . .50444263          .         .         .           .
        519          .  .6400056          .         .         .           .
        end
        format %tm date

        Code:
                gen t_cons = _b_cons/_se_cons            
                gen abs_t_cons=abs(t_cons)
                mvsumm abs_t_cons, stat(sum) win(20) /*win($roll)*/ gen(tsum_cons) end
                gen bw_cons = (abs_t_cons*_b_cons)/tsum_cons

        Comment


        • #5
          mvsumm is from SSC (as you are asked to explain) and does exactly what you say, work only with complete windows. For what you want, rangestat (SSC) seems preferable.

          Comment

          Working...
          X