def test_batch_constrain(self): # Test that batch constrain returns the correct boolean array for # the given model, obs and params obs_uncertainty = self.training_ensemble.data.std(axis=0) # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, obs_uncertainty=obs_uncertainty/obs.data, interann_uncertainty=0., repres_uncertainty=0., struct_uncertainty=0.) # Calculate the implausbility of the training points from a perturbed # training point. The emulator variance should be zero making testing # easier. valid_samples = sampler.batch_constrain(self.training_params, tolerance=0., threshold=2.) # The implausibility for the 10th sample (the one we perturbed around) # should be around one (and hence valid), some neighbouring points are # also valid, the rest should be invalid expected = np.asarray([True, False, False, True, False, True, False, False, True, False, True, False, False, True, True, True, True, True, True, True, False, False, False, False, False]) assert_array_equal(valid_samples, expected)
def test_implausibility_scalar_uncertainty_interface(self): # Test the interface correctly deals with 2d model and obs obs_uncertainty = 5. # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, obs_uncertainty=obs_uncertainty/obs.data.mean(), interann_uncertainty=0., repres_uncertainty=0., struct_uncertainty=0.) implausibility = sampler.get_implausibility(self.training_params) expected_shape = (len(self.training_params), ) + obs.shape assert implausibility.shape == expected_shape
def test_sample(self): # Test that batch constrain returns the correct boolean array for # the given model, obs and params obs_uncertainty = self.training_ensemble.data.std(axis=0) # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, obs_uncertainty=obs_uncertainty/obs.data, interann_uncertainty=0., repres_uncertainty=0., struct_uncertainty=0.) # Generate only valid samples valid_samples = sampler.sample(n_samples=100, tolerance=0., threshold=2.) assert valid_samples.shape == (100, 3)
def test_implausibility_repres(self): # Test the implausibility is correct obs_uncertainty = 5. # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, obs_uncertainty=0., interann_uncertainty=0., repres_uncertainty=obs_uncertainty/obs.data.mean(), struct_uncertainty=0.) # Calculate the implausbility of the training points from a perturbed # training point. The emulator variance should be zero making testing # easier. implausibility = sampler.get_implausibility(self.training_params) # The implausibility for the 10th sample (the one we perturbed around) # should be one - on average assert_allclose(implausibility.data[10, :].mean(), 1., rtol=1e-2)
def test_implausibility_vector_uncertainty(self): # Test with a vector obs uncertainty obs_uncertainty = self.training_ensemble.data.std(axis=0) # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, obs_uncertainty=obs_uncertainty/obs.data, interann_uncertainty=0., repres_uncertainty=0., struct_uncertainty=0.) # Calculate the implausbility of the training points from a perturbed # training point. The emulator variance should be zero making testing # easier. implausibility = sampler.get_implausibility(self.training_params) expected_shape = (len(self.training_params),) + obs.shape assert implausibility.shape == expected_shape
def test_struct_uncertainty_specified_twice(self): # Test with a vector obs uncertainty obs_uncertainty = self.training_ensemble.data.std(axis=0) # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty with pytest.raises(ValueError): sampler = ABCSampler(self.m, obs, struct_uncertainty=obs_uncertainty, abs_struct_uncertainty=obs_uncertainty)
def test_batch_constrain(self): # Test that batch constrain returns the correct boolean array for # the given model, obs and params obs_uncertainty = self.training_ensemble.data.std(axis=0) # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, obs_uncertainty=obs_uncertainty/obs.data, interann_uncertainty=0., repres_uncertainty=0., struct_uncertainty=0.) # Calculate the implausbility of the training points from a perturbed # training point. The emulator variance should be zero making testing # easier. valid_samples = sampler.batch_constrain(self.training_params, tolerance=0., threshold=2.) expected_shape = (len(self.training_params),) assert valid_samples.shape == expected_shape
def test_absolute_struct_uncertainty(self): # Test with a vector obs uncertainty obs_uncertainty = self.training_ensemble.data.std(axis=0) # Perturbing the obs by one sd should lead to an implausibility of 1. obs = self.training_ensemble[10].copy() + obs_uncertainty sampler = ABCSampler(self.m, obs, abs_obs_uncertainty=0., interann_uncertainty=0., repres_uncertainty=0., abs_struct_uncertainty=obs_uncertainty) # Calculate the implausbility of the training points from a perturbed # training point. The emulator variance should be zero making testing # easier. implausibility = sampler.get_implausibility(self.training_params) # The implausibility for the 10th sample (the one we perturbed around) # should be one. expected = np.ones((100,)) # The first element has zero variance expected[0] = 0. assert_allclose(implausibility.data[10, :], expected, rtol=1e-1)