/// <summary>
        ///   Computes the log-likelihood of the observations given this model.
        /// </summary>
        /// 
        public double[] LogLikelihood(T[] observations)
        {
            double[] logLikelihoods = new double[NumberOfOutputs];


#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;

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

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

            double[,] fwd = ForwardBackwardAlgorithm.LogForward(Function.Factors[0], observations, 0, out logLikelihood);
            return(logLikelihood);
        }
        private double[] computeLogLikelihood(T[] observations)
        {
            double[] logLikelihoods = new double[Outputs];

            // For all possible outputs for the model,
            Parallel.For(0, logLikelihoods.Length, y =>
            {
                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;

#if DEBUG
                if (Double.IsNaN(logLikelihood))
                {
                    throw new Exception();
                }
#endif
            });

            return(logLikelihoods);
        }
Beispiel #4
0
        private double[] computeLogLikelihood(T[] observations)
        {
            double[] logLikelihoods = new double[Outputs];


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

            for (int y = 0; y < logLikelihoods.Length; y++)
#else
            global::Accord.Threading.Tasks.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
);
#endif

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