简体   繁体   中英

Rolling standard deviation for multiple firm, with different time periods

I have a dataset with monthly stock return for approximately 100 firms. They have different time periods, and the reason for this is when they went on and off the stock exchange. I have ordered my dataset by Company, Year, Month and I want the standard deviation to account for this so that it starts for a firm after 24 months, and ends when the last observation for that firm is due.
This means that the command has to be able to tell the difference between firms, so that the window doesn't transfer over to the next firm.

Year, Month, Company, Return

1990, 1, Company 1, -0,005

1990, 2, Company 1 , 0,003

etc...

1990, 1, Company 2, ...

1990, 2, Company 2, ...

etc...

2017, 6, Company 50, ...

I have been trying with this code, but it just keeps going when the next row contains a new firm, ie it just does a rolling standard deviation for the whole dataset.

rolling_sd <- (rollapply(Dataset$RETURN, width=24, 
                                FUN = sd, fill=NA, align = "right"))

Also it does not align with the right date. If I have no align command, the first row of standard deviation should be 24 rows down, with the "right" it moves 12 down, but still not properly aligned. How can I make it to take Company name into account?

If you omit the align="right" argument the sd values would be centered as discussed in the question but since the code shown does use right alignment the sd values would start in row 24. I suspect you are confusing runs made with and without the align= argument.

Using the data shown in the Note at the end and changing 24 to 3 in order to demonstrate it with this smaller dataset we use ave to apply the rolling sd to each company separately. The r at the end of rollapplyr is a shorter way of specifying align="right" . With right alignment the sd shown in the ith row is the sd of the width rows ending in row i, ie rows i-width+1 to i inclusive.

library(zoo)

roll <- function(x) rollapplyr(x, width = 3, FUN = sd, fill = NA)
transform(Dataset, sd = ave(RETURN, Company, FUN = roll))

giving:

   Year Month Company       RETURN         sd
1     1     1       A -0.042484496         NA
2     1     2       A  0.057661027         NA
3     1     3       A -0.018204616 0.05224021
4     1     4       A  0.076603481 0.05017135
5     2     1       A  0.088093457 0.05833792
6     2     2       A -0.090888700 0.10018338
7     2     3       A  0.005621098 0.08958278
8     2     4       A  0.078483809 0.08496093
9     1     1       B -0.042484496         NA
10    1     2       B  0.057661027         NA
11    1     3       B -0.018204616 0.05224021
12    1     4       B  0.076603481 0.05017135
13    2     1       B  0.088093457 0.05833792
14    2     2       B -0.090888700 0.10018338
15    2     3       B  0.005621098 0.08958278
16    2     4       B  0.078483809 0.08496093

Note

Some data in reproducible form

set.seed(123)
tmp <- data.frame(Year = c(1, 1, 1, 1, 2, 2, 2, 2), Month = 1:4, Company = "A", 
  RETURN = runif(8, -.1, .1))
Dataset <- rbind(tmp, transform(tmp, Company = "B"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM