def main(): m = SimpleMacroModel() prior = UniformDistribution([[0, 1], [0, 1]]) u = SMCUpdater(m, 1000, prior) modelparams = prior.sample() expparams = np.array([(12.0,)], dtype=m.expparams_dtype) datum = m.simulate_experiment(modelparams, expparams) print datum u.update(datum, expparams) print u.est_mean() print m.call_count
def main(): m = SimpleMacroModel() prior = UniformDistribution([[0, 1], [0, 1]]) u = SMCUpdater(m, 1000, prior) modelparams = prior.sample() expparams = np.array([(12.0, )], dtype=m.expparams_dtype) datum = m.simulate_experiment(modelparams, expparams) print datum u.update(datum, expparams) print u.est_mean() print m.call_count
class TestSMCUpdater(DerandomizedTestCase): # True model parameter for test MODELPARAMS = np.array([ 1, ]) TEST_EXPPARAMS = np.linspace(1., 10., 100, dtype=np.float) PRIOR = UniformDistribution([[0, 2]]) N_PARTICLES = 10000 TEST_TARGET_COV = np.array([[0.01]]) def setUp(self): super(TestSMCUpdater, self).setUp() self.precession_model = SimplePrecessionModel() self.num_precession_model = NumericalSimplePrecessionModel() self.expparams = TestSMCUpdater.TEST_EXPPARAMS.reshape(-1, 1) self.outcomes = self.precession_model.simulate_experiment( TestSMCUpdater.MODELPARAMS, TestSMCUpdater.TEST_EXPPARAMS, repeat=1).reshape(-1, 1) self.updater = SMCUpdater(self.precession_model, TestSMCUpdater.N_PARTICLES, TestSMCUpdater.PRIOR) self.updater_bayes = SMCUpdaterBCRB(self.precession_model, TestSMCUpdater.N_PARTICLES, TestSMCUpdater.PRIOR, adaptive=True) self.num_updater = SMCUpdater(self.num_precession_model, TestSMCUpdater.N_PARTICLES, TestSMCUpdater.PRIOR) self.num_updater_bayes = SMCUpdaterBCRB(self.num_precession_model, TestSMCUpdater.N_PARTICLES, TestSMCUpdater.PRIOR, adaptive=True) def test_smc_fitting(self): """ Checks that the fitters converge on true value on simple precession_model. Is a stochastic test but I ran 100 times and there were no fails, with these parameters. """ self.updater.batch_update(self.outcomes, self.expparams) self.updater_bayes.batch_update(self.outcomes, self.expparams) self.num_updater.batch_update(self.outcomes, self.expparams) self.num_updater_bayes.batch_update(self.outcomes, self.expparams) #Assert that models have learned true model parameters from data #test means assert_almost_equal(self.updater.est_mean(), TestSMCUpdater.MODELPARAMS, 2) assert_almost_equal(self.updater_bayes.est_mean(), TestSMCUpdater.MODELPARAMS, 2) assert_almost_equal(self.num_updater.est_mean(), TestSMCUpdater.MODELPARAMS, 2) assert_almost_equal(self.num_updater_bayes.est_mean(), TestSMCUpdater.MODELPARAMS, 2) #Assert that covariances have been reduced below thresholds #test covs assert_array_less(self.updater.est_covariance_mtx(), TestSMCUpdater.TEST_TARGET_COV) assert_array_less(self.updater_bayes.est_covariance_mtx(), TestSMCUpdater.TEST_TARGET_COV) assert_array_less(self.num_updater.est_covariance_mtx(), TestSMCUpdater.TEST_TARGET_COV) assert_array_less(self.num_updater_bayes.est_covariance_mtx(), TestSMCUpdater.TEST_TARGET_COV) def test_bim(self): """ Checks that the fitters converge on true value on simple precession_model. Is a stochastic test but I ran 100 times and there were no fails, with these parameters. """ bim_currents = [] num_bim_currents = [] bim_adaptives = [] num_bim_adaptives = [] #track bims throughout experiments for i in range(self.outcomes.shape[0]): self.updater_bayes.update(self.outcomes[i], self.expparams[i]) self.num_updater_bayes.update(self.outcomes[i], self.expparams[i]) bim_currents.append(self.updater_bayes.current_bim) num_bim_currents.append(self.num_updater_bayes.current_bim) bim_adaptives.append(self.updater_bayes.adaptive_bim) num_bim_adaptives.append(self.num_updater_bayes.adaptive_bim) bim_currents = np.array(bim_currents) num_bim_currents = np.array(num_bim_currents) bim_adaptives = np.array(bim_adaptives) num_bim_adaptives = np.array(num_bim_adaptives) #compare numerical and analytical bims assert_almost_equal(bim_currents, num_bim_currents, 2) assert_almost_equal(bim_adaptives, num_bim_adaptives, 2) #verify that array copying of properties is working assert not np.all(bim_currents == bim_currents[0, ...]) assert not np.all(num_bim_currents == num_bim_currents[0, ...]) assert not np.all(bim_adaptives == bim_adaptives[0, ...]) assert not np.all(num_bim_adaptives == num_bim_adaptives[0, ...]) #verify that BCRB is approximately reached assert_almost_equal(self.updater_bayes.est_covariance_mtx(), np.linalg.inv(self.updater_bayes.current_bim), 2) assert_almost_equal(self.updater_bayes.est_covariance_mtx(), np.linalg.inv(self.updater_bayes.adaptive_bim), 2) assert_almost_equal(self.num_updater_bayes.est_covariance_mtx(), np.linalg.inv(self.updater_bayes.current_bim), 2) assert_almost_equal(self.num_updater_bayes.est_covariance_mtx(), np.linalg.inv(self.updater_bayes.adaptive_bim), 2)
def test_test_model_runs(self): model = MockModel() prior = UniformDistribution(np.array([[10, 12], [2, 3]])) eps = np.arange(10, 20).astype(model.expparams_dtype) test_model(model, prior, eps)
def instantiate_prior(self): return UniformDistribution(np.array([[5, 8]]))
def instantiate_prior(self): return UniformDistribution([[0, 1]] * 2)
def instantiate_prior(self): unif = UniformDistribution( np.array([[0, 1], [0, 1], [0, 1], [0, 1], [0, 1], [0, 1]])) return ConstrainedSumDistribution(unif, desired_total=1)
def instantiate_prior(self): return PostselectedDistribution( UniformDistribution(np.array([[0, 1], [0, 1], [0, 1], [0, 1]])), self.model)
def instantiate_prior(self): die = ConstrainedSumDistribution(UniformDistribution( np.array([[.45, .55]] * 6)), desired_total=1) walk = UniformDistribution([[0, 1]] * 3) return ProductDistribution(die, walk)
prob_measuring_zero = compute_prob_measuring_zero( modelparams, time=expparams["t"] # particles ) likelihood = FiniteOutcomeModel.pr0_to_likelihood_array( outcomes=outcomes, pr0=prob_measuring_zero, ) return likelihood # Make instance of our model and generate an SMCUpdater from it demo_model = DemoModel() uniform_distribution = UniformDistribution(ranges=[0, 1]) updater = SMCUpdater(model=demo_model, n_particles=3, prior=uniform_distribution) # Track particles to inspect before/after update track_particles = [copy(updater.particle_locations)] track_weights = [copy(updater.particle_weights)] # Design experiment experiment = np.empty((1, ), dtype=demo_model.expparams_dtype) experiment["t"] = 7.69 # Update distribution updater.update(outcome=0, expparams=experiment)