From 81b86f8022ef90f437e01d20e75a0f77e1c65786 Mon Sep 17 00:00:00 2001 From: Charles Young Date: Mon, 8 Mar 2021 17:18:07 +0800 Subject: [PATCH] Update test to cover changes in structured_cov_estimator --- tests/test_structured_cov_estimator.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_structured_cov_estimator.py b/tests/test_structured_cov_estimator.py index 6aeae3d89..8ac1e8477 100644 --- a/tests/test_structured_cov_estimator.py +++ b/tests/test_structured_cov_estimator.py @@ -27,6 +27,24 @@ class TestStructuredCovEstimator(unittest.TestCase): self.assertTrue(if_identical) + def test_nan_option_covariance(self): + # Try to estimate the covariance from a randomly generated matrix. + NUM_VARIABLE = 10 + NUM_OBSERVATION = 200 + EPS = 1e-6 + + estimator = StructuredCovEstimator(scale_return=False, assume_centered=True, nan_option='fill') + + X = np.random.rand(NUM_OBSERVATION, NUM_VARIABLE) + + est_cov = estimator.predict(X, is_price=False) + np_cov = np.cov(X.T) # While numpy assume row means variable, qlib assume the other wise. + + delta = abs(est_cov - np_cov) + if_identical = (delta < EPS).all() + + self.assertTrue(if_identical) + def test_constructed_covariance(self): # Try to estimate the covariance from a specially crafted matrix. # There should be some significant correlation since X is specially crafted.