def mpi_sampler(self, walker_ratio, burnin, samples, num, threads=-1): """ :param walker_ratio: the ratio of walkers and the count of sampled parameters :param burnin: burin iterations :param samples: no. of sample iterations :param num: number to put in output files e.g: string(name+num)=Pk_1,Bk_1 :param threads: no. of cpu threads """ # self.chain.setup() # print("find best fit point") # pso = MpiParticleSwarmOptimizer(self.chain, params[:, 1], params[:, 2]) # psoTrace = np.array([pso.gbest.position.copy() for _ in pso.sample()]) # params[:, 0] = pso.gbest.position mpi_sampler = MpiCosmoHammerSampler( params=params, likelihoodComputationChain=self.chain, filePrefix='%s' % self.name + '%d' % num, walkersRatio=walker_ratio, burninIterations=burnin, sampleIterations=samples, threadCount=threads) print("started sampling:") start = time.time() mpi_sampler.startSampling() end = time.time() tics = end - start print("The time taken %.2f sec. done!" % tics) print('Done!')
def mcmc_CH(self, walkerRatio, n_run, n_burn, mean_start, sigma_start, lowerLimit, upperLimit, threadCount=1, init_pos=None, mpi_monch=False): """ runs mcmc on the parameter space given parameter bounds with CosmoHammerSampler returns the chain """ params = np.array([mean_start, lowerLimit, upperLimit, sigma_start]).T chain = LikelihoodComputationChain( min=lowerLimit, max=upperLimit) temp_dir = tempfile.mkdtemp("Hammer") file_prefix = os.path.join(temp_dir, "logs") # chain.addCoreModule(CambCoreModule()) chain.addLikelihoodModule(self.chain) chain.setup() store = InMemoryStorageUtil() if mpi_monch is True: sampler = MpiCosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=file_prefix, walkersRatio=walkerRatio, burninIterations=n_burn, sampleIterations=n_run, threadCount=1, initPositionGenerator=init_pos, storageUtil=store) else: sampler = CosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=file_prefix, walkersRatio=walkerRatio, burninIterations=n_burn, sampleIterations=n_run, threadCount=threadCount, initPositionGenerator=init_pos, storageUtil=store) time_start = time.time() #if sampler.isMaster(): # print('Computing the MCMC...') # print('Number of walkers = ', len(mean_start)*walkerRatio) # print('Burn-in itterations: ', n_burn) # print('Sampling itterations:', n_run) sampler.startSampling() #if sampler.isMaster(): # time_end = time.time() # print(time_end - time_start, 'time taken for MCMC sampling') # if sampler._sampler.pool is not None: # sampler._sampler.pool.close() try: shutil.rmtree(temp_dir) except Exception as ex: print(ex) pass return store.samples, store.prob
def mcmc_CH(self, walkerRatio, n_run, n_burn, mean_start, sigma_start, threadCount=1, init_pos=None, mpi_monch=False): """ runs mcmc on the parameter space given parameter bounds with CosmoHammerSampler returns the chain """ lowerLimit, upperLimit = self.cosmoParam.param_bounds params = np.array([mean_start, lowerLimit, upperLimit, sigma_start]).T chain = LikelihoodComputationChain( min=lowerLimit, max=upperLimit) temp_dir = tempfile.mkdtemp("Hammer") file_prefix = os.path.join(temp_dir, "logs") # chain.addCoreModule(CambCoreModule()) chain.addLikelihoodModule(self.chain) chain.setup() store = InMemoryStorageUtil() if mpi_monch is True: sampler = MpiCosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=file_prefix, walkersRatio=walkerRatio, burninIterations=n_burn, sampleIterations=n_run, threadCount=1, initPositionGenerator=init_pos, storageUtil=store) else: sampler = CosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=file_prefix, walkersRatio=walkerRatio, burninIterations=n_burn, sampleIterations=n_run, threadCount=threadCount, initPositionGenerator=init_pos, storageUtil=store) time_start = time.time() if sampler.isMaster(): print('Computing the MCMC...') print('Number of walkers = ', len(mean_start)*walkerRatio) print('Burn-in itterations: ', n_burn) print('Sampling itterations:', n_run) sampler.startSampling() if sampler.isMaster(): time_end = time.time() print(time_end - time_start, 'time taken for MCMC sampling') # if sampler._sampler.pool is not None: # sampler._sampler.pool.close() try: shutil.rmtree(temp_dir) except Exception as ex: print(ex) pass return store.samples
def mcmc_CH(self, walkerRatio, n_run, n_burn, mean_start, sigma_start, threadCount=1, init_pos=None, mpi=False): """ runs mcmc on the parameter space given parameter bounds with CosmoHammerSampler returns the chain """ lowerLimit, upperLimit = self.lower_limit, self.upper_limit mean_start = np.maximum(lowerLimit, mean_start) mean_start = np.minimum(upperLimit, mean_start) low_start = mean_start - sigma_start high_start = mean_start + sigma_start low_start = np.maximum(lowerLimit, low_start) high_start = np.minimum(upperLimit, high_start) sigma_start = (high_start - low_start) / 2 mean_start = (high_start + low_start) / 2 params = np.array([mean_start, lowerLimit, upperLimit, sigma_start]).T chain = LikelihoodComputationChain(min=lowerLimit, max=upperLimit) temp_dir = tempfile.mkdtemp("Hammer") file_prefix = os.path.join(temp_dir, "logs") #file_prefix = "./lenstronomy_debug" # chain.addCoreModule(CambCoreModule()) chain.addLikelihoodModule(self.chain) chain.setup() store = InMemoryStorageUtil() #store = None if mpi is True: sampler = MpiCosmoHammerSampler(params=params, likelihoodComputationChain=chain, filePrefix=file_prefix, walkersRatio=walkerRatio, burninIterations=n_burn, sampleIterations=n_run, threadCount=1, initPositionGenerator=init_pos, storageUtil=store) else: sampler = CosmoHammerSampler(params=params, likelihoodComputationChain=chain, filePrefix=file_prefix, walkersRatio=walkerRatio, burninIterations=n_burn, sampleIterations=n_run, threadCount=threadCount, initPositionGenerator=init_pos, storageUtil=store) time_start = time.time() if sampler.isMaster(): print('Computing the MCMC...') print('Number of walkers = ', len(mean_start) * walkerRatio) print('Burn-in iterations: ', n_burn) print('Sampling iterations:', n_run) sampler.startSampling() if sampler.isMaster(): time_end = time.time() print(time_end - time_start, 'time taken for MCMC sampling') # if sampler._sampler.pool is not None: # sampler._sampler.pool.close() try: shutil.rmtree(temp_dir) except Exception as ex: print(ex, 'shutil.rmtree did not work') pass #samples = np.loadtxt(file_prefix+".out") #prob = np.loadtxt(file_prefix+"prob.out") return store.samples, store.prob
# if mins, and maxs included, the code performs checkin # and can calculate a null iteration of Generated Quantities in case of rejection chain = LikelihoodComputationChain() coremodule = CoreModule(szs) logPosterior = LogPosteriorModule(data, threads=k) # logPosterior = LogPosteriorModule(data,hypr) chain.addCoreModule(coremodule) chain.addLikelihoodModule(logPosterior) chain.setup() #---------- Initial Position Generator -------- pos_gen = UniformIntervalPositionGenerator() ############################## SAMPLER ######################################## #-------- Writes Derived parameters into a file------ storageUtil = DerivedParamterFileUtil(samp) #------------- Setup the sampler ----------- sampler = CHSampler(params=params, likelihoodComputationChain=chain, filePrefix=samp, walkersRatio=wr, burninIterations=Nbi, sampleIterations=Nit, storageUtil=storageUtil, initPositions=positions, threadCount=1, initPositionGenerator=pos_gen) #----------------------- Start sampling------------------------- sampler._sampler.a = a sampler.startSampling() print "Done!" sys.exit(0)
path2chain = args.path2rerunchain sampler = CosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=get_output_fname(config, path2chain), walkersRatio=ch_config_params['walkersRatio'], burninIterations=ch_config_params['burninIterations'], sampleIterations=ch_config_params['sampleIterations'], initPositionGenerator=InitializeFromChain(ch_config_params['path2rerunchain'], fraction=0.8)) else: if ch_config_params['rerun'] == 0: sampler = MpiCosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=get_output_fname(config, path2chain), walkersRatio=ch_config_params['walkersRatio'], burninIterations=ch_config_params['burninIterations'], sampleIterations=ch_config_params['sampleIterations']) else: assert ch_config_params[ 'path2rerunchain'] is not None, 'rerun is {}, but path to rerun chains not set. Aborting.'.format( ch_config_params['rerun']) sampler = MpiCosmoHammerSampler( params=params, likelihoodComputationChain=chain, filePrefix=get_output_fname(config, path2chain), walkersRatio=ch_config_params['walkersRatio'], burninIterations=ch_config_params['burninIterations'], sampleIterations=ch_config_params['sampleIterations'], initPositionGenerator=InitializeFromChain(ch_config_params['path2rerunchain'], fraction=0.8))
("NoH", [750.0, 10.0, 1510.0, 1.0])) #==========The parameter space is defined================================# #======================================================================================================================# chain = LikelihoodComputationChain(min=params[:, 1], max=params[:, 2]) chain.addCoreModule( PScore()) #=========setting up the modules===================# chain.addLikelihoodModule(slk()) chain.setup() #======================================================================================================================# sampler = MpiCosmoHammerSampler( params=params, likelihoodComputationChain= chain, #=============mpi sampler===============================# filePrefix="Powerspectrum_THANN_", walkersRatio=10, burninIterations=250, sampleIterations=250) #======================================================================================================================# print("start sampling: Here we go with the power of zeus.") sampler.startSampling() print("done!") #======================================================================================================================#
from wmap9Wrapper import WmapExtLikelihoodModule as wmap9 from cambWrapper import CambCoreModule #parameter start center, min, max, start width params = Params( ("hubble", [70, 65, 80, 3]), ("ombh2", [0.0226, 0.01, 0.03, 0.001]), ("omch2", [0.122, 0.09, 0.2, 0.01]), ("scalar_amp", [2.1e-9, 1.8e-9, 2.35e-9, 1e-10]), ("scalar_spectral_index", [0.96, 0.8, 1.2, 0.02]), ("re_optical_depth", [0.09, 0.01, 0.1, 0.03]), ("sz_amp", [1, 0, 2, 0.4])) chain = LikelihoodComputationChain(min=params[:, 1], max=params[:, 2]) camb = CambCoreModule.CambCoreModule() chain.addCoreModule(camb) chain.addLikelihoodModule(wmap9.WmapExtLikelihoodModule()) chain.setup() sampler = MpiCosmoHammerSampler(params=params, likelihoodComputationChain=chain, filePrefix="cosmoHammerWmap9_", walkersRatio=20, burninIterations=0, sampleIterations=50) print("start sampling") sampler.startSampling() print("done!")
("ombh2", [0.0226, 0.01, 0.03, 0.001]), ("omch2", [0.122, 0.09, 0.2, 0.01]), ("scalar_amp", [2.1e-9, 1.8e-9, 2.35e-9, 1e-10]), ("scalar_spectral_index", [0.96, 0.8, 1.2, 0.02]), ("re_optical_depth", [0.09, 0.01, 0.1, 0.03]), ("sz_amp", [1,0,2,0.4])) chain = LikelihoodComputationChain( min=params[:,1], max=params[:,2]) camb = CambCoreModule.CambCoreModule() chain.addCoreModule(camb) chain.addLikelihoodModule(wmap9.WmapExtLikelihoodModule()) chain.setup() sampler = MpiCosmoHammerSampler( params= params, likelihoodComputationChain=chain, filePrefix="cosmoHammerWmap9_", walkersRatio=20, burninIterations=0, sampleIterations=50) print("start sampling") sampler.startSampling() print("done!")