Exemple #1
0
    def test_history_correct_after_sampling_simple_model(self):
        """Test that the history saved matches with the returned sampled parameter values for a one-dimensional test model."""
        self.param, self.like = onedmodel()
        model = Model(self.like, self.param)
        step = Dream(model=model,
                     save_history=True,
                     history_thin=1,
                     model_name='test_history_correct',
                     adapt_crossover=False)
        sampled_params, logps = run_dream(self.param,
                                          self.like,
                                          niterations=10,
                                          nchains=5,
                                          save_history=True,
                                          history_thin=1,
                                          model_name='test_history_correct',
                                          adapt_crossover=False,
                                          verbose=False)
        history = np.load('test_history_correct_DREAM_chain_history.npy')
        self.assertEqual(
            len(history),
            step.total_var_dimension *
            ((10 * 5 / step.history_thin) + step.nseedchains))
        history_no_seedchains = history[(step.total_var_dimension *
                                         step.nseedchains)::]
        sorted_history = np.sort(history_no_seedchains)
        sorted_sampled_params = np.sort(np.array(sampled_params).flatten())

        for sampled_param, history_param in zip(sorted_history,
                                                sorted_sampled_params):
            self.assertEqual(sampled_param, history_param)

        remove('test_history_correct_DREAM_chain_history.npy')
        remove('test_history_correct_DREAM_chain_adapted_crossoverprob.npy')
        remove('test_history_correct_DREAM_chain_adapted_gammalevelprob.npy')
Exemple #2
0
 def test_history_recording_simple_model(self):
     """Test that history in memory matches with that recorded for test one-dimensional model."""
     self.param, self.like = onedmodel()
     model = Model(self.like, self.param)
     step = Dream(model=model, model_name='test_history_recording')
     history_arr = mp.Array('d', [0] * 4 * step.total_var_dimension)
     n = mp.Value('i', 0)
     nchains = mp.Value('i', 3)
     pydream.Dream_shared_vars.history = history_arr
     pydream.Dream_shared_vars.count = n
     pydream.Dream_shared_vars.nchains = nchains
     test_history = np.array([[1], [3], [5], [7]])
     for chainpoint in test_history:
         for point in chainpoint:
             step.record_history(nseedchains=0,
                                 ndimensions=step.total_var_dimension,
                                 q_new=point,
                                 len_history=len(history_arr))
     history_arr_np = np.frombuffer(
         pydream.Dream_shared_vars.history.get_obj())
     history_arr_np_reshaped = history_arr_np.reshape(
         np.shape(test_history))
     self.assertIs(np.array_equal(history_arr_np_reshaped, test_history),
                   True)
     remove('test_history_recording_DREAM_chain_history.npy')
     remove('test_history_recording_DREAM_chain_adapted_crossoverprob.npy')
     remove('test_history_recording_DREAM_chain_adapted_gammalevelprob.npy')
Exemple #3
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)
Exemple #4
0
 def test_DEpair_selec(self):
     """Test that fraction for selected DEpair value is consistent with number of specified DEPair value."""
     self.param, self.like = onedmodel()
     single_DEpair = np.array([1])
     multi_DEpair = np.array([1, 2, 3])
     nDE1 = 0
     nDE2 = 0
     nDE3 = 0
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     step = Dream(model=model, variables=self.param)
     self.assertEqual(step.set_DEpair(single_DEpair), 1)
     for iteration in range(10000):
         choice = step.set_DEpair(multi_DEpair)
         if choice == multi_DEpair[0]:
             nDE1 += 1
         elif choice == multi_DEpair[1]:
             nDE2 += 1
         else:
             nDE3 += 1
     emp_frac1 = nDE1 / 10000.0
     emp_frac2 = nDE2 / 10000.0
     emp_frac3 = nDE3 / 10000.0
     self.assertAlmostEqual(emp_frac1, .3, places=1)
     self.assertAlmostEqual(emp_frac2, .3, places=1)
     self.assertAlmostEqual(emp_frac3, .3, places=1)
Exemple #5
0
 def test_chain_sampling_simple_model(self):
     """Test that sampling from DREAM history for one dimensional model when the history is known matches with expected possible samples."""
     self.param, self.like = onedmodel()
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     dream = Dream(model=model)
     history_arr = mp.Array('d', [0] * 2 * dream.total_var_dimension)
     n = mp.Value('i', 0)
     pydream.Dream_shared_vars.history = history_arr
     pydream.Dream_shared_vars.count = n
     chains_added_to_history = []
     for i in range(2):
         start = i * dream.total_var_dimension
         end = start + dream.total_var_dimension
         chain = dream.draw_from_prior(dream.variables)
         pydream.Dream_shared_vars.history[start:end] = chain
         chains_added_to_history.append(chain)
     sampled_chains = dream.sample_from_history(
         nseedchains=2, DEpairs=1, ndimensions=dream.total_var_dimension)
     sampled_chains = np.array(sampled_chains)
     chains_added_to_history = np.array(chains_added_to_history)
     self.assertIs(
         np.array_equal(
             chains_added_to_history[chains_added_to_history[:,
                                                             0].argsort()],
             sampled_chains[sampled_chains[:, 0].argsort()]), True)
Exemple #6
0
 def test_gamma_snooker_choice(self):
     """Test that when a snooker move is made, gamma is set to a random value between 1.2 and 2.2."""
     self.param, self.like = onedmodel()
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     step = Dream(model=model)
     self.assertGreaterEqual(step.set_gamma(DEpairs=1, snooker_choice=True, gamma_level_choice=1, d_prime=3), 1.2)
     self.assertLess(step.set_gamma(DEpairs=1, snooker_choice=True, gamma_level_choice=1, d_prime=3), 2.2)
Exemple #7
0
    def test_history_file_loading(self):
        """Test that when a history file is provided it is loaded and appended to the new history."""
        self.param, self.like = onedmodel()
        model = Model(self.like, self.param)
        step = Dream(model=model)
        old_history = np.array([1, 3, 5, 7, 9, 11])
        step.save_history_to_disc(old_history, 'testing_history_load_')
        sampled_params, logps = run_dream(self.param, self.like, niterations=3, nchains=3, history_thin=1, history_file='testing_history_load_DREAM_chain_history.npy', save_history=True, model_name='test_history_loading', verbose=False)
        new_history = np.load('test_history_loading_DREAM_chain_history.npy')
        self.assertEqual(len(new_history), (len(old_history.flatten())+(3*step.total_var_dimension*3)))
        new_history_seed = new_history[:len(old_history.flatten())]
        new_history_seed_reshaped = new_history_seed.reshape(old_history.shape)
        self.assertIs(np.array_equal(old_history, new_history_seed_reshaped), True)
        
        added_history = new_history[len(old_history.flatten())::]
        sorted_history = np.sort(added_history)
        sorted_sampled_params = np.sort(np.array(sampled_params).flatten())

        for sampled_param, history_param in zip(sorted_history, sorted_sampled_params):
            self.assertEqual(sampled_param, history_param)
        remove('testing_history_load_DREAM_chain_history.npy')
        remove('testing_history_load_DREAM_chain_adapted_crossoverprob.npy')
        remove('testing_history_load_DREAM_chain_adapted_gammalevelprob.npy')
        remove('test_history_loading_DREAM_chain_adapted_crossoverprob.npy')
        remove('test_history_loading_DREAM_chain_adapted_gammalevelprob.npy')
        remove('test_history_loading_DREAM_chain_history.npy')
Exemple #8
0
 def test_prior_draw(self):
     """Test random draw from prior for normally distributed priors in test models."""
     self.param, self.like = onedmodel()
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     self.assertEqual(len(Dream(model=model).draw_from_prior(model.sampled_parameters)), 1)
     self.param, self.like = multidmodel()
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     self.assertEqual(len(Dream(model=model).draw_from_prior(model.sampled_parameters)), 4)
Exemple #9
0
 def test_gamma_array(self):
     """Test assigned value of gamma array matches for test data."""
     true_gamma_array = np.array([[1.683, 1.19, .972, .841, .753]])
     self.param, self.like = onedmodel()
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     dream = Dream(model=model, DEpairs=5, p_gamma_unity=0)
     for d_prime in range(1, dream.total_var_dimension+1):
         for n_DEpair in range(1, 6):
             self.assertAlmostEqual(true_gamma_array[d_prime-1][n_DEpair-1], dream.set_gamma(DEpairs=n_DEpair, snooker_choice=False, gamma_level_choice=1, d_prime=d_prime), places=3)
Exemple #10
0
    def test_total_var_dimension_init(self):
        """Test that DREAM correctly identifies the total number of dimensions in all sampled parameters for a few test cases."""
        self.param, self.like = onedmodel()
        model = Model(likelihood=self.like, sampled_parameters=self.param)
        step = Dream(model=model, variables=self.param)
        self.assertEqual(step.total_var_dimension, 1)

        self.param, self.like = multidmodel()
        model = Model(likelihood=self.like, sampled_parameters=self.param)
        step = Dream(model=model, variables=self.param)
        self.assertEqual(step.total_var_dimension, 4)
Exemple #11
0
 def test_fail_with_one_chain(self):
     """Test that DREAM fails if run with only one chain."""
     self.param, self.like = onedmodel()
     assertRaisesRegex = self.assertRaisesRegexp if sys.version_info[
         0] < 3 else self.assertRaisesRegex
     assertRaisesRegex(Exception,
                       'Dream should be run with at least ',
                       run_dream,
                       self.param,
                       self.like,
                       nchains=1)
Exemple #12
0
 def test_snooker_fraction(self):
     """Test that the fraction of snooker moves corresponds to the snooker parameter."""
     self.param, self.like = onedmodel()
     n_snooker_choices = 0
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     step = Dream(model=model)
     fraction = step.snooker
     for iteration in range(10000):
         choice = step.set_snooker()
         if choice == True:
             n_snooker_choices += 1
     emp_frac = n_snooker_choices / 10000.0
     self.assertAlmostEqual(emp_frac, fraction, places=1)
Exemple #13
0
 def test_gamma_unityfraction(self):
     """Test that gamma value is set to 1 the fraction of times indicated by the p_gamma_unity DREAM parameter."""
     self.param, self.like = onedmodel()
     n_unity_choices = 0
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     step = Dream(model=model)
     fraction = step.p_gamma_unity
     for iteration in range(10000):
        choice = step.set_gamma(DEpairs=1, snooker_choice=False, gamma_level_choice=1, d_prime=step.total_var_dimension)
        if choice == 1:
            n_unity_choices += 1
     emp_frac = n_unity_choices/10000.0
     self.assertAlmostEqual(emp_frac, fraction, places=1)
Exemple #14
0
 def test_CR_fraction(self):
     """Test that the crossover values chosen match with the crossover probability values for test data."""
     self.param, self.like = onedmodel()
     nCR1 = 0
     nCR2 = 0
     nCR3 = 0
     crossoverprobs = np.array([.10, .65, .25])
     crossovervals = np.array([.33, .66, 1.0])
     model = Model(likelihood=self.like, sampled_parameters=self.param)
     step = Dream(model=model, variables=self.param)
     for iteration in range(10000):
         choice = step.set_CR(crossoverprobs, crossovervals)
         if choice == crossovervals[0]:
             nCR1 += 1
         elif choice == crossovervals[1]:
             nCR2 += 1
         else:
             nCR3 += 1
     emp_frac1 = nCR1 / 10000.0
     emp_frac2 = nCR2 / 10000.0
     emp_frac3 = nCR3 / 10000.0
     self.assertAlmostEqual(emp_frac1, crossoverprobs[0], places=1)
     self.assertAlmostEqual(emp_frac2, crossoverprobs[1], places=1)
     self.assertAlmostEqual(emp_frac3, crossoverprobs[2], places=1)