Esempio n. 1
0
 def test_metropolis(self):
     n_samples = 200000
     x_init = np.random.rand(n)
     sigma = np.ones(n)
     samples = mcmc.metropolis(n_samples, x_init, log_pdf, sigma)
     assert samples.shape == (n_samples, n)
     cov = np.cov(samples[100000:, :].T)
     assert np.allclose(cov, true_cov, atol=0.3, rtol=0.1)
Esempio n. 2
0
    def acquire(self, n, t=None):
        """Acquire a batch of acquisition points.

        Parameters
        ----------
        n : int
            Number of acquisitions.
        t : int, optional
            Current iteration, (unused).

        Returns
        -------
        array_like
            Coordinates of the yielded acquisition points.

        """
        if n > self._n_samples:
            raise ValueError("The number of acquisitions, n, has to be lower"
                             "than the number of the samples (%d).".format(
                                 self._n_samples))

        logger.debug('Acquiring the next batch of %d values', n)
        gp = self.model

        # Updating the ABC threshold.
        self.eps = np.percentile(gp.Y, self.quantile_eps * 100)

        def _evaluate_gradient_logpdf(theta):
            denominator = self.evaluate(theta)
            if denominator == 0:
                return -np.inf
            pt_eval = self.evaluate_gradient(theta) / denominator
            return pt_eval.ravel()

        def _evaluate_logpdf(theta):
            val_pdf = self.evaluate(theta)
            if val_pdf == 0:
                return -np.inf
            return np.log(val_pdf)

        # Obtaining the RandMaxVar acquisition.
        for i in range(self._limit_faulty_init + 1):
            if i > self._limit_faulty_init:
                raise SystemExit("Unable to find a suitable initial point.")

            # Proposing the initial point.
            theta_init = np.zeros(shape=len(gp.bounds))
            for idx_param, range_bound in enumerate(gp.bounds):
                theta_init[idx_param] = self.random_state.uniform(
                    range_bound[0], range_bound[1])

            # Refusing to accept a faulty initial point.
            if np.isinf(_evaluate_logpdf(theta_init)):
                continue

            # Sampling the acquisition using the chosen sampler.
            if self.name_sampler == 'metropolis':
                if self._sigma_proposals_metropolis is None:
                    # Setting the default values of the sigma proposals to 1/10
                    # of each parameters interval's length.
                    sigma_proposals = []
                    for bound in self.model.bounds:
                        length_interval = bound[1] - bound[0]
                        sigma_proposals.append(length_interval / 10)
                    self._sigma_proposals_metropolis = sigma_proposals
                samples = mcmc.metropolis(
                    self._n_samples,
                    theta_init,
                    _evaluate_logpdf,
                    sigma_proposals=self._sigma_proposals_metropolis,
                    seed=self.seed)
            elif self.name_sampler == 'nuts':
                samples = mcmc.nuts(self._n_samples,
                                    theta_init,
                                    _evaluate_logpdf,
                                    _evaluate_gradient_logpdf,
                                    seed=self.seed)
            else:
                raise ValueError(
                    "Incompatible sampler. Please check the options in the documentation."
                )

            # Using the last n points of the MH chain for the acquisition batch.
            batch_theta = samples[-n:, :]
            break

        return batch_theta