I'm an engineer with minimal to zero knowledge about economical topics, please be patient with me if the following question makes no sense or is too basic ...

Currently I'm working through the "Python Data Science Handbook" of Jake van der Plas. In the chapter about time series data there are several examples with financial numbers, in particular stock rates.

In one example he calculates the ROI out of a series of stock rates:

from pandas_datareader import data

# data_source google won't work anymore, use yahoo
goog = data.DataReader('GOOG', start='2004', end='2016',
goog = goog.asfreq('D', method='pad')
ROI = 100 * (goog.tshift(-365) / goog - 1)
plt.ylabel('% Return on Investment');

So, he divides data from one year ago with the current data, plus 1, times 100 (to get it into percent values).

For my understanding, the central formular is (with $f_0$ for the data from one year ago and $f_1$ for the current data):

$\frac{f_0}{f_1} - 1 \equiv \frac{f_0}{f_1} - \frac{f_1}{f_1} \equiv \frac{f_0 - f_1}{f_1} $

What I found when googling for ROI are especically the definitions from Wikipedia:

return on investment = Net income / Investment


return on investment = (gain from investment – cost of investment) / cost of investment


return on investment = (revenue − cost of goods sold) / cost of goods sold

Although the latter both definitions come quite close to the formular above, I'm unable to see the relationship between stock rates used in the data of the code example and the words used in the definitons from Wikipedia.

Can anyone help me to understand this a bit better, with a brief explanation or a hint where to continue reading on this topic?

Thanks and cheers Wolfgang

  • 1
    $\begingroup$ I'm not certain his maths is exactly right, but I assume that he is working out the return on buying a stock and keeping it for a year, i.e. $f_0$ = "cost of investment" and $f_1$ = "gain from investment" in the second WP definition you give. I would have thought $\frac{f_1 - f_0}{f_0}$ made more sense -- could it be a typo? $\endgroup$
    – Dan
    Nov 8, 2019 at 16:50

3 Answers 3


I agree with Dan it is likely a typo.

Some definitions (using your notations)

Investment : $f_0$

Investment one year later : $f_1$

Net income : $f_1 - f_0$

ROI: $-1 + \frac{f_1}{f_0}$

It follows that the correct code should be

ROI = (goog/goog.tshift(-365) -1)*100
  • $\begingroup$ Putting aside that multiplying by $100$ boils down to considering that a percentage is a unit/constant, which is not to me. I mean: $0.01=1\%=10^{-2}$. But he does so probably only for the plot. $\endgroup$
    – keepAlive
    Nov 10, 2019 at 10:55

To understand the return formula, think of it this way: ROI is the effect/effort - 1. If you invest 100 dollars today, which is your effort, and one year later you have 105 dollars (effect), you get a 5% payoff.

  • $\begingroup$ It might be more intuitive if it's written as (effect-effort)/effort $\endgroup$ Sep 5, 2020 at 3:02

This article discusses ROI:


This article covers adjustments to stock price and volume data:


Some observations about efforts to maximize ROI from stock investing:

  1. When one enters a market position, such as in the stock market, this is a random experiment where the investment tends to produce a percent gain or loss over time.

  2. The invested capital may be taking a loss in the position while some other position in a different stock is typically making a large gain at the same time. Or the position is making a small gain while a large gain is made in an alternative position.

  3. If one makes a loss of -50% on the position then one must make a gain of 100%, either in the same position or a different position, just to break even. The total cost of entrance to the position, price paid plus commissions, is the denominator in the calculations.

  4. Surfing price waves is an effort to cut losses short when one gets into a random experiment that produces a loss and to let winners run when one gets into a random experiment that produces large gains.

  5. Those who hold to the efficient market hypothesis or random walk theory argue that one cannot anticipate the random price movements of the stock market so it is best to invest in an index of stocks.

  6. Some stock pickers seem to be able to beat the market but other sources say perhaps 80% or more of active money managers do not beat the S&P 500 index for significant periods of time.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.