public static double ldaInference(Document doc, LdaModel model,
        double[] varGamma, double[][] phi) {
        double converged = 1;
        double phisum = 0, likelihood = 0, likelihoodOld = Double.NEGATIVE_INFINITY;
        double[] oldphi = new double[model.getNumTopics()];
        int k, n, varIter;

        assert model.getNumTopics() > 0;
        for (k = 0; k < model.getNumTopics(); k++) {
            varGamma[k] = model.getAlpha() + doc.getTotal()
                / (double) model.getNumTopics();
            for (n = 0; n < doc.getLength(); n++)
                phi[n][k] = 1.0 / model.getNumTopics();
        }
        varIter = 0;
        while ((converged > VAR_CONVERGED) && (varIter < VAR_MAX_ITER)) {
            varIter++;
            for (n = 0; n < doc.getLength(); n++) {
                phisum = 0;
                for (k = 0; k < model.getNumTopics(); k++) {
                    oldphi[k] = phi[n][k];
                    assert varGamma[k] != 0;
                    if (model.getClassWord(k, doc.getWord(n)) > 0) {
                        assert model.getClassTotal(k) != 0;
                        phi[n][k] = digamma(varGamma[k])
                            + log(model.getClassWord(k, doc.getWord(n)))
                            - log(model.getClassTotal(k));
                    } else {
                        phi[n][k] = digamma(varGamma[k]) - 100;
                    }
                    if (k > 0) {
                        phisum = logSum(phisum, phi[n][k]);
                    } else {
                        phisum = phi[n][k];
                    }
                }
                for (k = 0; k < model.getNumTopics(); k++) {
                    phi[n][k] = exp(phi[n][k] - phisum);
                    varGamma[k] = varGamma[k] + doc.getCount(n)
                        * (phi[n][k] - oldphi[k]);
                }
            }
            likelihood = computeLikelihood(doc, model, phi, varGamma);
            assert likelihoodOld != 0;
            converged = (likelihoodOld - likelihood) / likelihoodOld;
            likelihoodOld = likelihood;
        }
        return likelihood;
    }