Esempio n. 1
0
def optLogLike(self,
               verbose=1,
               newtAndBrentPowell=1,
               allBrentPowell=0,
               simplex=0):
    """Calculate the likelihood of the tree, with optimization.

    There are 3 optimization methods-- choose one.  I've made
    'newtAndBrentPowell' the default, as it is fast and seems to be
    working.  The 'allBrentPowell' optimizer used to be the default,
    as it seems to be the most robust, although it is slow.  It would
    be good for checking important calculations.  The simplex
    optimizer is the slowest, and will sometimes find better optima
    for difficult data, but often fails to optimize (with no
    warning)."""

    if verbose:
        theStartTime = time.clock()
    self._commonCStuff()

    # We want only one opt method.
    if newtAndBrentPowell:
        newtAndBrentPowell = 1
    if allBrentPowell:
        allBrentPowell = 1
    if simplex:
        simplex = 1
    if (newtAndBrentPowell + allBrentPowell + simplex) != 1:
        gm = ['Tree.optLogLike()']
        gm.append("Choose 1 opt method.")
        raise Glitch, gm

    # Do the opt.
    if allBrentPowell:
        pf.p4_allBrentPowellOptimize(self.cTree)
    elif simplex:
        from Tree import Tree
        pf.p4_simplexOptimize(self.cTree, self, Tree.simplexDump)
    else:
        pf.p4_newtSetup(self.cTree)
        pf.p4_newtAndBrentPowellOpt(self.cTree)

    self.logLike = pf.p4_treeLogLike(self.cTree,
                                     0)  # second arg is getSiteLikes

    # get the brLens
    brLens = pf.p4_getBrLens(self.cTree)
    for n in self.iterNodesNoRoot():
        n.br.len = brLens[n.nodeNum]

    # get the other free prams
    prams = pf.p4_getFreePrams(self.cTree)
    self.model.restoreFreePrams(prams)

    if verbose:
        print "optLogLike = %f" % self.logLike
        theEndTime = time.clock()
        print "cpu time %s seconds." % (theEndTime - theStartTime)
Esempio n. 2
0
def optLogLike(self, verbose=1, newtAndBrentPowell=1, allBrentPowell=0, simplex=0):
    """Calculate the likelihood of the tree, with optimization.

    There are 3 optimization methods-- choose one.  I've made
    'newtAndBrentPowell' the default, as it is fast and seems to be
    working.  The 'allBrentPowell' optimizer used to be the default,
    as it seems to be the most robust, although it is slow.  It would
    be good for checking important calculations.  The simplex
    optimizer is the slowest, and will sometimes find better optima
    for difficult data, but often fails to optimize (with no
    warning)."""


    if verbose:
        theStartTime = time.clock()
    self._commonCStuff()

    # We want only one opt method.
    if newtAndBrentPowell:
        newtAndBrentPowell = 1
    if allBrentPowell:
        allBrentPowell = 1
    if simplex:
        simplex = 1
    if (newtAndBrentPowell + allBrentPowell + simplex) != 1:
        gm = ['Tree.optLogLike()']
        gm.append("Choose 1 opt method.")
        raise Glitch(gm)
    
    # Do the opt.
    if allBrentPowell:
        pf.p4_allBrentPowellOptimize(self.cTree)
    elif simplex:
        from .Tree import Tree
        pf.p4_simplexOptimize(self.cTree, self, Tree.simplexDump)
    else:
        pf.p4_newtSetup(self.cTree)
        pf.p4_newtAndBrentPowellOpt(self.cTree)

    self.logLike = pf.p4_treeLogLike(self.cTree, 0) # second arg is getSiteLikes

    # get the brLens
    brLens = pf.p4_getBrLens(self.cTree)
    for n in self.iterNodesNoRoot():
        n.br.len = brLens[n.nodeNum]

    # get the other free prams
    prams = pf.p4_getFreePrams(self.cTree)
    self.model.restoreFreePrams(prams)

    if verbose:
        print("optLogLike = %f" % self.logLike)
        theEndTime = time.clock()
        print("cpu time %s seconds." % (theEndTime - theStartTime))
Esempio n. 3
0
    def optLogLike(self, verbose=1, newtAndBrentPowell=1, allBrentPowell=0):
        """Calculate the likelihood of the tree, with optimization.

        There are two optimization methods-- choose one.  I've made
        'newtAndBrentPowell' the default, as it is fast and seems to be
        working.  The 'allBrentPowell' optimizer used to be the default,
        as it seems to be the most robust, although it is slow.  It would
        be good for checking important calculations.  
        """

        if verbose:
            theStartTime = time.clock()
        self._commonCStuff()

        # We want only one opt method.
        if newtAndBrentPowell:
            newtAndBrentPowell = 1
        if allBrentPowell:
            allBrentPowell = 1
        if (newtAndBrentPowell + allBrentPowell) != 1:
            gm = ['Tree.optLogLike()']
            gm.append("Choose 1 opt method.")
            raise P4Error(gm)

        # Do the opt.
        if allBrentPowell:
            pf.p4_allBrentPowellOptimize(self.cTree)
        else:
            pf.p4_newtSetup(self.cTree)
            pf.p4_newtAndBrentPowellOpt(self.cTree)

        # second arg is getSiteLikes
        self.logLike = pf.p4_treeLogLike(self.cTree, 0)

        # get the brLens
        brLens = pf.p4_getBrLens(self.cTree)
        for n in self.iterNodesNoRoot():
            n.br.len = brLens[n.nodeNum]

        # get the other free prams
        prams = pf.p4_getFreePrams(self.cTree)
        self.model.restoreFreePrams(prams)

        if verbose:
            print "optLogLike = %f" % self.logLike
            theEndTime = time.clock()
            print "cpu time %s seconds." % (theEndTime - theStartTime)