def setUp(self): init_pdf = pb.UniPdf(np.array([-5.]), np.array([5.])) p_xt_xtp = pb.MLinGaussCPdf(np.array([[2.]]), np.array([[1.]]), np.array([0.])) p_yt_xt = pb.MLinGaussCPdf(np.array([[1.]]), np.array([[1.]]), np.array([0.])) self.pf = pb.ParticleFilter(20, init_pdf, p_xt_xtp, p_yt_xt)
def test_rvs(self): self.assertEqual(self.uni.rv.dimension, 1) self.assertEqual(self.uni.cond_rv.dimension, 0) self.assertEqual(self.multiuni.rv.dimension, 3) self.assertEqual(self.multiuni.cond_rv.dimension, 0) a = pb.RVComp(2, "a") b = pb.RVComp(1, "b") test_uni = pb.UniPdf(np.array([0., -1., 2.]), np.array([1., 1., 4.]), pb.RV(a, b)) self.assertTrue(test_uni.rv.contains(a)) self.assertTrue(test_uni.rv.contains(b)) test_uni = pb.UniPdf(np.array([0., -1., 2.]), np.array([1., 1., 4.]), [a, b]) self.assertTrue(test_uni.rv.contains(a)) self.assertTrue(test_uni.rv.contains(b))
def test_sample(self): # we can only somehow test unifrom pdf, so we create a product of them uni_list = [] for i in range(10): uni_list.append(pb.UniPdf(np.array([i + 0.]), np.array([i + 1.]))) uni_prod = pb.ProdPdf(uni_list) # a product of 10 UniPdfs for i in range(100): sample = uni_prod.sample() for j in range(10): # test each component.. self.assertTrue(j <= sample[j] <= j + 1) # ..is within bounds
def run_pf(timer, pf_opts, nr_particles, pf_class): nr_steps = pf_opts.nr_steps # number of time steps # prepare initial particle density: init_pdf = pb.UniPdf(pf_opts.init_range[0], pf_opts.init_range[1]) # construct particle filter if pf_class is pb.ParticleFilter: pf = pf_class(nr_particles, init_pdf, pf_opts.p_xt_xtp, pf_opts.p_yt_xt) elif pf_class is pb.MarginalizedParticleFilter: pf = pf_class(nr_particles, init_pdf, pf_opts.p_bt_btp, pf_opts.kalman_args) else: raise NotImplementedError("This switch case not handled") x_t = pf_opts.x_t y_t = pf_opts.y_t mean = np.empty((nr_steps, 2)) timer.start() for i in range(nr_steps): pf.bayes(y_t[i]) mean[i] = pf.posterior().mean() timer.stop() cumerror = np.sum((mean - x_t)**2, 0) print((" {0}-{3} cummulative error for {1} steps: {2}".format( nr_particles, nr_steps, np.sqrt(cumerror), pf_class.__name__))) plt = None # disable plotting for now if plt: x = np.arange(nr_steps) plt.plot(x, mean[:, 0], 'x', label="{0}: {1}".format(nr_particles, pf_class.__name__)) plt.plot(x, mean[:, 1], '+', label="{0}: {1}".format(nr_particles, pf_class.__name__)) if plt and nr_particles == 90 and pf_class == pb.MarginalizedParticleFilter: plt.plot(x, x_t[:, 0], '-') plt.plot(x, x_t[:, 1], '--') plt.legend() plt.show()
def test_rvs(self): self.assertEqual(self.prod.rv.dimension, 3) # test that child rv components are copied into parent ProdPdf a, b, c = pb.RVComp(1, "a"), pb.RVComp(1, "b"), pb.RVComp(1, "c") uni = pb.UniPdf(np.array([0., 0.]), np.array([1., 2.]), pb.RV(a, b)) gauss = pb.GaussPdf(np.array([0.]), np.array([[1.]]), pb.RV(c)) prod = pb.ProdPdf((uni, gauss)) self.assertEquals(prod.rv.name, "[a, b, c]") for rv_comp in a, b, c: self.assertTrue(prod.rv.contains(rv_comp)) # that that custom rv passed to constructor is accepted d = pb.RVComp(3, "d") prod_custom = pb.ProdPdf((uni, gauss), pb.RV(d)) self.assertEquals(prod_custom.rv.name, "[d]") self.assertTrue(prod_custom.rv.contains(d)) self.assertFalse(prod_custom.rv.contains(a)) self.assertFalse(prod_custom.rv.contains(b)) self.assertFalse(prod_custom.rv.contains(c))
def setUp(self): self.uni = pb.UniPdf(np.array([0., 0.]), np.array([1., 2.])) self.gauss = pb.GaussPdf(np.array([0.]), np.array([[1.]])) self.prod = pb.ProdPdf((self.uni, self.gauss))
def setUp(self): self.uni = pb.UniPdf(np.array([-10.]), np.array([20.])) (self.a, self.b) = (np.array([0., -1., 2.]), np.array([1., 1., 4.])) self.multiuni = pb.UniPdf(self.a, self.b)
def setUp(self): ide = np.array([[1.]]) # 1x1 identity matrix self.gauss = pb.MLinGaussCPdf(ide, ide, np.array([0.])) self.uni = pb.UniPdf(np.array([0.]), np.array([2.])) self.prod = pb.ProdCPdf((self.gauss, self.uni))