Example #1
0
 def mcmc_emcee(self, n_walkers, n_run, n_burn, mean_start, sigma_start):
     """
     returns the mcmc analysis of the parameter space
     """
     sampler = emcee.EnsembleSampler(n_walkers, self.cosmoParam.numParam, self.chain.likelihood)
     p0 = emcee.utils.sample_ball(mean_start, sigma_start, n_walkers)
     new_pos, _, _, _ = sampler.run_mcmc(p0, n_burn)
     sampler.reset()
     store = InMemoryStorageUtil()
     for pos, prob, _, _ in sampler.sample(new_pos, iterations=n_run):
         store.persistSamplingValues(pos, prob, None)
     return store.samples
class TestCosmoHammerSampler(object):
    storageUtil = None
    samples = np.array([[1, 2, 3], [4, 5, 6]])
    prob = np.array([1, 1])

    def setup(self):
        self.storageUtil = InMemoryStorageUtil()

    def test_no_data(self):

        assert self.storageUtil.samplesBurnin == None
        assert self.storageUtil.probBurnin == None

    def test_persistBurninValues(self):
        self.storageUtil.persistBurninValues(self.samples, self.prob, [])

        assert self.storageUtil.samplesBurnin is not None
        assert self.storageUtil.probBurnin is not None
        assert self.storageUtil.samplesBurnin.shape == (2, 3)
        assert self.storageUtil.probBurnin.shape == (2, )

        self.storageUtil.persistBurninValues(self.samples, self.prob, [])

        assert self.storageUtil.samplesBurnin is not None
        assert self.storageUtil.probBurnin is not None
        assert self.storageUtil.samplesBurnin.shape == (4, 3)
        assert self.storageUtil.probBurnin.shape == (4, )

    def test_persistSamplingValues(self):
        self.storageUtil.persistSamplingValues(self.samples, self.prob, [])

        assert self.storageUtil.samples is not None
        assert self.storageUtil.prob is not None
        assert self.storageUtil.samples.shape == (2, 3)
        assert self.storageUtil.prob.shape == (2, )

        self.storageUtil.persistSamplingValues(self.samples, self.prob, [])

        assert self.storageUtil.samples is not None
        assert self.storageUtil.prob is not None
        assert self.storageUtil.samples.shape == (4, 3)
        assert self.storageUtil.prob.shape == (4, )
class TestCosmoHammerSampler(object):
    storageUtil = None
    samples = np.array([[1, 2, 3], [4, 5, 6]])
    prob = np.array([1, 1])

    def setup(self):
        self.storageUtil = InMemoryStorageUtil()

    def test_no_data(self):

        assert self.storageUtil.samplesBurnin == None
        assert self.storageUtil.probBurnin == None

    def test_persistBurninValues(self):
        self.storageUtil.persistBurninValues(self.samples, self.prob, [])

        assert self.storageUtil.samplesBurnin is not None
        assert self.storageUtil.probBurnin is not None
        assert self.storageUtil.samplesBurnin.shape == (2, 3)
        assert self.storageUtil.probBurnin.shape == (2,)

        self.storageUtil.persistBurninValues(self.samples, self.prob, [])

        assert self.storageUtil.samplesBurnin is not None
        assert self.storageUtil.probBurnin is not None
        assert self.storageUtil.samplesBurnin.shape == (4, 3)
        assert self.storageUtil.probBurnin.shape == (4,)

    def test_persistSamplingValues(self):
        self.storageUtil.persistSamplingValues(self.samples, self.prob, [])

        assert self.storageUtil.samples is not None
        assert self.storageUtil.prob is not None
        assert self.storageUtil.samples.shape == (2, 3)
        assert self.storageUtil.prob.shape == (2,)

        self.storageUtil.persistSamplingValues(self.samples, self.prob, [])

        assert self.storageUtil.samples is not None
        assert self.storageUtil.prob is not None
        assert self.storageUtil.samples.shape == (4, 3)
        assert self.storageUtil.prob.shape == (4,)