Announcement

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

  • Rolling window - serial correlation calculation

    I have a panel data set and I am trying to calculate the serial correlation of a time series of returns, cov(R(t+1), R(t)) . My Return variable is px_change. The estimation window is 2; i.e. I need to calculate the correlation between observation 1 & 2, after that I need to calculate the correlation between observation 2 & 3.


    Code:
    tsset isin date
    rolling, window(2) recursive clear start(1) end(1304) : corr L.px_change px_change


    The code "works". When I run the code it does not feedback mechanical errors and also separates the different panels nicely (panel variable "isin" is strongly balanced). The thing is that I only receive missing values for the correlation coefficient. Or do I fail to retrieve the data from somewhere?


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int date double px_change long isin float(bond count)
    19297    .005740245403982524 1 1   1
    19298    .001604174230207426 1 1   2
    19299   .0038185634446308548 1 1   3
    19302   .0025192301232743034 1 1   4
    19303  -.0029568451383769553 1 1   5
    19304    .010719896497551026 1 1   6
    19305  -.0003906672097217705 1 1   7
    19306    .003118243805088974 1 1   8
    19309   -.001914867161271677 1 1   9
    19310   .0021012590943822475 1 1  10
    19311  -.0016907431810835138 1 1  11
    19312  -.0032958913435116232 1 1  12
    19313 -.00019990671020191336 1 1  13
    19316   -.002707611303652383 1 1  14
    19317    -.00823677813327533 1 1  15
    19318   -.003242897212793062 1 1  16
    19319   -.001867563547863741 1 1  17
    19320    -.00328493417432156 1 1  18
    19323    .005827040525962547 1 1  19
    19324  -.0006249313842229998 1 1  20
    19325    .008771410946518026 1 1  21
    19326  -.0006115080794457333 1 1  22
    19327  -.0030258834573862084 1 1  23
    19330  -.0035815474639113018 1 1  24
    19331    .004750373364158749 1 1  25
    19332    .006911320120927171 1 1  26
    19333    .004728822464825676 1 1  27
    19334   -.003735369801610383 1 1  28
    19337  .00003332777870359014 1 1  29
    19338  -.0023578617609811476 1 1  30
    19339 -.00009186494183275302 1 1  31
    19340  -.0005595924162700015 1 1  32
    19341    .002206195733016973 1 1  33
    19344   -.002860073211203511 1 1  34
    19345   -.005301712603692804 1 1  35
    19346  -.0037746952501050697 1 1  36
    19347    .007240445228310224 1 1  37
    19348    .003912566291607661 1 1  38
    19351  -.0013352694740707074 1 1  39
    19352   .0002256279979275736 1 1  40
    19353   .0006433124744136932 1 1  41
    19354    .015312682641730009 1 1  42
    19355   .0002960428933258887 1 1  43
    19358  -.0014222295297599624 1 1  44
    19359   .0006339170309631273 1 1  45
    19360   -.022008490752320125 1 1  46
    19361   -.008858491280316965 1 1  47
    19362   -.010779520608406295 1 1  48
    19365    .006229300018876658 1 1  49
    19366    .004996930213520691 1 1  50
    19367   .0019005922381169673 1 1  51
    19368   -.011271828051692797 1 1  52
    19369    .003588834356879144 1 1  53
    19372    .004847657250149337 1 1  54
    19373    .005894443595101009 1 1  55
    19374   .0021109159686571195 1 1  56
    19375  -.0052071923290811515 1 1  57
    19376    .012959073045128076 1 1  58
    19379   -.005108951954111327 1 1  59
    19380 -.00047906003378635735 1 1  60
    19381    .001706943813800203 1 1  61
    19382  -.0070343912901141455 1 1  62
    19383   -.006348750116238687 1 1  63
    19386   -.004262378764675895 1 1  64
    19387   .0009484018147797248 1 1  65
    19388    .000452411438326908 1 1  66
    19389   .0050766618601912825 1 1  67
    19390  -.0016129306100273156 1 1  68
    19393    .009081014896945768 1 1  69
    19394   -.006572516768344072 1 1  70
    19395   .0009330257175815925 1 1  71
    19396   .0012795959527481335 1 1  72
    19397  .00037238589334526883 1 1  73
    19400   -.001615891574521267 1 1  74
    19401   -.003330226252012547 1 1  75
    19402   -.006189581438057497 1 1  76
    19403   .0029258527320791413 1 1  77
    19404  -.0005459306838634231 1 1  78
    19407    .003371256411788268 1 1  79
    19408 -.00026369064833867834 1 1  80
    19409   -.005743165632896949 1 1  81
    19410    .010551447935921169 1 1  82
    19411  -.0006012414365436066 1 1  83
    19414   .0002711450795641044 1 1  84
    19415    .013570520965692535 1 1  85
    19416    .000927690301875474 1 1  86
    19417  -.0005260389268806091 1 1  87
    19418    .007376775271512076 1 1  88
    19421   -.001683487722149913 1 1  89
    19422   -.005682006977903286 1 1  90
    19423  -.0017043176045982615 1 1  91
    19424    -.00331402939108895 1 1  92
    19425  -.0024769933499026136 1 1  93
    19428  -.0032575483371352133 1 1  94
    19429   .0053034269596500426 1 1  95
    19430  .00008400396498719036 1 1  96
    19431   .0002183919631757236 1 1  97
    19432    .001284872100639846 1 1  98
    19435    .007162566782128802 1 1  99
    19436    .015372444518466118 1 1 100
    end
    format %td date
    label values isin isin
    label def isin 1 "XS0295018070", modify



    Thanks!

  • #2
    There are several problems with your -rolling- command. The first is that you forgot to tell Stata what results you wanted. That has to come right after the word "rolling". Also, by specifying start(1) and end(1304) you are telling Stata to do this from date 1 to date 1304, but no such dates occur in your data. You mean start(19297) and end(19436) (or whatever the first and last dates in your actual data are. Next, there is an inconsistency between window(2) and recursive. These are contradictory options. If you use window(2) you are saying that you want to include two observations each time, moving your way through the data set. If you say recursive you say that you want always to start with the first observation and then the window expands with each subsequent observation. I'm not sure what you actually want to do here anyway. To say that you want to correlate px_change and L.px_change with a window of 2 really makes no sense, because, except when a date gap makes the lag undefined, the correlation of two things in two observations is always 1, 0, or -1 (there is always a straight line between any two points.) Since that's probably not a useful results for you, I think you need to clarify in your own mind what you want, and then try to code it properly.

    Finally, be aware that L.px_change is going to be undefined when there is a gap in the dates, which means that the corresponding results for that observation will be missing values.

    Well, let me add one more thing. If your dataset is large, using -rolling- will take a long time and you would be better off doing this with the -rangestat- command. -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer, and is available from SSC. Read -help rangestat- after you install it for detailed instructions, and see in particular worked examples near the bottom of the help page that are similar to what you want.

    Comment


    • #3
      Thanks for the keen feedback.

      Comment

      Working...
      X