Skip to content

Commit

Permalink
Merge pull request #276 from quantopian/tail_ratio
Browse files Browse the repository at this point in the history
Tail ratio and common sense ratio
  • Loading branch information
twiecki committed Jan 29, 2016
2 parents f4d0e51 + 7676770 commit 035093a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pyfolio/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,18 @@ def test_sortino(self, returns, expected):
timeseries.sortino_ratio(returns),
expected, DECIMAL_PLACES)

def test_tail_ratio(self):
returns = np.random.randn(10000)
self.assertAlmostEqual(
timeseries.tail_ratio(returns),
1., 1)

def test_common_sense_ratio(self):
returns = pd.Series(np.random.randn(1000) + .1)
self.assertAlmostEqual(
timeseries.common_sense_ratio(returns),
0.024031933021535612, DECIMAL_PLACES)


class TestMultifactor(TestCase):
simple_rets = pd.Series(
Expand Down
48 changes: 48 additions & 0 deletions pyfolio/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,52 @@ def stability_of_timeseries(returns):
return rhat


def tail_ratio(returns):
"""Determines the ratio between the right (95%) and left tail (5%).
For example, a ratio of 0.25 means that losses are four times
as bad as profits.
Parameters
----------
returns : pd.Series
Daily returns of the strategy, noncumulative.
- See full explanation in tears.create_full_tear_sheet.
Returns
-------
float
tail ratio
"""

return np.abs(np.percentile(returns, 95)) / \
np.abs(np.percentile(returns, 5))


def common_sense_ratio(returns):
"""Common sense ratio is the multiplication of the tail ratio and the
Gain-to-Pain-Ratio -- sum(profits) / sum(losses).
See http://bit.ly/1ORzGBk for more information on motivation of
this metric.
Parameters
----------
returns : pd.Series
Daily returns of the strategy, noncumulative.
- See full explanation in tears.create_full_tear_sheet.
Returns
-------
float
common sense ratio
"""
return tail_ratio(returns) * (1 + annual_return(returns))


SIMPLE_STAT_FUNCS = [
annual_return,
annual_volatility,
Expand All @@ -508,6 +554,8 @@ def stability_of_timeseries(returns):
sortino_ratio,
stats.skew,
stats.kurtosis,
tail_ratio,
common_sense_ratio,
]

FACTOR_STAT_FUNCS = [
Expand Down

0 comments on commit 035093a

Please sign in to comment.