Пример #1
0
def test_tune_not_inplace():
    orig_scaling = np.array([0.001, 0.1])
    returned_scaling = tune(orig_scaling, acc_rate=0.6)
    assert not returned_scaling is orig_scaling
    assert np.all(orig_scaling == np.array([0.001, 0.1]))
    pass
Пример #2
0
    def astep(self, q0):
        if self.stage == 0:
            l_new = self.logp_forw(q0)
            if not num.isfinite(l_new[self._llk_index]):
                raise ValueError('Got NaN in likelihood evaluation! '
                                 'Invalid model definition? '
                                 'Or starting point outside prior bounds!')

            q_new = q0

        else:
            if self.stage_sample == 0:
                self.proposal_samples_array = self.proposal_dist(
                    self.n_steps).astype(tconfig.floatX)

            if not self.steps_until_tune and self.tune:
                # Tune scaling parameter
                logger.debug('Tuning: Chain_%i step_%i' %
                             (self.chain_index, self.stage_sample))

                self.scaling = utility.scalar2floatX(
                    tune(self.scaling,
                         self.accepted / float(self.tune_interval)))

                # Reset counter
                self.steps_until_tune = self.tune_interval
                self.accepted = 0

            logger.debug('Get delta: Chain_%i step_%i' %
                         (self.chain_index, self.stage_sample))
            delta = self.proposal_samples_array[self.stage_sample, :] * \
                self.scaling

            if self.any_discrete:
                if self.all_discrete:
                    delta = num.round(delta, 0)
                    q0 = q0.astype(int)
                    q = (q0 + delta).astype(int)
                else:
                    delta[self.discrete] = num.round(delta[self.discrete],
                                                     0).astype(int)
                    q = q0 + delta
                    q = q[self.discrete].astype(int)
            else:

                q = q0 + delta

            try:
                l0 = self.chain_previous_lpoint[self.chain_index]
                llk0 = l0[self._llk_index]
            except IndexError:
                l0 = self.logp_forw(q0)
                self.chain_previous_lpoint[self.chain_index] = l0
                llk0 = l0[self._llk_index]

            if self.check_bound:
                logger.debug('Checking bound: Chain_%i step_%i' %
                             (self.chain_index, self.stage_sample))
                varlogp = self.check_bnd(q)

                if num.isfinite(varlogp):
                    logger.debug('Calc llk: Chain_%i step_%i' %
                                 (self.chain_index, self.stage_sample))

                    lp = self.logp_forw(q)

                    logger.debug('Select llk: Chain_%i step_%i' %
                                 (self.chain_index, self.stage_sample))

                    q_new, accepted = metrop_select(
                        self.beta *
                        (lp[self._llk_index] - l0[self._llk_index]), q, q0)

                    if accepted:
                        logger.debug('Accepted: Chain_%i step_%i' %
                                     (self.chain_index, self.stage_sample))
                        logger.debug('proposed: %f previous: %f' %
                                     (lp[self._llk_index], llk0))
                        self.accepted += 1
                        l_new = lp
                        self.chain_previous_lpoint[self.chain_index] = l_new
                    else:
                        logger.debug('Rejected: Chain_%i step_%i' %
                                     (self.chain_index, self.stage_sample))
                        logger.debug(
                            'proposed: %f previous: %f' %
                            (lp[self._llk_index], l0[self._llk_index]))
                        l_new = l0
                else:
                    q_new = q0
                    l_new = l0

            else:
                logger.debug('Calc llk: Chain_%i step_%i' %
                             (self.chain_index, self.stage_sample))

                lp = self.logp_forw(q)

                logger.debug('Select: Chain_%i step_%i' %
                             (self.chain_index, self.stage_sample))
                q_new, accepted = metrop_select(
                    self.beta * (lp[self._llk_index] - llk0), q, q0)

                if accepted:
                    self.accepted += 1
                    l_new = lp
                    self.chain_previous_lpoint[self.chain_index] = l_new
                else:
                    l_new = l0

            logger.debug('Counters: Chain_%i step_%i' %
                         (self.chain_index, self.stage_sample))
            self.steps_until_tune -= 1
            self.stage_sample += 1

            # reset sample counter
            if self.stage_sample == self.n_steps:
                self.stage_sample = 0

            logger.debug('End step: Chain_%i step_%i' %
                         (self.chain_index, self.stage_sample))

        return q_new, l_new