Announcement

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

  • Identifying peaks and throughs within a time series

    Dear all,

    By following the method of Bry and Boschan (1971), full reference placed below, I want to identify Peaks and Troughs for my quarterly time series data-set. I want to identify peaks and troughs in the earnings data. I'd start by creating a dummy variable earnings_point which is 1 for a peak (local maximum) and -1 for a trough (local minimum)

    I'd like to define a peak as a case where yt−2 ,yt-1< yt >yt+1,yt+2. Similarly, for a trough this would be the case where yt−2, yt−1, > yt < yt+1,yt+2.

    In theory, I understand this, unfortunately I am not that experienced with Stata so I have no clue how to code this. If someone could help me, I would be very grateful.

    I am also aware that there is a package available in Stata (sbbq), but I am interested in writing it as a code so I am able to understand better the process behind it and improve my knowledge of Stata. My data looks as follows:


    yearQ deflated_seasonal_earnings
    1947q1 8.743261
    1947q2 8.92267
    1947q3 8.990711
    1947q4 9.064721
    1948q1 9.124517
    1948q2 9.198627
    1948q3 9.39039
    1948q4 9.590679
    1949q1 9.667767
    1949q2 9.713504
    1949q3 9.758883
    1949q4 9.680266
    1950q1 9.902534
    1950q2 10.07589
    1950q3 10.0649
    1950q4 10.25335
    1951q1 10.16216
    1951q2 10.30928
    1951q3 10.42188
    1951q4 10.46898
    1952q1 10.59212
    1952q2 10.67364
    1952q3 10.67226
    1952q4 10.96858
    1953q1 11.17539
    1953q2 11.29197
    1953q3 11.38358
    1953q4 11.40842
    1954q1 11.37225
    1954q2 11.4531
    1954q3 11.41521
    1954q4 11.49819
    1955q1 11.6033
    1955q2 11.71379
    1955q3 11.87905
    1955q4 11.87453
    1956q1 11.84568
    1956q2 12.01675
    1956q3 11.99835
    1956q4 12.23063
    1957q1 12.19279
    1957q2 12.15003
    1957q3 12.22509
    1957q4 12.3208


    Thank you very much in advance!

    Rajendra



    Reference: Bry, Gerhard and Charlotte Boschan, 1971. Cyclical Analysis of Time Series: Procedures and Computer Programs, New York, NBER.
    Last edited by Ryan Marapin; 18 Feb 2019, 10:51.

  • #2
    Please use dataex to give us a data example (FAQ Advice #12).

    Comment


    • #3
      Dear Mr. Cox,

      My apologies for not following the FAQ Advice. I have used dataex and provide the following example of my dataset:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(yq deflated_seasonal_earnings unemployment_seasonal)
      -52  8.743261         .
      -51   8.92267         .
      -50  8.990711         .
      -49  9.064721         .
      -48  9.124517  3.266667
      -47  9.198627  3.333333
      -46   9.39039       3.3
      -45  9.590679  3.466667
      -44  9.667767  4.133333
      -43  9.713504  5.266666
      -42  9.758883  6.133333
      -41  9.680266  6.366667
      -40  9.902534  5.766666
      -39 10.075892  5.066667
      -38 10.064902       4.2
      -37 10.253347  3.766667
      -36 10.162162  3.166667
      -35 10.309278       2.8
      -34 10.421884  2.833333
      -33 10.468982  3.033333
      -32  10.59212       2.7
      -31  10.67364       2.6
      -30 10.672258  2.833333
      -29 10.968582 2.4333334
      -28  11.17539       2.4
      -27 11.291967       2.3
      -26 11.383578       2.4
      -25 11.408422       3.3
      -24 11.372252       4.7
      -23 11.453097  5.333333
      -22  11.41521  5.466667
      -21 11.498194  4.933333
      -20 11.603304 4.2999997
      -19 11.713794  3.966667
      -18  11.87905 3.6666665
      -17 11.874526  3.733333
      -16 11.845675 3.5666666
      -15 12.016753       3.7
      -14 11.998354  3.666667
      -13 12.230635 3.6333334
      -12 12.192786  3.466667
      -11 12.150026 3.5666666
      -10 12.225093       3.7
       -9 12.320804 4.4333334
       -8 12.168785  5.733333
       -7 12.236784       6.8
       -6 12.327385  6.633334
       -5  12.43292  5.733333
       -4  12.58787  5.266667
       -3  12.75146 4.4666667
       -2 12.682154       4.6
       -1 12.653102  4.933333
        0  12.92443       4.5
        1  12.91257 4.5666666
        2 12.928444       4.9
        3  12.93043  5.533333
        4 12.922525         6
        5 13.071506  6.333333
        6 13.156328  5.933333
        7 13.253417  5.433333
        8   13.3231  4.833333
        9 13.340406       4.8
       10  13.39058       4.9
       11 13.440544  4.833333
       12   13.4993  4.966667
       13 13.592345       4.8
       14 13.690648       4.6
       15 13.695463       4.7
      end
      format %tq yq
      Best,

      Ryan

      Comment


      • #4
        Thanks for the data example. So, a peak is strictly greater than the two previous and the two following values and a trough (NB spelling) strictly less. Windows of 5 are short enough for us to work directly with leading and lagged values.

        In doing that we need to think a little about what would happen if missing values were present. The code below is a little defensive on that, but I don't guarantee it bulletproof.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float(yq deflated_seasonal_earnings unemployment_seasonal)
        -52  8.743261         .
        -51   8.92267         .
        -50  8.990711         .
        -49  9.064721         .
        -48  9.124517  3.266667
        -47  9.198627  3.333333
        -46   9.39039       3.3
        -45  9.590679  3.466667
        -44  9.667767  4.133333
        -43  9.713504  5.266666
        -42  9.758883  6.133333
        -41  9.680266  6.366667
        -40  9.902534  5.766666
        -39 10.075892  5.066667
        -38 10.064902       4.2
        -37 10.253347  3.766667
        -36 10.162162  3.166667
        -35 10.309278       2.8
        -34 10.421884  2.833333
        -33 10.468982  3.033333
        -32  10.59212       2.7
        -31  10.67364       2.6
        -30 10.672258  2.833333
        -29 10.968582 2.4333334
        -28  11.17539       2.4
        -27 11.291967       2.3
        -26 11.383578       2.4
        -25 11.408422       3.3
        -24 11.372252       4.7
        -23 11.453097  5.333333
        -22  11.41521  5.466667
        -21 11.498194  4.933333
        -20 11.603304 4.2999997
        -19 11.713794  3.966667
        -18  11.87905 3.6666665
        -17 11.874526  3.733333
        -16 11.845675 3.5666666
        -15 12.016753       3.7
        -14 11.998354  3.666667
        -13 12.230635 3.6333334
        -12 12.192786  3.466667
        -11 12.150026 3.5666666
        -10 12.225093       3.7
         -9 12.320804 4.4333334
         -8 12.168785  5.733333
         -7 12.236784       6.8
         -6 12.327385  6.633334
         -5  12.43292  5.733333
         -4  12.58787  5.266667
         -3  12.75146 4.4666667
         -2 12.682154       4.6
         -1 12.653102  4.933333
          0  12.92443       4.5
          1  12.91257 4.5666666
          2 12.928444       4.9
          3  12.93043  5.533333
          4 12.922525         6
          5 13.071506  6.333333
          6 13.156328  5.933333
          7 13.253417  5.433333
          8   13.3231  4.833333
          9 13.340406       4.8
         10  13.39058       4.9
         11 13.440544  4.833333
         12   13.4993  4.966667
         13 13.592345       4.8
         14 13.690648       4.6
         15 13.695463       4.7
        end
        format %tq yq
        
        tsset yq 
        
        local y deflated 
        gen max = max(L2.`y', L1.`y', `y', F1.`y', F2.`y') 
        gen ispeak = ((L2.`y' < max) + (L1.`y' < max) + (F1.`y' < max) + (F2.`y' < max)) == 4 & !missing(max) 
        gen min = min(L2.`y', L1.`y', `y', F1.`y', F2.`y') 
        gen istrough = (((L2.`y' > min) + (L1.`y' > min) + (F1.`y' >  min) + (F2.`y' > min)) == 4) & !missing(L2.`y', L1.`y', F1.`y', F2.`y') 
        
        list yq deflated max min is*, sepby(is*) 
        
          
             +-------------------------------------------------------------+
             |     yq   deflat~s        max        min   ispeak   istrough |
             |-------------------------------------------------------------|
          1. | 1947q1   8.743261   8.990711   8.743261        0          0 |
          2. | 1947q2    8.92267   9.064721   8.743261        0          0 |
          3. | 1947q3   8.990711   9.124517   8.743261        0          0 |
          4. | 1947q4   9.064721   9.198627    8.92267        0          0 |
          5. | 1948q1   9.124517    9.39039   8.990711        0          0 |
          6. | 1948q2   9.198627   9.590679   9.064721        0          0 |
          7. | 1948q3    9.39039   9.667767   9.124517        0          0 |
          8. | 1948q4   9.590679   9.713504   9.198627        0          0 |
          9. | 1949q1   9.667767   9.758883    9.39039        0          0 |
         10. | 1949q2   9.713504   9.758883   9.590679        0          0 |
         11. | 1949q3   9.758883   9.902534   9.667767        0          0 |
             |-------------------------------------------------------------|
         12. | 1949q4   9.680266   10.07589   9.680266        0          1 |
             |-------------------------------------------------------------|
         13. | 1950q1   9.902534   10.07589   9.680266        0          0 |
         14. | 1950q2   10.07589   10.25335   9.680266        0          0 |
         15. | 1950q3    10.0649   10.25335   9.902534        0          0 |
         16. | 1950q4   10.25335   10.30928    10.0649        0          0 |
         17. | 1951q1   10.16216   10.42188    10.0649        0          0 |
         18. | 1951q2   10.30928   10.46898   10.16216        0          0 |
         19. | 1951q3   10.42188   10.59212   10.16216        0          0 |
         20. | 1951q4   10.46898   10.67364   10.30928        0          0 |
         21. | 1952q1   10.59212   10.67364   10.42188        0          0 |
         22. | 1952q2   10.67364   10.96858   10.46898        0          0 |
         23. | 1952q3   10.67226   11.17539   10.59212        0          0 |
         24. | 1952q4   10.96858   11.29197   10.67226        0          0 |
         25. | 1953q1   11.17539   11.38358   10.67226        0          0 |
         26. | 1953q2   11.29197   11.40842   10.96858        0          0 |
         27. | 1953q3   11.38358   11.40842   11.17539        0          0 |
         28. | 1953q4   11.40842    11.4531   11.29197        0          0 |
             |-------------------------------------------------------------|
         29. | 1954q1   11.37225    11.4531   11.37225        0          1 |
             |-------------------------------------------------------------|
         30. | 1954q2    11.4531   11.49819   11.37225        0          0 |
         31. | 1954q3   11.41521    11.6033   11.37225        0          0 |
         32. | 1954q4   11.49819   11.71379   11.41521        0          0 |
         33. | 1955q1    11.6033   11.87905   11.41521        0          0 |
         34. | 1955q2   11.71379   11.87905   11.49819        0          0 |
             |-------------------------------------------------------------|
         35. | 1955q3   11.87905   11.87905    11.6033        1          0 |
             |-------------------------------------------------------------|
         36. | 1955q4   11.87453   12.01675   11.71379        0          0 |
             |-------------------------------------------------------------|
         37. | 1956q1   11.84568   12.01675   11.84568        0          1 |
             |-------------------------------------------------------------|
         38. | 1956q2   12.01675   12.23063   11.84568        0          0 |
         39. | 1956q3   11.99835   12.23063   11.84568        0          0 |
             |-------------------------------------------------------------|
         40. | 1956q4   12.23063   12.23063   11.99835        1          0 |
             |-------------------------------------------------------------|
         41. | 1957q1   12.19279   12.23063   11.99835        0          0 |
             |-------------------------------------------------------------|
         42. | 1957q2   12.15003    12.3208   12.15003        0          1 |
             |-------------------------------------------------------------|
         43. | 1957q3   12.22509    12.3208   12.15003        0          0 |
             |-------------------------------------------------------------|
         44. | 1957q4    12.3208    12.3208   12.15003        1          0 |
             |-------------------------------------------------------------|
         45. | 1958q1   12.16879   12.32738   12.16879        0          1 |
             |-------------------------------------------------------------|
         46. | 1958q2   12.23678   12.43292   12.16879        0          0 |
         47. | 1958q3   12.32738   12.58787   12.16879        0          0 |
         48. | 1958q4   12.43292   12.75146   12.23678        0          0 |
         49. | 1959q1   12.58787   12.75146   12.32738        0          0 |
             |-------------------------------------------------------------|
         50. | 1959q2   12.75146   12.75146   12.43292        1          0 |
             |-------------------------------------------------------------|
         51. | 1959q3   12.68215   12.92443   12.58787        0          0 |
             |-------------------------------------------------------------|
         52. | 1959q4    12.6531   12.92443    12.6531        0          1 |
             |-------------------------------------------------------------|
         53. | 1960q1   12.92443   12.92844    12.6531        0          0 |
         54. | 1960q2   12.91257   12.93043    12.6531        0          0 |
         55. | 1960q3   12.92844   12.93043   12.91257        0          0 |
         56. | 1960q4   12.93043   13.07151   12.91257        0          0 |
             |-------------------------------------------------------------|
         57. | 1961q1   12.92253   13.15633   12.92253        0          1 |
             |-------------------------------------------------------------|
         58. | 1961q2   13.07151   13.25342   12.92253        0          0 |
         59. | 1961q3   13.15633    13.3231   12.92253        0          0 |
         60. | 1961q4   13.25342   13.34041   13.07151        0          0 |
         61. | 1962q1    13.3231   13.39058   13.15633        0          0 |
         62. | 1962q2   13.34041   13.44054   13.25342        0          0 |
         63. | 1962q3   13.39058    13.4993    13.3231        0          0 |
         64. | 1962q4   13.44054   13.59235   13.34041        0          0 |
         65. | 1963q1    13.4993   13.69065   13.39058        0          0 |
         66. | 1963q2   13.59235   13.69546   13.44054        0          0 |
         67. | 1963q3   13.69065   13.69546    13.4993        0          0 |
         68. | 1963q4   13.69546   13.69546   13.59235        0          0 |
             +-------------------------------------------------------------+

        Comment


        • #5
          Dear Mr. Cox,

          Thank you very much! The code worked and I also understand the intuition behind it, I am very grateful thank you.

          Best,

          Ryan

          Comment

          Working...
          X