PERF: Improve risk metrics update speed.

Remove the DataFrame of headline risk metrics, in favor of a numpy array
for each metric, like the underlying vectors.
This commit is contained in:
Eddie Hebert
2015-07-15 15:36:35 -04:00
parent 27ab36deb2
commit ace2b5c9e9
3 changed files with 45 additions and 34 deletions
+14 -7
View File
@@ -62,52 +62,59 @@ class TestRisk(unittest.TestCase):
def test_algorithm_volatility_06(self):
algo_vol_answers = answer_key.RISK_CUMULATIVE.volatility
for dt, value in algo_vol_answers.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
self.cumulative_metrics_06.metrics.algorithm_volatility[dt],
self.cumulative_metrics_06.algorithm_volatility[dt_loc],
value,
err_msg="Mismatch at %s" % (dt,))
def test_sharpe_06(self):
for dt, value in answer_key.RISK_CUMULATIVE.sharpe.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
self.cumulative_metrics_06.metrics.sharpe[dt],
self.cumulative_metrics_06.sharpe[dt_loc],
value,
err_msg="Mismatch at %s" % (dt,))
def test_downside_risk_06(self):
for dt, value in answer_key.RISK_CUMULATIVE.downside_risk.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
value,
self.cumulative_metrics_06.metrics.downside_risk[dt],
self.cumulative_metrics_06.downside_risk[dt_loc],
err_msg="Mismatch at %s" % (dt,))
def test_sortino_06(self):
for dt, value in answer_key.RISK_CUMULATIVE.sortino.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
self.cumulative_metrics_06.metrics.sortino[dt],
self.cumulative_metrics_06.sortino[dt_loc],
value,
decimal=4,
err_msg="Mismatch at %s" % (dt,))
def test_information_06(self):
for dt, value in answer_key.RISK_CUMULATIVE.information.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
value,
self.cumulative_metrics_06.metrics.information[dt],
self.cumulative_metrics_06.information[dt_loc],
err_msg="Mismatch at %s" % (dt,))
def test_alpha_06(self):
for dt, value in answer_key.RISK_CUMULATIVE.alpha.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
self.cumulative_metrics_06.metrics.alpha[dt],
self.cumulative_metrics_06.alpha[dt_loc],
value,
err_msg="Mismatch at %s" % (dt,))
def test_beta_06(self):
for dt, value in answer_key.RISK_CUMULATIVE.beta.iteritems():
dt_loc = self.cumulative_metrics_06.cont_index.get_loc(dt)
np.testing.assert_almost_equal(
value,
self.cumulative_metrics_06.metrics.beta[dt],
self.cumulative_metrics_06.beta[dt_loc],
err_msg="Mismatch at %s" % (dt,))
def test_max_drawdown_06(self):
+3 -2
View File
@@ -161,7 +161,7 @@ class TestEventsThroughRisk(unittest.TestCase):
decimal=6)
np.testing.assert_almost_equal(
crm.metrics.sharpe[current_dt],
crm.sharpe[dt_loc],
expected_sharpe[current_dt],
decimal=6,
err_msg="Mismatch at %s" % (current_dt,))
@@ -294,6 +294,7 @@ class TestEventsThroughRisk(unittest.TestCase):
gen = algo._create_generator(sim_params)
crm = algo.perf_tracker.cumulative_risk_metrics
dt_loc = crm.cont_index.get_loc(algo.datetime)
first_msg = next(gen)
@@ -309,7 +310,7 @@ class TestEventsThroughRisk(unittest.TestCase):
self.assertEquals(
0,
crm.metrics.algorithm_volatility[algo.datetime.date()],
crm.algorithm_volatility[dt_loc],
"On the first day algorithm volatility does not exist.")
second_msg = next(gen)