Ejemplo n.º 1
0
    def test_filt_KPM(self):
        kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_results'))
        # process model
        A = numpy.array([[1, 0, 1, 0],
                         [0, 1, 0, 1],
                         [0, 0, 1,  0],
                         [0, 0, 0,  1]],
                        dtype=numpy.float64)
        # observation model
        C = numpy.array([[1, 0, 0, 0],
                         [0, 1, 0, 0]],
                        dtype=numpy.float64)
        ss=4; os=2
        # process covariance
        Q = 0.1*numpy.eye(ss)
        # measurement covariance
        R = 1.0*numpy.eye(os)
        initx = numpy.array([10, 10, 1, 0],dtype=numpy.float64)
        initV = 10.0*numpy.eye(ss)

        x = kpm['x'].T
        y = kpm['y'].T

        xfilt, Vfilt = adskalman.kalman_filter(y, A, C, Q, R, initx, initV)
        assert numpy.allclose(xfilt.T,kpm['xfilt'])
        assert_3d_vs_kpm_close(Vfilt,kpm['Vfilt'])

        xsmooth, Vsmooth = adskalman.kalman_smoother(y,A,C,Q,R,initx,initV)
        assert numpy.allclose(xsmooth.T,kpm['xsmooth'])
        assert_3d_vs_kpm_close(Vsmooth,kpm['Vsmooth'])
Ejemplo n.º 2
0
    def test_DRO_smooth(self):
        kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_results'))
        # process model
        A = numpy.array([[1, 0, 1, 0],
                         [0, 1, 0, 1],
                         [0, 0, 1,  0],
                         [0, 0, 0,  1]],
                        dtype=numpy.float64)
        # observation model
        C = numpy.array([[1, 0, 0, 0],
                         [0, 1, 0, 0]],
                        dtype=numpy.float64)
        ss=4; os=2
        # process covariance
        Q = 0.1*numpy.eye(ss)
        # measurement covariance
        R = 1.0*numpy.eye(os)
        initx = numpy.array([10, 10, 1, 0],dtype=numpy.float64)
        initV = 10.0*numpy.eye(ss)

        x = kpm['x'].T
        y = kpm['y'].T

        xfilt, Vfilt = adskalman.DROsmooth(y,A,C,Q,R,initx,initV,mode='forward_only')
        if 0:
            xfilt_kpm, Vfilt_kpm = adskalman.kalman_filter(y, A, C, Q, R, initx, initV)
            print 'xfilt.T',xfilt.T
            print 'xfilt_kpm.T',xfilt_kpm.T
            print "kpm['xfilt']",kpm['xfilt']
        assert numpy.allclose(xfilt.T,kpm['xfilt'])
        assert_3d_vs_kpm_close(Vfilt,kpm['Vfilt'])

        xsmooth, Vsmooth = adskalman.DROsmooth(y,A,C,Q,R,initx,initV)
        if 0:
            xsmooth_kpm, Vsmooth_kpm = adskalman.kalman_smoother(y,A,C,Q,R,initx,initV)
            print 'xsmooth.T',xsmooth.T
            print 'xsmooth_kpm.T',xsmooth_kpm.T
            print "kpm['xsmooth']",kpm['xsmooth']

            print 'Vsmooth',Vsmooth
        assert numpy.allclose(xsmooth.T[:,:-1],kpm['xsmooth'][:,:-1]) # KPM doesn't update last timestep
        assert_3d_vs_kpm_close(Vsmooth[:-1],kpm['Vsmooth'][:,:,:-1])
Ejemplo n.º 3
0
    def test_loglik_KPM(self):
        # this test broke the loglik calculation
        kpm=scipy.io.loadmat(pkg_resources.resource_filename(__name__,'kpm_learn_results'))
        y = kpm['y'].T # data vector is transposed from KPM
        F1 = kpm['F1']
        H1 = kpm['H1']
        Q1 = kpm['Q1']
        R1 = kpm['R1']
        initx1 = kpm['initx']
        initV1 = kpm['initV']
        xfilt, Vfilt, VVfilt, loglik_filt =  adskalman.kalman_filter(
            y, F1, H1, Q1, R1, initx1, initV1,full_output=True)
        assert numpy.allclose(xfilt, kpm['xfilt'].T)
        assert_3d_vs_kpm_close(Vfilt, kpm['Vfilt'])
        assert_3d_vs_kpm_close(VVfilt, kpm['VVfilt'])
        assert numpy.allclose(loglik_filt, kpm['loglik_filt'])

        xsmooth, Vsmooth, VVsmooth, loglik_smooth =  adskalman.kalman_smoother(
            y, F1, H1, Q1, R1, initx1, initV1,full_output=True)
        assert numpy.allclose(xsmooth, kpm['xsmooth'].T)
        assert_3d_vs_kpm_close(Vsmooth, kpm['Vsmooth'])
        assert_3d_vs_kpm_close(VVsmooth, kpm['VVsmooth'])
        assert numpy.allclose(loglik_smooth, kpm['loglik_smooth'])