コード例 #1
0
    def 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
        """

        sampler = CosmoHammerSampler(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()
        sampler.startSampling()
        end = time.time()
        tics = end - start
        print("The time taken %.2f sec. done!" % tics)
        print('Done!')
コード例 #2
0
    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
コード例 #3
0
    def mcmc_CH(self,
                walkerRatio,
                n_run,
                n_burn,
                mean_start,
                sigma_start,
                lowerLimit,
                upperLimit,
                X2_chain,
                threadCount=1,
                init_pos=None):
        """
        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)

        # chain.addCoreModule(CambCoreModule())
        chain.addLikelihoodModule(X2_chain)
        chain.setup()

        store = InMemoryStorageUtil()
        sampler = CosmoHammerSampler(params=params,
                                     likelihoodComputationChain=chain,
                                     filePrefix="testHammer",
                                     walkersRatio=walkerRatio,
                                     burninIterations=n_burn,
                                     sampleIterations=n_run,
                                     threadCount=threadCount,
                                     initPositionGenerator=init_pos,
                                     storageUtil=store)

        sampler.startSampling()

        if sampler._sampler.pool is not None:
            sampler._sampler.pool.close()

        return store.samples
コード例 #4
0
chain.params = params

chain.addLikelihoodModule(PseudoCmbModule())
chain.setup()

# find the best fit value and update our params knowledge
print("find best fit point")
pso = ParticleSwarmOptimizer(chain, params[:, 1], params[:, 2])
psoTrace = np.array([pso.gbest.position.copy() for _ in pso.sample()])
params[:, 0] = pso.gbest.position

storageUtil = InMemoryStorageUtil()
sampler = CosmoHammerSampler(params=params,
                             likelihoodComputationChain=chain,
                             filePrefix="pseudoCmb_pso",
                             walkersRatio=50,
                             burninIterations=0,
                             sampleIterations=100,
                             storageUtil=storageUtil,
                             threadCount=4)

print("start sampling")
sampler.startSampling()
print("done!")

print("plotting")
import matplotlib.pyplot as plt
import triangle
data = storageUtil.samples
K = data.shape[1]
factor = 2.0  # size of one side of one panel
lbdim = 0.5 * factor  # size of left/bottom margin
コード例 #5
0
ファイル: sampler.py プロジェクト: guoxiaowhu/lenstronomy
    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
コード例 #6
0
from cosmoHammer.util import Params

#parameter start center, min, max, start width
params = Params(("x", [1, -10, 10, 0.1]), ("y", [1, -10, 10, 0.1]))

chain = LikelihoodComputationChain()

rosenbrock = RosenbrockModule()

chain.addLikelihoodModule(rosenbrock)
chain.setup()

sampler = CosmoHammerSampler(params=params,
                             likelihoodComputationChain=chain,
                             filePrefix="rosenbrock",
                             walkersRatio=50,
                             burninIterations=100,
                             sampleIterations=100)
sampler.startSampling()

try:
    import numpy as np
    import matplotlib.pyplot as plt
    data = np.loadtxt("rosenbrock.out")
    plt.scatter(data[:, 0], data[:, 1], 1)
    plt.xlabel(params.keys[0])
    plt.ylabel(params.keys[1])
    plt.show()
except Exception:
    print("Plotting failed. Missing libs?")
コード例 #7
0
ファイル: mcmc_runner.py プロジェクト: damonge/GSKY
chain.addLikelihoodModule(GSKYLike(saccfile_coadd, noise_saccfile_coadd))

chain.setup()

chaindir = os.path.join('chains', config['output_run_dir'])
if not os.path.isdir(get_output_fname(config, chaindir)):
    os.makedirs(get_output_fname(config, chaindir))

path2chain = os.path.join('chains', config['output_run_dir'] + '/' + ch_config_params['chainsPrefix'])

if ch_config_params['use_mpi'] == 0:
    if ch_config_params['rerun'] == 0:
        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'])
    else:
        assert ch_config_params[
                   'path2rerunchain'] is not None, 'rerun is {}, but path to rerun chains not set. Aborting.'.format(
            ch_config_params['rerun'])

        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'],
コード例 #8
0
                ("y", [1, -10, 10, 0.1]))



chain = LikelihoodComputationChain()

rosenbrock = RosenbrockModule()

chain.addLikelihoodModule(rosenbrock)
chain.setup()


sampler = CosmoHammerSampler(
                params= params, 
                likelihoodComputationChain=chain, 
                filePrefix="rosenbrock", 
                walkersRatio=50, 
                burninIterations=100, 
                sampleIterations=100)
sampler.startSampling()

try:
    import numpy as np
    import matplotlib.pyplot as plt
    data = np.loadtxt("rosenbrock.out")
    plt.scatter(data[:,0], data[:, 1], 1)
    plt.xlabel(params.keys[0])
    plt.ylabel(params.keys[1])
    plt.show()
except Exception:
    print("Plotting failed. Missing libs?")
コード例 #9
0
# ...and a positive definite, non-trivial covariance matrix.
cov  = 0.5-np.random.rand(ndim**2).reshape((ndim, ndim))
cov  = np.triu(cov)
cov += cov.T - np.diag(cov.diagonal())
cov  = np.dot(cov,cov)

# Invert the covariance matrix first.
icov = np.linalg.inv(cov)

params = [[0, -5, 5, 1] ]*ndim

params = np.array(params)

chain = LikelihoodComputationChain(params[:,1],params[:,2])

multivarianteGaussian = MultivarianteGaussianModule(icov, means)
chain.addLikelihoodModule(multivarianteGaussian)
chain.setup()

sampler = CosmoHammerSampler(
                params= params, 
                likelihoodComputationChain=chain, 
                filePrefix="multivariantGausian", 
                walkersRatio=50, 
                burninIterations=0, 
                sampleIterations=100,
                )

print("start sampling")
sampler.startSampling()
print("done!")
コード例 #10
0
logPosterior = LogPosteriorModule(data, threads=k)
# logPosterior = LogPosteriorModule(data,hypr)
chain.addCoreModule(coremodule)
chain.addLikelihoodModule(logPosterior)
chain.setup()
#######################################################################################
############################## 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,
)
#----------------------- Start sampling-------------------------

print("start sampling")
t1 = time()
sampler.startSampling()
b = time() - t1
print("done!")
print 'Doing ', Nit, ' iterations takes ', b, ' seconds'
sys.exit(0)