Deedle icon indicating copy to clipboard operation
Deedle copied to clipboard

The example of calculating daily returns on the homepage is wrong

Open Eddi-S opened this issue 4 years ago • 2 comments

On the homepage it is described how you can use the diff method on a series to calculate the daily returns from a series of prices Link to homepage

I think that example is wrong - you write "To calculate daily returns, we need to subtract the price on previous day from the price on the current day. This is done by using the Diff extension method (another option is to use Shift together with the overloaded subtraction operator). Then we divide the difference by the current price and multiply the result by 100.0 to get value in percents." This gives the formula: (CurrentPrice - PreviousPrice) / CurrentPrice

But this is wrong the formula should be: (CurrentPrice - PreviousPrice) / PreviousPrice As described on wikipedia

The code on the homepage looks like this: var returns = msft.Diff(1) / msft * 100.0; But should be like this (assuming the key is a DateTime): var returns = msft.Diff(1) / msft.SelectKeys(x => x.Key.AddDays(1) * 100.0;

Eddi-S avatar Sep 24 '20 14:09 Eddi-S

Thanks for pointing it out. This is definitely wrong. Will fix it.

zyzhu avatar Sep 24 '20 15:09 zyzhu

@zyzhu I found a more elegant way to calculate the return/pct change: var returns = msft.Diff(1) / msft.Shift(1) * 100; It could be useful to have a built in function called PctChange (like pandas pct_change)

Eddi-S avatar Oct 06 '20 08:10 Eddi-S