示例#1
0
    def conditionedExpectedSufficientStats( self, ys, u, alphas, betas, forMStep=False ):

        Ext1_xt1 = np.zeros( ( self.D_latent, self.D_latent ) )
        Ext1_xt = np.zeros( ( self.D_latent, self.D_latent ) )
        Ext_xt = np.zeros( ( self.D_latent, self.D_latent ) )
        if( forMStep ):
            Eut_ut = np.zeros( ( self.D_latent, self.D_latent ) )
            Ext_ut = np.zeros( ( self.D_latent, self.D_latent ) )
            Ext1_ut = np.zeros( ( self.D_latent, self.D_latent ) )
            allT = 0
            allM = 0

        Eyt_yt = np.zeros( ( self.D_obs, self.D_obs ) )
        Eyt_xt = np.zeros( ( self.D_obs, self.D_latent ) )
        Ext_xt_y = np.zeros( ( self.D_latent, self.D_latent ) )

        Ex0_x0 = np.zeros( ( self.D_latent, self.D_latent ) )
        Ex0 = np.zeros( self.D_latent )

        if( u is not None and u.ndim == 3 ):
            # Multiple u's
            assert len( ys ) == len( u )
            it = zip( ys, alphas, betas, u )
        else:
            assert u is None or u.ndim == 2
            if( u is None ):
                J, _, _ = alphas[ 0 ][ 0 ]
                u = np.zeros( ( len( alphas[ 0 ] ), J.shape[ 0 ] ) )
            it = zip( ys, alphas, betas, itertools.repeat( u, len( ys ) ) )

        for i, ( _ys, _alphas, _betas, _u ) in enumerate( it ):

            uMask = np.isnan( _u )
            _u = MaskedData( _u, uMask, None )

            M, T, _ = _ys.shape

            if( forMStep ):
                allT += T - 1
                allM += T * M

            for t in range( 1, T ):

                _Ext1_xt1, _Ext1_xt, _Ext_xt = self.expectedTransitionStatsBlock( t - 1, _alphas, _betas, ys=_ys, u=_u )
                Ext1_xt1 += _Ext1_xt1
                Ext1_xt += _Ext1_xt
                Ext_xt += _Ext_xt

                if( forMStep ):
                    J, h, _ = np.add( _alphas[ t - 1 ], _betas[ t - 1 ] )
                    J1, h1, _ = np.add( _alphas[ t ], _betas[ t ] )
                    Ext = Normal.natToStandard( J, h, fromPrecision=True )[ 0 ]
                    Ex1t = Normal.natToStandard( J1, h1, fromPrecision=True )[ 0 ]
                    Eut_ut += np.outer( _u[ t - 1 ], _u[ t - 1 ] )
                    Ext_ut += np.outer( Ext, _u[ t - 1 ] )
                    Ext1_ut += np.outer( Ex1t, _u[ t - 1 ] )

            for t in range( T ):
                _Eyt_yt, _Eyt_xt, _Ext_xt = self.expectedEmissionStats( t, _ys, _alphas, _betas, conditionOnY=True )
                Eyt_yt += _Eyt_yt
                Eyt_xt += _Eyt_xt
                Ext_xt_y += _Ext_xt

            _Ex0_x0, _Ex0 = self.expectedInitialStats( _alphas, _betas )
            Ex0_x0 += _Ex0_x0
            Ex0 += _Ex0

        if( forMStep ):
            return Ext1_xt1, Ext1_xt, Ext_xt, Eyt_yt, Eyt_xt, Ext_xt_y, Ex0_x0, Ex0, Eut_ut, Ext_ut, Ext1_ut, allT, allM

        return Ext1_xt1, Ext1_xt, Ext_xt, Eyt_yt, Eyt_xt, Ext_xt_y, Ex0_x0, Ex0
示例#2
0
 def sampleStep( self, J, h ):
     return Normal.unpackSingleSample( Normal.sample( params=Normal.natToStandard( J, h, fromPrecision=True ) ) )
示例#3
0
 def likelihoodStep( self, x, J, h ):
     return Normal.log_likelihood( x, params=Normal.natToStandard( J, h, fromPrecision=True ) )
示例#4
0
 def natToStandard( cls, n1, n2, n3, n4, n5, n6, n7, n8 ):
     A, sigma = Regression.natToStandard( n1, n2, n3 )
     C, R = Regression.natToStandard( n4, n5, n6 )
     mu0, sigma0 = Normal.natToStandard( n7, n8 )
     return A, sigma, C, R, mu0, sigma0