Esempio n. 1
0
    def __init__(self, nr_steps):
        print("Generating random data for particle filter stresses A...")
        self.nr_steps = nr_steps

        # prepare random variable components:
        a_t, b_t = pb.RVComp(1, 'a_t'), pb.RVComp(1, 'b_t')  # state in t
        a_tp, b_tp = pb.RVComp(1,
                               'a_{t-1}'), pb.RVComp(1,
                                                     'b_{t-1}')  # state in t-1

        # arguments to Kalman filter part of the marginalized particle filter
        self.kalman_args = {}

        # prepare callback functions
        sigma_sq = np.array([0.0001])

        def f(cond):  # log(b_{t-1}) - 1/2 \sigma^2
            return np.log(cond) - sigma_sq / 2.

        def g(cond):  # \sigma^2
            return sigma_sq

        # p(a_t | a_{t-1} b_t) density:
        p_at_atpbt = pb.LinGaussCPdf(1., 0., 1., 0., [a_t], [a_tp, b_t])
        self.kalman_args['A'] = np.array([[1.]])  # process model
        # p(b_t | b_{t-1}) density:
        self.p_bt_btp = pb.GaussCPdf(1,
                                     1,
                                     f,
                                     g,
                                     rv=[b_t],
                                     cond_rv=[b_tp],
                                     base_class=pb.LogNormPdf)
        # p(x_t | x_{t-1}) density:
        self.p_xt_xtp = pb.ProdCPdf((p_at_atpbt, self.p_bt_btp), [a_t, b_t],
                                    [a_tp, b_tp])

        # prepare p(y_t | x_t) density:
        self.p_yt_xt = pb.LinGaussCPdf(1., 0., 1., 0.)
        self.kalman_args['C'] = np.array([[1.]])  # observation model

        # Initial [a_t, b_t] from .. to:
        self.init_range = np.array([[-18., 0.3], [-14., 0.7]])
        init_mean = (self.init_range[0] + self.init_range[1]) / 2.

        x_t = np.zeros((nr_steps, 2))
        x_t[0] = init_mean.copy()
        y_t = np.empty((nr_steps, 1))
        for i in range(nr_steps):
            # set b_t:
            x_t[i, 1] = i / 100. + init_mean[1]
            # simulate random process:
            x_t[i, 0:1] = p_at_atpbt.sample(
                x_t[i])  # this is effectively [a_{t-1}, b_t]
            y_t[i] = self.p_yt_xt.sample(x_t[i])
            # DEBUG: print "simulated x_{0} = {1}".format(i, x_t[i])
            # DEBUG: print "simulated y_{0} = {1}".format(i, y_t[i])
        self.x_t = x_t
        self.y_t = y_t
Esempio n. 2
0
    def setUp(self):
        def f(x):
            return -x

        self.f = f

        def g(x):
            return np.diag(-x)

        self.g = g
        self.cgauss = pb.GaussCPdf(2, 2, f, g)
        self.gauss = pb.GaussPdf(np.array([1., 2.]),
                                 np.array([[1., 0.], [0., 2.]]))
        self.cond = np.array([-1., -2.
                              ])  # condition that makes cgauss behave as gauss
Esempio n. 3
0
    def test_different_base_class(self):
        condlognorm = pb.GaussCPdf(1,
                                   1,
                                   self.f,
                                   self.g,
                                   base_class=pb.LogNormPdf)
        lognorm = pb.LogNormPdf(np.array([1.]), np.array([[1.]]))
        cond = np.array([-1.])  # makes condlognorm behave as lognorm

        self.assertEqual(condlognorm.mean(cond), lognorm.mean())
        self.assertEqual(condlognorm.variance(cond), lognorm.variance())
        for x in np.array([[-0.4], [2.4], [4.5], [12.5]]):
            self.assertEqual(condlognorm.eval_log(x, cond),
                             lognorm.eval_log(x))
        for i in range(30):
            # only test that samples are positive
            self.assertTrue(np.all(condlognorm.sample(cond) >= 0))