Ejemplo n.º 1
0
 def setUp(self):
     self.obsd1 = ObservationData(
         metric_names=["m1", "m2"],
         means=np.array([0.0, 0.0]),
         covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
     )
     self.obsd2 = ObservationData(
         metric_names=["m1", "m2"],
         means=np.array([1.0, 5.0]),
         covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
     )
     self.obsd3 = ObservationData(
         metric_names=["m1", "m2"],
         means=np.array([2.0, 25.0]),
         covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
     )
     self.obsd4 = ObservationData(
         metric_names=["m1", "m2"],
         means=np.array([3.0, 125.0]),
         covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
     )
     self.obsd_mid = ObservationData(
         metric_names=["m1", "m2"],
         means=np.array([1.5, 50.0]),
         covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
     )
     self.obsd_extreme = ObservationData(
         metric_names=["m1", "m2"],
         means=np.array([-1.0, 126.0]),
         covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
     )
     self.t = PercentileY(
         search_space=None,
         observation_features=None,
         observation_data=[
             deepcopy(self.obsd1),
             deepcopy(self.obsd2),
             deepcopy(self.obsd3),
             deepcopy(self.obsd4),
         ],
     )
     self.t_with_winsorization = PercentileY(
         search_space=None,
         observation_features=None,
         observation_data=[
             deepcopy(self.obsd1),
             deepcopy(self.obsd2),
             deepcopy(self.obsd3),
             deepcopy(self.obsd4),
         ],
         config={"winsorize": True},
     )
Ejemplo n.º 2
0
 def testInit(self):
     with self.assertRaises(ValueError):
         PercentileY(search_space=None,
                     observation_features=[],
                     observation_data=[])
Ejemplo n.º 3
0
class PercentileYTransformTest(TestCase):
    def setUp(self):
        self.obsd1 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([0.0, 0.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd2 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([1.0, 5.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd3 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([2.0, 25.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd4 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([3.0, 125.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd_mid = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([1.5, 50.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd_extreme = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([-1.0, 126.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.t = PercentileY(
            search_space=None,
            observation_features=None,
            observation_data=[
                deepcopy(self.obsd1),
                deepcopy(self.obsd2),
                deepcopy(self.obsd3),
                deepcopy(self.obsd4),
            ],
        )
        self.t_with_winsorization = PercentileY(
            search_space=None,
            observation_features=None,
            observation_data=[
                deepcopy(self.obsd1),
                deepcopy(self.obsd2),
                deepcopy(self.obsd3),
                deepcopy(self.obsd4),
            ],
            config={"winsorize": True},
        )

    def testInit(self):
        with self.assertRaises(ValueError):
            PercentileY(search_space=None,
                        observation_features=[],
                        observation_data=[])

    def testTransformObservations(self):
        self.assertListEqual(self.t.percentiles["m1"], [0.0, 1.0, 2.0, 3.0])
        self.assertListEqual(self.t.percentiles["m2"], [0.0, 5.0, 25.0, 125.0])
        transformed_obsd_mid = self.t.transform_observation_data(
            [deepcopy(self.obsd_mid)], [])[0]
        self.assertListEqual(list(transformed_obsd_mid.means), [0.5, 0.75])
        transformed_obsd_extreme = self.t.transform_observation_data(
            [deepcopy(self.obsd_extreme)], [])[0]
        self.assertListEqual(list(transformed_obsd_extreme.means), [0.0, 1.0])

    def testTransformObservationsWithWinsorization(self):
        self.assertListEqual(self.t.percentiles["m1"], [0.0, 1.0, 2.0, 3.0])
        self.assertListEqual(self.t.percentiles["m2"], [0.0, 5.0, 25.0, 125.0])
        transformed_obsd_mid = self.t_with_winsorization.transform_observation_data(
            [deepcopy(self.obsd_mid)], [])[0]
        self.assertListEqual(list(transformed_obsd_mid.means), [0.5, 0.75])
        transformed_obsd_extreme = self.t_with_winsorization.transform_observation_data(
            [deepcopy(self.obsd_extreme)], [])[0]
        # Approximate assertion for winsorization
        results = np.array(list(transformed_obsd_extreme.means))
        self.assertTrue(
            all(np.isclose(results, np.array([0.0847075, 0.9152924]))))
Ejemplo n.º 4
0
class PercentileYTransformTest(TestCase):
    def setUp(self):
        self.obsd1 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([0.0, 0.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd2 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([1.0, 5.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd3 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([2.0, 25.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd4 = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([3.0, 125.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd_mid = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([1.5, 50.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.obsd_extreme = ObservationData(
            metric_names=["m1", "m2"],
            means=np.array([-1.0, 126.0]),
            covariance=np.array([[1.0, 0.0], [0.0, 1.0]]),
        )
        self.t = PercentileY(
            search_space=None,
            observation_features=None,
            observation_data=[
                deepcopy(self.obsd1),
                deepcopy(self.obsd2),
                deepcopy(self.obsd3),
                deepcopy(self.obsd4),
            ],
        )
        self.t_with_winsorization = PercentileY(
            search_space=None,
            observation_features=None,
            observation_data=[
                deepcopy(self.obsd1),
                deepcopy(self.obsd2),
                deepcopy(self.obsd3),
                deepcopy(self.obsd4),
            ],
            config={"winsorize": True},
        )

    def testInit(self):
        with self.assertRaises(ValueError):
            PercentileY(search_space=None, observation_features=[], observation_data=[])

    def testTransformObservations(self):
        self.assertListEqual(self.t.percentiles["m1"], [0.0, 1.0, 2.0, 3.0])
        self.assertListEqual(self.t.percentiles["m2"], [0.0, 5.0, 25.0, 125.0])

        # Mid-range value transformation
        transformed_obsd_mid = self.t.transform_observation_data(
            [deepcopy(self.obsd_mid)], []
        )[0]
        mean_results = np.array(list(transformed_obsd_mid.means))
        expected = np.array([0.5, 0.75])
        self.assertTrue(
            np.allclose(mean_results, expected),
            msg=f"Unexpected mean Results: {mean_results}. Expected: {expected}.",
        )
        cov_results = np.array(transformed_obsd_mid.covariance)
        self.assertTrue(
            np.all(np.isnan(cov_results)),
            msg=f"Unexpected covariance Result: {cov_results}. Expected all nans.",
        )

        # Extreme value transformation
        transformed_obsd_extreme = self.t.transform_observation_data(
            [deepcopy(self.obsd_extreme)], []
        )[0]
        mean_results = np.array(list(transformed_obsd_extreme.means))
        expected = np.array([0.0, 1.0])
        self.assertTrue(
            np.allclose(mean_results, expected),
            msg=f"Unexpected mean Results: {mean_results}. Expected: {expected}.",
        )
        cov_results = np.array(transformed_obsd_extreme.covariance)
        self.assertTrue(
            np.all(np.isnan(cov_results)),
            msg=f"Unexpected covariance Result: {cov_results}. Expected all nans.",
        )

    def testTransformObservationsWithWinsorization(self):
        self.assertListEqual(self.t.percentiles["m1"], [0.0, 1.0, 2.0, 3.0])
        self.assertListEqual(self.t.percentiles["m2"], [0.0, 5.0, 25.0, 125.0])
        transformed_obsd_mid = self.t_with_winsorization.transform_observation_data(
            [deepcopy(self.obsd_mid)], []
        )[0]
        mean_results = np.array(list(transformed_obsd_mid.means))
        expected = np.array([0.5, 0.75])
        self.assertTrue(
            np.allclose(mean_results, expected),
            msg=f"Unexpected mean Results: {mean_results}. Expected: {expected}.",
        )
        transformed_obsd_extreme = self.t_with_winsorization.transform_observation_data(
            [deepcopy(self.obsd_extreme)], []
        )[0]
        mean_results = np.array(list(transformed_obsd_extreme.means))
        expected = np.array([0.0847075, 0.9152924])
        self.assertTrue(
            np.allclose(mean_results, expected),
            msg=f"Unexpected mean Results: {mean_results}. Expected: {expected}.",
        )