/// <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);
            }

            System.Diagnostics.Debug.Assert(!Double.IsNaN(sum));

            return Math.Log(sum);
        }
        /// <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 Forward probabilities for a given potential function and a set of observations.
        /// </summary>
        ///
        public static double[,] LogForward <TObservation>(FactorPotential <TObservation> function,
                                                          TObservation[] observations, int output, out double logLikelihood)
        {
            int states = function.States;

            double[,] lnFwd = new double[observations.Length, states];
            int T = observations.Length;

            ForwardBackwardAlgorithm.LogForward(function, observations, output, lnFwd);

            logLikelihood = Double.NegativeInfinity;
            for (int j = 0; j < states; j++)
            {
                logLikelihood = Special.LogSum(logLikelihood, lnFwd[T - 1, j]);
            }

            return(lnFwd);
        }
Exemple #4
0
        /// <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);
        }