Ejemplo n.º 1
0
    def test_createChainContext_params(self):
        keys = ["a", "b"]
        params = Params((keys[0], 0), (keys[1], 1))
        chain = LikelihoodComputationChain()
        chain.params = params

        p = np.array([1, 2])
        ctx = chain.createChainContext(p)

        assert ctx is not None
        assert np.all(ctx.getParams().keys == keys)
        assert np.all(ctx.getParams()[0] == p[0])
        assert np.all(ctx.getParams()[1] == p[1])
 def test_createChainContext_params(self):
     keys = ["a", "b"]
     params = Params((keys[0], 0),
                     (keys[1], 1))
     chain = LikelihoodComputationChain()
     chain.params = params
     
     p = np.array([1,2])
     ctx = chain.createChainContext(p)
     
     assert ctx is not None
     assert np.all(ctx.getParams().keys == keys) 
     assert np.all(ctx.getParams()[0] == p[0]) 
     assert np.all(ctx.getParams()[1] == p[1]) 
Ejemplo n.º 3
0
    def __init__(self, data, nbins, noise=0., div=1.0, like_func='c'):
        """
        :param data: load your data
        :param nbins: number of k-modes in powerspectrum OR
         number of triangle contributions in bispectrum (for covariance matrix)
        :param noise: system noise, e.g. SKA, MWA noise response (if any), default 0.0,
        :param div: likelihood normalization factor, default 1.0,
        :param like_func: choose between complex likelihood function (use 'c'), and normal function (use 'n')

        """
        chain = LikelihoodComputationChain(min=params[:, 1], max=params[:, 2])
        chain.params = params
        if like_func == 'n':
            chain.addLikelihoodModule(LikeModule(data, nbins, noise, div))
        else:
            chain.addLikelihoodModule(ComplexLikeModule(data, nbins, noise))
        self.chain = chain
from cosmoHammer.util import InMemoryStorageUtil
from cosmoHammer.util import Params

from cosmoHammer.modules import PseudoCmbModule
from cosmoHammer.pso.ParticleSwarmOptimizer import ParticleSwarmOptimizer

#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(params[:, 1], params[:, 2])
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,
from cosmoHammer.util import Params

from cosmoHammer.modules import PseudoCmbModule
from cosmoHammer.pso.ParticleSwarmOptimizer import ParticleSwarmOptimizer

#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(params[:,1], params[:,2])
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",