/// <summary> /// Computes Forward probabilities for a given potential function and a set of observations. /// </summary> /// public static double[,] Forward <TObservation>(FactorPotential <TObservation> function, TObservation[] observations, int output, out double logLikelihood) { int states = function.States; double[,] fwd = new double[observations.Length, states]; double[] coefficients = new double[observations.Length]; ForwardBackwardAlgorithm.Forward(function, observations, output, coefficients, fwd); logLikelihood = 0; for (int i = 0; i < coefficients.Length; i++) { logLikelihood += Math.Log(coefficients[i]); } return(fwd); }
/// <summary> /// Computes the log-partition function ln Z(x). /// </summary> /// public double LogPartition(T[] x) { double sum = 0; for (int j = 0; j < Outputs; j++) { double logLikelihood; ForwardBackwardAlgorithm.Forward(Function.Factors[j], x, j, out logLikelihood); sum += Math.Exp(logLikelihood); } #if DEBUG if (Double.IsNaN(sum)) throw new Exception(); #endif return Math.Log(sum); }