quantopian / zipline

Zipline, a Pythonic Algorithmic Trading Library
https://www.zipline.io
Apache License 2.0
17.56k stars 4.71k forks source link

log (ln) returns in volatility calculation and annualization of metrics #29

Open twmeggs opened 11 years ago

twmeggs commented 11 years ago

It's common practice in quant finance to use log returns (natural logarithm) when calculating an assets volatility, See: http://en.wikipedia.org/wiki/Volatility_(finance)

Log returns are primarily used because asset price paths are usually modelled as following geometric Browniam motion, which avoids the problem of negative prices and brings other useful atributes. See: http://www.risklatte.com/Articles/QuantitativeFinance/QF79.php

From looking at the below it looks like simple daily returns are being used in the volatility function, which probably needs to be changed

def calculate_volatility(self, daily_returns):        
    return np.std(daily_returns, ddof=1) * math.sqrt(self.trading_days)

Also, I think

math.sqrt(self.trading_days)

in the volatility function also needs to be changed. It is common to present volatilities as annualised metrics, regardless of the periodicity used in calculating the returns. The function is using daily returns, therefore if we wish to present annualised volatility we should correctly use:

math.sqrt(number_of_trading_days_in_year)

where number_of_trading_days_in_year is usually assumed to be ~252, but can be calculated accurately with correct use of holiday calendars. Alternatively, if you wished the volatility to be a monthly volatility, it would similarly require the calculation to be:

math.sqrt(number_of_trading_days_in_month)

Likewise, if we had monthly returns, we could annualise them by multiplying by sqrt(12). In general, we can annualize interval standard deviations by multiplying by the square root of the number of intervals in a year:

Finally, this 'annualisation' is usually carried over into the calculation of Sharpe ratios. Therefore, (ignoring risk free rates) if we have daily returns and the stdev of these daily returns, the ratio needs to be multiplied by sqrt(252). If we have monthly returns and the stdev of these monthly returns, the ratio needs to be multiplied by sqrt(12).

Sharpe himself proposes this standardisation (to aid comparison) below point (10) here: http://www.stanford.edu/~wfsharpe/art/sr/sr.htm

ehebert commented 11 years ago

Thank you for the write up, @twmeggs.

To brain dump some discussion about this the other day, we had the thought of keeping track of two returns.

i.e. the log returns and the percent returns.

Since the percent returns are used by some quants when discussing/reporting the risk performance.

But, as you've pointed out the log returns are important to use while calculating other metrics.

twmeggs commented 11 years ago

Agree totally that having both types of returns available (ln and %) would be useful for users.

philiphdf commented 9 years ago

From what tweggs wrote, would the correct equation for volatility with natural logs use the natural log of prices instead of absolute prices? OR should we take the (natural log of return i) - (natural log of average return)

Thanks