Accord.Statistics.Models.Markov.Learning.BaumWelchLearningBase.HasConverged C# (CSharp) Method

HasConverged() protected method

Checks if a model has converged given the likelihoods between two iterations of the Baum-Welch algorithm and a criteria for convergence.
protected HasConverged ( double oldLikelihood, double newLikelihood, int currentIteration ) : bool
oldLikelihood double
newLikelihood double
currentIteration int
return bool
        protected bool HasConverged(double oldLikelihood, double newLikelihood, int currentIteration)
        {
            // Update and verify stop criteria
            if (tolerance > 0)
            {
                // Stopping criteria is likelihood convergence
                if (System.Math.Abs(oldLikelihood - newLikelihood) <= tolerance)
                    return true;

                if (maxIterations > 0)
                {
                    // Maximum iterations should also be respected
                    if (currentIteration >= maxIterations)
                        return true;
                }
            }
            else
            {
                // Stopping criteria is number of iterations
                if (currentIteration == maxIterations)
                    return true;
            }

            // Check if we have reached an invalid state
            if (Double.IsNaN(newLikelihood) || Double.IsInfinity(newLikelihood))
            {
                return true;
            }

            return false;
        }