Announcement

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

  • How can I calculate the average value of the 5 largest daily returns in the past 1 month?


    I can use "rangestat(mean) daily_ret, interval(yearmonth -1 0) by(stock_code)" to calculate the average value of returns in the past 1 month. If I need to accurate to a specific days, not the entire month, for example 5 largest return day. How can I do it, please help me, thx.

  • #2
    I recently added max1 to max5 statistics to asrol. See this thread https://www.statalist.org/forums/for...nge-with-asrol
    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


    • #3
      rangestat is from SSC, as you are asked to explain. See https://www.statalist.org/forums/help#stata in the FAQ Advice all members are asked to read before posting.

      (So too is asrol.)

      As explained in the help of rangestat, you can write Mata functions to calculate such statistics. Here is one way to do it. I look for the 5 highest in the previous 10 "days".


      Code:
      mata:  
      mata clear
      real rowvector mean5high(real matrix X) {
          X = select(X, (X :< .))
          _sort(X, -1)
          n = rowmin((5, rows(X)))
          return(mean(X[1::n,]))
      }
      
      end 
      
      * sandbox 
      clear 
      set obs 100 
      egen group = seq(), block(20)
      egen time = seq(), to(20)
      gen y = _n 
      
      rangestat (mean5high) y, int(time -10 -1) by(group)
      
      l in 1/20 
      
      
          +------------------------------+
           | group   time    y   mean5h~1 |
           |------------------------------|
        1. |     1      1    1          . |
        2. |     1      2    2          1 |
        3. |     1      3    3        1.5 |
        4. |     1      4    4          2 |
        5. |     1      5    5        2.5 |
           |------------------------------|
        6. |     1      6    6          3 |
        7. |     1      7    7          4 |
        8. |     1      8    8          5 |
        9. |     1      9    9          6 |
       10. |     1     10   10          7 |
           |------------------------------|
       11. |     1     11   11          8 |
       12. |     1     12   12          9 |
       13. |     1     13   13         10 |
       14. |     1     14   14         11 |
       15. |     1     15   15         12 |
           |------------------------------|
       16. |     1     16   16         13 |
       17. |     1     17   17         14 |
       18. |     1     18   18         15 |
       19. |     1     19   19         16 |
       20. |     1     20   20         17 |
           +------------------------------+

      Comment


      • #4
        Originally posted by Nick Cox View Post
        rangestat is from SSC, as you are asked to explain. See https://www.statalist.org/forums/help#stata in the FAQ Advice all members are asked to read before posting.

        (So too is asrol.)

        As explained in the help of rangestat, you can write Mata functions to calculate such statistics. Here is one way to do it. I look for the 5 highest in the previous 10 "days".


        Code:
        mata:
        mata clear
        real rowvector mean5high(real matrix X) {
        X = select(X, (X :< .))
        _sort(X, -1)
        n = rowmin((5, rows(X)))
        return(mean(X[1::n,]))
        }
        
        end
        
        * sandbox
        clear
        set obs 100
        egen group = seq(), block(20)
        egen time = seq(), to(20)
        gen y = _n
        
        rangestat (mean5high) y, int(time -10 -1) by(group)
        
        l in 1/20
        
        
        +------------------------------+
        | group time y mean5h~1 |
        |------------------------------|
        1. | 1 1 1 . |
        2. | 1 2 2 1 |
        3. | 1 3 3 1.5 |
        4. | 1 4 4 2 |
        5. | 1 5 5 2.5 |
        |------------------------------|
        6. | 1 6 6 3 |
        7. | 1 7 7 4 |
        8. | 1 8 8 5 |
        9. | 1 9 9 6 |
        10. | 1 10 10 7 |
        |------------------------------|
        11. | 1 11 11 8 |
        12. | 1 12 12 9 |
        13. | 1 13 13 10 |
        14. | 1 14 14 11 |
        15. | 1 15 15 12 |
        |------------------------------|
        16. | 1 16 16 13 |
        17. | 1 17 17 14 |
        18. | 1 18 18 15 |
        19. | 1 19 19 16 |
        20. | 1 20 20 17 |
        +------------------------------+
        Amazingļ¼
        I want to start learning how to use mata in stata. Thx a lot.

        Comment

        Working...
        X