Skip to content
All library documents

Comparing Floating-Point Formulas for Simple Returns

Article Quant Q&A · Author: HaroldFinch

Summary

The document compares two algebraically equivalent ways to compute a single-period simple return: subtracting the initial price before dividing, or dividing prices and then subtracting one. It reports that compiled versions use similar instruction counts and registers, and argues that their performance difference is negligible in ordinary financial calculations.

The answer says double precision is adequate for reasonable inputs and that floating-point discrepancies are small compared with data quality issues such as bid-ask noise. It also cautions that division by returns can be problematic when returns are zero. A separate answer notes that log returns may have useful statistical properties, including stationarity and ergodicity, citing prior research. The discussion is brief and does not provide a systematic numerical error analysis, compiler benchmark, or conditions for the statistical claims.

Key ideas

  • The two common simple-return formulas are algebraically equivalent.
  • The cited compiled implementations use similar numbers of instructions and registers.
  • For ordinary financial inputs, the answer considers double-precision rounding differences negligible.
  • Data quality and market noise may matter more than the arithmetic form used.
  • Log returns are mentioned as an alternative with potentially useful statistical properties.

Tags

Full text
# best way to calculate the return


# best way to calculate the return












Suppose that you want to calculate the single period return. Let `p0` be the initial price and `p1` be the final price over the period.

The most common formula is

```
(p1 - p0) / p0
```

But I also sometimes see people use

```
(p1 / p0) - 1
```

Algebraically, both forms are equivalent. However, when you are writing computer code, is there any difference?

From a performance perspective, both forms involve 1 division and 1 subtraction, so they should have similar performance. (Perhaps the first form only needs 3 registers while the second needs 4?)

From an accuracy perspective, is there any difference? Does one form have lower floating point error than the other? Assume that a 64 bit floating point type is being used (e.g. numpy float64 or Java double).

I am not sure whether the language used matters, but if you think it might, I now typically use Python (Pandas), R, or Java. And if I am using Pandas or R, the return is usually calculated as a vector operation.

## Answer by Bob Jansen (score 5, accepted)

https://quant.stackexchange.com/a/61104

The language would matter but if performance is an issue you would want to make sure that the code is optimal. Optimized assembly code for a single return calculation looks like this (on Godbolt):

```
method1(double, double):
        divsd   xmm0, xmm1
        subsd   xmm0, QWORD PTR .LC0[rip]
        ret
method2(double, double):
        subsd   xmm0, xmm1
        divsd   xmm0, xmm1
        ret
.LC0:
        .long   0
        .long   1072693248
```

The number of instructions and registers used is the same. I don't know whether there is a difference between applying `subsd` on two register or one register and one value from the data segment. All compilers behave more or less the same so I guess this is a quick way to subtract 1.

The Godbolt link also contains vectorized code, this code is quite similar and from a quick check seems to use the same amount of registers (`xmm0`, `xmm1`, `xmm2` and loop accounting registers).

For financial purposes double numbers are precise enough. The rounding differences you might encounter in return calculations are negligible, smaller than `1e-10` for all reasonable numbers. It's much more important to get the correct data. But even if you have the correct data, any noise in the data such as bid-ask spreads would swamp the floating point error. Floating point errors blow up when you divide. It's probably a bad idea by divide returns in any case because they can easily be 0.

### To conclude

I wouldn't worry about this. Performance differences will be negligible and if they are not you should measure both. The floating point error shouldn't hurt, much better to focus on getting good data.

## Answer by IHonda (score 1)

https://quant.stackexchange.com/a/61102

Any difference would be negligible. On the other hand, there are statistical advantages when calculating the log return. Remember that the log return is simply the log difference of the value / price from one day to the next. Log returns have some more favorable properties for statistical analysis than the simple net returns as shown by Quigley and Ramsey (2008), such as stationarity and ergodicity.

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.