class bo(object): def __init__(self, f, X, y, x_range, eval_only, extra, options): self.f = f self.t = options['t'] self.x_range = x_range self.options = options self.well_defined = X.shape[0] > 0 self.solver = GibbsSampler(X, y, options) self.eval_only = eval_only self.opt_n = options['opt_n'] self.dx = options['dx'] if eval_only: self.newX = extra else: self.n_bo = extra self.opt_n = np.maximum(self.opt_n, self.n_bo * 2) if 'ziw' in options: self.max_value = self.options['max_value'] + np.random.normal( ) * 0.05 else: self.max_value = self.options['max_value'] self.n_bo_top_percent = self.options['n_bo_top_percent'] def learn(self): self.gp, self.z, self.k = self.solver.run(self.options['gibbs_iter']) def run(self): if self.eval_only: ynew = [self.f(x) for x in self.newX] return ynew # return random inputs if X is empty if not self.well_defined: xnew = np.random.uniform(self.x_range[0], self.x_range[1], (self.n_bo, self.dx)) acfnew = [self.max_value] * self.n_bo return xnew, acfnew, self.solver.z, self.solver.k, self.max_value # learn and optimize self.learn() # initialization xnew = np.empty((self.n_bo, self.dx)) xnew[0] = np.random.uniform(self.x_range[0], self.x_range[1]) # optimize group by group all_cat = np.unique(self.z) for a in np.random.permutation(all_cat): active = self.z == a af = lambda x: MAX_VALUE(x, xnew[0], active, self.max_value, self. gp) # lower confidential bound xnew[:, active], acfnew = global_minimize(af, self.x_range[:,active], \ self.opt_n, self.n_bo, self.n_bo_top_percent) return xnew, acfnew, self.z, self.k, self.max_value
parser.add_argument("-y", "--yaml", help = "Specify yaml file") args = parser.parse_args() yaml_file = args.yaml with open(yaml_file) as yml: cdict = yaml.load(yml, Loader = yaml.FullLoader) Ldict = {key : cdict[key] for key in cdict if (key in LikelihoodFg.__init__.__code__.co_varnames)} Lclass = LikelihoodFg(**Ldict) Gdict = {key : cdict[key] for key in cdict if (key in GibbsSampler.__init__.__code__.co_varnames)} Nstep = 30000 gibbs_sampler = GibbsSampler(Nstep, Lclass, args.resume, **Gdict) gibbs_sampler.run() def get_lag(id, kmax, accepted): a = accepted[:, id] a_avg = np.mean(a) N = len(a) denom = np.sum((a-a_avg)**2) lag = [] for k in range(1, kmax + 1): num = 0 for i in range(0, N - k): num += (a[i] - a_avg) * (a[i+k]-a_avg) lag.append(num) lag = np.array(lag)
def main(): ### Parse command line options usage = "usage: %prog [options] datafile" cmdline_parser = OptionParser(usage=usage) cmdline_parser.add_option('-o', '--output', dest='output_filename', metavar='FILE', default='results.pkl', help='Serialize results to pickle FILE') cmdline_parser.add_option('-m', '--model', dest='model', metavar='FILE', default='model', help='Choose model to use') cmdline_parser.add_option('-v', '--verbose', dest='loglevel', default=logging.WARNING, action='store_const', const=logging.DEBUG, help='Debug logging level mode') cmdline_parser.add_option('-i', '--iterations', dest='iterations', type='int', default=25000, help='Number of Gibbs iterations') cmdline_parser.add_option('-b', '--burnin', dest='burnin', type='int', default=500, help='Number of burn-in iterations') cmdline_parser.add_option('-s', '--subsample', dest='subsample', type='int', default=10, help='Subsample rate') cmdline_parser.add_option('-t', '--start', dest='start', type='int', default=None, help='start') cmdline_parser.add_option('-e', '--end', dest='end', type='int', default=None, help='end') cmdline_parser.add_option('-g', '--visualize', dest='visualize', action='store_true', help='Visualize intermediate results') cmdline_parser.add_option('-G', '--visualize-priors', dest='visualize_priors', action='store_true', help='Visualize prior distributions') cmdline_parser.add_option( '-p', '--parameter-file', dest='parameter_filename', help='Use known parameters in file (i.e. simulated file).') options, args = cmdline_parser.parse_args() logging.basicConfig(level=options.loglevel, format='%(asctime)s %(message)s') data_filename = args[0] gibbs_iters = options.iterations burnin = options.burnin subsample = options.subsample model_module = __import__(options.model) if options.parameter_filename is not None: known_params = pickle.load(open(options.parameter_filename, 'rb')) # Build model and load data data = load_as_frame(data_filename, start=options.start, end=options.end) model = model_module.define_model(data, known_params) # Setup gibbs sampler sampler = GibbsSampler(model=model, iterations=gibbs_iters, burnin=burnin, subsample=subsample) if options.visualize: sampler.add_visualizer(model_module.visualize_gibbs) if options.visualize_priors: model_module.visualize_priors(model.priors) # Begin sampling start_time = time.time() sampler.run() gibbs_time = time.time() - start_time print "Gibbs sampler ran %d.1 minutes" % (gibbs_time / 60.) # Write out results results = {} results['options'] = options results['variable_names'] = model.variable_names results['known_params'] = model.known_params results['hyper_params'] = model.hyper_params results['filename'] = data_filename results['data'] = data results['gibbs_results'] = sampler.results() pickle.dump(results, open(options.output_filename, 'wb'))
''' This is an example using 3 input ensembles Adenylate Kinase pdb_chainID: 1AKE_A, 4AKE_A, 1ANK_A ''' import numpy as np from gibbs import load_coordinates, GibbsSampler from matplotlib import pyplot as plt input_coordinate = load_coordinates('1AKE_A', '4AKE_A', '1ANK_A') #Run Gibb sampler using 2 priors gibb = GibbsSampler(input_coordinate, prior=2) gibb.run(500) print 'Number of Domain = ', np.unique(gibb.membership).shape[0] print 'Membership', gibb.membership print 'Log likelihood = ', gibb.log_likelihood plt.plot(gibb.membership) plt.title('Membership') plt.show()
def main(): ### Parse command line options usage = "usage: %prog [options] datafile" cmdline_parser = OptionParser(usage=usage) cmdline_parser.add_option( '-o', '--output', dest='output_filename', metavar='FILE', default='results.pkl', help='Serialize results to pickle FILE') cmdline_parser.add_option( '-m', '--model', dest='model', metavar='FILE', default='model', help='Choose model to use') cmdline_parser.add_option( '-v', '--verbose', dest='loglevel', default=logging.WARNING, action='store_const', const=logging.DEBUG, help='Debug logging level mode') cmdline_parser.add_option( '-i', '--iterations', dest='iterations', type='int', default=25000, help='Number of Gibbs iterations') cmdline_parser.add_option( '-b', '--burnin', dest='burnin', type='int', default=500, help='Number of burn-in iterations') cmdline_parser.add_option( '-s', '--subsample', dest='subsample', type='int', default=10, help='Subsample rate') cmdline_parser.add_option( '-t', '--start', dest='start', type='int', default=None, help='start') cmdline_parser.add_option( '-e', '--end', dest='end', type='int', default=None, help='end') cmdline_parser.add_option( '-g', '--visualize', dest='visualize', action='store_true', help='Visualize intermediate results') cmdline_parser.add_option( '-G', '--visualize-priors', dest='visualize_priors', action='store_true', help='Visualize prior distributions') cmdline_parser.add_option( '-p', '--parameter-file', dest='parameter_filename', help='Use known parameters in file (i.e. simulated file).') options, args = cmdline_parser.parse_args() logging.basicConfig(level=options.loglevel, format='%(asctime)s %(message)s') data_filename = args[0] gibbs_iters = options.iterations burnin = options.burnin subsample = options.subsample model_module = __import__(options.model) if options.parameter_filename is not None: known_params = pickle.load(open(options.parameter_filename, 'rb')) # Build model and load data data = load_as_frame(data_filename, start=options.start, end=options.end) model = model_module.define_model(data, known_params) # Setup gibbs sampler sampler = GibbsSampler( model=model, iterations=gibbs_iters, burnin=burnin, subsample=subsample) if options.visualize: sampler.add_visualizer(model_module.visualize_gibbs) if options.visualize_priors: model_module.visualize_priors(model.priors) # Begin sampling start_time = time.time() sampler.run() gibbs_time = time.time() - start_time print "Gibbs sampler ran %d.1 minutes" % (gibbs_time/60.) # Write out results results = {} results['options'] = options results['variable_names'] = model.variable_names results['known_params'] = model.known_params results['hyper_params'] = model.hyper_params results['filename'] = data_filename results['data'] = data results['gibbs_results'] = sampler.results() pickle.dump(results, open(options.output_filename, 'wb'))