I have been reading AFML ( Marcos López de Prado ) and I am having trouble understanding snippet 3.1 which provides the following code:

def getDailyVol(close,span0=100):
    # daily vol, reindexed to close
    df0=pd.Series(close.index[df0-1], index=close.index[close.shape[0]-df0.shape[0]:])
    df0=close.loc[df0.index]/close.loc[df0.values].values-1 # daily returns
    return df0

Could anyone explain what is wrong with computing daily volatility as:

span0 = 100
close.pct_change().ewm(span = span0).std()

The results in the two methods of computation differ as the previous date used in getDailyVol is different from the day before.

For E-Mini S&P 500 Jun 23, I obtain (blue curve obtained using snippet 3.1 vs. orange curve obtained using pct_change): enter image description here

Could anyone please explain?

Thank you!


1 Answer 1


I can't comment on why MLDP is calculating returns this way (perhaps there is a reason explained in the book, which I don't have). However, from a pure code point-of-view he seems to be calculating returns (incorrectly) over a 3 day period. For example, for end date 2000-01-05 (Wednesday) he takes the start date 2000-01-03 (Monday).

Take some dummy data:

close = pd.DataFrame([100, 101, 104, 103, 101], index=pd.date_range("2000-01-03", "2000-01-07"))

Now if you print out df0 before he calculates the daily returns, you get the following:

2000-01-05   2000-01-03
2000-01-06   2000-01-04
2000-01-07   2000-01-05

This doesn't look right, the return on 2000-01-05 should just be the value from that day vs the previous day. He calculates 104/100-1 which is 4% vs the actual day-on-day return of 104/101-1 of 2.9%.

I'd stick to using the pandas pct_change(), it's not only a lot less convoluted but also allows easy chaining of commands (e.g. if you want to filter out weekends, lag the series by a day or remove zero-values in your time series).

As a final side note, although I appreciate a lot of his work, his Python snippets should perhaps best be rewritten to match current best practices and code standards.

  • $\begingroup$ Thank you @oronimbus. Your explanation is very clear! $\endgroup$
    – md0101
    Jun 6, 2023 at 19:09
  • $\begingroup$ No worries mate $\endgroup$
    – oronimbus
    Jun 6, 2023 at 19:52

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.