# Path: mlpy/optimize/utils.py
# def is_converged(fval, prev_fval, thresh=1e-4, warn=False):
#     """Check if an objective function has converged.
# 
#     Parameters
#     ----------
#     fval : float
#         The current value.
#     prev_fval : float
#         The previous value.
#     thresh : float
#         The convergence threshold.
#     warn : bool
#         Flag indicating whether to warn the user when the
#         fval decreases.
# 
#     Returns
#     -------
#     bool :
#         Flag indicating whether the objective function has converged or not.
# 
#     Notes
#     -----
#     The test returns true if the slope of the function falls below the threshold; i.e.
# 
#     .. math::
# 
#         \\frac{|f(t) - f(t-1)|}{\\text{avg}} < \\text{thresh},
# 
#     where
# 
#     .. math::
# 
#          \\text{avg} = \\frac{|f(t)| + |f(t+1)|}{2}
# 
#     .. note::
#         Ported from Matlab:
# 
#         | Project: `Probabilistic Modeling Toolkit for Matlab/Octave <https://github.com/probml/pmtk3>`_.
#         | Copyright (2010) Kevin Murphy and Matt Dunham
#         | License: `MIT <https://github.com/probml/pmtk3/blob/5fefd068a2e84ae508684d3e4750bd72a4164ba0/license.txt>`_
# 
#     """
#     converged = False
#     delta_fval = abs(fval - prev_fval)
#     # noinspection PyTypeChecker
#     avg_fval = np.true_divide(abs(fval) + abs(prev_fval) + np.spacing(1), 2)
# 
#     if float(delta_fval) / float(avg_fval) < thresh:
#         converged = True
# 
#     if warn and (fval - prev_fval) < -2 * np.spacing(1):
#         print("is_converged: fval decrease, objective decreased!")
# 
#     return converged


from six.moves import range
from abc import ABCMeta, abstractmethod
from .utils import is_converged
import numpy as np
                if self._verbose:
                    print("\n********** Random Restart {0} **********".format(i+1))
                ll_hists.append(self._em(x, init_count=i))
                models.append(self.__dict__.copy())
                self.__dict__ = init_model
                best_ll[i] = ll_hists[i][-1]
            best_ndx = best_ll.argmax()
            self.__dict__ = models[best_ndx]
            return ll_hists[best_ndx]

        if self._verbose:
            print("initializing model for EM")
        self._initialize(x, init_count)
        ndx = 0
        done = False
        loglik_hist = np.zeros(self._n_iter)

        while not done:
            # stats, loglik_hist[ndx] = self._estep(x)
            loglik_hist[ndx] = self._estep(x)
            if self._verbose:
                print("\t{0} loglik: {1}".format(ndx+1, loglik_hist[ndx]))

            self._plot()

            self._mstep()

            if ndx >= self._n_iter - 1:
                break
            elif ndx > 0:

# Path: mlpy/optimize/utils.py
# def is_converged(fval, prev_fval, thresh=1e-4, warn=False):
#     """Check if an objective function has converged.
# 
#     Parameters
#     ----------
#     fval : float
#         The current value.
#     prev_fval : float
#         The previous value.
#     thresh : float
#         The convergence threshold.
#     warn : bool
#         Flag indicating whether to warn the user when the
#         fval decreases.
# 
#     Returns
#     -------
#     bool :
#         Flag indicating whether the objective function has converged or not.
# 
#     Notes
#     -----
#     The test returns true if the slope of the function falls below the threshold; i.e.
# 
#     .. math::
# 
#         \\frac{|f(t) - f(t-1)|}{\\text{avg}} < \\text{thresh},
# 
#     where
# 
#     .. math::
# 
#          \\text{avg} = \\frac{|f(t)| + |f(t+1)|}{2}
# 
#     .. note::
#         Ported from Matlab:
# 
#         | Project: `Probabilistic Modeling Toolkit for Matlab/Octave <https://github.com/probml/pmtk3>`_.
#         | Copyright (2010) Kevin Murphy and Matt Dunham
#         | License: `MIT <https://github.com/probml/pmtk3/blob/5fefd068a2e84ae508684d3e4750bd72a4164ba0/license.txt>`_
# 
#     """
#     converged = False
#     delta_fval = abs(fval - prev_fval)
#     # noinspection PyTypeChecker
#     avg_fval = np.true_divide(abs(fval) + abs(prev_fval) + np.spacing(1), 2)
# 
#     if float(delta_fval) / float(avg_fval) < thresh:
#         converged = True
# 
#     if warn and (fval - prev_fval) < -2 * np.spacing(1):
#         print("is_converged: fval decrease, objective decreased!")
# 
#     return converged


# Path: mlpy/optimize/algorithms.py
from six.moves import range
from abc import ABCMeta, abstractmethod
from .utils import is_converged
import numpy as np

                if self._verbose:
                    print("\n********** Random Restart {0} **********".format(i+1))
                ll_hists.append(self._em(x, init_count=i))
                models.append(self.__dict__.copy())
                self.__dict__ = init_model
                best_ll[i] = ll_hists[i][-1]
            best_ndx = best_ll.argmax()
            self.__dict__ = models[best_ndx]
            return ll_hists[best_ndx]

        if self._verbose:
            print("initializing model for EM")
        self._initialize(x, init_count)
        ndx = 0
        done = False
        loglik_hist = np.zeros(self._n_iter)

        while not done:
            # stats, loglik_hist[ndx] = self._estep(x)
            loglik_hist[ndx] = self._estep(x)
            if self._verbose:
                print("\t{0} loglik: {1}".format(ndx+1, loglik_hist[ndx]))

            self._plot()

            self._mstep()

            if ndx >= self._n_iter - 1:
                break
            elif ndx > 0:

                done = is_converged(loglik_hist[ndx], loglik_hist[ndx - 1], thresh=self._thresh)