Announcement

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

  • Calculate the standard deviation of the firm's daily stock return over the past three months

    Hello everyone,

    I have daily panel data and I want to calculate the standard deviation of the firm's daily stock return over the past three months. Could anyone help me to find an appropriate code to calculate annualized 3-month rolling sample standard deviation and assuming the standard deviation is centered on zero, instead of centered around mean value given a time period.

    Sample of my dataset is attached :



    Kind Regards
    Obada






    Attached Files

  • #2
    I want to calculate the standard deviation of the firm's daily stock return over the past three months.
    Code:
    rangestat (sd) ret, by(permno) interval(date -89 0)
    Note: to use this code you must install -rangestat-, written by Robert Picard, Roberto Ferrer, and Nick Cox, available from SSC.

    I have interpreted "past three months" to mean a 90 day period that includes the current date, hence -interval(date -89 0)-. If that is not what you meant by "past three months," modify the -interval- option accordingly.

    Could anyone help me to find an appropriate code to calculate annualized 3-month rolling sample standard deviation and assuming the standard deviation is centered on zero, instead of centered around mean value given a time period.
    I do not understand this.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      In the following example, I shall use asrol, which can be downloaded from SSC. You can read further resources related to asrol here on my website.

      In the following example, I assume that the rolling window is based on a fixed length of three months. Later on, I also show the code where the rolling window is based on the past 90 days.

      Use example dataset from my site. The data set has three variables, (1) symbols: a string variable to identify each firm (2) close - a numeric variable that records companies' share prices (3) date - a numeric variable that tracks daily dates

      Code:
      use http://fintechprofessor.com/stocks.dta
      
      * List the first 10 observations
           +-----------------------------+
           | symbol        date    close |
           |-----------------------------|
        1. |   AABS   07jan2013    103.5 |
        2. |   AABS   14jan2013      103 |
        3. |   AABS   16jan2013      103 |
        4. |   AABS   17jan2013   105.75 |
        5. |   AABS   25feb2013     96.7 |
           |-----------------------------|
        6. |   AABS   27feb2013      101 |
        7. |   AABS   05mar2013      106 |
        8. |   AABS   19mar2013      101 |
        9. |   AABS   28mar2013      101 |
       10. |   AABS   10apr2013      101 |
           +-----------------------------+
      
      * Create a month identifier
      gen mofd = mofd(date)
      format mofd %tm
      
      * generate returns
      bys symbol (date): gen returns = ln(close / close[_n-1])
      
      * Install asrol
      ssc install asrol
      
      * Find 3 months rolling window sd with asrol
      bys symbol: asrol returns, window(mofd 3) stat(sd)
      
      * Convert to annual volatility
      gen annual_sd = sd3_returns*sqrt(12)/sqrt(3)
      
      list  in 1/10
           +-------------------------------------------------------------------------+
           | symbol        date    close     mofd     returns   sd3_ret~s   annual~d |
           |-------------------------------------------------------------------------|
        1. |   AABS   07jan2013    103.5   2013m1           .     .016786    .033572 |
        2. |   AABS   14jan2013      103   2013m1   -.0048426     .016786    .033572 |
        3. |   AABS   16jan2013      103   2013m1           0     .016786    .033572 |
        4. |   AABS   17jan2013   105.75   2013m1    .0263488     .016786    .033572 |
        5. |   AABS   25feb2013     96.7   2013m2   -.0894644   .05122231   .1024446 |
           |-------------------------------------------------------------------------|
        6. |   AABS   27feb2013      101   2013m2    .0435071   .05122231   .1024446 |
        7. |   AABS   05mar2013      106   2013m3    .0483186   .04661255   .0932251 |
        8. |   AABS   19mar2013      101   2013m3   -.0483186   .04661255   .0932251 |
        9. |   AABS   28mar2013      101   2013m3           0   .04661255   .0932251 |
       10. |   AABS   10apr2013      101   2013m4           0   .05331813   .1066363 |
           +-------------------------------------------------------------------------+
      Method 2: Using trading days

      Code:
      bys symbol: asrol returns, window(date 90) stat(sd) gen(sd_90days)
      
      * Convert to annual volatility
      gen annual_sd2 = sd_90days *sqrt(252)/sqrt(90)
      
      
      list  in 1/10
      
           +------------------------------------------------------------------------------------------------+
           | symbol        date    close     mofd     returns   sd3_ret~s   annual~d   sd_90days   annual~2 |
           |------------------------------------------------------------------------------------------------|
        1. |   AABS   07jan2013    103.5   2013m1           .     .016786    .033572           .          . |
        2. |   AABS   14jan2013      103   2013m1   -.0048426     .016786    .033572           .          . |
        3. |   AABS   16jan2013      103   2013m1           0     .016786    .033572   .00342425   .0057299 |
        4. |   AABS   17jan2013   105.75   2013m1    .0263488     .016786    .033572     .016786   .0280883 |
        5. |   AABS   25feb2013     96.7   2013m2   -.0894644   .05122231   .1024446    .0502229    .084039 |
           |------------------------------------------------------------------------------------------------|
        6. |   AABS   27feb2013      101   2013m2    .0435071   .05122231   .1024446   .05122231   .0857113 |
        7. |   AABS   05mar2013      106   2013m3    .0483186   .04661255   .0932251   .05070348   .0848431 |
        8. |   AABS   19mar2013      101   2013m3   -.0483186   .04661255   .0932251   .05032963   .0842176 |
        9. |   AABS   28mar2013      101   2013m3           0   .04661255   .0932251   .04661255   .0779977 |
       10. |   AABS   10apr2013      101   2013m4           0   .05331813   .1066363   .04361395   .0729801 |
           +------------------------------------------------------------------------------------------------+
      The case of centering on zero

      Code:
      gen return2 = returns * returns
      bys symbol: asrol return2 , window(date 90) stat(mean) gen(sd0_90days)
      gen annual_sd0 = sqrt( sd0_90days ) *sqrt(252)/sqrt(90)
      
      . list symbol date returns annual_sd0 in 1/10
      
           +-------------------------------------------+
           | symbol        date     returns   annual~0 |
           |-------------------------------------------|
        1. |   AABS   07jan2013           .          . |
        2. |   AABS   14jan2013   -.0048426   .0081033 |
        3. |   AABS   16jan2013           0   .0057299 |
        4. |   AABS   17jan2013    .0263488   .0258817 |
        5. |   AABS   25feb2013   -.0894644   .0781353 |
           |-------------------------------------------|
        6. |   AABS   27feb2013    .0435071    .077098 |
        7. |   AABS   05mar2013    .0483186   .0777363 |
        8. |   AABS   19mar2013   -.0483186   .0781891 |
        9. |   AABS   28mar2013           0   .0731392 |
       10. |   AABS   10apr2013           0   .0689563 |
           +-------------------------------------------+
      Last edited by Attaullah Shah; 11 Feb 2019, 07:02.
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        I have a question concerning post #2 as I have a similar issue. I used it for 21 days (1 month of trading days). From that I want to annualize it. However, I don't know exactly what the rangestat command is giving you. Is this

        a) the daily standard deviation calculated over the interval, or

        b) the standard deviation over the interval?

        I mean, I could use Excel to check it but someone may know the solution and may want to share it. I have not really found the answer in the help rangestat.

        Comment


        • #5
          Michael Craig In turn I really don't follow what distinction you are making or alluding to. rangestat (SSC) does not pay attention to whether data are daily or anything else specifically; it just works on what it is instructed to work on.

          You don't give a data example or exact code you used (FAQ Advice #12 remains as valid as ever), so answers are restricted to explanations of principle or other examples invented for the purpose.

          Whether trading days do or do not complicate your problem I can't understand either.

          Here's an example you can run:

          Code:
          . clear
          
          . set obs 100
          number of observations (_N) was 0, now 100
          
          . gen y = _n
          
          . gen t = y  
          
          . rangestat (count) y (sd) y, int(t -20 0)
          
          .
          . list in 1/30
          
               +-------------------------------+
               |  y    t   y_count        y_sd |
               |-------------------------------|
            1. |  1    1         1           . |
            2. |  2    2         2   .70710678 |
            3. |  3    3         3           1 |
            4. |  4    4         4   1.2909944 |
            5. |  5    5         5   1.5811388 |
               |-------------------------------|
            6. |  6    6         6   1.8708287 |
            7. |  7    7         7   2.1602469 |
            8. |  8    8         8   2.4494897 |
            9. |  9    9         9   2.7386128 |
           10. | 10   10        10   3.0276504 |
               |-------------------------------|
           11. | 11   11        11   3.3166248 |
           12. | 12   12        12   3.6055513 |
           13. | 13   13        13   3.8944405 |
           14. | 14   14        14   4.1833001 |
           15. | 15   15        15    4.472136 |
               |-------------------------------|
           16. | 16   16        16   4.7609523 |
           17. | 17   17        17   5.0497525 |
           18. | 18   18        18   5.3385391 |
           19. | 19   19        19   5.6273143 |
           20. | 20   20        20   5.9160798 |
               |-------------------------------|
           21. | 21   21        21   6.2048368 |
           22. | 22   22        21   6.2048368 |
           23. | 23   23        21   6.2048368 |
           24. | 24   24        21   6.2048368 |
           25. | 25   25        21   6.2048368 |
               |-------------------------------|
           26. | 26   26        21   6.2048368 |
           27. | 27   27        21   6.2048368 |
           28. | 28   28        21   6.2048368 |
           29. | 29   29        21   6.2048368 |
           30. | 30   30        21   6.2048368 |
               +-------------------------------+
          
          .
          . su t in 1/21
          
              Variable |        Obs        Mean    Std. Dev.       Min        Max
          -------------+---------------------------------------------------------
                     t |         21          11    6.204837          1         21
          Key points that may illuminate your puzzlement:

          1. If given a window, rangestat uses whatever is available in that window. So in this example, windows potentially of length 21 only contain 21 observations from observation 21 on. Unlike some other commands, rangestat lets (indeed makes) users decide what they want to do with incomplete windows. Further, the interval() option could catch more observations than are implied by its length, as nothing in rangestat assumes that distinct values of the variable within that interval occur at most once. For example, rangestat neither assumes xtset or tsset beforehand nor makes use of either if either has been declared.

          2. The standard deviation recipe used is precisely that of summarize, which is documented. Sample size MINUS 1 is used, not sample size.

          3. You refer to annualized rates but I fear that rangestat may disappoint you there. In no sense is it geared to financial data; it's a general command and does not offer hooks for specialised financial calculations. I don't work in that area at all; Robert Picard who is first author and prime mover of rangestat, I think knows more about it than I do, but I think he would affirm that we don't explicitly offer hooks for particular kinds of data. However, what is important is that rangestat can be extended by writing your own Mata functions, as the help explains at length.

          I will refer you also to asrol (SSC), which in stark contrast is written with specific financial calculations in mind. I don't advise on its use, not least because its author Attaullah Shah watches the forum for mentions and is always willing to help.

          Comment


          • #6
            Thanks for the helpful explanation.

            I was just wondering if the code in #2
            rangestat (sd) ret, by(permno) interval(date -89 0) is calculating a daily standard deviation or a standard deviation over 90 days. For example, if it calculates daily and I need 90 days, I need to take sqrt(90) times daily std. I think it calculates 90day std but I am not totally sure, therefore my question. There is no actually code from my side required, as the interpretation of #2 gives me everything I need.

            Comment


            • #7
              Sorry, but you seem to be asking the same question, and I don't have a different answer. You make a verbal distinction

              daily standard deviation

              or

              a standard deviation over 90 days

              but I fail to understand what you're getting at.

              Also, I have explained, or at least implied, that multiplication by sqrt(90) is not an option that rangestat offers, but it is easy enough to apply outside rangestat, or (according to taste and expertise) write your own function for use with rangestat.

              I've already explained about incomplete windows in #5.

              My claiming ignorance of specialist calculations for finance is not false modesty, but genuine.

              There's a key principle behind #5. Don't wonder what code does: read the code itself (in the case of rangestat nothing is hidden) and/or experiment with simple examples where you can check independently what is happening, as I checked with summarize what the SD was.

              Comment


              • #8
                Dear Nick Cox ,

                I would like to calculate the standard deviation of previous two years' weekly stock returns. Assume I get weekly stock returns already. How can I use rangestat command to calculate the standard divation over previous two years? Normally, one years include 52 weeks in financial area. but some years, there are 53 weekly obs....
                Because the number of weeks for each year maybe vary. So I cannot do ... interval(month -103 0) .....

                Do you have any suggestion for me?

                Thank you very much in advance.

                Comment


                • #9
                  #8

                  From #7 I echo

                  My claiming ignorance of specialist calculations for finance is not false modesty, but genuine.
                  What do other people do with such data?

                  Otherwise p\lease give a data example. FAQ Advice #12 applies as always.

                  What does a variable month have to do with a question about weekly data?

                  Comment


                  • #10
                    Dear Nick Cox ,

                    Thank you for your reply. I do not ask about specialist calculations. I understand what your responese at #7. Therefore, ,my question is different. I would like to know how to use rangestat to calulate the standard deviation of certain number of obs.

                    Here is my data.

                    Code:
                    TRCode Date return
                    GPRL 1/2/2014 
                    GPRL 1/9/2014 .0308133
                    GPRL 1/16/2014 -.0013903
                    GPRL 1/23/2014 -.03063
                    GPRL 1/30/2014 .0233393
                    GPRL 2/6/2014 -.0614035
                    GPRL 2/13/2014 .0007477
                    GPRL 2/20/2014 .0085917
                    GPRL 2/27/2014 -.0003704
                    GPRL 3/6/2014 .1081882
                    GPRL 3/13/2014 .0177198
                    GPRL 3/20/2014 -.0466491
                    GPRL 3/27/2014 .0434183
                    GPRL 4/3/2014 .0904888
                    GPRL 4/10/2014 .0529982
                    GPRL 4/17/2014 -.0353753
                    GPRL 4/24/2014 .0399523
                    GPRL 5/1/2014 .016055
                    GPRL 5/8/2014 -.0513544
                    GPRL 5/15/2014 .0960738
                    GPRL 5/22/2014 .1082768
                    GPRL 5/29/2014 -.0022037
                    GPRL 6/5/2014 -.0262577
                    GPRL 6/12/2014 -.0438508
                    GPRL 6/19/2014 -.0005271
                    GPRL 6/26/2014 .0780591
                    GPRL 7/3/2014 .0273973
                    GPRL 7/10/2014 .0754762
                    GPRL 7/17/2014 .0225814
                    GPRL 7/24/2014 .0441654
                    GPRL 7/31/2014 -.0470661
                    GPRL 8/7/2014 -.0461271
                    GPRL 8/14/2014 -.0866788
                    GPRL 8/21/2014 -.017982
                    GPRL 8/28/2014 .0155137
                    GPRL 9/4/2014 -.0042575
                    GPRL 9/11/2014 -.02666
                    GPRL 9/18/2014 .0173127
                    GPRL 9/25/2014 -.001016
                    GPRL 10/2/2014 -.0030511
                    GPRL 10/9/2014 .0015302
                    GPRL 10/16/2014 -.1033868
                    GPRL 10/23/2014 -.0227208
                    GPRL 10/30/2014 .0784656
                    GPRL 11/6/2014 -.0285637
                    GPRL 11/13/2014 .0183079
                    GPRL 11/20/2014 -.0332335
                    GPRL 11/27/2014 -.0369118
                    GPRL 12/4/2014 -.0263312
                    GPRL 12/11/2014 -.0090144
                    GPRL 12/18/2014 -.0451789
                    GPRL 12/25/2014 -.0123849
                    GPRL 1/1/2015 -.0318328
                    GPRL 1/8/2015 -.0099635
                    GPRL 1/15/2015 .0073801
                    GPRL 1/22/2015 .038628
                    GPRL 1/29/2015 .0057711
                    GPRL 2/5/2015 .0044629
                    GPRL 2/12/2015 -.0729927
                    GPRL 2/19/2015 .0619651
                    GPRL 2/26/2015 .0367505
                    GPRL 3/5/2015 -.0951493
                    GPRL 3/12/2015 .0683849
                    GPRL 3/19/2015 .0331296
                    GPRL 3/26/2015 .0554172
                    GPRL 4/2/2015 .0073746
                    GPRL 4/9/2015 .0430454
                    GPRL 4/16/2015 .0235823
                    GPRL 4/23/2015 .0112452
                    GPRL 4/30/2015 .0008137
                    GPRL 5/7/2015 -.0468835
                    GPRL 5/14/2015 .005118
                    GPRL 5/21/2015 -.0523338
                    GPRL 5/28/2015 .0295522
                    GPRL 6/4/2015 .0034793
                    GPRL 6/11/2015 .0167582
                    GPRL 6/18/2015 .0048309
                    GPRL 6/25/2015 -.0079186
                    GPRL 7/2/2015 -.0142531
                    GPRL 7/9/2015 -.0575477
                    GPRL 7/16/2015 -.0104326
                    GPRL 7/23/2015 -.020155
                    GPRL 7/30/2015 -.0170886
                    GPRL 8/6/2015 .0534449
                    GPRL 8/13/2015 -.021088
                    GPRL 8/20/2015 -.049641
                    GPRL 8/27/2015 -.0588042
                    GPRL 9/3/2015 -.0104712
                    GPRL 9/10/2015 -.038448
                    GPRL 9/17/2015 .0172414
                    GPRL 9/24/2015 .0151461
                    GPRL 10/1/2015 -.0017762
                    GPRL 10/8/2015 .03879
                    GPRL 10/15/2015 .0226105
                    GPRL 10/22/2015 -.0107203
                    GPRL 10/29/2015 -.1381646
                    GPRL 11/5/2015 -.0227898
                    GPRL 11/12/2015 .0261359
                    GPRL 11/19/2015 -.0525078
                    GPRL 11/26/2015 -.0107527
                    GPRL 12/3/2015 -.0426421
                    GPRL 12/10/2015 .0004367
                    GPRL 12/17/2015 -.045395
                    GPRL 12/24/2015 -.0310928
                    GPRL 12/31/2015 .0122699
                    GPRL 1/7/2016 -.0097902
                    GPRL 1/14/2016 .0390772
                    GPRL 1/21/2016 .0616221
                    GPRL 1/28/2016 .0072557
                    GPRL 2/4/2016 .0224576
                    GPRL 2/11/2016 .002901
                    GPRL 2/18/2016 -.0082645
                    GPRL 2/25/2016 .0004167
                    GPRL 3/3/2016 .18409
                    GPRL 3/10/2016 -.2244108
                    GPRL 3/17/2016 .0435374
                    GPRL 3/24/2016 -.0147762
                    GPRL 3/31/2016 .0816056
                    GPRL 4/7/2016 -.0619902
                    GPRL 4/14/2016 .0530435
                    GPRL 4/21/2016 -.0082576
                    GPRL 4/28/2016 .0399667
                    GPRL 5/5/2016 .0064051
                    GPRL 5/12/2016 -.0453461
                    GPRL 5/19/2016 -.0079167
                    GPRL 5/26/2016 .0041999
                    GPRL 6/2/2016 .019657
                    GPRL 6/9/2016 .4364233
                    GPRL 6/16/2016 -.1956025
                    GPRL 6/23/2016 .0099397
                    GPRL 6/30/2016 -.0042179
                    GPRL 7/7/2016 .204377
                    GPRL 7/14/2016 -.0521688
                    GPRL 7/21/2016 -.3107607
                    GPRL 7/28/2016 .0897263
                    GPRL 8/4/2016 -.0526966
                    GPRL 8/11/2016 -.0425902
                    GPRL 8/18/2016 .1130277
                    GPRL 8/25/2016 .0297716
                    GPRL 9/1/2016 .0154455
                    GPRL 9/8/2016 -.0292512
                    GPRL 9/15/2016 .0269184
                    GPRL 9/22/2016 .0449922
                    GPRL 9/29/2016 .0475477
                    GPRL 10/6/2016 .0268049
                    GPRL 10/13/2016 -.0090498
                    GPRL 10/20/2016 .0779768
                    GPRL 10/27/2016 .0130336
                    GPRL 11/3/2016 -.0681891
                    GPRL 11/10/2016 -.0176044
                    GPRL 11/17/2016 -.0049192
                    GPRL 11/24/2016 -.0105932
                    GPRL 12/1/2016 .0021413
                    GPRL 12/8/2016 .0192308
                    GPRL 12/15/2016 -.0132774
                    GPRL 12/22/2016 .0290368
                    GPRL 12/29/2016 .0141087
                    GPRL 1/5/2017 .0302002
                    GPRL 1/12/2017 .0032938
                    GPRL 1/19/2017 -.0338148
                    GPRL 1/26/2017 .0502888
                    GPRL 2/2/2017 .0116467
                    GPRL 2/9/2017 -.0070355
                    GPRL 2/16/2017 -.0144928
                    GPRL 2/23/2017 .0395425
                    GPRL 3/2/2017 .0543854
                    GPRL 3/9/2017 -.0068575
                    GPRL 3/16/2017 -.0141099
                    GPRL 3/23/2017 .0301462
                    GPRL 3/30/2017 -.0381318
                    GPRL 4/6/2017 .0236632
                    GPRL 4/13/2017 .0030021
                    GPRL 4/20/2017 .0565699
                    GPRL 4/27/2017 .0127479
                    GPRL 5/4/2017 .0237762
                    GPRL 5/11/2017 -.0795082
                    GPRL 5/18/2017 .0418522
                    GPRL 5/25/2017 .0635328
                    GPRL 6/1/2017 -.0008036
                    GPRL 6/8/2017 .016622
                    GPRL 6/15/2017 .0263713
                    GPRL 6/22/2017 .0465057
                    GPRL 6/29/2017 .0078566
                    GPRL 7/6/2017 -.0267966
                    GPRL 7/13/2017 .0297872
                    GPRL 7/20/2017 .033544
                    GPRL 7/27/2017 -.020461
                    GPRL 8/3/2017 -.0489796
                    GPRL 8/10/2017 -.01641
                    GPRL 8/17/2017 -.0277207
                    GPRL 8/24/2017 .029831
                    GPRL 8/31/2017 .0158934
                    GPRL 9/7/2017 -.0406258
                    GPRL 9/14/2017 -.0068385
                    GPRL 9/21/2017 -.0007945
                    GPRL 9/28/2017 -.0055659
                    GPRL 10/5/2017 -.0029318
                    GPRL 10/12/2017 -.02085
                    GPRL 10/19/2017 -.015834
                    GPRL 10/26/2017 -.0061026
                    GPRL 11/2/2017 .0178621
                    GPRL 11/9/2017 -.0008226
                    GPRL 11/16/2017 .0321076
                    GPRL 11/23/2017 .0071789
                    GPRL 11/30/2017 -.0285111
                    GPRL 12/7/2017 .0548913
                    GPRL 12/14/2017 -.052035
                    GPRL 12/21/2017 -.0407609
                    GPRL 12/28/2017 .0048159
                    TISE 1/2/2014 
                    TISE 1/9/2014 -.0278686
                    TISE 1/16/2014 -.0013164
                    TISE 1/23/2014 .0458407
                    TISE 1/30/2014 -.0211455
                    TISE 2/6/2014 -.0187411
                    TISE 2/13/2014 .0508821
                    TISE 2/20/2014 -.0226138
                    TISE 2/27/2014 .0242725
                    TISE 3/6/2014 -.0467018
                    TISE 3/13/2014 -.0267481
                    TISE 3/20/2014 -.0089619
                    TISE 3/27/2014 -.0001507
                    TISE 4/3/2014 .0064818
                    TISE 4/10/2014 .0220159
                    TISE 4/17/2014 .0033705
                    TISE 4/24/2014 -.0163575
                    TISE 5/1/2014 .009948
                    TISE 5/8/2014 .0263158
                    TISE 5/15/2014 -.0508523
                    TISE 5/22/2014 -.0105644
                    TISE 5/29/2014 .0359976
                    TISE 6/5/2014 .0136926
                    TISE 6/12/2014 .042992
                    TISE 6/19/2014 -.0403843
                    TISE 6/26/2014 -.0007256
                    TISE 7/3/2014 .0027592
                    TISE 7/10/2014 .0194062
                    TISE 7/17/2014 -.0099446
                    TISE 7/24/2014 .0007175
                    TISE 7/31/2014 -.0055922
                    TISE 8/7/2014 -.0027397
                    TISE 8/14/2014 -.0005784
                    TISE 8/21/2014 .0021701
                    TISE 8/28/2014 -.0153024
                    TISE 9/4/2014 -.0042516
                    TISE 9/11/2014 -.0278269
                    TISE 9/18/2014 -.0137816
                    TISE 9/25/2014 -.0116708
                    TISE 10/2/2014 .0023306
                    TISE 10/9/2014 -.0148814
                    TISE 10/16/2014 -.0314713
                    TISE 10/23/2014 -.029082
                    TISE 10/30/2014 -.001004
                    TISE 11/6/2014 .0335008
                    TISE 11/13/2014 -.0427877
                    TISE 11/20/2014 .0220115
                    TISE 11/27/2014 .018224
                    TISE 12/4/2014 -.0261959
                    TISE 12/11/2014 -.0203843
                    TISE 12/18/2014 .0006822
                    TISE 12/25/2014 -.0352821
                    TISE 1/1/2015 .0641343
                    TISE 1/8/2015 -.0654159
                    TISE 1/15/2015 .0191864
                    TISE 1/22/2015 -.0282378
                    TISE 1/29/2015 -.0001794
                    TISE 2/5/2015 .0405454
                    TISE 2/12/2015 -.0432759
                    TISE 2/19/2015 -.0720851
                    TISE 2/26/2015 .0075743
                    TISE 3/5/2015 -.0354665
                    TISE 3/12/2015 -.044964
                    TISE 3/19/2015 -.000837
                    TISE 3/26/2015 .0433508
                    TISE 4/2/2015 .007226
                    TISE 4/9/2015 -.0984456
                    TISE 4/16/2015 .061229
                    TISE 4/23/2015 .0131223
                    TISE 4/30/2015 -.0117188
                    TISE 5/7/2015 -.0062409
                    TISE 5/14/2015 -.025539
                    TISE 5/21/2015 .0227712
                    TISE 5/28/2015 -.0247847
                    TISE 6/4/2015 .003446
                    TISE 6/11/2015 .0002146
                    TISE 6/18/2015 .0834764
                    TISE 6/25/2015 -.0883343
                    TISE 7/2/2015 .011297
                    TISE 7/9/2015 -.0238453
                    TISE 7/16/2015 -.0103433
                    TISE 7/23/2015 .019791
                    TISE 7/30/2015 -.0196249
                    TISE 8/6/2015 -.0507117
                    TISE 8/13/2015 .0513121
                    TISE 8/20/2015 -.0109204
                    TISE 8/27/2015 -.3050924
                    TISE 9/3/2015 .0311284
                    TISE 9/10/2015 .0003145
                    TISE 9/17/2015 .0204338
                    TISE 9/24/2015 .0070856
                    TISE 10/1/2015 .3386357
                    TISE 10/8/2015 -.226691
                    TISE 10/15/2015 .0227541
                    TISE 10/22/2015 .0366946
                    TISE 10/29/2015 .0604794
                    TISE 11/5/2015 -.0010512
                    TISE 11/12/2015 .2423047
                    TISE 11/19/2015 -.2166455
                    TISE 11/26/2015 .0481211
                    TISE 12/3/2015 .0247614
                    TISE 12/10/2015 -.0201359
                    TISE 12/17/2015 -.0133573
                    TISE 12/24/2015 .0106743
                    TISE 12/31/2015 .0528078
                    TISE 1/7/2016 -.0680206
                    TISE 1/14/2016 -.003413
                    TISE 1/21/2016 -.1454162
                    TISE 1/28/2016 .0317509
                    TISE 2/4/2016 .1443083
                    TISE 2/11/2016 .024282
                    TISE 2/18/2016 -.1738465
                    TISE 2/25/2016 .1255785
                    TISE 3/3/2016 .0317982
                    TISE 3/10/2016 .0286929
                    TISE 3/17/2016 .0433884
                    TISE 3/24/2016 -.0556931
                    TISE 3/31/2016 .0180865
                    TISE 4/7/2016 .0399073
                    TISE 4/14/2016 -.0505076
                    TISE 4/21/2016 .011734
                    TISE 4/28/2016 0
                    TISE 5/5/2016 -.075
                    TISE 5/12/2016 .0401226
                    TISE 5/19/2016 .0375033
                    TISE 5/26/2016 .091402
                    TISE 6/2/2016 -.0603265
                    TISE 6/9/2016 .0392749
                    TISE 6/16/2016 -.0016957
                    TISE 6/23/2016 .009949
                    TISE 6/30/2016 -.0456511
                    TISE 7/7/2016 .041289
                    TISE 7/14/2016 .0050774
                    TISE 7/21/2016 -.0108251
                    TISE 7/28/2016 .0026751
                    TISE 8/4/2016 -.0133398
                    TISE 8/11/2016 .0427729
                    TISE 8/18/2016 .0014144
                    TISE 8/25/2016 -.0301318
                    TISE 9/1/2016 -.0245146
                    TISE 9/8/2016 .0303558
                    TISE 9/15/2016 -.0326008
                    TISE 9/22/2016 .0072391
                    TISE 9/29/2016 .0146221
                    TISE 10/6/2016 -.0075721
                    TISE 10/13/2016 -.0504553
                    TISE 10/20/2016 -.0049248
                    TISE 10/27/2016 .0630372
                    TISE 11/3/2016 -.1359961
                    TISE 11/10/2016 .1463415
                    TISE 11/17/2016 -.065809
                    TISE 11/24/2016 -.0323093
                    TISE 12/1/2016 .0021894
                    TISE 12/8/2016 .0027307
                    TISE 12/15/2016 .0087146
                    TISE 12/22/2016 -.0029698
                    TISE 12/29/2016 -.0232873
                    TISE 1/5/2017 .03715
                    TISE 1/12/2017 .0248597
                    TISE 1/19/2017 -.0245175
                    TISE 1/26/2017 -.018984
                    TISE 2/2/2017 -.0106296
                    TISE 2/9/2017 .038292
                    TISE 2/16/2017 -.0212258
                    TISE 2/23/2017 .0759013
                    TISE 3/2/2017 .0788612
                    TISE 3/9/2017 -.0450724
                    TISE 3/16/2017 .0141844
                    TISE 3/23/2017 .0217024
                    TISE 3/30/2017 -.0160491
                    TISE 4/6/2017 .0741185
                    TISE 4/13/2017 .0031264
                    TISE 4/20/2017 -.0552093
                    TISE 4/27/2017 .0188501
                    TISE 5/4/2017 -.0407031
                    TISE 5/11/2017 .0460463
                    TISE 5/18/2017 .0313436
                    TISE 5/25/2017 .024581
                    TISE 6/1/2017 -.0008724
                    TISE 6/8/2017 .0406025
                    TISE 6/15/2017 -.0004196
                    TISE 6/22/2017 .0304302
                    TISE 6/29/2017 -.0099796
                    TISE 7/6/2017 .0514297
                    TISE 7/13/2017 .1062414
                    TISE 7/20/2017 .0083127
                    TISE 7/27/2017 -.0114015
                    TISE 8/3/2017 .0168559
                    TISE 8/10/2017 -.0342
                    TISE 8/17/2017 -.0325203
                    TISE 8/24/2017 -.0304388
                    TISE 8/31/2017 .0025039
                    TISE 9/7/2017 .081268
                    TISE 9/14/2017 -.0069296
                    TISE 9/21/2017 .0309537
                    TISE 9/28/2017 -.0208261
                    TISE 10/5/2017 .0436016
                    TISE 10/12/2017 .0823709
                    TISE 10/19/2017 .0400126
                    TISE 10/26/2017 -.006035
                    TISE 11/2/2017 .0034912
                    TISE 11/9/2017 .0654969
                    TISE 11/16/2017 -.0340715
                    TISE 11/23/2017 -.0191064
                    TISE 11/30/2017 .0394067
                    TISE 12/7/2017 .0106674
                    TISE 12/14/2017 -.0359435
                    TISE 12/21/2017 .0285545
                    TISE 12/28/2017 -.0208573
                    I only provide the weekly data for 2 companies from 2014 to 2017. Now, I would like to calculate the variable which is standard deviations of previous two years' stock return. For example, the value of this variable of company GPRL for year 2016 will be the standard devidation of previous 104 weeks (from year 2014 to year 2015, I count that there are 104 weeks, each year has 52 weeks ).

                    However, there are some problem here. The number of weeks for each year maybe different . I means it is not always 52 weeks each year. So, I do not know what should I do.

                    Could you please help. Thank you very much in advance.

                    I do apologise because my data is messy. Honestly, I do not how to post it here as others do.
                    Last edited by Celine Tran; 22 Feb 2020, 18:11.

                    Comment


                    • #11
                      https://www.statalist.org/forums/help#stata is the link I alluded to earlier, which tells you how to improve your data examples.

                      Comment


                      • #12
                        However, there are some problem here. The number of weeks for each year maybe different . I means it is not always 52 weeks each year. So, I do not know what should I do.

                        Well, imagine for the moment that you were going to do these calculations by hand. (To be very, very clear, I am not recommending you actually do that--this is a thought experiment.) How would you know which weeks to include in each case? If you can explain that, it will probably not be too hard to write code that corresponds to it. Looking at your example data, it is not obvious to me why the number of weeks should vary from one instance to another, nor where the information defining the included weeks would come from.

                        Comment


                        • #13
                          Dear Nick Cox and Clyde Schechter ,

                          For #12, I collect weekly stock price form Datastream. All data, including datem, is automatically download from Datastream. The raw data looks like:
                          Code:
                          input str10 A str17(IM IP)
                          "Code"       "DISA.SI(RI)~U$"    "SUNR.SI(RI)~U$"  
                          "  1/1/1998" "20.29"             "47.43"          
                          "  1/8/1998" "17.06"             "28.04"          
                          " 1/15/1998" "15.81"             "42.54"          
                          " 1/22/1998" "15.82"             "42.56"          
                          " 1/29/1998" "16.15"             "43.44"          
                          "  2/5/1998" "24.41"             "45.14"          
                          " 2/12/1998" "23.77"             "63.54"          
                          " 2/19/1998" "22.6"              "64.55"          
                          " 2/26/1998" "26.52"             "64.63"          
                          "  3/5/1998" "21.5"              "63.61"          
                          " 3/12/1998" "21.09"             "64.70999999999999"
                          " 3/19/1998" "22.07"             "65.3"            
                          " 3/26/1998" "23.01"             "62.59"          
                          "  4/2/1998" "18.72"             "61.53"          
                          "  4/9/1998" "18.3"              "39.24"          
                          " 4/16/1998" "16.59"             "65.44"          
                          " 4/23/1998" "16.73"             "50.3"            
                          " 4/30/1998" "16.79"             "50.47"          
                          "  5/7/1998" "14.8"              "38.41"          
                          " 5/14/1998" "13.76"             "48.24"          
                          " 5/21/1998" "15.49"             "48.88"          
                          " 5/28/1998" "12.1"              "41.76"          
                          "  6/4/1998" "11.26"             "42.94"          
                          " 6/11/1998" "10.18"             "35.84"          
                          " 6/18/1998" "11.38"             "37.41"          
                          " 6/25/1998" "10.65"             "36.75"          
                          "  7/2/1998" "9.66"              "47.02"          
                          "  7/9/1998" "10.32"             "43.63"          
                          " 7/16/1998" "9.060000000000001" "40.04"          
                          " 7/23/1998" "8.140000000000001" "42.27"          
                          " 7/30/1998" "7.37"              "42.1"            
                          "  8/6/1998" "8"                 "41.55"          
                          " 8/13/1998" "7.25"              "37"              
                          " 8/20/1998" "7.98"              "36.99"          
                          " 8/27/1998" "7.88"              "36.52"          
                          "  9/3/1998" "7.94"              "35.34"          
                          " 9/10/1998" "8.050000000000001" "35.83"          
                          " 9/17/1998" "8.82"              "36"              
                          " 9/24/1998" "7.37"              "39.1"            
                          " 10/1/1998" "7.5"               "39.78"          
                          " 10/8/1998" "6.91"              "35.27"          
                          "10/15/1998" "8.66"              "40.14"          
                          "10/22/1998" "9.380000000000001" "40.67"          
                          "10/29/1998" "11.73"             "37.49"          
                          " 11/5/1998" "12.5"              "47.83"          
                          "11/12/1998" "10.69"             "51.39"          
                          "11/19/1998" "11.71"             "54.14"          
                          "11/26/1998" "11.5"              "53.21"          
                          " 12/3/1998" "10.78"             "56.54"          
                          "12/10/1998" "11.66"             "57.1"            
                          "12/17/1998" "11.58"             "44.11"          
                          "12/24/1998" "10.77"             "43.94"          
                          "12/31/1998" "10.74"             "43.84"          
                          "  1/7/1999" "11.37"             "43.29"          
                          " 1/14/1999" "13.56"             "54.56"          
                          " 1/21/1999" "13.64"             "47.16"          
                          " 1/28/1999" "11.98"             "45.85"          
                          "  2/4/1999" "12"                "39.04"          
                          " 2/11/1999" "12"                "42.86"          
                          " 2/18/1999" "13.4"              "42.54"          
                          " 2/25/1999" "12.47"             "37.43"          
                          "  3/4/1999" "13.13"             "37.21"          
                          " 3/11/1999" "13.13"             "37.2"            
                          " 3/18/1999" "12.47"             "37.42"          
                          " 3/25/1999" "13.93"             "41.89"          
                          "  4/1/1999" "13.19"             "37.37"          
                          "  4/8/1999" "14.64"             "41.82"          
                          " 4/15/1999" "14.96"             "55.7"            
                          " 4/22/1999" "22.97"             "55.95"          
                          " 4/29/1999" "20.15"             "51.78"          
                          "  5/6/1999" "23.08"             "53.94"          
                          " 5/13/1999" "22.27"             "46.95"          
                          " 5/20/1999" "22.81"             "52.55"          
                          " 5/27/1999" "26.33"             "74.63"          
                          "  6/3/1999" "30.08"             "79.34"          
                          " 6/10/1999" "35.43"             "78.33"          
                          " 6/17/1999" "55.08"             "83.54000000000001"
                          " 6/24/1999" "62.5"              "91.84999999999999"
                          "  7/1/1999" "72.31999999999999" "108.02"          
                          "  7/8/1999" "55.18"             "81.41"          
                          " 7/15/1999" "44.74"             "57.82"          
                          " 7/22/1999" "41.01"             "56.3"            
                          " 7/29/1999" "42.76"             "59.69"          
                          "  8/5/1999" "39.35"             "55.59"          
                          " 8/12/1999" "34.06"             "52.51"          
                          " 8/19/1999" "36.99"             "55.45"          
                          " 8/26/1999" "47.15"             "65.67"          
                          "  9/2/1999" "42.84"             "56.74"          
                          "  9/9/1999" "42.77"             "58.18"          
                          " 9/16/1999" "41.89"             "58"              
                          " 9/23/1999" "41.37"             "54.27"          
                          " 9/30/1999" "37.23"             "53.18"          
                          " 10/7/1999" "40.71"             "55.38"          
                          "10/14/1999" "39.97"             "53.86"          
                          "10/21/1999" "37.92"             "51.07"          
                          "10/28/1999" "36.45"             "50.36"          
                          " 11/4/1999" "40.27"             "55.82"          
                          "11/11/1999" "46.1"              "60.92"          
                          "11/18/1999" "45.49"             "59.56"          
                          end
                          Because the raw date is exported from Datastream in a form of row, the first column is Date, the sencond column is stock price of firm 1, the thirst column is for firm 2 and so on.....

                          Before calculating the standard deviation that I mentioned in #10, I need to format the raw data. In details, I need to transpose the panel data to the form that all firms are in one column, and then the next column is Date. But I do not know how to do. Could you please help? The one I post in #10 is from my mamuanlly transposing.

                          Thank you very much in advance.

                          Regards,
                          Celine
                          Last edited by Celine Tran; 24 Feb 2020, 18:46.

                          Comment


                          • #14
                            This is a -reshape- problem, not a transposition.

                            Code:
                            rename A str_date
                            foreach v of varlist IM-IP {
                                rename `v' price_`=strtoname(`v'[1])'
                            }
                            drop in 1
                            destring price_*, replace
                            gen date = daily(str_date, "MDY")
                            assert missing(date) == missing(str_date)
                            format date %td
                            drop str_date
                            
                            reshape long price_, i(date) j(firm) string
                            rename price_ price
                            This question really does not belong in this thread; it is off topic. While it is easy to think of these threads as dialogs between a questioner and a responder, in fact there are many others who read long on selected topics of interest, or who come and search for answers to their own questions in earlier posts. For the sake of those people and not wasting their time, it is important to keep threads on topic. In the future, when you are posing a question that is not clearly related to an existing thread, start a New Topic instead.

                            Comment


                            • #15
                              Dear Clyde Schechter ,

                              I appreciate your help. The main question I would like to ask is related to calcuate standard deviation at #10.

                              I though that I can deal with "reshape" by myself ..., so that I post the question about standard deviation here. I am sorry.

                              Regarding your code to reshape the panel data, it seems not to work with my data. I post in a new topic and would like to mention you there.

                              I will back to this topic about standard deviation later.

                              One a gain, thank you so much.

                              Comment

                              Working...
                              X