예제 #1
0
    def __init__(self, step, n_workers, model, progressbar, buffer_size,
                 swap_interval, beta_tune_interval, n_workers_posterior):

        self.n_workers = n_workers
        self.n_workers_posterior = n_workers_posterior
        self.n_workers_tempered = int(self.n_workers -
                                      self.n_workers_posterior)

        self._worker_package_mapping = OrderedDict()
        self._posterior_workers = None
        self._betas = None
        self._t_scale_min = 1.01
        self._t_scale_max = 2.0

        self.step = step
        self.model = model
        self.buffer_size = buffer_size

        self.acceptance_matrix = num.zeros((n_workers, n_workers),
                                           dtype='int32')
        self.sample_count = num.zeros_like(self.acceptance_matrix)
        self.beta_tune_interval = beta_tune_interval

        self.current_scale = 1.2

        # make sampling history reloadable
        self.history = SamplingHistory()
        self._worker_update_check = num.zeros(self.n_workers, dtype='bool')

        self._default_package_kwargs = {
            'draws':
            choose_proposal('DiscreteBoundedUniform',
                            lower=swap_interval[0],
                            upper=swap_interval[1]),
            'step':
            None,
            'start':
            None,
            'chain':
            None,
            'trace':
            None,
            'tune':
            None,
            'progressbar':
            progressbar,
            'model':
            model,
            'random_seed':
            -1,
        }
예제 #2
0
def sample_pt_chain(draws,
                    step=None,
                    start=None,
                    trace=None,
                    chain=0,
                    tune=None,
                    progressbar=True,
                    model=None,
                    random_seed=-1):
    """
    Sample a single chain of the Parallel Tempering algorithm and return
    the last sample of the chain.
    Depending on the step object the MarkovChain can have various step
    behaviour, e.g. Metropolis, NUTS, ...

    Parameters
    ----------
    draws : int or :class:`beat.sampler.base.Proposal`
        The number of samples to draw for each Markov-chain per stage
        or a Proposal distribution
    step : :class:`sampler.metropolis.Metropolis`
        Metropolis initialisation object
    start : dict
        Starting point in parameter space (or partial point)
        Defaults to random draws from variables (defaults to empty dict)
    chain : int
        Chain number used to store sample in backend.
    stage : int
        Stage where to start or continue the calculation. It is possible to
        continue after completed stages (stage should be the number of the
        completed stage + 1). If None the start will be at stage = 0.
    tune : int
        Number of iterations to tune, if applicable (defaults to None)
    progressbar : bool
        Flag for displaying a progress bar
    model : :class:`pymc3.Model`
        (optional if in `with` context) has to contain deterministic
        variable name defined under step.likelihood_name' that contains the
        model likelihood

    Returns
    -------
    :class:`numpy.NdArray` with end-point of the MarkovChain
    """
    if isinstance(draws, Proposal):
        n_steps = int(draws())
    else:
        n_steps = draws

    step.n_steps = n_steps
    sampling = _iter_sample(n_steps, step, start, trace, chain, tune, model,
                            random_seed)

    try:
        for strace in sampling:
            pass

    except KeyboardInterrupt:
        raise

    if step.proposal_name in multivariate_proposals:
        if strace.count > strace.buffer_size:
            logger.debug('Evaluating sampled trace covariance at '
                         'sample %i' % strace.count)
            cov = strace.get_sample_covariance(lij=step.lij,
                                               bij=step.bij,
                                               beta=step.beta)

            if cov is not None:
                step.proposal_dist = choose_proposal(step.proposal_name,
                                                     scale=cov)

    rsamle = step.lij.l2a(strace.buffer[-1])
    #print 'chain return', rsamle
    return rsamle