Ejemplo n.º 1
0
    def test_paralleltempering_sampledreamfxn(self):
        """test that the parallel tempering DREAM sampling function returns values of the expected shape."""
        self.params, self.like = multidmodel()
        model = Model(self.like, self.params)
        dream = Dream(model=model, verbose=False, save_history=False)

        #The pool will be used within the fxn and, also, we need to initialize the shared variables.
        pool = _setup_mp_dream_pool(nchains=3,
                                    niterations=10,
                                    step_instance=dream)
        pool._initializer(*pool._initargs)

        start = np.array([-100, 5, 8, .001])

        sampled_params, logps = _sample_dream_pt(nchains=3,
                                                 niterations=10,
                                                 step_instance=dream,
                                                 start=start,
                                                 pool=pool,
                                                 verbose=False)

        self.assertEqual(len(sampled_params), 3)
        self.assertEqual(len(sampled_params[0]), 20)
        self.assertEqual(len(sampled_params[0][0]), 4)
        self.assertEqual(len(logps), 3)
        self.assertEqual(len(logps[0]), 20)
        self.assertEqual(len(logps[0][0]), 1)
Ejemplo n.º 2
0
    def test_astep_onedmodel(self):
        """Test that a single step with a one-dimensional model returns values of the expected type and a move that is expected or not given the test logp."""
        """Test a single step with a one-dimensional model with a normal parameter."""
        self.param, self.like = onedmodel()
        model = Model(self.like, self.param)
        dream = Dream(model=model, save_history=False, verbose=False)
        #Even though we're calling the steps separately we need to call these functions
        # to initialize the shared memory arrays that are called in the step fxn
        pool = _setup_mp_dream_pool(nchains=3,
                                    niterations=10,
                                    step_instance=dream)
        pool._initializer(*pool._initargs)

        #Test initial step (when last logp and prior values aren't set)
        q_new, last_prior, last_like = dream.astep(q0=np.array([-5]))

        self.assertTrue(isinstance(q_new, np.ndarray))
        self.assertTrue(isinstance(last_prior, numbers.Number))
        self.assertTrue(isinstance(last_like, numbers.Number))

        #Test later iteration after last logp and last prior have been set
        q_new, last_prior, last_like = dream.astep(q0=np.array(8),
                                                   last_logprior=-300,
                                                   last_loglike=-500)

        self.assertTrue(isinstance(q_new, np.ndarray))
        self.assertTrue(isinstance(last_prior, numbers.Number))
        self.assertTrue(isinstance(last_like, numbers.Number))

        if np.any(q_new != np.array(8)):
            self.assertTrue(last_prior + last_like >= -800)

        else:
            self.assertTrue(last_prior == -300)
            self.assertTrue(last_like == -500)
Ejemplo n.º 3
0
    def test_astep_multidmodel_uniform(self):
        """Test a single step of DREAM with a multi-dimensional model and uniform prior."""

        self.param, self.like = multidmodel_uniform()
        model = Model(self.like, self.param)
        dream = Dream(model=model, save_history=False, verbose=False)

        # Even though we're calling the steps separately we need to call these functions
        # to initialize the shared memory arrays that are called in the step fxn
        pool = _setup_mp_dream_pool(nchains=3, niterations=10, step_instance=dream)
        pool._initializer(*pool._initargs)

        # Test initial step (when last logp and prior values aren't set)
        q_new, last_prior, last_like = dream.astep(q0=np.array([-5, 4, -3, 0]))

        self.assertTrue(isinstance(q_new, np.ndarray))
        self.assertTrue(isinstance(last_prior, numbers.Number))
        self.assertTrue(isinstance(last_like, numbers.Number))

        # Test later iteration after last logp and last prior have been set
        q_new, last_prior, last_like = dream.astep(q0=np.array([8, 4, -2, 9]), last_logprior=100, last_loglike=-600)

        self.assertTrue(isinstance(q_new, np.ndarray))
        self.assertTrue(isinstance(last_prior, numbers.Number))
        self.assertTrue(isinstance(last_like, numbers.Number))

        if np.any(q_new != np.array(8)):
            self.assertTrue(last_prior + last_like >= -500)

        else:
            self.assertTrue(last_prior == 100)
            self.assertTrue(last_like == -600)
Ejemplo n.º 4
0
    def test_mp_paralleltempering_sampledreamfxn(self):
        """Test individual chain sampling function for parallel tempering returns an object of the correct type and with a better logp."""
        self.params, self.like = multidmodel()
        model = Model(self.like, self.params)
        dream = Dream(model=model, verbose=False, save_history=False)

        # Even though the pool won't be used, we need to initialize the shared variables.
        pool = _setup_mp_dream_pool(nchains=3, niterations=10, step_instance=dream)
        pool._initializer(*pool._initargs)

        start = np.array([-.33, 10, 0, 99])
        T = .33
        last_loglike = -300
        last_logprior = -100
        args = [dream, start, T, last_loglike, last_logprior]
        qnew, logprior_new, loglike_new, dream_instance = _sample_dream_pt_chain(args)

        self.assertTrue(isinstance(qnew, np.ndarray))
        self.assertTrue((logprior_new + loglike_new) >= -400)
Ejemplo n.º 5
0
    def test_mp_sampledreamfxn(self):
        """Test the multiprocessing DREAM sample function returns data of the correct shape independently of the run_dream wrapper."""
        self.params, self.like = multidmodel()
        model = Model(self.like, self.params)
        dream = Dream(model=model, verbose=False, save_history=False)

        #Even though the pool won't be used, we need to initialize the shared variables.
        pool = _setup_mp_dream_pool(nchains=3, niterations=10, step_instance=dream)
        pool._initializer(*pool._initargs)

        iterations = 10
        start = np.array([-7, 8, 1.2, 0])
        verbose = False
        nverbose = 10
        args = [dream, iterations, start, verbose, nverbose]
        sampled_params, logps = _sample_dream(args)

        self.assertEqual(len(sampled_params), 10)
        self.assertEqual(len(sampled_params[0]), 4)
        self.assertEqual(len(logps), 10)
        self.assertEqual(len(logps[0]), 1)