MAINT: Factor out downside risk calculation.

Move the downside risk calculation into the main risk module;
so that the same calculation can eventually be used by both
the period and cumulative calculations, to prevent implementation
drift.
This commit is contained in:
Eddie Hebert
2014-03-25 12:22:17 -04:00
parent d00edbcf04
commit 51750a3a35
2 changed files with 12 additions and 4 deletions
+4 -4
View File
@@ -30,6 +30,7 @@ from . risk import (
alpha,
check_entry,
choose_treasury,
downside_risk,
)
log = logbook.Logger('Risk Cumulative')
@@ -449,10 +450,9 @@ algorithm_returns ({algo_count}) in range {start} : {end} on {dt}"
return np.std(daily_returns) * math.sqrt(252)
def calculate_downside_risk(self):
rets = self.algorithm_returns
mar = self.mean_returns
downside_diff = (rets[rets < mar] - mar).valid()
return np.std(downside_diff) * math.sqrt(252)
return downside_risk(self.algorithm_returns,
self.mean_returns,
252)
def calculate_beta(self):
"""
+8
View File
@@ -56,6 +56,7 @@ Risk Report
"""
import logbook
import math
import numpy as np
from zipline.finance import trading
@@ -103,6 +104,13 @@ def sharpe_ratio(algorithm_volatility, algorithm_return, treasury_return):
return (algorithm_return - treasury_return) / algorithm_volatility
def downside_risk(algorithm_returns, mean_returns, normalization_factor):
rets = algorithm_returns
mar = mean_returns
downside_diff = (rets[rets < mar] - mar).valid()
return np.std(downside_diff) * math.sqrt(normalization_factor)
def sortino_ratio(algorithm_returns, algorithm_period_return, mar):
"""
http://en.wikipedia.org/wiki/Sortino_ratio