Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tail ratio and common sense ratio #276

Merged
merged 6 commits into from
Jan 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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