Beispiel #1
0
    def init_log_like(self):
        # initialize log_like
        if self.do_radial:
            self.model.rate = init_rate_matrix(self.model.dim_v, self.model.v, self.model.w, self.pbc)
            log_like = rad_log_like_lag(
                self.model.dim_v,
                self.model.dim_rad,
                self.data.dim_lt,
                self.model.rate,
                self.model.wrad,
                self.data.list_lt,
                self.data.list_trans,
                self.model.redges,
                self.lmax,
                self.model.bessel0_zeros,
                self.model.bessels,
                0.0,
            )
        else:
            log_like = log_like_lag(
                self.model.dim_v,
                self.data.dim_lt,
                self.model.v,
                self.model.w,
                self.model.list_lt,
                self.data.list_trans,
                self.pbc,
            )

        if log_like is None:
            raise ValueError("Initial propagator has non-positive elements")
        elif np.isnan(log_like):
            raise ValueError("Initial likelihood diverges")
        self.log_like = log_like

        # add smoothing to diffusion profile
        if self.k > 0.0:
            E_w = string_energy(self.model.w, self.k, self.pbc)
            self.string_vecs = string_vecs(len(self.model.w), self.pbc)
            self.log_like = log_like - E_w  # minus sign because surface=log_like

        print "initial log-likelihood:", self.log_like
        self.all_log_like = np.zeros(self.nmc, float)

        # TODO make nicer
        if self.model.ncosF > 0:
            self.naccv_coeff = np.zeros(self.model.ncosF, int)
        if self.model.ncosD > 0:
            self.naccw_coeff = np.zeros(self.model.ncosD, int)
        if self.do_radial:
            if self.model.ncosDrad > 0:
                self.naccwrad_coeff = np.zeros(self.model.ncosDrad, int)
            else:
                self.model.ncosDrad = -1
Beispiel #2
0
    def mcmove_diffusion_radial(self):
        # propose temporary wrad
        if self.model.ncosDrad <= 0:
            index = np.random.randint(0, self.model.dim_wrad)
            wradt = copy.deepcopy(self.model.wrad)  # temporary wrad
            wradt[index] += self.dwrad * (np.random.random() - 0.5)

        else:
            index = np.random.randint(0, self.model.ncosDrad)
            coefft = copy.deepcopy(self.model.wrad_coeff)
            coefft[index] += self.dwrad * (np.random.random() - 0.5)
            wradt = self.model.calc_profile(coefft, self.model.wrad_basis)

        log_like_try = rad_log_like_lag(
            self.model.dim_v,
            self.model.dim_rad,
            self.data.dim_lt,
            self.model.rate,
            wradt,
            self.data.list_lt,
            self.data.list_trans,
            self.model.redges,
            self.lmax,
            self.model.bessel0_zeros,
            self.model.bessels,
            0.0,
        )

        # print "dlog",log_like_try - self.log_like
        # Metropolis acceptance
        if log_like_try is not None and not np.isnan(log_like_try):  # propagator is well behaved  TODO implement
            dlog = log_like_try - self.log_like
            # print "dlog",dlog,log_like_try
            r = np.random.random()  # in [0,1[
            # if dlog > 0: print "aha",
            print "dlog", dlog, self.log_like, log_like_try
            if r < np.exp(dlog / self.temp):  # accept if dlog increases, accept maybe if decreases
                # print "accpet"
                self.model.wrad = wradt
                if self.model.ncosDrad > 0:
                    # print self.model.wrad_coeff
                    self.model.wrad_coeff = coefft
                    self.naccwrad_coeff[index] += 1
                self.naccwrad += 1
                self.naccwrad_update += 1
                self.log_like = log_like_try
        else:
            print "WARNING: log_like_try behaves badly"