def test_default_crp_solution(self): """ Tests the calculation of CRP weights with default settings. """ # Initialize CRP. crp = CRP() # Allocates asset prices to CRP. crp.allocate(self.data, resample_by='M') # Create np.array of all_weights. all_weights = np.array(crp.all_weights) # All weights for the strategy have to be the same. one_weight = all_weights[0] # Iterate through all_weights to check that weights equal to the first weight. for i in range(all_weights.shape[0]): weights = all_weights[i] assert (weights >= 0).all() assert len(weights) == self.data.shape[1] assert (weights == one_weight).all() np.testing.assert_almost_equal(np.sum(weights), 1)
def test_bcrp_returns(self): """ Tests that BCRP returns are higher than other CRP's. """ # Initialize BCRP bcrp1 = BCRP() # Allocates asset prices to BCRP. bcrp1.allocate(self.data, resample_by='M') # Get final returns for bcrp1. bcrp1_returns = np.array(bcrp1.portfolio_return)[-1] # Set an arbitray weight to test. weight = bcrp1._uniform_weight() # Initialize CRP. crp = CRP(weight) crp.allocate(self.data, resample_by='M') # Get final returns for CRP. crp_returns = np.array(crp.portfolio_return)[-1] # Check that CRP returns are lower than BCRP returns. np.testing.assert_array_less(crp_returns, bcrp1_returns)
def test_given_allocate_weights_crp_solution(self): """ Test calculation of constant rebalanced portfolio weights with weights given in allocate. """ # Create user input weights weights = np.zeros(self.data.shape[1]) # Set 1 on the first stock and 0 on the rest. weights[0] = 1 # Initialize CRP. crp = CRP() # Allocates asset prices to CRP. crp.allocate(self.data, weights, resample_by='M') # Create np.array of all_weights. all_weights = np.array(crp.all_weights) # All weights for the strategy have to be the same. one_weight = all_weights[0] # Iterate through all_weights to check that weights equal to the first weight. for i in range(all_weights.shape[0]): weights = all_weights[i] assert (weights >= 0).all() assert len(weights) == self.data.shape[1] assert (weights == one_weight).all() np.testing.assert_almost_equal(np.sum(weights), 1)