def test_region_est_ellipsoid(self):
        """
        Tests that region_est_ellipsoid works.
        """

        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(4), self.N_PARTICLES, dist)

        # ask for a confidence level of 0.5
        A, c = u.region_est_ellipsoid(level=0.5)

        # center of ellipse should be the mean of the multinormal
        assert_almost_equal(np.round(c), self.MEAN, 1)

        # finally, the principal lengths of the ellipsoid
        # should be the same as COV
        _, QA, _ = np.linalg.svd(A)
        _, QC, _ = np.linalg.svd(self.COV)
        QA, QC = np.sqrt(QA), np.sqrt(QC)
        assert_almost_equal(
            QA / np.linalg.norm(QA),
            QC / np.linalg.norm(QC),
            1
        )
    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 perf_test(
        model, n_particles, prior,
        n_exp, heuristic_class,
        true_model=None, true_prior=None
    ):
    """
    Runs a trial of using SMC to estimate the parameters of a model, given a
    number of particles, a prior distribution and an experiment design
    heuristic.

    :param qinfer.Model model: Model whose parameters are to
        be estimated.
    :param int n_particles: Number of SMC particles to use.
    :param qinfer.Distribution prior: Prior to use in selecting
        SMC particles.
    :param int n_exp: Number of experimental data points to draw from the
        model.
    :param qinfer.Heuristic heuristic_class: Constructor function
        for the experiment design heuristic to be used.
    :param qinfer.Model true_model: Model to be used in
        generating experimental data. If ``None``, assumed to be ``model``.
    :param qinfer.Distribution true_prior: Prior to be used in
        selecting the true model parameters. If ``None``, assumed to be
        ``prior``.
    :rtype np.ndarray: See :ref:`perf_testing_struct` for more details on 
        the type returned by this function.
    :return: A record array of performance metrics, indexed by the number
        of experiments performed.
    """

    if true_model is None:
        true_model = model

    if true_prior is None:
        true_prior = prior

    true_mps = true_prior.sample()

    performance = np.zeros((n_exp,), dtype=PERFORMANCE_DTYPE)

    updater = SMCUpdater(model, n_particles, prior)
    heuristic = heuristic_class(updater)

    for idx_exp in xrange(n_exp):
        expparams = heuristic()
        datum = true_model.simulate_experiment(true_mps, expparams)

        with timing() as t:
            updater.update(datum, expparams)

        delta = updater.est_mean() - true_mps

        performance[idx_exp]['elapsed_time'] = t.delta_t
        performance[idx_exp]['loss'] = np.dot(model.Q, delta**2)
        performance[idx_exp]['resample_count'] = updater.resample_count

    return performance
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 setUp(self):

        super(TestBayesRisk, self).setUp()

        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = BinomialModel(self.coin_model)

        # Set up updaters for these models using particle approximations
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                                           TestBayesRisk.N_PARTICLES,
                                           TestBayesRisk.PRIOR_BETA)
	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 do_update(model, n_particles, prior, outcomes, expparams, return_all, resampler=None):
    updater = SMCUpdater(model, n_particles, prior,
        resampler=resampler
    )
    updater.batch_update(outcomes, expparams, resample_interval=1)

    mean = updater.est_mean()
    cov = updater.est_covariance_mtx()

    if model.n_modelparams == 1:
        mean = mean[0]
        cov = cov[0, 0]

    if not return_all:
        return mean, cov
    else:
        return mean, cov, {
            'updater': updater
        }
    def test_region_est_hull(self):
        """
        Tests that test_region_est_hull works
        """
        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(self.N_MPS), self.N_PARTICLES, dist)

        faces, vertices = u.region_est_hull(level=0.95)

        # In this multinormal case, the convex hull surface
        # should be centered at MEAN
        assert_almost_equal(np.round(np.mean(vertices, axis=0)),
                            np.round(self.MEAN))

        # And a lower level should result in a smaller hull
        # and therefore smaller sample variance
        faces2, vertices2 = u.region_est_hull(level=0.2)
        assert_array_less(np.var(vertices2, axis=0), np.var(vertices, axis=0))
Beispiel #9
0
    def setUp(self):

        super(TestInformationGain,self).setUp()
        
        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = BinomialModel(self.coin_model)
        
        # Set up updaters for these models using particle approximations 
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                TestInformationGain.N_PARTICLES,TestInformationGain.PRIOR_BETA)
    def test_region_est_hull(self):
        """
        Tests that test_region_est_hull works
        """
        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(self.N_MPS), self.N_PARTICLES, dist)

        faces, vertices = u.region_est_hull(level=0.95)

        # In this multinormal case, the convex hull surface
        # should be centered at MEAN
        assert_almost_equal(
            np.round(np.mean(vertices, axis=0)),
            np.round(self.MEAN)
        )

        # And a lower level should result in a smaller hull
        # and therefore smaller sample variance
        faces2, vertices2 = u.region_est_hull(level=0.2)
        assert_array_less(np.var(vertices2, axis=0), np.var(vertices, axis=0))
Beispiel #11
0
    def setUp(self):

        super(TestFisherInformation, self).setUp()

        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = DifferentiableBinomialModel(self.coin_model)

        # Set up updaters for these models using particle approximations
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                                           TestFisherInformation.N_PARTICLES,
                                           TestFisherInformation.PRIOR_BETA)
    def test_est_credible_region(self):
        """
        Tests that est_credible_region doesn't fail miserably
        """
        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(self.N_MPS), self.N_PARTICLES, dist)

        # first check that 0.95 confidence points consume 0.9 confidence points
        points1 = u.est_credible_region(level=0.95)
        points2 = u.est_credible_region(level=0.9)
        assert_almost_equal(
            np.sort(unique_rows(np.concatenate([points1, points2])), axis=0),
            np.sort(points1, axis=0)
        )

        # do the same thing with different slice
        points1 = u.est_credible_region(level=0.95, modelparam_slice=self.SLICE)
        points2 = u.est_credible_region(level=0.9, modelparam_slice=self.SLICE)
        assert_almost_equal(
            np.sort(unique_rows(np.concatenate([points1, points2])), axis=0),
            np.sort(points1, axis=0)
        )
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
Beispiel #14
0
class TestInformationGain(DerandomizedTestCase):
    # Test the implementation of numerical information gain by comparing to
    # numbers which were derived by doing analytic/numeric
    # integrals of simple models (binomialm, poisson, and gaussian) in
    # Mathematica. This test trusts that these calculations
    # were done correctly.

    ALPHA = 1
    BETA = 3
    PRIOR_BETA = BetaDistribution(alpha=ALPHA, beta=BETA)
    N_PARTICLES = 10000
    # Calculated in Mathematica, IG for the binomial model and the given expparams
    NMEAS_EXPPARAMS = np.arange(1, 11, dtype=int)
    BINOM_IG = np.array([
        0.104002, 0.189223, 0.261496, 0.324283, 0.379815, 0.429613, 0.474764,
        0.516069, 0.554138, 0.589446
    ])

    def setUp(self):

        super(TestInformationGain, self).setUp()

        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = BinomialModel(self.coin_model)

        # Set up updaters for these models using particle approximations
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                                           TestInformationGain.N_PARTICLES,
                                           TestInformationGain.PRIOR_BETA)

    def test_finite_outcomes_ig(self):
        # The binomial model has a finite number of outcomes. Test the
        # ig calculation in this case.

        expparams = self.NMEAS_EXPPARAMS.astype(
            self.binomial_model.expparams_dtype)

        # estimate the information gain
        est_ig = self.updater_binomial.expected_information_gain(expparams)

        # see if they roughly match
        assert_almost_equal(est_ig, TestInformationGain.BINOM_IG, decimal=2)
Beispiel #15
0
class TestBayesRisk(DerandomizedTestCase):
    # Test the implementation of numerical Bayes Risk by comparing to
    # numbers which were derived by doing analytic/numeric
    # integrals of simple models in Mathematica. This test trusts that
    # these calculations were done correctly.

    ALPHA = 1.
    BETA = 3.
    PRIOR_BETA = BetaDistribution(alpha=ALPHA, beta=BETA)
    N_PARTICLES = 10000
    NMEAS_EXPPARAMS = np.arange(1, 11, dtype=int)

    def setUp(self):

        super(TestBayesRisk, self).setUp()

        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = BinomialModel(self.coin_model)

        # Set up updaters for these models using particle approximations
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                                           TestBayesRisk.N_PARTICLES,
                                           TestBayesRisk.PRIOR_BETA)

    def test_finite_outcomes_risk(self):
        # The binomial model has a finite number of outcomes. Test the
        # risk calculation in this case.

        expparams = self.NMEAS_EXPPARAMS.astype(
            self.binomial_model.expparams_dtype)

        # estimate the risk
        est_risk = self.updater_binomial.bayes_risk(expparams)

        # compute exact risk
        a, b = TestBayesRisk.ALPHA, TestBayesRisk.BETA
        exact_risk = a * b / ((a + b) * (a + b + 1) *
                              (a + b + expparams['n_meas']))

        # see if they roughly match
        assert_almost_equal(est_risk, exact_risk, decimal=3)
Beispiel #16
0
class TestInformationGain(DerandomizedTestCase):
    # Test the implementation of numerical information gain by comparing to 
    # numbers which were derived by doing analytic/numeric
    # integrals of simple models (binomialm, poisson, and gaussian) in 
    # Mathematica. This test trusts that these calculations
    # were done correctly.

    ALPHA = 1
    BETA = 3
    PRIOR_BETA = BetaDistribution(alpha=ALPHA, beta=BETA)
    N_PARTICLES = 10000
    # Calculated in Mathematica, IG for the binomial model and the given expparams
    NMEAS_EXPPARAMS = np.arange(1, 11, dtype=int)
    BINOM_IG = np.array([0.104002,0.189223,0.261496,0.324283,0.379815,0.429613,0.474764,0.516069,0.554138,0.589446])
    
    def setUp(self):

        super(TestInformationGain,self).setUp()
        
        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = BinomialModel(self.coin_model)
        
        # Set up updaters for these models using particle approximations 
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                TestInformationGain.N_PARTICLES,TestInformationGain.PRIOR_BETA)


    def test_finite_outcomes_ig(self):
        # The binomial model has a finite number of outcomes. Test the 
        # ig calculation in this case.

        expparams = self.NMEAS_EXPPARAMS.astype(self.binomial_model.expparams_dtype)

        # estimate the information gain
        est_ig = self.updater_binomial.expected_information_gain(expparams)

        # see if they roughly match
        assert_almost_equal(est_ig, TestInformationGain.BINOM_IG, decimal=2)
Beispiel #17
0
class TestBayesRisk(DerandomizedTestCase):
    # Test the implementation of numerical Bayes Risk by comparing to 
    # numbers which were derived by doing analytic/numeric
    # integrals of simple models in Mathematica. This test trusts that 
    # these calculations were done correctly.

    ALPHA = 1.
    BETA = 3.
    PRIOR_BETA = BetaDistribution(alpha=ALPHA, beta=BETA)
    N_PARTICLES = 10000
    NMEAS_EXPPARAMS = np.arange(1, 11, dtype=int)
    
    def setUp(self):

        super(TestBayesRisk,self).setUp()
        
        # Set up relevant models.
        self.coin_model = CoinModel()
        self.binomial_model = BinomialModel(self.coin_model)

        # Set up updaters for these models using particle approximations 
        # of conjugate priors
        self.updater_binomial = SMCUpdater(self.binomial_model,
                TestBayesRisk.N_PARTICLES,TestBayesRisk.PRIOR_BETA)

    def test_finite_outcomes_risk(self):
        # The binomial model has a finite number of outcomes. Test the 
        # risk calculation in this case.

        expparams = self.NMEAS_EXPPARAMS.astype(self.binomial_model.expparams_dtype)

        # estimate the risk
        est_risk = self.updater_binomial.bayes_risk(expparams)

        # compute exact risk
        a, b = TestBayesRisk.ALPHA, TestBayesRisk.BETA
        exact_risk = a * b / ((a + b) * (a + b + 1) * (a + b + expparams['n_meas']))

        # see if they roughly match
        assert_almost_equal(est_risk, exact_risk, decimal=3)
Beispiel #18
0
def do_update(model, n_particles, prior, outcomes, expparams, return_all, resampler=None):
    updater = SMCUpdater(model, n_particles, prior,
        resampler=resampler
    )
    updater.batch_update(outcomes, expparams, resample_interval=1)

    mean = updater.est_mean()
    cov = updater.est_covariance_mtx()

    if model.n_modelparams == 1:
        mean = mean[0]
        cov = cov[0, 0]

    if not return_all:
        return mean, cov
    else:
        return mean, cov, {
            'updater': updater
        }
    def test_est_credible_region(self):
        """
        Tests that est_credible_region doesn't fail miserably
        """
        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(self.N_MPS), self.N_PARTICLES, dist)

        # first check that 0.95 confidence points consume 0.9 confidence points
        points1 = u.est_credible_region(level=0.95)
        points2 = u.est_credible_region(level=0.9)
        assert_almost_equal(
            np.sort(unique_rows(np.concatenate([points1, points2])), axis=0),
            np.sort(points1, axis=0))

        # do the same thing with different slice
        points1 = u.est_credible_region(level=0.95,
                                        modelparam_slice=self.SLICE)
        points2 = u.est_credible_region(level=0.9, modelparam_slice=self.SLICE)
        assert_almost_equal(
            np.sort(unique_rows(np.concatenate([points1, points2])), axis=0),
            np.sort(points1, axis=0))
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)
Beispiel #21
0
        mps_buf.release()
        eps_buf.release()
        dest_buf.release()

        # Now we concatenate over outcomes.
        return FiniteOutcomeModel.pr0_to_likelihood_array(outcomes, pr0)


## SCRIPT ######################################################################

if __name__ == "__main__":
    # NOTE: This is now redundant with the perf_testing module.

    simple_model = SimplePrecessionModel()

    for model in [AcceleratedPrecessionModel(), SimplePrecessionModel()]:

        true = np.random.random(1)
        updater = SMCUpdater(model, 100000, UniformDistribution([0, 1]))

        tic = time.time()

        for idx_exp in range(200):
            if not (idx_exp % 20):
                print(idx_exp)
            expparams = np.array([(9 / 8)**idx_exp])
            updater.update(simple_model.simulate_experiment(true, expparams),
                           expparams)

        print(model, updater.est_mean(), true, time.time() - tic)
    # Model and prior initialization.
    prior = distributions.HilbertSchmidtUniform()
    model = QubitStatePauliModel()
    expparams = np.array(
        [
            ([1, 0, 0], 1),  # Records are indicated by tuples.
            ([0, 1, 0], 1),
            ([0, 0, 1], 1)
        ],
        dtype=model.expparams_dtype)

    # Make a dict of updater constructors. This will define what kinds
    # of perfomance data we care about.
    updater_ctors = dict()
    if do_smc:
        updater_ctors['SMC'] = lambda: SMCUpdater(model, n_particles, prior)
    if do_ale:
        ale_model = ale.ALEApproximateModel(model,
                                            error_tol=err_tol,
                                            est_hedge=hedge,
                                            adapt_hedge=hedge)
        updater_ctors['SMC_ALE'] = lambda: SMCUpdater(ale_model, n_particles,
                                                      prior)

    # Make a dtype for holding performance data in a record array.
    # Note that we could do this with out record arrays, but it's easy to
    # use field names this way.
    performance_dtype = [
        ('est_mean', 'f8'),
        ('est_cov_mat', 'f8'),
        ('true_err', 'f8'),
Beispiel #23
0
 def _mk_updater(self, n_particles, **kwargs):
     return SMCUpdater(self.model, n_particles, UniformDistribution([0, 1]), **kwargs)
from qinfer.distributions import UniformDistribution
from qinfer.smc import SMCUpdater
from qinfer.resamplers import LiuWestResampler
import numpy as np
import matplotlib.pyplot as plt
from qinfer.expdesign import ExperimentDesigner
import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

model = T1Model()
prior = UniformDistribution(np.array([0, 10]))
N_particles = 1000000
updater = SMCUpdater(
        model, N_particles, prior, resampler=LiuWestResampler(),
        zero_weight_policy='reset'
)
designer = ExperimentDesigner(updater, opt_algo=1)

# Set the value of T1 to Learn, pick 1 value from prior
true_model = prior.sample()
# true_model=np.array([11.032], dtype=model.expparams_dtype)

performance_dtype = [
    ('expparams', 'float'),
    ('sim_outcome', 'float'),
    ('est_mean', 'float'),
]

# NMR EXPERIMENT Initialization*******************************
# going to normalize Mo max of 1.
Beispiel #25
0
        # Copy the buffer back from the GPU and free memory there.
        cl.enqueue_copy(self._queue, pr0, dest_buf)
        mps_buf.release()
        eps_buf.release()
        dest_buf.release()
        
        # Now we concatenate over outcomes.
        return FiniteOutcomeModel.pr0_to_likelihood_array(outcomes, pr0)

## SCRIPT ######################################################################

if __name__ == "__main__":
    # NOTE: This is now redundant with the perf_testing module.

    simple_model = SimplePrecessionModel()

    for model in [AcceleratedPrecessionModel(), SimplePrecessionModel()]:
        
        true = np.random.random(1)
        updater = SMCUpdater(model, 100000, UniformDistribution([0, 1]))
        
        tic = time.time()
        
        for idx_exp in range(200):
            if not (idx_exp % 20):
                print(idx_exp)
            expparams = np.array([(9 / 8) ** idx_exp])
            updater.update(simple_model.simulate_experiment(true, expparams), expparams)
            
        print(model, updater.est_mean(), true, time.time() - tic)
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)
Beispiel #27
0
def perf_test(
        model, n_particles, prior, n_exp, heuristic_class,
        true_model=None, true_prior=None, true_mps=None,
        extra_updater_args=None
    ):
    """
    Runs a trial of using SMC to estimate the parameters of a model, given a
    number of particles, a prior distribution and an experiment design
    heuristic.

    :param qinfer.Model model: Model whose parameters are to
        be estimated.
    :param int n_particles: Number of SMC particles to use.
    :param qinfer.Distribution prior: Prior to use in selecting
        SMC particles.
    :param int n_exp: Number of experimental data points to draw from the
        model.
    :param qinfer.Heuristic heuristic_class: Constructor function
        for the experiment design heuristic to be used.
    :param qinfer.Model true_model: Model to be used in
        generating experimental data. If ``None``, assumed to be ``model``.
    :param qinfer.Distribution true_prior: Prior to be used in
        selecting the true model parameters. If ``None``, assumed to be
        ``prior``.
    :param np.ndarray true_mps: The true model parameters. If ``None``,
        it will be sampled from ``true_prior``. Note that the performance
        record can only handle one outcome and therefore ONLY ONE TRUE MODEL.
        An error will occur if ``true_mps.shape[0] > 1`` returns ``True``.
    :param dict extra_updater_args: Extra keyword arguments for the updater,
        such as resampling and zero-weight policies.
    :rtype np.ndarray: See :ref:`perf_testing_struct` for more details on 
        the type returned by this function.
    :return: A record array of performance metrics, indexed by the number
        of experiments performed.
    """

    if true_model is None:
        true_model = model

    if true_prior is None:
        true_prior = prior

    if true_mps is None:
        true_mps = true_prior.sample()

    if extra_updater_args is None:
        extra_updater_args = {}

    dtype, is_scalar_exp = actual_dtype(model)
    performance = np.zeros((n_exp,), dtype=dtype)

    updater = SMCUpdater(model, n_particles, prior, **extra_updater_args)
    heuristic = heuristic_class(updater)

    performance['true'] = true_mps

    for idx_exp in xrange(n_exp):
        expparams = heuristic()
        datum = true_model.simulate_experiment(true_mps, expparams)

        with timing() as t:
            updater.update(datum, expparams)

        est_mean = updater.est_mean()
        delta = est_mean - true_mps
        loss = np.dot(delta**2, model.Q)

        performance[idx_exp]['elapsed_time'] = t.delta_t
        performance[idx_exp]['loss'] = loss
        performance[idx_exp]['resample_count'] = updater.resample_count
        performance[idx_exp]['outcome'] = datum
        performance[idx_exp]['est'] = est_mean
        if is_scalar_exp:
            performance[idx_exp]['experiment'] = expparams
        else:
            for param_name in [param[0] for param in model.expparams_dtype]:
                performance[idx_exp][param_name] = expparams[param_name]

    return performance
Beispiel #28
0
def sim_qubit_fid(n_meas, n_meas_rep, meas_dist, n_trials=100, n_particles=1000, n_rec=15):
    r"""Calculates the average fidelity of the optimal estimator (approximated
    by SMC) averaged over Haar random pure states and a random sample of
    measurement outcomes. The estimator is calculated at a given number of
    interim times throughout the tomographic process.

    :param n_meas:      The number of copies of the system given in each
                        tomographic run
    :type n_meas:       Integer
    :param n_meas_rep:  The number of measurement outcomes to average the
                        fidelity over for each copy of the system in a
                        tomographic run
    :type n_meas_rep:   Integer
    :param meas_dist:   Object defining the distribution from which to draw
                        measurement directions
    :type meas_dist:    Object possessing `sample(n)` function that returns a
                        numpy.array((2, n)) of unit vectors in
                        :math:`\mathbb{C}^2`
    :param n_trials:    The number of tomographic runs (aka samples from the
                        pure state prior) the fidelity is averaged over
    :type n_trials:     Integer
    :param n_particles: Number of SMC particles to use
    :type n_particles:  Integer
    :param n_rec:       Number of places to record average fidelity (on a log
                        scale)
    :type n_rec:        Integer
    :returns:           An array with the calculated average fidelities at the
                        specified times for all tomographic runs
    :return type:       numpy.array((n_trials, n_rec))

    """
    n_qubits = 1  # This function isn't guaranteed to generalize by changing
    # this value, but it is included here to aid readability and
    # aid any future generalization efforts
    dim = 2 * n_qubits
    # Record data on a logarithmic scale
    rec_idxs = np.unique(np.round(np.logspace(0, np.log10(n_meas), n_rec)))
    n_rec = rec_idxs.shape[0]

    # Allocate result array
    fidelities = np.empty((n_trials, n_rec))

    # Instantiate model and state prior
    model = HaarTestModel(n_qubits=n_qubits)
    prior = HaarDistribution(n_qubits=n_qubits)

    # Sample all the measurement directions used at once (since some samplers
    # might be more efficient doing things this way)
    raw_meas_dirs = meas_dist.sample(n_trials * n_meas)
    # Reshape the measurement directions to be a n_trials x n_meas array of unit
    # vectors in C^2
    meas_dirs = np.reshape(raw_meas_dirs.T, (n_trials, n_meas, 2))

    for trial_idx in xrange(n_trials):

        # Pick a random true state and instantiate the Bayes updater
        true_state = prior.sample()
        true_vec = model.param2vec(true_state)

        updater = SMCUpdater(model, n_particles, prior, resampler=LiuWestResampler(a=0.95, h=None))

        rec_idx = 0
        for meas_idx in xrange(n_meas):
            meas_dir = meas_dirs[trial_idx, meas_idx]

            # Set the experimental parameters for the measurement
            expparams = np.array([(meas_dir, n_meas_rep)], dtype=model.expparams_dtype)

            # Simulate data and update
            data = model.simulate_experiment(true_state, expparams)
            updater.update(data, expparams)

            if meas_idx + 1 in rec_idxs:
                # Generate the estimated state -> average then maximal
                # eigenvector

                weights = updater.particle_weights
                locs = updater.particle_locations

                avg_state = 1j * np.zeros([dim, dim])

                for idx_locs in xrange(n_particles):
                    psi = model.param2vec(locs[idx_locs][np.newaxis])
                    avg_state += weights[idx_locs] * np.outer(psi, psi.conj())

                eigs = la.eig(avg_state)
                max_eig = eigs[1][:, np.argmax(eigs[0])]

                fidelities[trial_idx, rec_idx] = model.fidelity(true_vec, max_eig)

                rec_idx += 1

        # Give progress updates
        print(100 * ((trial_idx + 1) / n_trials))

    return fidelities
import numpy as np
import matplotlib.pyplot as plt

import time
import Lorentzian_fit as LF
from qinfer.expdesign import ExperimentDesigner
import os
import logging

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

model = T1Model()
prior = UniformDistribution([0, 100])
N_particles=100000
updater = SMCUpdater(model, N_particles, prior, resampler=LiuWestResampler(0.98),zero_weight_policy='reset')
designer=ExperimentDesigner(updater,opt_algo=1)

#Set the value of T1 to Learn, pick 1 value from prior 
#true_model=prior.sample()
true_model=np.array([6.8], dtype=model.expparams_dtype)

performance_dtype = [
    ('expparams', 'float'),
    ('sim_outcome', 'float'),
    ('est_mean', 'float'),
    ('covariance', 'float'),
    
]

#NMR EXPERIMENT Initialization*******************************
def perf_test(
        model, n_particles, prior, n_exp, heuristic_class,
        true_model=None, true_prior=None, true_mps=None,
        extra_updater_args=None
    ):
    """
    Runs a trial of using SMC to estimate the parameters of a model, given a
    number of particles, a prior distribution and an experiment design
    heuristic.

    :param qinfer.Model model: Model whose parameters are to
        be estimated.
    :param int n_particles: Number of SMC particles to use.
    :param qinfer.Distribution prior: Prior to use in selecting
        SMC particles.
    :param int n_exp: Number of experimental data points to draw from the
        model.
    :param qinfer.Heuristic heuristic_class: Constructor function
        for the experiment design heuristic to be used.
    :param qinfer.Model true_model: Model to be used in
        generating experimental data. If ``None``, assumed to be ``model``.
        Note that if the true and estimation models have different numbers
        of parameters, the loss will be calculated by aligning the
        respective model vectors "at the right," analogously to the
        convention used by NumPy broadcasting.
    :param qinfer.Distribution true_prior: Prior to be used in
        selecting the true model parameters. If ``None``, assumed to be
        ``prior``.
    :param numpy.ndarray true_mps: The true model parameters. If ``None``,
        it will be sampled from ``true_prior``. Note that as this function
        runs exactly one trial, only one model parameter vector may be passed.
        In particular, this requires that ``len(true_mps.shape) == 1``. 
    :param dict extra_updater_args: Extra keyword arguments for the updater,
        such as resampling and zero-weight policies.
    :rtype np.ndarray: See :ref:`perf_testing_struct` for more details on 
        the type returned by this function.
    :return: A record array of performance metrics, indexed by the number
        of experiments performed.
    """

    if true_model is None:
        true_model = model

    if true_prior is None:
        true_prior = prior

    if true_mps is None:
        true_mps = true_prior.sample()

    if extra_updater_args is None:
        extra_updater_args = {}

    n_min_modelparams = min(model.n_modelparams, true_model.n_modelparams)

    dtype, is_scalar_exp = actual_dtype(model, true_model)
    performance = np.zeros((n_exp,), dtype=dtype)

    updater = SMCUpdater(model, n_particles, prior, **extra_updater_args)
    heuristic = heuristic_class(updater)

    for idx_exp in range(n_exp):
        # Set inside the loop to handle the case where the
        # true model is time-dependent as well as the estimation model.
        performance[idx_exp]['true'] = true_mps

        expparams = heuristic()
        datum = true_model.simulate_experiment(true_mps, expparams)

        with timing() as t:
            updater.update(datum, expparams)

        # Update the true model.
        true_mps = true_model.update_timestep(
            promote_dims_left(true_mps, 2), expparams
        )[:, :, 0]

        est_mean = updater.est_mean()
        delta = np.subtract(*shorten_right(est_mean, true_mps))
        loss = np.dot(delta**2, model.Q[-n_min_modelparams:])

        performance[idx_exp]['elapsed_time'] = t.delta_t
        performance[idx_exp]['loss'] = loss
        performance[idx_exp]['resample_count'] = updater.resample_count
        performance[idx_exp]['outcome'] = datum
        performance[idx_exp]['est'] = est_mean
        if is_scalar_exp:
            performance[idx_exp]['experiment'] = expparams
        else:
            for param_name in [param[0] for param in model.expparams_dtype]:
                performance[idx_exp][param_name] = expparams[param_name]

    return performance
Beispiel #31
0
def fid_smc(n_meas, K, n_qubits=1, n_trials=100, n_particles=1000, n_rec=15):
    """
    Evaluates the average fidelity incurred by using sequential Monte Carlo
    (SMC) to estimate pure states.

    :param n_meas:      The number of copies of the system given in each
                        tomographic run
    :type n_meas:       Integer
    :param K:           Number of single-shot measurements
    :type K:            Integer
    :param n_trials:    Number of times to run the SMC estimation procedure.
    :type n_trials:     Integer
    :param n_particles: Number of SMC particles to use.
    :type n_particles:  Integer
    :param n_rec:       Number of place to record data (on a log scale)
    :type n_rec:        Integer
    :returns:           Dictionary of various fidelities and timings
    :return type:       Dictionary

    """
    dim = int(2 ** n_qubits)
    # Record data on a logarithmic scale
    rec_idx = np.unique(np.round(np.logspace(0, np.log10(n_meas), n_rec)))
    n_rec = rec_idx.shape[0]

    # Allocate arrays to hold results.
    fidelity_mub = np.empty((n_trials, n_rec))
    fidelity_opt = np.empty((n_trials, n_rec))
    fidelity_WM = np.empty((n_trials, n_rec))
    fidelity_DST = np.empty((n_trials, n_rec))

    # Instantiate models and distributions
    model = HaarTestModel(n_qubits=n_qubits)
    prior = HaarDistribution(n_qubits=n_qubits)
    measMUB = MUBDistribution()
    measWM = WeakMeasDistribution(eps=0.05)
    measDST = DSTDistribution(0.1)

    timing = np.empty((n_trials,))

    # Make and show a progress bar.
    """
    prog = ProgressBar()
    prog.show()
    """

    for idx_trial in xrange(n_trials):

        # Pick a random true state and instantiate the Bayes updater
        true_state = prior.sample()
        true_vec = model.param2vec(true_state)

        updater_opt = SMCUpdater(model, n_particles, prior, resampler=LiuWestResampler(a=0.95, h=None))
        updater_mub = SMCUpdater(model, n_particles, prior, resampler=LiuWestResampler(a=0.95, h=None))
        updater_WM = SMCUpdater(model, n_particles, prior, resampler=LiuWestResampler(a=0.95, h=None))
        updater_DST = SMCUpdater(model, n_particles, prior, resampler=LiuWestResampler(a=0.95, h=None))

        # Record the start time.
        tic = time.time()

        idx_rec = 0
        for idx_meas in xrange(n_meas):
            # Choose a random measurement direction
            foo = prior.sample()
            meas_opt = model.param2vec(foo)
            meas_mub = measMUB.sample()
            meas_WM = measWM.sample()[:, 0]
            meas_DST = measDST.sample()[:, 0]

            expparams_opt = np.array([(meas_opt, K)], dtype=model.expparams_dtype)
            expparams_mub = np.array([(meas_mub, K)], dtype=model.expparams_dtype)
            expparams_WM = np.array([(meas_WM, K)], dtype=model.expparams_dtype)
            expparams_DST = np.array([(meas_DST, K)], dtype=model.expparams_dtype)

            # Simulate data and update
            data_opt = model.simulate_experiment(true_state, expparams_opt)
            updater_opt.update(data_opt, expparams_opt)

            data_mub = model.simulate_experiment(true_state, expparams_mub)
            updater_mub.update(data_mub, expparams_mub)

            data_WM = model.simulate_experiment(true_state, expparams_WM)
            updater_WM.update(data_WM, expparams_WM)

            data_DST = model.simulate_experiment(true_state, expparams_DST)
            updater_DST.update(data_DST, expparams_DST)

            if idx_meas + 1 in rec_idx:
                # Generate the estimated state -> average then maximal eigenvector

                weights = updater_opt.particle_weights
                locs = updater_opt.particle_locations

                ave_state = 1j * np.zeros([dim, dim])

                for idx_locs in xrange(n_particles):
                    psi = model.param2vec(locs[idx_locs][np.newaxis])
                    ave_state += weights[idx_locs] * np.outer(psi, psi.conj())

                eigs = la.eig(ave_state)
                max_eig = eigs[1][:, np.argmax(eigs[0])]

                fidelity_opt[idx_trial, idx_rec] = model.fidelity(true_vec, max_eig)

                # MUB
                weights = updater_mub.particle_weights
                locs = updater_mub.particle_locations

                ave_state = 1j * np.zeros([dim, dim])

                for idx_locs in xrange(n_particles):
                    psi = model.param2vec(locs[idx_locs][np.newaxis])
                    ave_state += weights[idx_locs] * np.outer(psi, psi.conj())

                eigs = la.eig(ave_state)
                max_eig = eigs[1][:, np.argmax(eigs[0])]

                fidelity_mub[idx_trial, idx_rec] = model.fidelity(true_vec, max_eig)

                # Weak Measurement
                weights = updater_WM.particle_weights
                locs = updater_WM.particle_locations

                ave_state = 1j * np.zeros([dim, dim])

                for idx_locs in xrange(n_particles):
                    psi = model.param2vec(locs[idx_locs][np.newaxis])
                    ave_state += weights[idx_locs] * np.outer(psi, psi.conj())

                eigs = la.eig(ave_state)
                max_eig = eigs[1][:, np.argmax(eigs[0])]

                fidelity_WM[idx_trial, idx_rec] = model.fidelity(true_vec, max_eig)

                # DST Measurement
                weights = updater_DST.particle_weights
                locs = updater_DST.particle_locations

                ave_state = 1j * np.zeros([dim, dim])

                for idx_locs in xrange(n_particles):
                    psi = model.param2vec(locs[idx_locs][np.newaxis])
                    ave_state += weights[idx_locs] * np.outer(psi, psi.conj())

                eigs = la.eig(ave_state)
                max_eig = eigs[1][:, np.argmax(eigs[0])]

                fidelity_DST[idx_trial, idx_rec] = model.fidelity(true_vec, max_eig)

                idx_rec += 1

        # Record how long it took us.
        timing[idx_trial] = time.time() - tic

        print(100 * ((idx_trial + 1) / n_trials))
        # prog.value = 100 * ((idx_trial + 1) / n_trials)

    return {
        "fidelity_opt": fidelity_opt,
        "fidelity_mub": fidelity_mub,
        "fidelity_WM": fidelity_WM,
        "fidelity_DST": fidelity_DST,
        "timing": timing,
    }
    def test_in_credible_region(self):
        """
        Tests that in_credible_region works.
        """

        dist = MultivariateNormalDistribution(self.MEAN, self.COV)
        # the model is irrelevant; we just want the updater to have some particles
        # with the desired normal distribution.
        u = SMCUpdater(MockModel(4), self.N_PARTICLES, dist)

        # some points to test with
        test_points = np.random.multivariate_normal(self.MEAN, self.COV, self.N_PARTICLES)

        # method='pce'
        results = [
            u.in_credible_region(test_points, level=0.9, method='pce'),
            u.in_credible_region(test_points, level=0.84, method='pce'),
            u.in_credible_region(test_points, level=0.5, method='pce'),
        ]
        assert_almost_equal(
            np.array([np.mean(x.astype('float')) for x in results]),
            np.array([0.9, 0.84, 0.5]),
            3
        )

        # method='hpd-hull'
        results1 = [
            u.in_credible_region(test_points, level=0.9, method='hpd-hull'),
            u.in_credible_region(test_points, level=0.84, method='hpd-hull'),
            u.in_credible_region(test_points, level=0.5, method='hpd-hull'),
        ]
        assert_array_less(
            np.array([0.9, 0.84, 0.5]),
            np.array([np.mean(x.astype('float')) for x in results1])
        )

        # method='hpd-mvee'
        results2 = [
            u.in_credible_region(test_points, level=0.9, method='hpd-mvee'),
            u.in_credible_region(test_points, level=0.84, method='hpd-mvee'),
            u.in_credible_region(test_points, level=0.5, method='hpd-mvee'),
        ]
        assert_array_less(
            np.array([0.9, 0.84, 0.5]),
            np.array([np.mean(x.astype('float')) for x in results2])
        )

        # the mvee should be bigger than the convex hull.
        # this passes iff all points in the ellipses are
        # also in the hulls.
        assert_array_less(
            np.hstack([x.astype('float') for x in results1]),
            np.hstack([x.astype('float') for x in results2]) + 0.5
        )

        # check for no failures with slices.
        u.in_credible_region(test_points[:100,self.SLICE], level=0.9, method='pce', modelparam_slice=self.SLICE)
        u.in_credible_region(test_points[:100,self.SLICE], level=0.9, method='hpd-hull', modelparam_slice=self.SLICE)
        u.in_credible_region(test_points[:100,self.SLICE], level=0.9, method='hpd-mvee', modelparam_slice=self.SLICE)

        # check for no failures with single inputs
        assert(u.in_credible_region(test_points[0,:], level=0.9, method='pce').size == 1)
        assert(u.in_credible_region(test_points[0,:], level=0.9, method='hpd-hull').size == 1)
        assert(u.in_credible_region(test_points[0,:], level=0.9, method='hpd-mvee').size == 1)
Beispiel #33
0
def perf_test(model,
              n_particles,
              prior,
              n_exp,
              heuristic_class,
              true_model=None,
              true_prior=None,
              true_mps=None,
              extra_updater_args=None):
    """
    Runs a trial of using SMC to estimate the parameters of a model, given a
    number of particles, a prior distribution and an experiment design
    heuristic.

    :param qinfer.Model model: Model whose parameters are to
        be estimated.
    :param int n_particles: Number of SMC particles to use.
    :param qinfer.Distribution prior: Prior to use in selecting
        SMC particles.
    :param int n_exp: Number of experimental data points to draw from the
        model.
    :param qinfer.Heuristic heuristic_class: Constructor function
        for the experiment design heuristic to be used.
    :param qinfer.Model true_model: Model to be used in
        generating experimental data. If ``None``, assumed to be ``model``.
        Note that if the true and estimation models have different numbers
        of parameters, the loss will be calculated by aligning the
        respective model vectors "at the right," analogously to the
        convention used by NumPy broadcasting.
    :param qinfer.Distribution true_prior: Prior to be used in
        selecting the true model parameters. If ``None``, assumed to be
        ``prior``.
    :param numpy.ndarray true_mps: The true model parameters. If ``None``,
        it will be sampled from ``true_prior``. Note that as this function
        runs exactly one trial, only one model parameter vector may be passed.
        In particular, this requires that ``len(true_mps.shape) == 1``. 
    :param dict extra_updater_args: Extra keyword arguments for the updater,
        such as resampling and zero-weight policies.
    :rtype np.ndarray: See :ref:`perf_testing_struct` for more details on 
        the type returned by this function.
    :return: A record array of performance metrics, indexed by the number
        of experiments performed.
    """

    if true_model is None:
        true_model = model

    if true_prior is None:
        true_prior = prior

    if true_mps is None:
        true_mps = true_prior.sample()

    if extra_updater_args is None:
        extra_updater_args = {}

    n_min_modelparams = min(model.n_modelparams, true_model.n_modelparams)

    dtype, is_scalar_exp = actual_dtype(model, true_model)
    performance = np.zeros((n_exp, ), dtype=dtype)

    updater = SMCUpdater(model, n_particles, prior, **extra_updater_args)
    heuristic = heuristic_class(updater)

    for idx_exp in range(n_exp):
        # Set inside the loop to handle the case where the
        # true model is time-dependent as well as the estimation model.
        performance[idx_exp]['true'] = true_mps

        expparams = heuristic()
        datum = true_model.simulate_experiment(true_mps, expparams)

        with timing() as t:
            updater.update(datum, expparams)

        # Update the true model.
        true_mps = true_model.update_timestep(promote_dims_left(true_mps, 2),
                                              expparams)[:, :, 0]

        est_mean = updater.est_mean()
        delta = np.subtract(*shorten_right(est_mean, true_mps))
        loss = np.dot(delta**2, model.Q[-n_min_modelparams:])

        performance[idx_exp]['elapsed_time'] = t.delta_t
        performance[idx_exp]['loss'] = loss
        performance[idx_exp]['resample_count'] = updater.resample_count
        performance[idx_exp]['outcome'] = datum
        performance[idx_exp]['est'] = est_mean
        if is_scalar_exp:
            performance[idx_exp]['experiment'] = expparams
        else:
            for param_name in [param[0] for param in model.expparams_dtype]:
                performance[idx_exp][param_name] = expparams[param_name]

    return performance
#imports and intializations
from __future__ import division

from t1_model import T1Model
from qinfer.distributions import UniformDistribution
#from qinfer.distributions import NormalDistribution
from qinfer.smc import SMCUpdater
from qinfer.resamplers import LiuWestResampler
import numpy as np
import matplotlib.pyplot as plt

model = T1Model()
prior = UniformDistribution([0, 1])
N_particles=10000
updater = SMCUpdater(model, N_particles, prior, resampler=LiuWestResampler(0.98),zero_weight_policy='reset')

#Set the value of T1 to Learn, pick 1 value from prior 
true_model=prior.sample()

performance_dtype = [
    ('expparams', 'float'),
    ('sim_outcome', 'float'),
    ('est_mean', 'float'),
]

trials=10

data = np.zeros((trials, 1), dtype=performance_dtype)

for idx_trials in xrange(trials):