code: def _fit_m(D, a0, logp, tol=1e-7, maxiter=1000):
      '''With fixed precision s, maximize mean m'''
      N,K = D.shape
      s = a0.sum()
  
      for i in xrange(maxiter):
          m = a0 / s
          a1 = _ipsi(logp + (m*(psi(a0) - logp)).sum())
          a1 = a1/a1.sum() * s
  
          if norm(a1 - a0) < tol:
              return a1
          a0 = a1
  
      raise Exception('Failed to converge after {} iterations, s is {}'
              .format(maxiter, s)f _fit_m(D, a0, logp, tol=1e-7, maxiter=1000):
      '''With fixed precision s, maximize mean m'''
      N,K = D.shape
      s = a0.sum()
  
      for i in xrange(maxiter):
          m = a0 / s
          a1 = _ipsi(logp + (m*(psi(a0) - logp)).sum())
          a1 = a1/a1.sum() * s
  
          if norm(a1 - a0) < tol:
              return a1
          a0 = a1
  
      raise Exception('Failed to converge after {} iterations, s is {}'
              .format(maxiter, s))
docs: With fixed precision s, maximize mean m