Announcement

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

  • Calculation of Cash flow volatility with a rolling window using loops

    Hi guys!
    I want to calculate the cash flow volatility for every firm (gvkey) and every year (year) in my dataset, using the standard deviation of cash flows for the previous 5 years. To do this, I first claculated the cash flow (gen cfl=(oibdp-xint-txt-dvc)). For example, to get the cash flow volatility for a firm in year 2000, I calculate the standard deviation of the cash flows of this firm for the years from1985 to 1989. The cash flow volatility for the same firm in year 1991 is claculated as the standard deviation of cash flows from 1986 to 1990, and so on. Since my dataset is very large, it is necessary to use a loop. Can somebody help me with this problem?
    Cheers, Jamie

  • #2
    Code:
    ssc desc tsegen
    
    ssc inst tsegen
    
    tsset gvkey year
    
    tsegen cfl_vol = rowsd(L(1/5).cfl)

    Comment


    • #3
      Thanks!! it worked, but I recalculated the outcome for some of the produced values and seems that the standard deviation is claculated by using n-1 observations instead of n?! I tried a few things, but couldn“t get the right results. Any ideas?

      For example:
      Variance=((8-5)^2+(10-5)^2+(1-5)^2)/2 instead of ((8-5)^2+(10-5)^2+(1-5)^2)/3
      SD=square root of Variance

      Comment


      • #4
        Many would argue that using n-1 in the denominator of the calculation is the correct thing to do. However, you can multiply the calculated standard deviation by the square root of (n-1)/n where n is the number of nonmissing observations used to calculate the standard deviation. Following on Nick's code
        Code:
        tsgen cfl_n = rownonmiss(L(1/5).cfl)
        
        replace cfl_vol = cfl_vol * sqrt((cfl_n-1)/cfl_n)
        Note that in the absence of data for a reproducible example, I haven't tested this code, but I'm reasonably sure it's at least pointed in the right direction, given that using n rather than n-1 in the denominator is what you want to do.

        Comment

        Working...
        X