Prices to returns


Create a matrix of returns given a matrix of prices.


  • price matrix of assets

You need to have your matrix of asset prices available.  Assets should be in the columns and times in the rows (earliest first, most recent last).

Doing the example

  • pprobeData package

To do the example, the pprobeData package must be loaded in the session:


If that package is not installed on your machine, see “Using R packages”.

Doing it

To get log returns given a price matrix, do:

xaLogRet <- diff(log(xassetPrices))

If instead you want simple returns, do:

xaSimpRet <- tail(xassetPrices, -1) / head(xassetPrices, -1) - 1


The orientation of assets along columns and time along rows (and in order) is important.  The computations will do the wrong thing if those conditions are not met.

The log function returns an object like the input that contains the natural logarithm of each number.  Given a matrix, the diff function takes differences within each column.

The head function returns the first rows of a matrix.  When the second argument is -1, it returns all but the last row.  Likewise when the second argument to tail is -1, it returns all but the first row.

Both of these calls will work on a price vector as well as a matrix.

See also

To read in your prices, see “Read a comma-separated file into R” or “Read a tab-separated file into R”.

To use your return matrix to create a variance matrix, see “Returns to variance matrix”.

An explanation of simple versus log returns is in “A tale of two returns”.