Esempio n. 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
Esempio n. 2
0
    def mcmove_diffusion(self):
        # propose temporary w vector: wt
        if self.model.ncosD <= 0:
            if self.k > 0:
                index = np.random.randint(
                    0, self.model.dim_w
                )  # TODO what if string_vecs has different dimension???
                wt = self.model.w + self.dw * (
                    np.random.random() - 0.5) * self.string_vecs[:, index]
            else:
                index = np.random.randint(0, self.model.dim_w)
                wt = copy.deepcopy(self.model.w)  # temporary w
                wt[index] += self.dw * (np.random.random() - 0.5)
        else:
            index = np.random.randint(0, self.model.ncosD)
            coefft = copy.deepcopy(self.model.w_coeff)
            coefft[index] += self.dw * (np.random.random() - 0.5)
            wt = self.model.calc_profile(coefft, self.model.w_basis)

        log_like_try = log_like_lag(self.model.dim_v, self.data.dim_lt,
                                    self.model.v, wt, self.model.list_lt,
                                    self.data.list_trans, self.pbc)

        if log_like_try is not None and not np.isnan(
                log_like_try):  # propagator is well behaved
            # add restraints to smoothen
            if self.k > 0.:
                E_wt = string_energy(wt, self.k, self.pbc)
                log_like_try -= E_wt  # minus sign because surface=log_like

            # Metropolis acceptance
            dlog = log_like_try - self.log_like
            r = np.random.random()  #in [0,1[
            if r < np.exp(
                    dlog / self.temp
            ):  # accept if dlog increases, accept maybe if decreases
                self.model.w[:] = wt[:]
                if self.model.ncosD > 0:
                    self.model.w_coeff[:] = coefft[:]
                    self.naccw_coeff[index] += 1
                self.naccw += 1
                self.naccw_update += 1
                self.log_like = log_like_try
        if False:
            self.check_propagator(self.model.list_lt[0])
            print "loglike", self.log_like
Esempio n. 3
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.)
        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.:
            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
Esempio n. 4
0
    def mcmove_diffusion(self):
        # propose temporary w vector: wt
        if self.model.ncosD <= 0:
            if self.k > 0:
                index = np.random.randint(0, self.model.dim_w)  # TODO what if string_vecs has different dimension???
                wt = self.model.w + self.dw * (np.random.random() - 0.5) * self.string_vecs[:, index]
            else:
                index = np.random.randint(0, self.model.dim_w)
                wt = copy.deepcopy(self.model.w)  # temporary w
                wt[index] += self.dw * (np.random.random() - 0.5)
        else:
            index = np.random.randint(0, self.model.ncosD)
            coefft = copy.deepcopy(self.model.w_coeff)
            coefft[index] += self.dw * (np.random.random() - 0.5)
            wt = self.model.calc_profile(coefft, self.model.w_basis)

        log_like_try = log_like_lag(
            self.model.dim_v, self.data.dim_lt, self.model.v, wt, self.model.list_lt, self.data.list_trans, self.pbc
        )

        if log_like_try is not None and not np.isnan(log_like_try):  # propagator is well behaved
            # add restraints to smoothen
            if self.k > 0.0:
                E_wt = string_energy(wt, self.k, self.pbc)
                log_like_try -= E_wt  # minus sign because surface=log_like

            # Metropolis acceptance
            dlog = log_like_try - self.log_like
            r = np.random.random()  # in [0,1[
            if r < np.exp(dlog / self.temp):  # accept if dlog increases, accept maybe if decreases
                self.model.w[:] = wt[:]
                if self.model.ncosD > 0:
                    self.model.w_coeff[:] = coefft[:]
                    self.naccw_coeff[index] += 1
                self.naccw += 1
                self.naccw_update += 1
                self.log_like = log_like_try
        if False:
            self.check_propagator(self.model.list_lt[0])
            print "loglike", self.log_like