Exemplo n.º 1
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)
Exemplo n.º 2
0
    def test_boundaries_obeyed_aftersampling(self):
        """Test that boundaries are respected if included."""

        self.param, self.like = multidmodel_uniform()
        model = Model(self.like, self.param)
        step = Dream(model=model,
                     save_history=True,
                     history_thin=1,
                     model_name='test_boundaries',
                     adapt_crossover=False,
                     hardboundaries=True,
                     nverbose=10)
        sampled_params, logps = run_dream(self.param,
                                          self.like,
                                          niterations=1000,
                                          nchains=5,
                                          save_history=True,
                                          history_thin=1,
                                          model_name='test_boundaries',
                                          adapt_crossover=False,
                                          verbose=True,
                                          hardboundaries=True,
                                          nverbose=10)
        history = np.load('test_boundaries_DREAM_chain_history.npy')
        variables = model.sampled_parameters
        dim = 0
        for var in variables:
            interval = var.interval()
            dim += var.dsize

        lowerbound = interval[0]
        upperbound = interval[1]

        npoints = int(len(history) / float(dim))
        reshaped_history = np.reshape(history, (npoints, dim))

        print('reshaped history: ', reshaped_history)
        print('upper ', upperbound, ' and lower ', lowerbound)
        print('lower bounds: ', (reshaped_history < lowerbound).any())
        print('upper bounds: ', (reshaped_history > upperbound).any())
        print('disobeyed lower: ',
              reshaped_history[reshaped_history < lowerbound])
        print('disobeyed upper: ',
              reshaped_history[reshaped_history > upperbound])

        self.assertFalse((reshaped_history < lowerbound).any())
        self.assertFalse((reshaped_history > upperbound).any())

        remove('test_boundaries_DREAM_chain_adapted_crossoverprob.npy')
        remove('test_boundaries_DREAM_chain_adapted_gammalevelprob.npy')
        remove('test_boundaries_DREAM_chain_history.npy')