0

I'm about to show non-constant volatility for a time series. To do so, I was thinking about making different plots containing different amount of data to show that the volatility vary in the different plots. But I have to define a function that takes n consecutive data points and produces one output variable. The function obviously has to with the standard deviation formula, but I do not know how to construct such a loop that satisfies my criteria.

Picture of what my function has to do

In the picture n = 5. Thanks.

Edit: I will try to clarify from an example, note I have 2700 observations. My function has to take five data points and look at the discrete standard deviation of these five data points. That will be the first output variable. The next output variable will take the next five consecutive data points and look at the discrete standard deviation and return the value. Then the third output variable will take the next five consecutive data points and so on... Though, instead of five it should be able to look at some random number of consecutive data points.

mas2
  • 75
  • 11
  • Sorry, I'm using R. And right now, I only have `sqrt(sum((sp_logret[1:n]-mu)^2/(length(sp_logret[1:n])-1)))` but this is not what I want, I just can't figure out how to make the desired loop. – mas2 Feb 26 '20 at 23:27
  • Welcome to Stack Overflow! I've marked your question as a duplicate of a question that was asking about rolling standard deviations for a time series, which sounded to me like what you want. If this doesn't clear up your issue, please edit your question to clarify what you want that is different from that question. – josliber Feb 26 '20 at 23:31
  • We are almost there. But if I use `rollapplyr` how do I take more points into account? Right now it takes 1:2, 2:3, 3:4, but I also want to take 1:5, 6:11, 12:17, ... – mas2 Feb 26 '20 at 23:39
  • @mas2 Why did you remove the [tag:r] tag? Without it, R experts are much less likely to see your question. – John Montgomery Feb 26 '20 at 23:39
  • Woops. That was a mistake. – mas2 Feb 26 '20 at 23:40
  • @josliber I hope my new edit clarifies the problem. – mas2 Feb 26 '20 at 23:50
  • @ma2 the duplicate would give you 1:5, 2:6, 3:7, 4:8, 5:9, 6:10, 7:11, ... So if you took every fifth element of that output, you would have 1:5, 6:10, 11:15, ... – josliber Feb 27 '20 at 01:19

0 Answers0