/** * Digamma function (the first derivative of the logarithm of the gamma function). * * @param array - variational parameter * @return */ static double[][] dirichletExpectation(double[][] array) { int numRows = array.length; int numCols = array[0].length; double[] vector = new double[numRows]; Arrays.fill(vector, 0.0); for (int k = 0; k < numRows; ++k) { for (int w = 0; w < numCols; ++w) { try { vector[k] += array[k][w]; } catch (Exception e) { throw new RuntimeException(e); } } } for (int k = 0; k < numRows; ++k) { vector[k] = Gamma.digamma(vector[k]); } double[][] approx = new double[numRows][]; for (int k = 0; k < numRows; ++k) { approx[k] = new double[numCols]; for (int w = 0; w < numCols; ++w) { double z = Gamma.digamma(array[k][w]); approx[k][w] = z - vector[k]; } } return approx; }
static double[] dirichletExpectation(double[] array) { double sum = 0; for (double d : array) { sum += d; } double d = Gamma.digamma(sum); double[] result = new double[array.length]; for (int i = 0; i < array.length; ++i) { result[i] = Gamma.digamma(array[i]) - d; } return result; }