def sample(self): """ Method to pick the sample satisfying the likelihood constraint using uniform sampling Returns ------- new : object The evolved sample number : int Number of likelihood calculations after sampling """ new = Source() x_l, x_u = self.getPrior_X() y_l, y_u = self.getPrior_Y() r_l, r_u = self.getPrior_R() a_l, a_u = self.getPrior_A() while(True): new.X = np.random.uniform(x_l,x_u) new.Y = np.random.uniform(y_l,y_u) new.A = np.random.uniform(a_l,a_u) new.R = np.random.uniform(r_l,r_u) new.logL = self.log_likelihood(new) self.number+=1 if(new.logL > self.LC): break return new, self.number
def sample(self): """ Method to pick the sample satisfying the likelihood constraint using uniform sampling Returns ------- new : object The evolved sample number : int Number of likelihood calculations after sampling """ new = Source() x_l, x_u = self.getPrior_X() y_l, y_u = self.getPrior_Y() r_l, r_u = self.getPrior_R() a_l, a_u = self.getPrior_A() while (True): new.X = np.random.uniform(x_l, x_u) new.Y = np.random.uniform(y_l, y_u) new.A = np.random.uniform(a_l, a_u) new.R = np.random.uniform(r_l, r_u) new.logL = self.log_likelihood(new) self.number += 1 if (new.logL > self.LC): break return new, self.number
def sample_source(self): """ Sampling the object from prior distribution. Returns ------- src : object The source object with X,Y,A,R sampled from their prior distribution and log likelihood calculated. """ src = Source() src.X = random.uniform(0.0, self.x_upper) src.Y = random.uniform(0.0, self.y_upper) src.A = random.uniform(self.amplitude_lower, self.amplitude_upper) src.R = random.uniform(self.R_lower, self.R_upper) src.logL = self.log_likelihood(src) return src
def sample(self): """ Sampling from the built ellipsoids. Returns ------- clust : object The sampled point satisfying the likelihood constraint number : int The number of likelihood calculations until this """ trial = Source() clust = Source() z = int(np.random.uniform(0, len(self.ellipsoid_set))) points = None try: points = self.ellipsoid_set[z].sample(n_points=50) except IndexError: raise Exception( "Adjust clustering parameters or number of active samples") max_likelihood = self.LC count = 0 r_l, r_u = self.getPrior_R() a_l, a_u = self.getPrior_A() while count < 50: trial.X = points[count][0] trial.Y = points[count][1] trial.A = np.random.uniform(a_l, a_u) trial.R = np.random.uniform(r_l, r_u) trial.logL = self.log_likelihood(trial) self.number += 1 if (trial.logL > max_likelihood): clust.__dict__ = trial.__dict__.copy() max_likelihood = trial.logL break count += 1 return clust, self.number
def sample(self): """ Method to pick the sample satisfying the likelihood constraint using metropolis sampling Returns ------- metro : object The evolved sample number : int Number of likelihood calculations until now """ metro = Source() metro.__dict__ = self.source.__dict__.copy() start = Source() start.__dict__ = self.source.__dict__.copy() new = Source() self.number+=1 count = 0 hit = 0 miss = 0 x_l, x_u = self.getPrior_X() y_l, y_u = self.getPrior_Y() r_l, r_u = self.getPrior_R() a_l, a_u = self.getPrior_A() stepnormalize = self.step/x_u stepX = self.step stepY = stepnormalize*(y_u-y_l) stepA = stepnormalize*(a_u - a_l) stepR = stepnormalize*(r_u-r_l) bord = 1 while(count<20): while bord==1: bord = 0 new.X = metro.X + stepX * (2.*np.random.uniform(0, 1) - 1.); new.Y = metro.Y + stepY * (2.*np.random.uniform(0, 1) - 1.); new.A = metro.A + stepA * (2.*np.random.uniform(0, 1) - 1.); new.R = metro.R + stepR * (2.*np.random.uniform(0, 1) - 1.); if(new.X > x_u or new.X < x_l): bord = 1; if(new.Y > y_u or new.Y < y_l): bord = 1; if(new.A > a_u or new.A < a_l): bord = 1; if(new.R > r_u or new.R < r_l): bord = 1; new.logL = self.log_likelihood(new) self.number+=1 if(new.logL > self.LC): metro.__dict__ = new.__dict__.copy() hit+=1 else: miss+=1 if( hit > miss ): self.step *= exp(1.0 / hit); if( hit < miss ): self.step /= exp(1.0 / miss); stepnormalize = self.step/x_u stepX = self.step stepY = stepnormalize*(y_u-y_l) stepA = stepnormalize*(a_u - a_l) stepR = stepnormalize*(r_u-r_l) count+=1 bord=1 return metro, self.number
def sample(self): """ Method to pick the sample satisfying the likelihood constraint using metropolis sampling Returns ------- metro : object The evolved sample number : int Number of likelihood calculations until now """ metro = Source() metro.__dict__ = self.source.__dict__.copy() start = Source() start.__dict__ = self.source.__dict__.copy() new = Source() self.number += 1 count = 0 hit = 0 miss = 0 x_l, x_u = self.getPrior_X() y_l, y_u = self.getPrior_Y() r_l, r_u = self.getPrior_R() a_l, a_u = self.getPrior_A() stepnormalize = self.step / x_u stepX = self.step stepY = stepnormalize * (y_u - y_l) stepA = stepnormalize * (a_u - a_l) stepR = stepnormalize * (r_u - r_l) bord = 1 while (count < 20): while bord == 1: bord = 0 new.X = metro.X + stepX * (2. * np.random.uniform(0, 1) - 1.) new.Y = metro.Y + stepY * (2. * np.random.uniform(0, 1) - 1.) new.A = metro.A + stepA * (2. * np.random.uniform(0, 1) - 1.) new.R = metro.R + stepR * (2. * np.random.uniform(0, 1) - 1.) if (new.X > x_u or new.X < x_l): bord = 1 if (new.Y > y_u or new.Y < y_l): bord = 1 if (new.A > a_u or new.A < a_l): bord = 1 if (new.R > r_u or new.R < r_l): bord = 1 new.logL = self.log_likelihood(new) self.number += 1 if (new.logL > self.LC): metro.__dict__ = new.__dict__.copy() hit += 1 else: miss += 1 if (hit > miss): self.step *= exp(1.0 / hit) if (hit < miss): self.step /= exp(1.0 / miss) stepnormalize = self.step / x_u stepX = self.step stepY = stepnormalize * (y_u - y_l) stepA = stepnormalize * (a_u - a_l) stepR = stepnormalize * (r_u - r_l) count += 1 bord = 1 return metro, self.number