StatsViaCsh
StatsViaCsh

Reputation: 2640

Is there a vectorized implementation to refer to a value in the previous value in a time series?

I've seen threads saying there isn't (without looping) and those that seem to get it to work, which I can't as of yet apply.

Here is my data. The 3rd column is what I seek. It represents investment returns by month. I'd like to calculate a hypothetical initial investment of 10,000, multiply that by 1.10 (the first amount), then multiply that result by 1.4 for the second, etc. Note that the first calculation is just the series value x 10,000 and all subsequent values are dependent on the prior.

1/1/2000    .10    11,000
2/1/2000    .40    15,400
3/1/2000    .20    18,480

Upvotes: 1

Views: 42

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176698

Use cumprod (for arithmetic returns) or cumsum (for log returns)

cumprod(1+c(0.10, 0.40, 0.20)) * 10000
cumsum(1+c(0.10, 0.40, 0.20)) * 10000

Upvotes: 3

Related Questions