def test_ELBO_Z_terms_are_correct_merge(self):
    aModel = self.hmodel.allocModel
    LP1 = self.hmodel.calc_local_params(self.batchData1)
    LP2 = self.hmodel.calc_local_params(self.batchData2)
 
    beforeElogqZ = aModel.E_logqZ(self.batchData1, LP1) \
                 + aModel.E_logqZ(self.batchData2, LP2)
    print beforeElogqZ
    # ----------------------------------------  Consider merge of comps 0 and 1
    kA = 0
    kB = 1
    mLP1 = self.calc_mergeLP(LP1, kA, kB)
    mLP2 = self.calc_mergeLP(LP2, kA, kB)
    # 1) Calculate the ELBO terms directly
    directElogpZ = aModel.E_logpZ(self.batchData1, mLP1) \
                 + aModel.E_logpZ(self.batchData2, mLP2)
                         
    directElogqZ = aModel.E_logqZ(self.batchData1, mLP1) \
                 + aModel.E_logqZ(self.batchData2, mLP2)
                         
    assert LP1['DocTopicCount'].shape[1] == aModel.K # still have originals!
    assert LP2['DocTopicCount'].shape[1] == aModel.K # still have originals!
    # 2) Calculate via suff stats
    SS1 = self.hmodel.get_global_suff_stats(self.batchData1, LP1,
                           doPrecompEntropy=True, doPrecompMergeEntropy=True)
    SS2 = self.hmodel.get_global_suff_stats(self.batchData2, LP2,
                           doPrecompEntropy=True, doPrecompMergeEntropy=True)
    SS = SS1 + SS2
    SS.mergeComps(kA, kB)
    memoElogpZ = SS.getELBOTerm('ElogpZ')
    memoElogqZ = SS.getELBOTerm('ElogqZ')
    assert np.allclose(directElogpZ, memoElogpZ)
    assert np.allclose(directElogqZ, memoElogqZ)
    # ----------------------------------------  Follow-up merge of comps 2 and 3
    # since we've collapsed 0,1 into 0 previously,
    #  original comps 2,3 have been "renamed" 1,2
    kA = 1
    kB = 2
    mLP1 = self.calc_mergeLP(mLP1, kA, kB)
    mLP2 = self.calc_mergeLP(mLP2, kA, kB)
    assert mLP1['DocTopicCount'].shape[1] == aModel.K - 2 # we've done two merges
    # 1) Calculate the ELBO terms directly
    directElogpZ = aModel.E_logpZ(self.batchData1, mLP1) \
                 + aModel.E_logpZ(self.batchData2, mLP2)
                         
    directElogqZ = aModel.E_logqZ(self.batchData1, mLP1) \
                 + aModel.E_logqZ(self.batchData2, mLP2)
    # 2) Calculate via suff stats (carried over from earlier merge!)
    SS.mergeComps(kA, kB)
    memoElogpZ = SS.getELBOTerm('ElogpZ')
    memoElogqZ = SS.getELBOTerm('ElogqZ')
    assert np.allclose(directElogpZ, memoElogpZ)
    assert np.allclose(directElogqZ, memoElogqZ)
    # Make sure we don't have any more valid precomputed merge terms
    WMat = SS.getMergeTerm('ElogpZ')
    numZeros = np.sum( WMat == 0)
    numNans = np.isnan(WMat).sum()
    assert numZeros + numNans == WMat.size

  ######################################################### Verify Pi terms
  #########################################################
Verify that the ELBO terms for ElogpZ, ElogqZ are correct
dataset/ETHPy150Open daeilkim/refinery/refinery/bnpy/bnpy-dev/tests/merge/TestMathForHDPMerges.py/TestMathForHDPMerges.test_ELBO_Z_terms_are_correct_merge