def test_wrong_update(self):
     """
     Tests ValueError if the passing update rule is not correct.
     """
     # Put in incorrect update rule.
     expectation_maximization = EG(update_rule='SS')
     with self.assertRaises(ValueError):
         # Running allocate will raise ValueError.
         expectation_maximization.allocate(self.data, resample_by='M')
 def test_em_solution(self):
     """
     Test calculation of exponential gradient weights with expectation maximization update rule.
     """
     # Use expectation maximization update rule.
     expectation_maximization = EG(update_rule='EM')
     # Allocates asset prices to EM.
     expectation_maximization.allocate(self.data, resample_by='M')
     all_weights = np.array(expectation_maximization.all_weights)
     for i in range(all_weights.shape[0]):
         weights = all_weights[i]
         assert (weights >= 0).all()
         assert len(weights) == self.data.shape[1]
         np.testing.assert_almost_equal(np.sum(weights), 1)
 def test_mu_solution(self):
     """
     Test calculation of exponential gradient weights with multiplicative update rule.
     """
     # Use multiplicative update rule.
     multiplicative_update = EG(update_rule='MU')
     # Allocates asset prices to MU.
     multiplicative_update.allocate(self.data, resample_by='M')
     all_weights = np.array(multiplicative_update.all_weights)
     for i in range(all_weights.shape[0]):
         weights = all_weights[i]
         assert (weights >= 0).all()
         assert len(weights) == self.data.shape[1]
         np.testing.assert_almost_equal(np.sum(weights), 1)