Exemplo n.º 1
0
    def test_UnscentedTransform1D(self):
        # ===== 1D model ===== #
        norm = Normal(0., 1.)
        linear = AffineProcess((f, g), (1., 1.), norm, norm)
        linearobs = AffineObservations((fo, go), (1., 1.), norm)
        model = StateSpaceModel(linear, linearobs)

        # ===== Perform unscented transform ===== #
        x = model.hidden.i_sample(shape=3000)

        ut = UnscentedTransform(model).initialize(x).construct(0.)

        assert isinstance(ut.x_dist, Normal)
Exemplo n.º 2
0
    def test_UnscentedTransform1D(self):
        # ===== 1D model ===== #
        norm = DistributionWrapper(Normal, loc=0.0, scale=1.0)
        linear = AffineProcess((f, g), (1.0, 1.0), norm, norm)
        linearobs = AffineObservations((fo, go), (1.0, 1.0), norm)
        model = StateSpaceModel(linear, linearobs)

        # ===== Perform unscented transform ===== #
        uft = UnscentedFilterTransform(model)
        res = uft.initialize(3000)
        p = uft.predict(res)
        c = uft.correct(torch.tensor(0.0), p, res)

        assert isinstance(
            c.x_dist(), Normal) and c.x_dist().mean.shape == torch.Size([3000])
Exemplo n.º 3
0
    def test_UnscentedTransform2D(self):
        # ===== 2D model ===== #
        mat = torch.eye(2)
        scale = torch.diag(mat)

        norm = Normal(0., 1.)
        mvn = MultivariateNormal(torch.zeros(2), torch.eye(2))
        mvnlinear = AffineProcess((fmvn, g), (mat, scale), mvn, mvn)
        mvnoblinear = AffineObservations((fomvn, gomvn), (1., ), norm)

        mvnmodel = StateSpaceModel(mvnlinear, mvnoblinear)

        # ===== Perform unscented transform ===== #
        x = mvnmodel.hidden.i_sample(shape=3000)

        ut = UnscentedTransform(mvnmodel).initialize(x).construct(0.)

        assert isinstance(ut.x_dist, MultivariateNormal) and isinstance(
            ut.y_dist, Normal)
        assert isinstance(ut.x_dist_indep, Independent)
Exemplo n.º 4
0
    def test_UnscentedTransform2D(self):
        # ===== 2D model ===== #
        mat = torch.eye(2)
        scale = torch.diag(mat)

        norm = Normal(0., 1.)
        mvn = MultivariateNormal(torch.zeros(2), torch.eye(2))
        mvnlinear = AffineProcess((fmvn, g), (mat, scale), mvn, mvn)
        mvnoblinear = AffineObservations((fomvn, gomvn), (1., ), norm)

        mvnmodel = StateSpaceModel(mvnlinear, mvnoblinear)

        # ===== Perform unscented transform ===== #
        uft = UnscentedFilterTransform(mvnmodel)
        res = uft.initialize(3000)
        p = uft.predict(res)
        c = uft.correct(0., p)

        assert isinstance(
            c.x_dist(),
            MultivariateNormal) and c.x_dist().mean.shape == torch.Size(
                [3000, 2])
Exemplo n.º 5
0
    def test_StateDict(self):
        # ===== Define model ===== #
        norm = Normal(0., 1.)
        linear = AffineProcess((f, g), (1., 1.), norm, norm)
        linearobs = AffineObservations((fo, go), (1., 1.), norm)
        model = StateSpaceModel(linear, linearobs)

        # ===== Define filter ===== #
        filt = SISR(model, 100).initialize()

        # ===== Get statedict ===== #
        sd = filt.state_dict()

        # ===== Verify that we don't save multiple instances ===== #
        assert '_model' in sd and '_model' not in sd['_proposal']

        newfilt = SISR(model, 1000).load_state_dict(sd)
        assert newfilt._w_old is not None and newfilt.ssm is newfilt._proposal._model

        # ===== Test same with UKF and verify that we save UT ===== #
        ukf = UKF(model).initialize()
        sd = ukf.state_dict()

        assert '_model' in sd and '_model' not in sd['_ut']
Exemplo n.º 6
0
class Tests(unittest.TestCase):
    # ===== 1D model ===== #
    norm = Normal(0., 1.)
    linear = AffineModel((f0, g0), (f, g), (1., 1.), (norm, norm))
    linearobs = Observable((fo, go), (1., 1.), norm)
    model = StateSpaceModel(linear, linearobs)

    # ===== 2D model ===== #
    mat = torch.eye(2)
    scale = torch.diag(mat)

    mvn = MultivariateNormal(torch.zeros(2), torch.eye(2))
    mvnlinear = AffineModel((f0mvn, g0mvn), (fmvn, gmvn), (mat, scale),
                            (mvn, mvn))
    mvnoblinear = Observable((fomvn, gomvn), (1., ), norm)

    mvnmodel = StateSpaceModel(mvnlinear, mvnoblinear)

    def test_InitializeModel1D(self):
        sample = self.model.initialize()

        assert isinstance(sample, torch.Tensor)

    def test_InitializeModel(self):
        sample = self.model.initialize(1000)

        assert sample.shape == (1000, )

    def test_Propagate(self):
        x = self.model.initialize(1000)

        sample = self.model.propagate(x)

        assert sample.shape == (1000, )

    def test_Sample(self):
        x, y = self.model.sample(50)

        assert len(x) == 50 and len(y) == 50 and np.array(x).shape == (50, 1)

    def test_SampleMultivariate(self):
        x, y = self.mvnmodel.sample(30)

        assert len(x) == 30 and x[0].shape == (2, )

    def test_SampleMultivariateSamples(self):
        shape = (100, 100)
        x, y = self.mvnmodel.sample(30, samples=shape)

        assert x.shape == (30, 2, *shape) and isinstance(
            x, torch.Tensor) and isinstance(y, torch.Tensor)
        assert self.mvnmodel.h_weight(x[1], x[0]).shape == shape
        if len(shape) > 1:
            assert self.mvnmodel.h_weight(x[1, :, 0, 0], x[0]).shape == shape
        assert self.mvnmodel.weight(y[0, 0], x[0]).shape == shape

    def test_Parameter(self):
        param = Parameter(Beta(1, 3)).sample_()

        assert param.values.shape == torch.Size([])

        newshape = (3000, 1000)
        with self.assertRaises(ValueError):
            param.values = Normal(0., 1.).sample(newshape)

        newvals = Beta(1, 3).sample(newshape)
        param.values = newvals

        assert param.values.shape == newshape

        param.t_values = Normal(0., 1.).sample(newshape)

        assert (param.values != newvals).all()

    def test_LinearGaussianObservations(self):
        linearmodel = LinearGaussianObservations(self.linear)

        steps = 30

        x, y = linearmodel.sample(steps)

        assert len(x) == steps and len(y) == steps

        mat = torch.eye(2)

        mvn_linearmodel = LinearGaussianObservations(self.mvnlinear, a=mat)

        x, y = mvn_linearmodel.sample(30)

        assert len(x) == steps and len(y) == steps