示例#1
0
    def _sample_numerical(
            self,
            low,  # type: float
            high,  # type: float
            below,  # type: np.ndarray
            above,  # type: np.ndarray
            q=None,  # type: Optional[float]
            is_log=False  # type: bool
    ):
        # type: (...) -> float

        if is_log:
            low = np.log(low)
            high = np.log(high)
            below = np.log(below)
            above = np.log(above)

        size = (self.n_ei_candidates, )

        parzen_estimator_below = ParzenEstimator(
            mus=below,
            low=low,
            high=high,
            parameters=self.parzen_estimator_parameters)
        samples_below = self._sample_from_gmm(
            parzen_estimator=parzen_estimator_below,
            low=low,
            high=high,
            q=q,
            is_log=is_log,
            size=size)
        log_likelihoods_below = self._gmm_log_pdf(
            samples=samples_below,
            parzen_estimator=parzen_estimator_below,
            low=low,
            high=high,
            q=q,
            is_log=is_log)

        parzen_estimator_above = ParzenEstimator(
            mus=above,
            low=low,
            high=high,
            parameters=self.parzen_estimator_parameters)

        log_likelihoods_above = self._gmm_log_pdf(
            samples=samples_below,
            parzen_estimator=parzen_estimator_above,
            low=low,
            high=high,
            q=q,
            is_log=is_log)

        return float(
            TPESampler._compare(samples=samples_below,
                                log_l=log_likelihoods_below,
                                log_g=log_likelihoods_above)[0])
    def test_calculate(mus, flags, expected):
        # type: (List[float], Dict[str, bool], Dict[str, List[float]]) -> None

        s_weights, s_mus, s_sigmas = \
            ParzenEstimator._calculate(mus, -1.0, 1.0, prior_weight=1.0,
                                       consider_prior=flags['prior'],
                                       consider_magic_clip=flags['magic_clip'],
                                       consider_endpoints=flags['endpoints'],
                                       weights_func=default_weights)

        # Result contains an additional value for a prior distribution if consider_prior is True.
        np.testing.assert_almost_equal(s_weights, expected['weights'])
        np.testing.assert_almost_equal(s_mus, expected['mus'])
        np.testing.assert_almost_equal(s_sigmas, expected['sigmas'])
    def test_calculate_shape_check(mus, prior, magic_clip, endpoints):
        # type: (List[float], bool, bool, bool) -> None

        s_weights, s_mus, s_sigmas = \
            ParzenEstimator._calculate(mus, -1.0, 1.0, prior_weight=1.0,
                                       consider_prior=prior,
                                       consider_magic_clip=magic_clip,
                                       consider_endpoints=endpoints,
                                       weights_func=default_weights)

        # Result contains an additional value for a prior distribution if prior is True.
        assert len(s_weights) == len(mus) + int(prior)
        assert len(s_mus) == len(mus) + int(prior)
        assert len(s_sigmas) == len(mus) + int(prior)