From dd634ddff062c80efac3dc37fd37adca278be1fe Mon Sep 17 00:00:00 2001 From: Ana Ruelas Date: Fri, 18 Aug 2017 04:47:12 -0500 Subject: [PATCH] ENH: Use nanstd and nanmean, remove skipped test (#37) --- empyrical/stats.py | 4 ++-- empyrical/tests/test_stats.py | 22 +--------------------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/empyrical/stats.py b/empyrical/stats.py index 558a448..6e5d076 100644 --- a/empyrical/stats.py +++ b/empyrical/stats.py @@ -463,10 +463,10 @@ def sharpe_ratio(returns, risk_free=0, period=DAILY, annualization=None): returns_risk_adj = np.asanyarray(_adjust_returns(returns, risk_free)) returns_risk_adj = returns_risk_adj[~np.isnan(returns_risk_adj)] - if np.std(returns_risk_adj, ddof=1) == 0: + if nanstd(returns_risk_adj, ddof=1) == 0: return np.nan - return np.mean(returns_risk_adj) / np.std(returns_risk_adj, ddof=1) * \ + return nanmean(returns_risk_adj) / nanstd(returns_risk_adj, ddof=1) * \ np.sqrt(ann_factor) diff --git a/empyrical/tests/test_stats.py b/empyrical/tests/test_stats.py index 435ca3a..e7281a3 100644 --- a/empyrical/tests/test_stats.py +++ b/empyrical/tests/test_stats.py @@ -3,7 +3,7 @@ import random from copy import copy from operator import attrgetter -from unittest import TestCase, skip, SkipTest +from unittest import TestCase, SkipTest from parameterized import parameterized import numpy as np @@ -217,26 +217,6 @@ def test_max_drawdown(self, returns, expected): expected, DECIMAL_PLACES) - # Multiplying returns by a positive constant larger than 1 will increase - # the maximum drawdown by a factor greater than or equal to the constant. - # Similarly, a positive constant smaller than 1 will decrease maximum - # drawdown by at least the constant. - @parameterized.expand([ - (noise_uniform, 1.1), - (noise, 2), - (noise_uniform, 10), - (noise_uniform, 0.99), - (noise, 0.5) - ]) - @skip("Randomly fails") - def test_max_drawdown_transformation(self, returns, constant): - max_dd = self.empyrical.max_drawdown(returns) - transformed_dd = self.empyrical.max_drawdown(constant*returns) - if constant >= 1: - assert constant*max_dd <= transformed_dd - else: - assert constant*max_dd >= transformed_dd - # Maximum drawdown is always less than or equal to zero. Translating # returns by a positive constant should increase the maximum # drawdown to a maximum of zero. Translating by a negative constant