fslaborg / Deedle

Easy to use .NET library for data and time series manipulation and for scientific programming
http://fslab.org/Deedle/
BSD 2-Clause "Simplified" License
924 stars 196 forks source link

The example of calculating daily returns on the homepage is wrong #517

Open Eddi-S opened 3 years ago

Eddi-S commented 3 years ago

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;

zyzhu commented 3 years ago

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

Eddi-S commented 3 years ago

@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)