pypesto/sample/parallel_tempering.py
def swap_samples(self, last_samples: List[Union[InternalSample, None]]) -> List[Union[InternalSample, None]]:
        """Swap samples as in Vousden2016."""
        # for recording swaps
        swapped = copy.deepcopy(last_samples)

        if len(self.betas) == 1:
            # nothing to be done
            return swapped

        # beta differences
        dbetas = self.betas[:-1] - self.betas[1:]

        # loop over chains from highest temperature down
        for dbeta, sampler1_idx, sampler2_idx in reversed(list(zip(
                dbetas, list(range(len(self.samplers[:-1]))), list(range(len(self.samplers[1:])))))):
            # extract samples
            sample1 = last_samples[sampler1_idx]
            sample2 = last_samples[sampler2_idx]

            # extract log likelihood values
            sample1_llh = sample1.lpost - sample1.lprior
            sample2_llh = sample2.lpost - sample2.lprior

            # swapping probability
            p_acc_swap = dbeta * (sample2_llh - sample1_llh)

            # flip a coin
            u = np.random.uniform(0, 1)

            # check acceptance
            swap = np.log(u) < p_acc_swap
            if swap:
                # swap
                # sampler2.set_last_sample(sample1)
                # sampler1.set_last_sample(sample2)
                swapped[sampler2_idx] = sample1
                swapped[sampler1_idx] = sample2
            else:
                swapped[sampler2_idx] = sample2
                swapped[sampler1_idx] = sample1
            # record
            # swapped.insert(0, swap)
        return swapped
Swap samples as in Vousden2016.
Swap samples as in Vousden2016.
Swap samples as in Vousden2016 .
def swap_samples(self, last_samples: List[Union[InternalSample, None]]) -> List[Union[InternalSample, None]]:
        swapped = copy.deepcopy(last_samples)
        if len(self.betas) == 1:
            return swapped
        dbetas = self.betas[:-1] - self.betas[1:]
        for dbeta, sampler1_idx, sampler2_idx in reversed(list(zip(
                dbetas, list(range(len(self.samplers[:-1]))), list(range(len(self.samplers[1:])))))):
            sample1 = last_samples[sampler1_idx]
            sample2 = last_samples[sampler2_idx]
            sample1_llh = sample1.lpost - sample1.lprior
            sample2_llh = sample2.lpost - sample2.lprior
            p_acc_swap = dbeta * (sample2_llh - sample1_llh)
            u = np.random.uniform(0, 1)
            swap = np.log(u) < p_acc_swap
            if swap:
                swapped[sampler2_idx] = sample1
                swapped[sampler1_idx] = sample2
            else:
                swapped[sampler2_idx] = sample2
                swapped[sampler1_idx] = sample1
            ped.insert(0, swap)
        return swapped
def swap_samples ( self , last_samples : List [ Union [ InternalSample , None ] ] ) -> List [ Union [ InternalSample , None ] ] : swapped = copy . deepcopy ( last_samples ) if len ( self . betas ) == 1 : return swapped dbetas = self . betas [ : - 1 ] - self . betas [ 1 : ] for dbeta , sampler1_idx , sampler2_idx in reversed ( list ( zip ( dbetas , list ( range ( len ( self . samplers [ : - 1 ] ) ) ) , list ( range ( len ( self . samplers [ 1 : ] ) ) ) ) ) ) : sample1 = last_samples [ sampler1_idx ] sample2 = last_samples [ sampler2_idx ] sample1_llh = sample1 . lpost - sample1 . lprior sample2_llh = sample2 . lpost - sample2 . lprior p_acc_swap = dbeta * ( sample2_llh - sample1_llh ) u = np . random . uniform ( 0 , 1 ) swap = np . log ( u ) < p_acc_swap if swap : swapped [ sampler2_idx ] = sample1 swapped [ sampler1_idx ] = sample2 else : swapped [ sampler2_idx ] = sample2 swapped [ sampler1_idx ] = sample1 return swapped
Swap samples as in Vousden2016.
Swap samples as in Vousden2016 .
"""Swap samples as in Vousden2016.""" # for recording swaps # nothing to be done # beta differences # loop over chains from highest temperature down # extract samples # extract log likelihood values # swapping probability # flip a coin # check acceptance # swap # sampler2.set_last_sample(sample1) # sampler1.set_last_sample(sample2) # record # swapped.insert(0, swap)
self
last_samples
List[Union[InternalSample, None]]