How to calculate returns from a vector of prices?

Using your sample data, I think you mean the following:

a <- c(10.25, 11.26, 14, 13.56) 
> diff(a)/a[-length(a)]
[1]  0.09853659  0.24333925 -0.03142857

diff returns the vector of lagged differences and a[-length(a)] drops the last element of a.


You may find the functions in quantmod relevant for your work:

> require(quantmod)
> Delt(a)
     Delt.1.arithmetic
[1,]                NA
[2,]        0.09853659
[3,]        0.24333925
[4,]       -0.03142857

You can also use the exact relationship that returns are equal to the exponent of log returns minus one. Thus, if Prices contains your prices, the following will give you your returns:

Returns = exp(diff(log(Prices))) - 1

Note that this is an exact relationship, rather than the approximate relationship given in the answer by @PBS.

Tags:

R