def test_to_quadratic_program(self): """Test to_quadratic_program""" portfolio_diversification = PortfolioDiversification( similarity_matrix=self.similarity_matrix, num_assets=self.n, num_clusters=self.q, ) actual_op = portfolio_diversification.to_quadratic_program() expected_op = QuadraticProgram(name="Portfolio diversification") for i in range(self.n): for j in range(self.n): expected_op.binary_var(name="x_{0}_{1}".format(i, j)) for i in range(self.n): expected_op.binary_var(name="y_{0}".format(i)) linear = {"x_0_0": 1, "x_0_1": 0.8, "x_1_0": 0.8, "x_1_1": 1} expected_op.maximize(linear=linear) expected_op.linear_constraint(linear={"y_0": 1, "y_1": 1}, sense="==", rhs=1) expected_op.linear_constraint(linear={"x_0_0": 1, "x_0_1": 1}, sense="==", rhs=1) expected_op.linear_constraint(linear={"x_1_0": 1, "x_1_1": 1}, sense="==", rhs=1) expected_op.linear_constraint(linear={"x_0_0": 1, "y_0": -1}, sense="==", rhs=0) expected_op.linear_constraint(linear={"x_1_1": 1, "y_1": -1}, sense="==", rhs=0) expected_op.linear_constraint(linear={"x_0_0": 1, "y_0": -1}, sense="<=", rhs=0) expected_op.linear_constraint(linear={"x_0_1": 1, "y_1": -1}, sense="<=", rhs=0) expected_op.linear_constraint(linear={"x_1_0": 1, "y_0": -1}, sense="<=", rhs=0) expected_op.linear_constraint(linear={"x_1_1": 1, "y_1": -1}, sense="<=", rhs=0) self.assertEqualQuadraticProgram(actual_op, expected_op)
def test_num_clusters(self): """test num_clusters""" portfolio_diversification = PortfolioDiversification( similarity_matrix=self.similarity_matrix, num_assets=self.n, num_clusters=self.q) portfolio_diversification.num_clusters = 3 self.assertEqual(portfolio_diversification.num_clusters, 3)
def test_interpret(self): """Test interpret""" portfolio_diversification = PortfolioDiversification( similarity_matrix=self.similarity_matrix, num_assets=self.n, num_clusters=self.q) result_x = np.array([0, 1, 0, 1, 0, 1]) self.assertEqual(portfolio_diversification.interpret(result_x), [1])
def test_smilarity_matrix(self): """Test similarity_matrix""" portfolio_diversification = PortfolioDiversification( similarity_matrix=self.similarity_matrix, num_assets=self.n, num_clusters=self.q, ) portfolio_diversification.similarity_matrix = np.array([[0, 1], [1, 0]]) self.assertEqual(portfolio_diversification.similarity_matrix.tolist(), [[0, 1], [1, 0]])
def test_to_quadratic_program(self): """Test to_quadratic_program""" portfolio_diversification = PortfolioDiversification( similarity_matrix=self.similarity_matrix, num_assets=self.n, num_clusters=self.q) actual_op = portfolio_diversification.to_quadratic_program() expected_op = QuadraticProgram(name='Portfolio diversification') for i in range(self.n): for j in range(self.n): expected_op.binary_var(name='x_{0}_{1}'.format(i, j)) for i in range(self.n): expected_op.binary_var(name='y_{0}'.format(i)) linear = {'x_0_0': 1, 'x_0_1': 0.8, 'x_1_0': 0.8, 'x_1_1': 1} expected_op.maximize(linear=linear) expected_op.linear_constraint(linear={ 'y_0': 1, 'y_1': 1 }, sense='==', rhs=1) expected_op.linear_constraint(linear={ 'x_0_0': 1, 'x_0_1': 1 }, sense='==', rhs=1) expected_op.linear_constraint(linear={ 'x_1_0': 1, 'x_1_1': 1 }, sense='==', rhs=1) expected_op.linear_constraint(linear={ 'x_0_0': 1, 'y_0': -1 }, sense='==', rhs=0) expected_op.linear_constraint(linear={ 'x_1_1': 1, 'y_1': -1 }, sense='==', rhs=0) expected_op.linear_constraint(linear={ 'x_0_0': 1, 'y_0': -1 }, sense='<=', rhs=0) expected_op.linear_constraint(linear={ 'x_0_1': 1, 'y_1': -1 }, sense='<=', rhs=0) expected_op.linear_constraint(linear={ 'x_1_0': 1, 'y_0': -1 }, sense='<=', rhs=0) expected_op.linear_constraint(linear={ 'x_1_1': 1, 'y_1': -1 }, sense='<=', rhs=0) self.assertEqualQuadraticProgram(actual_op, expected_op)