PERF: Reduce number of times the downside mask is created.

Assign the downside mask, `rets < mar` to a value instead of
calculating the downsides twice.
This commit is contained in:
Eddie Hebert
2014-04-16 15:58:29 -04:00
parent 1406f8e9ba
commit acd0aeacf0
+2 -1
View File
@@ -107,7 +107,8 @@ def sharpe_ratio(algorithm_volatility, algorithm_return, treasury_return):
def downside_risk(algorithm_returns, mean_returns, normalization_factor):
rets = algorithm_returns.round(8)
mar = mean_returns.round(8)
downside_diff = (rets[rets < mar] - mar[rets < mar])
mask = rets < mar
downside_diff = rets[mask] - mar[mask]
if len(downside_diff) <= 1:
return 0.0
return np.std(downside_diff, ddof=1) * math.sqrt(normalization_factor)