Example #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
Example #2
0
 def test_init_with_rvs(self):
     x, y = pb.RVComp(1, "result"), pb.RVComp(1, "condition")
     self.uni.rv = pb.RV(y)
     self.gauss.cond_rv = pb.RV(y)
     self.gauss.rv = pb.RV(x)
     prod2 = pb.ProdCPdf((self.gauss, self.uni), pb.RV(x, y), pb.RV())
Example #3
0
 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))