def test_sample_ball():
    p0 = np.ones(10)
    std = np.ones(10)
    sample = sampling_util.sample_ball(p0, std, size=10000, dist='normal')
    mean = np.mean(sample, axis=0)
    npt.assert_almost_equal(mean, p0, decimal=1)
    sigma = np.std(sample, axis=0)
    npt.assert_almost_equal(sigma, std, decimal=1)

    sample = sampling_util.sample_ball(p0, std, size=10000, dist='uniform')
    mean = np.mean(sample, axis=0)
    npt.assert_almost_equal(mean, p0, decimal=1)
    sigma = np.std(sample, axis=0)
    npt.assert_almost_equal(sigma, std*0.607, decimal=1)
Esempio n. 2
0
    def mcmc_emcee(self, n_walkers, n_run, n_burn, mean_start, sigma_start, mpi=False, progress=False, threadCount=1):
        numParam, _ = self.chain.param.num_param()
        p0 = sampling_util.sample_ball(mean_start, sigma_start, n_walkers)
        time_start = time.time()
        if mpi is True:
            pool = MPIPool()
            if not pool.is_master():
                pool.wait()
                sys.exit(0)
            is_master_pool = pool.is_master()
            sampler = emcee.EnsembleSampler(n_walkers, numParam, self.chain.logL, pool=pool)
        else:
            is_master_pool = True
            if threadCount > 1 :
                pool = Pool(processes=threadCount)
            else :
                pool = None
            sampler = emcee.EnsembleSampler(n_walkers, numParam, self.chain.likelihood, pool=pool)

        sampler.run_mcmc(p0, n_burn + n_run, progress=progress)
        flat_samples = sampler.get_chain(discard=n_burn, thin=1, flat=True)
        dist = sampler.get_log_prob(flat=True, discard=n_burn, thin=1)
        if is_master_pool:
            print('Computing the MCMC...')
            print('Number of walkers = ', n_walkers)
            print('Burn-in iterations: ', n_burn)
            print('Sampling iterations:', n_run)
            time_end = time.time()
            print(time_end - time_start, 'time taken for MCMC sampling')
        return flat_samples, dist
Esempio n. 3
0
    def mcmc_emcee(self,
                   n_walkers,
                   n_run,
                   n_burn,
                   mean_start,
                   sigma_start,
                   mpi=False,
                   progress=False,
                   threadCount=1):
        """
        Run MCMC with emcee.
        For details, please have a look at the documentation of the emcee packager.

        :param n_walkers: number of walkers in the emcee process
        :type n_walkers: integer
        :param n_run: number of sampling (after burn-in) of the emcee
        :type n_run: integer
        :param n_burn: number of burn-in iterations (those will not be saved in the output sample)
        :type n_burn: integer
        :param mean_start: mean of the parameter position of the initialising sample
        :type mean_start: numpy array of length the number of parameters
        :param sigma_start: spread of the parameter values (uncorrelated in each dimension) of the initialising sample
        :type sigma_start: numpy array of length the number of parameters
        :param mpi: if True, initializes an MPIPool to allow for MPI execution of the sampler
        :type mpi: bool
        :param progress: if True, prints the progress bar
        :type progress: bool
        :param threadCount: number of threats in multi-processing (not applicable for MPI)
        :type threadCount: integer
        :return: samples, ln likelihood value of samples
        :rtype: numpy 2d array, numpy 1d array
        """
        num_param, _ = self.chain.param.num_param()
        p0 = sampling_util.sample_ball(mean_start, sigma_start, n_walkers)
        time_start = time.time()

        pool, is_master = choose_pool(mpi=mpi,
                                      processes=threadCount,
                                      use_dill=True)

        sampler = emcee.EnsembleSampler(n_walkers,
                                        num_param,
                                        self.chain.logL,
                                        pool=pool)

        sampler.run_mcmc(p0, n_burn + n_run, progress=progress)
        flat_samples = sampler.get_chain(discard=n_burn, thin=1, flat=True)
        dist = sampler.get_log_prob(flat=True, discard=n_burn, thin=1)
        if is_master:
            print('Computing the MCMC...')
            print('Number of walkers = ', n_walkers)
            print('Burn-in iterations: ', n_burn)
            print('Sampling iterations:', n_run)
            time_end = time.time()
            print(time_end - time_start, 'time taken for MCMC sampling')
        return flat_samples, dist
Esempio n. 4
0
    def mcmc_emcee(self,
                   n_walkers,
                   n_burn,
                   n_run,
                   kwargs_mean_start,
                   kwargs_sigma_start,
                   continue_from_backend=False,
                   **kwargs_emcee):
        """
        runs the EMCEE MCMC sampling

        :param n_walkers: number of walkers
        :param n_burn: number of iteration of burn in (not stored in the output sample
        :param n_run: number of iterations (after burn in) to be sampled
        :param kwargs_mean_start: keyword arguments of the mean starting position
        :param kwargs_sigma_start: keyword arguments of the spread in the initial particles per parameter
        :param continue_from_backend: bool, if True and 'backend' in kwargs_emcee, will continue a chain sampling from backend
        :param kwargs_emcee: keyword argument for the emcee (e.g. to specify backend)
        :return: samples of the EMCEE run
        """

        num_param = self.param.num_param
        sampler = emcee.EnsembleSampler(n_walkers,
                                        num_param,
                                        self.chain.likelihood,
                                        args=(),
                                        **kwargs_emcee)
        mean_start = self.param.kwargs2args(**kwargs_mean_start)
        sigma_start = self.param.kwargs2args(**kwargs_sigma_start)
        p0 = sampling_util.sample_ball(mean_start, sigma_start, n_walkers)
        backend = kwargs_emcee.get('backend', None)
        if backend is not None:
            if continue_from_backend:
                p0 = None
            else:
                backend.reset(n_walkers, num_param)
        sampler.run_mcmc(p0, n_burn + n_run, progress=True)
        flat_samples = sampler.get_chain(discard=n_burn, thin=1, flat=True)
        log_prob = sampler.get_log_prob(discard=n_burn, thin=1, flat=True)
        return flat_samples, log_prob
Esempio n. 5
0
    def mcmc_emcee(self, n_walkers, n_run, n_burn, mean_start, sigma_start, mpi=False, progress=False, threadCount=1,
                   initpos=None, backup_filename=None, start_from_backup=False):
        """
        Run MCMC with emcee.
        For details, please have a look at the documentation of the emcee packager.

        :param n_walkers: number of walkers in the emcee process
        :type n_walkers: integer
        :param n_run: number of sampling (after burn-in) of the emcee
        :type n_run: integer
        :param n_burn: number of burn-in iterations (those will not be saved in the output sample)
        :type n_burn: integer
        :param mean_start: mean of the parameter position of the initialising sample
        :type mean_start: numpy array of length the number of parameters
        :param sigma_start: spread of the parameter values (uncorrelated in each dimension) of the initialising sample
        :type sigma_start: numpy array of length the number of parameters
        :param mpi: if True, initializes an MPIPool to allow for MPI execution of the sampler
        :type mpi: bool
        :param progress: if True, prints the progress bar
        :type progress: bool
        :param threadCount: number of threats in multi-processing (not applicable for MPI)
        :type threadCount: integer
        :param initpos: initial walker position to start sampling (optional)
        :type initpos: numpy array of size num param x num walkser
        :param backup_filename: name of the HDF5 file where sampling state is saved (through emcee backend engine)
        :type backup_filename: string
        :param start_from_backup: if True, start from the state saved in `backup_filename`.
         Otherwise, create a new backup file with name `backup_filename` (any already existing file is overwritten!).
        :type start_from_backup: bool
        :return: samples, ln likelihood value of samples
        :rtype: numpy 2d array, numpy 1d array
        """
        num_param, _ = self.chain.param.num_param()
        if initpos is None:
            initpos = sampling_util.sample_ball(mean_start, sigma_start, n_walkers, dist='normal')

        pool = choose_pool(mpi=mpi, processes=threadCount, use_dill=True)

        if backup_filename is not None:
            backend = emcee.backends.HDFBackend(backup_filename, name="lenstronomy_mcmc_emcee")
            if pool.is_master():
                print("Warning: All samples (including burn-in) will be saved in backup file '{}'.".format(backup_filename))
            if start_from_backup:
                initpos = None
                n_run_eff = n_run
            else:
                n_run_eff = n_burn + n_run
                backend.reset(n_walkers, num_param)
                if pool.is_master():
                    print("Warning: backup file '{}' has been reset!".format(backup_filename))
        else:
            backend = None
            n_run_eff = n_burn + n_run

        time_start = time.time()

        sampler = emcee.EnsembleSampler(n_walkers, num_param, self.chain.logL,
                                        pool=pool, backend=backend)

        sampler.run_mcmc(initpos, n_run_eff, progress=progress)
        flat_samples = sampler.get_chain(discard=n_burn, thin=1, flat=True)
        dist = sampler.get_log_prob(flat=True, discard=n_burn, thin=1)
        if pool.is_master():
            print('Computing the MCMC...')
            print('Number of walkers = ', n_walkers)
            print('Burn-in iterations: ', n_burn)
            print('Sampling iterations (in current run):', n_run_eff)
            time_end = time.time()
            print(time_end - time_start, 'time taken for MCMC sampling')
        return flat_samples, dist
    def test_raise(self):

        with self.assertRaises(ValueError):
            sampling_util.sample_ball(p0=np.ones(10), std=np.ones(10), size=1000, dist='BAD')