**Portfolio Probe » R language**, and kindly contributed to R-bloggers)

It was the best of times, it was the worst of times.

As you may have guessed, this is a mashup of a novel by Charles Dickens and an explanation of financial returns.

The key plot element of *A Tale of Two Cities* is that there are two men, Charles Darnay and Sydney Carton, who look almost identical. They are different, but in some respects interchangeable.

Simple returns and log returns are different, but in some respects interchangeable.

## Return definitions

Traditionally simple returns are denoted with a capital *R* and log returns with a lower-case *r*. These are defined as:

*R _{t} = (P_{t} – P_{t-1}) / P_{t-1} = P_{t} / P_{t-1} – 1*

*r _{t} = log(P_{t} / P_{t-1}) = log(P_{t}) – log(P_{t-1})*

where *P _{t}* is the price of the asset at time

*t*. We are defining the return from time

*t-1*to time

*t*. The

*log*function here is the natural logarithm.

## R computation

In the R language if you have a vector of prices (for the same asset at different times), you can compute the simple returns with:

`> R <- priceVec[-1] / priceVec[-length(priceVec)] - 1`

It is slightly different if you have a price matrix (with times in the rows and assets along the columns):

`> R <- priceMat[-1, ] / priceMat[-nrow(priceMat), ] - 1`

For log returns you can do the same command in either case:

`> r <- diff(log(prices))`

**Caveat:** This assumes the positions are all long. Read on for an explanation of returns for short positions.

## Nomenclature

We have two similar concepts and we need to distinguish between them. Unfortunately this is about as messy as the French revolution in which our Dickensian characters are embroiled.

First off, some would think that what we are calling “return” is an abbreviation of “rate of return”.

Table 1: Names for return concepts.

r |
R |
R+1 |

log | simple | total |

continuously compounded | net | gross |

I find it hard to believe that Table 1 lists all the words that are used. Let me know if there are others.

Extreme care is needed. Some of these words are used inconsistently by different people.

The word “total” can (and in my experience more probably does) mean the return that includes dividends as well as prices. A (simple) total return in this sense is:

*R _{t} = (P_{t} + D_{t} – P_{t-1}) / P_{t-1}*

where *D _{t}* is the dividend or interest that is paid between times

*t-1*and

*t*. This concept is sometimes expressed as the “overall” return.

The terms “gross” and “net” are more commonly used to mean something like before and after tax.

**Take-away message:** There is no magic potion. You’ll need to threaten violence on whomever is speaking until they make clear what they mean.

## Transmutation

It is easy to convert one type of return into the other. Shortly we’ll see why that is useful.

To go from simple to log returns, do:

*r = log(R + 1)*

To go from log return to simple return, do:

*R = exp(r) – 1*

These formulas work exactly as is in R — whether the returns are vectors or matrices.

Figure 1: Comparison of simple and log returns.

Figure 1 compares simple and log returns. It shows that log returns are always smaller than simple returns. (You can remember which is smaller because the smallest possible simple return is -100%, that is minus infinity for the log return.) For daily returns or intraday returns the differences between the two types are going to be trivial, generally.

## Aggregation

The two types act very differently when it comes to aggregation. Each has an advantage over the other:

- simple returns aggregate across assets
- log returns aggregate across time

The simple return of a portfolio is the weighted sum of the simple returns of the constituents of the portfolio.

The log return for a time period is the sum of the log returns of partitions of the time period. For example the log return for a year is the sum of the log returns of the days within the year.

## Time reversal

Physicists tend to think that time only goes one way. In finance time can go backwards — you can short an asset.

Or you can think of it as having been misled so far. What has been identified as the initial time in the return formulas should really be called the buying time, and what has been identified as the final time should be the selling time. When we short an asset, the buying time is after the selling time.

The log return of a short position is the negative of the log return of the long position. The relationship of the simple return of a short position relative to that of the long position is a little more complicated:

*-R / (R + 1)*

The computations we saw before in the R language assume that the positions are long and the times are in order. When computing log returns of short positions, you can always just take the negative of the computation for the long positions. For simple returns, you have some choices.

## Foreign exchange

The return you experience for an asset that is priced in a foreign currency depends both on the return in the foreign currency and the return of that currency relative to your home currency.

There is a simple relationship for log returns: it is the sum of the log return of the asset in the foreign currency and the log return of the currency. The formula for simple returns is slightly complicated.

If you have the return for euros per dollar, then what is the return for dollars per euro? Again, it is easy for log returns: one is the negative of the other.

## Questions

The material in this post is fairly pedestrian, and most people in finance at least sort of know a lot of it. So why is this information so hard for a novice to find?

## Epilogue

*A Tale of Two Cities* ends with Carton taking the place of Darnay in prison and at the guillotine. He does this for the woman that they both love who chose Darnay and not Carton to marry. The final sentence of the novel is the thoughts of Carton just before he is executed:

It is a far, far better thing I do, than I have ever done; it is a far, far better rest that I go to, than I have ever known.

## Appendix R

To create Figure 1 for your own consumption with R you might do commands like:

> retseq <- seq(-.75, .75, length=100)

> plot(retseq, log(retseq + 1), type='l')

> abline(0,1, lty=2)

The commands that actually created Figure 1 were:

> retseq <- seq(-.75, .75, length=100)

> plot(retseq, log(retseq + 1), type="l",

+ axes=FALSE, col="blue", lwd=3, xlab="Simple return",

+ ylab="Log return")

> axis(1, at=c(-.5, 0, .5), label=c("-50%", "0%", "50%"))

> axis(2, at=c(-1, -.5, 0, .5),

+ label=c("-100%", "-50%", "0%", "50%"))

> box()

> abline(0,1, lty=2, col="gold")

**leave a comment**for the author, please follow the link and comment on his blog:

**Portfolio Probe » R language**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...