Beispiel #1
0
    def setPostFactors(self,
                       obsModel=None,
                       SS=None,
                       LP=None,
                       Data=None,
                       **param_kwargs):
        ''' Set attribute Post to provided values.
        '''
        self.ClearCache()
        if obsModel is not None:
            if hasattr(obsModel, 'Post'):
                self.Post = obsModel.Post.copy()
                self.K = self.Post.K
            else:
                self.setPostFromEstParams(obsModel.EstParams)
            return

        if LP is not None and Data is not None:
            SS = self.calcSummaryStats(Data, None, LP)
        if SS is not None:
            self.updatePost(SS)
        else:
            self.Post = DiagGaussX.packParamBagForPost(**param_kwargs)
            self.Post = RegressY.packParamBagForPost(Post=self.Post,
                                                     **param_kwargs)

        self.K = self.Post.K
Beispiel #2
0
    def calcELBO_Memoized(self, SS, returnVec=0, afterMStep=False, **kwargs):
        """ Calculate obsModel's objective using suff stats SS and Post.

        Args
        -------
        SS : bnpy SuffStatBag
        afterMStep : boolean flag
            if 1, elbo calculated assuming M-step just completed

        Returns
        -------
        elbo_K : scalar float
            Equal to E[ log p(x) + log p(phi) - log q(phi)]
        """
        elbo_XModel = DiagGaussX.calcELBOFromSSAndPost(SS=SS,
                                                       Post=self.Post,
                                                       Prior=self.Prior,
                                                       returnVec=returnVec,
                                                       afterMStep=afterMStep)
        elbo_YModel = RegressY.calcELBOFromSSAndPost(SS=SS,
                                                     Post=self.Post,
                                                     Prior=self.Prior,
                                                     returnVec=returnVec,
                                                     afterMStep=afterMStep)
        return elbo_XModel + elbo_YModel
Beispiel #3
0
    def calcSummaryStats(self, Data, SS, LP, **kwargs):
        ''' Calculate summary statistics for given dataset and local parameters

        Returns
        --------
        SS : SuffStatBag object, with K components.
        '''
        SS = DiagGaussX.calcSummaryStats(Data, SS, LP, **kwargs)
        SS = RegressY.calcSummaryStats(Data, SS, LP, **kwargs)
        return SS
Beispiel #4
0
    def calcLogSoftEvMatrix_FromPost(self, Data, **kwargs):
        ''' Compute expected log soft evidence of each item under each cluster

        Returns
        -------
        E_log_soft_ev_NK : 2D array, size N x K
        '''
        E_log_soft_ev_NK = DiagGaussX.calcLogSoftEvMatrix_FromPost(
            Data, Post=self.Post)
        if hasattr(Data, 'Y'):
            # Only incorporate Y attribute if present
            E_log_soft_ev_NK = RegressY.calcLogSoftEvMatrix_FromPost(
                Data, Post=self.Post, E_log_soft_ev_NK=E_log_soft_ev_NK)
        return E_log_soft_ev_NK
Beispiel #5
0
    def calcHardMergeGap(self, SS, kA, kB):
        ''' Calculate change in ELBO after a hard merge applied to this model

        Returns
        ---------
        gap : scalar real, indicates change in ELBO after merge of kA, kB
        '''
        gapX, _, _ = DiagGaussX.calcHardMergeGapForPair(SS=SS,
                                                        Post=self.Post,
                                                        Prior=self.Prior,
                                                        kA=kA,
                                                        kB=kB)
        gapY, _, _ = RegressY.calcHardMergeGapForPair(SS=SS,
                                                      Post=self.Post,
                                                      Prior=self.Prior,
                                                      kA=kA,
                                                      kB=kB)
        return gapX + gapY
Beispiel #6
0
    def updatePost(self, SS):
        ''' Update attribute Post for all comps given suff stats.

        Optimizes the variational objective for approximating the posterior

        Post Condition
        --------------
        Attributes K and Post updated in-place.
        '''
        self.ClearCache()
        if not hasattr(self, 'Post') or SS.K != self.Post.K:
            self.Post = None
        self.Post = DiagGaussX.calcPostParamsFromSS(SS=SS,
                                                    Prior=self.Prior,
                                                    Post=self.Post)
        self.Post = RegressY.calcPostParamsFromSS(SS=SS,
                                                  Prior=self.Prior,
                                                  Post=self.Post)
        self.K = SS.K
Beispiel #7
0
    def __init__(self, inferType='VB', D=0, Data=None, **PriorArgs):
        ''' Initialize bare obsmodel with valid prior hyperparameters.

        Returns
        -------
        obsmodel : bare observation model
            Resulting object lacks either EstParams or Post attributes.
            which must be created separately (see init_global_params).
        '''
        if Data is not None:
            self.D = Data.dim
        else:
            self.D = int(D)
        self.E = self.D + 1
        self.K = 0
        self.inferType = inferType
        self.Cache = dict()
        PriorArgs['D'] = self.D
        PriorArgs['E'] = self.E
        self.Prior = DiagGaussX.createParamBagForPrior(Data, **PriorArgs)
        self.Prior = RegressY.createParamBagForPrior(Data,
                                                     Prior=self.Prior,
                                                     **PriorArgs)
Beispiel #8
0
 def get_info_string_prior(self):
     return DiagGaussX.getStringSummaryOfPrior(self.Prior) \
         + RegressY.getStringSummaryOfPrior(self.Prior)