Skip to content

Commit

Permalink
BUG: fix annualization of alpha (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikram Narayan authored and twiecki committed Aug 25, 2019
1 parent 92011b7 commit 4cb0508
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
16 changes: 12 additions & 4 deletions empyrical/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -1283,10 +1283,18 @@ def alpha_aligned(returns,
adj_factor_returns = _adjust_returns(factor_returns, risk_free)
alpha_series = adj_returns - (_beta * adj_factor_returns)

out = np.multiply(
nanmean(alpha_series, axis=0, out=out),
ann_factor,
out=out,
out = np.subtract(
np.power(
np.add(
nanmean(alpha_series, axis=0, out=out),
1,
out=out
),
ann_factor,
out=out
),
1,
out=out
)

if allocated_output and isinstance(returns, pd.DataFrame):
Expand Down
17 changes: 10 additions & 7 deletions empyrical/tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ def test_excess_sharpe_trans(self, returns, add_noise, translation):
@parameterized.expand([
(empty_returns, simple_benchmark, (np.nan, np.nan)),
(one_return, one_return, (np.nan, np.nan)),
(mixed_returns, negative_returns[1:], (-8.306666666666668,
(mixed_returns, negative_returns[1:], (-0.9997853834885004,
-0.71296296296296313)),
(mixed_returns, mixed_returns, (0.0, 1.0)),
(mixed_returns, -mixed_returns, (0.0, -1.0)),
Expand Down Expand Up @@ -801,6 +801,7 @@ def test_alpha_beta_translation(self, mean_returns, translation):
benchmark = pd.Series(
bench,
index=pd.date_range('2000-1-30', periods=1000, freq='D'))

# Translate returns and generate alphas and betas.
returns_depressed = returns-translation
returns_raised = returns+translation
Expand All @@ -814,11 +815,13 @@ def test_alpha_beta_translation(self, mean_returns, translation):
# Alpha should change proportionally to how much returns were
# translated.
assert_almost_equal(
(alpha_standard - alpha_depressed)/252,
((alpha_standard + 1) ** (1/252)) -
((alpha_depressed + 1) ** (1/252)),
translation,
DECIMAL_PLACES)
assert_almost_equal(
(alpha_raised - alpha_standard)/252,
((alpha_raised + 1) ** (1/252)) -
((alpha_standard + 1) ** (1/252)),
translation,
DECIMAL_PLACES)
# Beta remains constant.
Expand Down Expand Up @@ -1108,8 +1111,8 @@ def test_down_capture(self, returns, factor_returns, expected):
[(np.nan, np.nan)] * len(simple_benchmark)),
(one_return, one_return, 1, [(np.nan, np.nan)]),
(mixed_returns, negative_returns,
6, [(-3.81286957, -0.7826087), (-4.03558719, -0.76156584),
(-2.66915888, -0.61682243), (-7.8987541, -0.41311475)]),
6, [(-0.97854954, -0.7826087), (-0.9828927, -0.76156584),
(-0.93166924, -0.61682243), (-0.99967288, -0.41311475)]),
(mixed_returns, mixed_returns,
6, [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0)]),
(mixed_returns, -mixed_returns,
Expand Down Expand Up @@ -1209,7 +1212,7 @@ def test_roll_up_capture(self, returns, factor_returns, window, expected):
(empty_returns, simple_benchmark, (np.nan, np.nan)),
(one_return, one_return, (np.nan, np.nan)),
(mixed_returns[1:], negative_returns[1:],
(-8.306666666666668, -0.71296296296296313)),
(-0.9997853834885004, -0.71296296296296313)),
(mixed_returns, mixed_returns, (0.0, 1.0)),
(mixed_returns, -mixed_returns, (0.0, -1.0))
])
Expand All @@ -1231,7 +1234,7 @@ def test_down_alpha_beta(self, returns, benchmark, expected):
(empty_returns, simple_benchmark, (np.nan, np.nan)),
(one_return, one_return, (np.nan, np.nan)),
(mixed_returns[1:], positive_returns[1:],
(0.3599999999999995, 0.4285714285)),
(0.432961242076658, 0.4285714285)),
(mixed_returns, mixed_returns, (0.0, 1.0)),
(mixed_returns, -mixed_returns, (0.0, -1.0))
])
Expand Down

0 comments on commit 4cb0508

Please sign in to comment.