def test_random_wrong_use(self): """ Random wrong use test """ try: rnd = RandomDataProvider(seed=1) # Now, the .run() method is expected, which does the actual data loading # (and can take seconds or minutes, # depending on the data volumes, hence not ok in the constructor) with self.subTest('test RandomDataProvider get_covariance_matrix'): self.assertRaises(QiskitFinanceError, rnd.get_covariance_matrix) with self.subTest('test RandomDataProvider get_similarity_matrix'): self.assertRaises(QiskitFinanceError, rnd.get_similarity_matrix) wiki = WikipediaDataProvider(token=self._quandl_token, tickers=["GOOG", "AAPL"], start=datetime.datetime(2016, 1, 1), end=datetime.datetime(2016, 1, 30)) # Now, the .run() method is expected, which does the actual data loading with self.subTest( 'test WikipediaDataProvider get_covariance_matrix'): self.assertRaises(QiskitFinanceError, wiki.get_covariance_matrix) with self.subTest( 'test WikipediaDataProvider get_similarity_matrix'): self.assertRaises(QiskitFinanceError, wiki.get_similarity_matrix) except MissingOptionalLibraryError as ex: self.skipTest(str(ex))
def test_random_divide_0(self): """ Random divide by 0 test """ # This will create data with some 0 values, it should not throw # divide by 0 errors try: seed = 8888 num_assets = 4 stocks = [("TICKER%s" % i) for i in range(num_assets)] random_data = RandomDataProvider( tickers=stocks, start=datetime.datetime(2016, 1, 1), end=datetime.datetime(2016, 1, 30), seed=seed, ) random_data.run() mu_value = random_data.get_period_return_mean_vector() sigma_value = random_data.get_period_return_covariance_matrix() with self.subTest( "test get_period_return_mean_vector is numpy array"): self.assertIsInstance(mu_value, np.ndarray) with self.subTest( "test get_period_return_covariance_matrix is numpy array"): self.assertIsInstance(sigma_value, np.ndarray) except MissingOptionalLibraryError as ex: self.skipTest(str(ex))
def test_random(self): """ random test """ rnd = RandomDataProvider(seed=1) rnd.run() similarity = np.array([[1.00000000e+00, 6.2284804e-04], [6.2284804e-04, 1.00000000e+00]]) covariance = np.array([[1.75870991, -0.32842528], [-0.32842528, 2.31429182]]) np.testing.assert_array_almost_equal(rnd.get_covariance_matrix(), covariance, decimal=3) np.testing.assert_array_almost_equal(rnd.get_similarity_matrix(), similarity, decimal=3)
def test_wrong_use(self): """ wrong use test """ rnd = RandomDataProvider(seed=1) # Now, the .run() method is expected, which does the actual data loading # (and can take seconds or minutes, # depending on the data volumes, hence not ok in the constructor) self.assertRaises(QiskitFinanceError, rnd.get_covariance_matrix) self.assertRaises(QiskitFinanceError, rnd.get_similarity_matrix) wiki = WikipediaDataProvider(token="", tickers=["GOOG", "AAPL"], stockmarket=StockMarket.NASDAQ, start=datetime.datetime(2016, 1, 1), end=datetime.datetime(2016, 1, 30)) # Now, the .run() method is expected, which does the actual data loading self.assertRaises(QiskitFinanceError, wiki.get_covariance_matrix) self.assertRaises(QiskitFinanceError, wiki.get_similarity_matrix)
def test_random(self): """ random test """ rnd = RandomDataProvider(seed=1) rnd.run() similarity = np.array([[1.00000000e+00, 6.2284804e-04], [6.2284804e-04, 1.00000000e+00]]) covariance = np.array([[2.08413157, 0.20842107], [0.20842107, 1.99542187]]) np.testing.assert_array_almost_equal(rnd.get_covariance_matrix(), covariance, decimal=3) np.testing.assert_array_almost_equal(rnd.get_similarity_matrix(), similarity, decimal=3)
def test_random(self): """ random test """ similarity = np.array([[1.00000000e+00, 6.2284804e-04], [6.2284804e-04, 1.00000000e+00]]) covariance = np.array([[2.08413157, 0.20842107], [0.20842107, 1.99542187]]) try: rnd = RandomDataProvider(seed=1) rnd.run() with self.subTest('test RandomDataProvider get_covariance_matrix'): np.testing.assert_array_almost_equal(rnd.get_covariance_matrix(), covariance, decimal=3) with self.subTest('test RandomDataProvider get_similarity_matrix'): np.testing.assert_array_almost_equal(rnd.get_similarity_matrix(), similarity, decimal=3) except NameError as ex: self.skipTest(str(ex))
def setUp(self): super().setUp() self.seed = 50 aqua_globals.random_seed = self.seed num_assets = 4 stocks = [("TICKER%s" % i) for i in range(num_assets)] data = RandomDataProvider(tickers=stocks, start=datetime.datetime(2016, 1, 1), end=datetime.datetime(2016, 1, 30), seed=self.seed) data.run() self.muu = data.get_period_return_mean_vector() self.sigma = data.get_period_return_covariance_matrix() self.risk = 0.5 self.budget = int(num_assets / 2) self.penalty = num_assets self.qubit_op, self.offset = portfolio.get_operator( self.muu, self.sigma, self.risk, self.budget, self.penalty)