private double[] computeLogLikelihood(T[] observations)
        {
            double[] logLikelihoods = new double[Outputs];


#if SERIAL || DEBUG  // For all possible outputs for the model,

            for (int y = 0; y < logLikelihoods.Length; y++)
#else
            Parallel.For(0, logLikelihoods.Length, y =>
#endif
            {
                double logLikelihood;

                // Compute the factor log-likelihood for the output
                ForwardBackwardAlgorithm.LogForward(Function.Factors[y],
                    observations, y, out logLikelihood);

                // Accumulate output's likelihood
                logLikelihoods[y] = logLikelihood;

                System.Diagnostics.Debug.Assert(!Double.IsNaN(logLikelihood));
            }
#if !(SERIAL || DEBUG)
);
#endif

            return logLikelihoods;
        }
Ejemplo n.º 2
0
        /// <summary>
        ///   Computes the Log of the partition function.
        /// </summary>
        ///
        public double LogPartition(T[] observations)
        {
            double logLikelihood;

            ForwardBackwardAlgorithm.LogForward(Function.Factors[0], observations, 0, out logLikelihood);
            return(logLikelihood);
        }
Ejemplo n.º 3
0
        /// <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)
        {
            int states = function.States;

            double[,] lnFwd = new double[observations.Length, states];
            ForwardBackwardAlgorithm.LogForward(function, observations, output, lnFwd);
            return(lnFwd);
        }
Ejemplo n.º 4
0
        /// <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]);
            }

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

            return(lnFwd);
        }