public void OptimizePhi(LLNAModel model, CorpusDocument doc) { // Compute phi proportion in log space. for (int n = 0; n < doc.NbTerms; n++) { double logSumN = 0; for (int i = 0; i < model.K; i++) { var word = doc.GetWord(n); LogPhi[n, i] = Lambda[i] + model.LogBeta[i, word]; if (i == 0) { logSumN = LogPhi[n, i]; } else { logSumN = MathHelper.LogSum(logSumN, LogPhi[n, i]); } } for (int i = 0; i < model.K; i++) { LogPhi[n, i] = LogPhi[n, i] - logSumN; Phi[n, i] = Math.Exp(LogPhi[n, i]); } } }
public void Update(VariationalInferenceParameter varInference, CorpusDocument document) { for (int i = 0; i < CovarianceSufficientStatistic.RowCount; i++) { MuSufficientStatistic[i] = MuSufficientStatistic[i] + varInference.Lambda[i]; for (int j = 0; j < CovarianceSufficientStatistic.ColumnCount; j++) { double tmp = varInference.Lambda[i] * varInference.Lambda[j]; if (i == j) { CovarianceSufficientStatistic[i, j] = CovarianceSufficientStatistic[i, j] + varInference.Nu[i] + tmp; } else { CovarianceSufficientStatistic[i, j] = CovarianceSufficientStatistic[i, j] + tmp; } } } for (int i = 0; i < document.NbTerms; i++) { for (int j = 0; j < BetaSufficientStatistic.RowCount; j++) { var word = document.GetWord(i); var count = document.GetCount(i); BetaSufficientStatistic[j, word] = BetaSufficientStatistic[j, word] + count * varInference.Phi[i, j]; } } NData++; }
public void UpdateLikelihoodBound(CorpusDocument doc, LLNAModel model) { TopicScores.SetValue(0); // p(η | μ, Σ) (distribution of topic proportions) && q(η | λ, ν) // E[log p(\eta | \mu, \Sigma)] + H(q(\eta | \lambda, \nu) double lHood = 0.5 * model.LogDeterminantInvCovariance + 0.5 * (model.K - 1); for (int i = 0; i < (model.K - 1); i++) { double v = -0.5 * Nu[i] * model.InvCovariance[i, i]; for (int j = 0; j < model.K - 1; j++) { v -= 0.5 * (Lambda[i] - model.Mu[i]) * model.InvCovariance[i, j] * (Lambda[j] - model.Mu[j]); } v += 0.5 * Math.Log(Nu[i]); lHood += v; } // E[log p(z_n | \eta)] + E[log p(w_n | \beta)] + H(q(z_n | \phi_n)) lHood -= CalculateLikelihoodBound() * doc.Total; for (int i = 0; i < doc.NbTerms; i++) { for (int j = 0; j < model.K; j++) { var phi = Phi[i, j]; var logPhi = LogPhi[i, j]; if (phi > 0) { TopicScores[j] += phi * doc.GetCount(i); double a1 = Lambda[j]; int ww = doc.GetWord(i); double a2 = model.LogBeta[j, doc.GetWord(i)]; var ss = doc.GetCount(i) * phi * ((Lambda[j] + model.LogBeta[j, doc.GetWord(i)]) - logPhi); lHood += ss; } } } LHood = lHood; }