def maximizeLikelihood(self, optimize_brlen=False,<EOL>approx_grad=False, logliktol=<NUM_LIT>, nparamsretry=<NUM_LIT:1>,<EOL>printfunc=None):
<EOL>assert len(self.paramsarray) > <NUM_LIT:0>, "<STR_LIT>"<EOL>assert nparamsretry >= <NUM_LIT:0><EOL>assert logliktol > <NUM_LIT:0><EOL>def paramsfunc(x):<EOL><INDENT>"""<STR_LIT>"""<EOL>self.paramsarray = x<EOL>return -self.loglik<EOL><DEDENT>def paramsdfunc(x):<EOL><INDENT>"""<STR_LIT>"""<EOL>self.paramsarray = x<EOL>return -self.dloglikarray<EOL><DEDENT>def tfunc(x):<EOL><INDENT>"""<STR_LIT>"""<EOL>self.t = x<EOL>return -self.loglik<EOL><DEDENT>def tdfunc(x):<EOL><INDENT>"""<STR_LIT>"""<EOL>self.t = x<EOL>return -self.dloglik_dt<EOL><DEDENT>if approx_grad:<EOL><INDENT>paramsdfunc = False<EOL>tdfunc = False<EOL>self.dtcurrent = False<EOL>self.dparamscurrent = False<EOL><DEDENT>def _printResult(opttype, result, i, old, new):<EOL><INDENT>"""<STR_LIT>"""<EOL>if printfunc is not None:<EOL><INDENT>printfunc('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(<EOL>i, opttype, old, new,<EOL>scipy.absolute(result.jac).max(), result))<EOL><DEDENT><DEDENT>oldloglik = self.loglik<EOL>converged = False<EOL>firstbrlenpass = True<EOL>options = {'<STR_LIT>':<NUM_LIT>} <EOL>summary = []<EOL>i = <NUM_LIT:1><EOL>while not converged:<EOL><INDENT>if (not self.dparamscurrent) and (not approx_grad):<EOL><INDENT>self.dtcurrent = False<EOL>self.dparamscurrent = True<EOL><DEDENT>nparamstry = <NUM_LIT:0><EOL>origparamsarray = self.paramsarray.copy()<EOL>paramsconverged = False<EOL>while not paramsconverged:<EOL><INDENT>result = scipy.optimize.minimize(paramsfunc, self.paramsarray,<EOL>method='<STR_LIT>', jac=paramsdfunc,<EOL>bounds=self.paramsarraybounds, options=options)<EOL>_printResult('<STR_LIT>', result, i, oldloglik, self.loglik)<EOL>msg = ('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(i, oldloglik, self.loglik, result.nit,<EOL>result.nfev))<EOL>summary.append(msg)<EOL>if result.success and (not (oldloglik - self.loglik > logliktol)):<EOL><INDENT>paramsconverged = True<EOL>jacmax = scipy.absolute(result.jac).max()<EOL>if (jacmax > <NUM_LIT:1000>) and not (firstbrlenpass and optimize_brlen):<EOL><INDENT>warnings.warn("<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>jacmax, summary))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if not result.success:<EOL><INDENT>resultmessage = result.message<EOL><DEDENT>else:<EOL><INDENT>resultmessage = ('<STR_LIT>'<EOL>'<STR_LIT>'.format(oldloglik, self.loglik))<EOL><DEDENT>nparamstry += <NUM_LIT:1><EOL>failmsg = ("<STR_LIT>".format(<EOL>nparamstry, resultmessage, '<STR_LIT:\n>'.join(summary)))<EOL>if nparamstry > nparamsretry:<EOL><INDENT>raise RuntimeError(failmsg)<EOL><DEDENT>else:<EOL><INDENT>warnings.warn(failmsg + '<STR_LIT>' +<EOL>"<STR_LIT>")<EOL>scipy.random.seed(nparamstry)<EOL>minarray = scipy.array([self.paramsarraybounds[j][<NUM_LIT:0>] for<EOL>j in range(len(self.paramsarray))])<EOL>maxarray = scipy.array([self.paramsarraybounds[j][<NUM_LIT:1>] for<EOL>j in range(len(self.paramsarray))])<EOL>randarray = scipy.random.uniform(minarray, maxarray)<EOL>newarray = (minarray * maxarray * randarray *<EOL>origparamsarray)**(<NUM_LIT:1> / <NUM_LIT>) <EOL>assert newarray.shape == self.paramsarray.shape<EOL>assert (newarray > minarray).all()<EOL>assert (newarray < maxarray).all()<EOL>self.paramsarray = newarray<EOL><DEDENT><DEDENT><DEDENT>i += <NUM_LIT:1><EOL>assert oldloglik - self.loglik <= logliktol<EOL>if (self.loglik - oldloglik >= logliktol) or firstbrlenpass:<EOL><INDENT>firstbrlenpass = False<EOL>oldloglik = self.loglik<EOL>if optimize_brlen:<EOL><INDENT>if not approx_grad:<EOL><INDENT>self.dparamscurrent = False<EOL>self.dtcurrent = True<EOL><DEDENT>result = scipy.optimize.minimize(tfunc, self.t,<EOL>method='<STR_LIT>', jac=tdfunc, options=options,<EOL>bounds=[(ALMOST_ZERO, None)] * len(self.t))<EOL>_printResult('<STR_LIT>', result, i, oldloglik, self.loglik)<EOL>summary.append('<STR_LIT>'<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(i, oldloglik,<EOL>self.loglik, result.nit, result.nfev))<EOL>i += <NUM_LIT:1><EOL>assert result.success, ("<STR_LIT>"<EOL>"<STR_LIT>".format(result.message, self.t,<EOL>'<STR_LIT:\n>'.join(summary)))<EOL>if oldloglik - self.loglik > logliktol:<EOL><INDENT>raise RuntimeError("<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>oldloglik, self.loglik))<EOL><DEDENT>elif self.loglik - oldloglik >= logliktol:<EOL><INDENT>oldloglik = self.loglik<EOL><DEDENT>else:<EOL><INDENT>converged = True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>converged = True<EOL><DEDENT><DEDENT>else:<EOL><INDENT>converged = True<EOL><DEDENT><DEDENT>return '<STR_LIT:\n>'.join(summary)<EOL>
Maximize the log likelihood.

        Maximizes log likelihood with respect to model parameters
        and potentially branch lengths depending on `optimize_brlen`.
        If optimizing the branch lengths, iterates between optimizing
        the model parameters and branch lengths.

        Uses the L-BFGS-B method implemented in `scipy.optimize`.

        There is no return variable, but after call object attributes
        will correspond to maximimum likelihood values.

        Args:
            `optimize_brlen` (bool)
                Do we optimize branch lengths?
            `approx_grad` (bool)
                If `True`, then we numerically approximate the gradient
                rather than using the analytical values.
            `logliktol` (float)
                When using `optimize_brlen`, keep iterating between
                optimization of parameters and branch lengths until
                change in log likelihood is less than `logliktol`.
            `nparamsretry` (int >= 0)
                Number of times to retry parameter optimization from
                different initial values if it fails the first time.
            `printfunc` (`None` or a function)
                If not `None`, then we print using `printfunc` the
                detailed results of the optimization at each step.
                For instance, `printfunc` might be `sys.stderr.write`
                or `logger.info`.

        Returns:
            A string giving a summary of the maximization.