Announcement

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

  • Compute standard deviation of daily stock returns

    Hi,
    I have a set of data including daily stock returns data for about 1500 companies. The example is shown below. I want to compute the "standard deviation of daily stock returns in a given quarter" for each company (shown by ISIN). My data is only one quarter (starting from 01-01-2016 to 03-30-2016).
    I am not sure how to use rangestat while I have daily data.
    I would be grateful if you help to genrate this variable.

    input str12 ISIN float date1 double Daily_return
    "AT0000827209" 20457 1.203007518797
    "AT0000827209" 20458 3.12035661218423
    "AT0000827209" 20459 .
    "AT0000827209" 20460 3.74639769452449
    "AT0000827209" 20461 -5.13888888888887
    "AT0000827209" 20462 .
    "AT0000827209" 20463 .
    "AT0000827209" 20464 -4.39238653001465
    "AT0000827209" 20465 .612557427258808
    "AT0000827209" 20466 1.82648401826485
    "AT0000827209" 20467 -1.64424514200299
    "AT0000827209" 20468 .91185410334346
    "AT0000827209" 20469 .
    "AT0000827209" 20470 .
    "AT0000827209" 20471 -1.80722891566266
    "AT0000827209" 20472 -.153374233128822
    "AT0000827209" 20473 -.768049155145937
    "AT0000827209" 20474 1.39318885448918
    "AT0000827209" 20475 .763358778625944
    "AT0000827209" 20476 .
    "AT0000827209" 20477 .
    "AT0000827209" 20478 0
    "AT0000827209" 20479 .909090909090922
    "AT0000827209" 20480 -3.45345345345347
    "AT0000827209" 20481 -.777604976671859
    "AT0000827209" 20482 -.783699059561119
    "AT0000827209" 20483 .
    "AT0000827209" 20484 .
    "AT0000827209" 20485 -.631911532385469
    "AT0000827209" 20486 -.953895071542126
    "AT0000827209" 20487 .802568218298545
    "AT0000827209" 20488 .159235668789814
    "AT0000827209" 20489 .158982511923694
    "AT0000827209" 20490 .
    "AT0000827209" 20491 .
    "AT0000827209" 20492 -.476190476190474
    "AT0000827209" 20493 -4.3062200956938
    "AT0000827209" 20494 6.50000000000001
    "AT0000827209" 20495 .469483568075115
    "AT0000827209" 20496 -4.82866043613707
    "AT0000827209" 20497 .
    "AT0000827209" 20498 .
    "AT0000827209" 20499 4.58265139116203
    "AT0000827209" 20500 -3.75586854460094
    "AT0000827209" 20501 .975609756097556
    "AT0000827209" 20502 4.50885668276973
    "AT0000827209" 20503 .770416024653321
    "AT0000827209" 20504 .
    "AT0000827209" 20505 .
    "AT0000827209" 20506 3.21100917431193
    "AT0000827209" 20507 -6.66666666666667
    "AT0000827209" 20508 2.38095238095237
    "AT0000827209" 20509 .310077519379855
    "AT0000827209" 20510 .154559505409588
    "AT0000827209" 20511 .
    "AT0000827209" 20512 .
    "AT0000827209" 20513 -3.08641975308643
    "AT0000827209" 20514 2.86624203821657
    "AT0000827209" 20515 -.154798761609912
    "AT0000827209" 20516 -.775193798449603
    "AT0000827209" 20517 -1.09375
    "AT0000827209" 20518 .
    "AT0000827209" 20519 .
    "AT0000827209" 20520 .157977883096372
    "AT0000827209" 20521 .946372239747629
    "AT0000827209" 20522 -2.96875000000001
    "AT0000827209" 20523 .644122383252821
    "AT0000827209" 20524 .160000000000005
    "AT0000827209" 20525 .
    "AT0000827209" 20526 .
    "AT0000827209" 20527 2.07667731629393
    "AT0000827209" 20528 0
    "AT0000827209" 20529 -.625978090766826
    "AT0000827209" 20530 2.51968503937009
    "AT0000827209" 20531 -2.91858678955454
    "AT0000827209" 20532 .
    "AT0000827209" 20533 .
    "AT0000827209" 20534 -.632911392405066
    "AT0000827209" 20535 1.59235668789809
    "AT0000827209" 20536 -2.03761755485893
    "AT0000827209" 20537 1.92000000000001
    "AT0000827209" 20538 .
    "AT0000827209" 20539 .
    "AT0000827209" 20540 .
    "AT0000827209" 20541 .
    "AT0000827209" 20542 .
    "AT0000827209" 20543 -1.88383045525904
    "AT0000831706" 20457 .058513751
    "AT0000831706" 20458 -1.169590643
    "AT0000831706" 20459 .
    "AT0000831706" 20460 -2.130177515
    "AT0000831706" 20461 -4.746070133
    "AT0000831706" 20462 .
    "AT0000831706" 20463 .
    "AT0000831706" 20464 -2.348460806
    "AT0000831706" 20465 .519987
    "AT0000831706" 20466 -4.10604591
    "AT0000831706" 20467 -2.697235334
    "AT0000831706" 20468 -3.846153846
    "AT0000831706" 20469 .
    end
    Last edited by Seyed Mahmoud Hosseinniakani; 21 Jul 2018, 07:17.

  • #2
    I'm not sure I understand what you are asking for. If all your data is in a single quarter and you want the standard deviation of daily returns, you don't need anything fancy. Just
    Code:
    summ Daily_return
    local std_deviation = r(sd)
    will show you the standard deviation in the output of the -summ- command and store that value in the local macro std_deviation, where you can access it for use in later calculations if needed.

    Now, perhaps this is too simple. I notice that your data has two different ISIN's and maybe you need the standard deviation calculated separately for each, rather than a single standard deviation for all. In that case:

    Code:
    by ISIN, sort: egen std_deviation = sd(Daily_return)
    If you meant that your example contains only one quarter, but your real data has several quarters and you need to do this every quarter, you still don't need rangestat to do it. You just need to extract quarterly dates from variable date1.

    Code:
    gen qdate = qofd(date1)
    format qdate %tq
    
    by ISIN qdate, sort: egen std_deviation = sd(Daily_return)

    Comment


    • #3
      Hi Clyde,
      Thank you so much. The last code is exactly what I want. That works well.

      Comment


      • #4
        Hi again,

        Another question raised here since we are using the following code which you suggested to me. I used the following code to compute the standard deviation of daily returns for the given quarter (including 8 quarters) for a large number of companies (identified by ISIN). Then, I need to convert the data in a format which include only one value of each variable (here std_deviation) for each company for a quarter. For one company I should have only 8 value for 8 quarters. Should I use duplicates command in order to delete duplicated observation or something else?
        Thanks in advance!

        Code: gen qdate = qofd(date1) format qdate %tq by ISIN qdate, sort: egen std_deviation = sd(Daily_return)

        Comment


        • #5
          Hi Seyed,

          Yes, you can use the duplicates command for this. I suggest you save ISIN, std_deviation, and any other variables you want only on a quarterly basis to a new dataset, and then eliminate the duplicates from that.

          Best,
          Devra
          Devra Golbe
          Professor Emerita, Dept. of Economics
          Hunter College, CUNY

          Comment


          • #6
            Or -collapse-.

            Comment

            Working...
            X