def propagate_with_sink(time, F, D, edges, sink_index): n = len(F) rate = init_rate_matrix(n, F, D, pbc=True) # undo PBC rate[0, -1] = 0. rate[-1, 0] = 0. rate[0, 0] = -rate[1, 0] rate[-1, -1] = -rate[-2, -1] # sink rate[-1, -1] = -1000000 rate[-2, -1] = 0. #rate[-1,-2] = 0. # fixed #rate[:,0] = 0. rate[0, :] = 0. #rate[0,0] = 1. # test #print "rate",rate #print "test",np.sum(rate,axis=0) prop = scipy.linalg.expm2(time * rate) #print prop init = np.ones(len(prop)) init[0] = 1. profile = np.dot(prop, init) return profile
def check_propagator(self, lagtime): import scipy rate = init_rate_matrix(self.model.dim_v, self.model.v, self.model.w, self.model.pbc) vals, vecs = np.linalg.eig(rate) line = "" for v in vals: if v.imag < 1e-10: VAL = v.real else: VAL = v line += str(VAL) + " " propagator = scipy.linalg.expm2(lagtime * rate) vals, vecs = np.linalg.eig(propagator) line2 = "" for v in vals: if v.imag < 1e-10: VAL = v.real else: VAL = v line2 += str(VAL) + " " tiny = 1e-10 count = np.sum(propagator < tiny) #log_like = np.float64(0.0) # use high precision #b = transition[ilag,:,:]*np.log(propagator.clip(tiny)) #log_like += np.sum(b) print "count", count print "ratematrix", line print "propagatormatrix", line2
def set_model(self, model, data, ncosF, ncosD, ncosDrad): self.data = data # transitions etc ncosP = 0 # derive model if self.do_radial > 0: if model == "RadCosinusModel": self.model = RadCosinusModel(self.data, self.D0, ncosF, ncosD, ncosP, ncosDrad) elif model == "RadModel": self.model = RadModel(self.data, self.D0, ncosF, ncosD, ncosP) else: raise ValueError("model %s not found" % model) bessel0_zeros, bessels = setup_bessel_functions(self.lmax, self.model.dim_rad) self.model.bessels = bessels self.model.bessel0_zeros = bessel0_zeros self.model.rate = init_rate_matrix(self.model.dim_v, self.model.v, self.model.w, self.pbc) else: if model == "CosinusModel": self.model = CosinusModel(self.data, self.D0, ncosF, ncosD, ncosP) # this will default to Model(self,data) if ncosF and ncosD are both 0 elif model == "SinusCosinusModel": self.model = SinusCosinusModel(self.data, self.D0, ncosF, ncosD, ncosP) elif model == "StepModel": self.model = StepModel(self.data, self.D0, ncosF, ncosD, ncosP) elif model == "OneStepModel": self.model = OneStepModel(self.data, self.D0, ncosF, ncosD, ncosP) elif model == "Model": self.model = Model(self.data, self.D0) else: raise ValueError("model %s not found" % model) assert self.pbc == self.model.pbc # make sure PBC for model and transition matrix are identical
def check_propagator(self, lagtime): import scipy rate = init_rate_matrix(self.model.dim_v, self.model.v, self.model.w, self.model.pbc) vals, vecs = np.linalg.eig(rate) line = "" for v in vals: if v.imag < 1e-10: VAL = v.real else: VAL = v line += str(VAL) + " " propagator = scipy.linalg.expm2(lagtime * rate) vals, vecs = np.linalg.eig(propagator) line2 = "" for v in vals: if v.imag < 1e-10: VAL = v.real else: VAL = v line2 += str(VAL) + " " tiny = 1e-10 count = np.sum(propagator < tiny) # log_like = np.float64(0.0) # use high precision # b = transition[ilag,:,:]*np.log(propagator.clip(tiny)) # log_like += np.sum(b) print "count", count print "ratematrix", line print "propagatormatrix", line2
def propagate_with_sink(time,F,D,edges,sink_index): n = len(F) rate = init_rate_matrix(n,F,D,pbc=True) # undo PBC rate[0,-1] = 0. rate[-1,0] = 0. rate[0,0] = - rate[1,0] rate[-1,-1] = -rate[-2,-1] # sink rate[-1,-1] = -1000000 rate[-2,-1] = 0. #rate[-1,-2] = 0. # fixed #rate[:,0] = 0. rate[0,:] = 0. #rate[0,0] = 1. # test #print "rate",rate #print "test",np.sum(rate,axis=0) prop = scipy.linalg.expm2(time*rate) #print prop init = np.ones(len(prop)) init[0] = 1. profile = np.dot(prop,init) return profile
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
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
def set_model(self, model, data, ncosF, ncosD, ncosDrad, F_profile=None): self.data = data # transitions etc ncosP = 0 # derive model if self.do_radial > 0: if model == "RadCosinusModel": self.model = RadCosinusModel(self.data, self.D0, ncosF, ncosD, ncosP, ncosDrad) elif model == "RadModel": self.model = RadModel(self.data, self.D0, ncosF, ncosD, ncosP) else: raise ValueError("model %s not found" % model) bessel0_zeros, bessels = setup_bessel_functions( self.lmax, self.model.dim_rad, ) self.model.bessels = bessels self.model.bessel0_zeros = bessel0_zeros self.model.rate = init_rate_matrix(self.model.dim_v, self.model.v, self.model.w, self.pbc) else: if model == "CosinusModel": self.model = CosinusModel(self.data, self.D0, ncosF, ncosD, ncosP) # this will default to Model(self,data) if ncosF and ncosD are both 0 elif model == "SinusCosinusModel": self.model = SinusCosinusModel(self.data, self.D0, ncosF, ncosD, ncosP) elif model == "StepModel": self.model = StepModel(self.data, self.D0, ncosF, ncosD, ncosP) elif model == "OneStepModel": self.model = OneStepModel(self.data, self.D0, ncosF, ncosD, ncosP) elif model == "Model": self.model = Model(self.data, self.D0) else: raise ValueError("model %s not found" % model) assert self.pbc == self.model.pbc # make sure PBC for model and transition matrix are identical