Esempio n. 1
0
        def d2branchlk(probs1, probs2, seqlen, bgfreq, kappa, t):

            model1 = spidir.make_hky_matrix(bgfreq, kappa, t)
            model2 = spidir.make_hky_matrix(bgfreq, kappa, 0.0)

            dmodel1 = spidir.make_hky_deriv_matrix(bgfreq, kappa, t)
            dmodel2 = spidir.make_hky_deriv_matrix(bgfreq, kappa, 0.0)

            d2model1 = spidir.make_hky_deriv2_matrix(bgfreq, kappa, t)
            d2model2 = spidir.make_hky_deriv2_matrix(bgfreq, kappa, 0.0)

            logl = 0.0
            for j in xrange(seqlen):
                g = sum(bgfreq[k] * sum(model1[k][x] * probs1[4 * j + x]
                                        for x in xrange(4)) *
                        sum(model2[k][y] * probs2[4 * j + y]
                            for y in xrange(4)) for k in xrange(4))

                dg = sum(bgfreq[k] * sum(dmodel1[k][x] * probs1[4 * j + x]
                                         for x in xrange(4)) *
                         sum(model2[k][y] * probs2[4 * j + y]
                             for y in xrange(4)) for k in xrange(4))

                d2g = sum(bgfreq[k] * sum(d2model1[k][x] * probs1[4 * j + x]
                                          for x in xrange(4)) *
                          sum(model2[k][y] * probs2[4 * j + y]
                              for y in xrange(4)) for k in xrange(4))


                logl += - safediv(dg*dg, g*g, INF) + \
                        safediv(d2g, g, INF)
            return logl
Esempio n. 2
0
        def d2branchlk(probs1, probs2, seqlen, bgfreq, kappa, t):

            model1 = spidir.make_hky_matrix(bgfreq, kappa, t)
            model2 = spidir.make_hky_matrix(bgfreq, kappa, 0.0)

            dmodel1 = spidir.make_hky_deriv_matrix(bgfreq, kappa, t)
            dmodel2 = spidir.make_hky_deriv_matrix(bgfreq, kappa, 0.0)

            d2model1 = spidir.make_hky_deriv2_matrix(bgfreq, kappa, t)
            d2model2 = spidir.make_hky_deriv2_matrix(bgfreq, kappa, 0.0)

            logl = 0.0
            for j in xrange(seqlen):
                g = sum(
                    bgfreq[k]
                    * sum(model1[k][x] * probs1[4 * j + x] for x in xrange(4))
                    * sum(model2[k][y] * probs2[4 * j + y] for y in xrange(4))
                    for k in xrange(4)
                )

                dg = sum(
                    bgfreq[k]
                    * sum(dmodel1[k][x] * probs1[4 * j + x] for x in xrange(4))
                    * sum(model2[k][y] * probs2[4 * j + y] for y in xrange(4))
                    for k in xrange(4)
                )

                d2g = sum(
                    bgfreq[k]
                    * sum(d2model1[k][x] * probs1[4 * j + x] for x in xrange(4))
                    * sum(model2[k][y] * probs2[4 * j + y] for y in xrange(4))
                    for k in xrange(4)
                )

                logl += -safediv(dg * dg, g * g, INF) + safediv(d2g, g, INF)
            return logl
Esempio n. 3
0
    def test_hky_deriv2(self):
        """general test"""

        bgfreq = [.3, .2, .3, .2]
        kappa = 2.0
        i = random.randint(0, 3)
        j = random.randint(0, 3)

        x = list(frange(0, 1.0, .01))
        y = [spidir.make_hky_deriv_matrix(bgfreq, kappa, t)[i][j] for t in x]
        dy = [spidir.make_hky_deriv2_matrix(bgfreq, kappa, t)[i][j] for t in x]
        dy2 = [(spidir.make_hky_deriv_matrix(bgfreq, kappa, t + .01)[i][j] -
                spidir.make_hky_deriv_matrix(bgfreq, kappa, t)[i][j]) / .01
               for t in x]

        prep_dir("test/output/hky2")

        rplot_start("test/output/hky2/hky_deriv2.pdf")
        rplot("plot", x, y, t="l", ylim=[min(dy2 + dy + y), max(dy2 + dy + y)])
        rp.lines(x, dy, col="red")
        rp.lines(x, dy2, col="blue")
        rplot_end(True)
Esempio n. 4
0
    def test_hky_deriv2(self):
        """general test"""
        
        bgfreq = [.3, .2, .3, .2]
        kappa = 2.0
        i = random.randint(0, 3)
        j = random.randint(0, 3)
        
        x = list(frange(0, 1.0, .01))
        y = [spidir.make_hky_deriv_matrix(bgfreq, kappa, t)[i][j]
             for t in x]
        dy = [spidir.make_hky_deriv2_matrix(bgfreq, kappa, t)[i][j]
              for t in x]
        dy2 = [(spidir.make_hky_deriv_matrix(bgfreq, kappa, t+.01)[i][j] -
                spidir.make_hky_deriv_matrix(bgfreq, kappa, t)[i][j]) / .01
               for t in x]

        prep_dir("test/output/hky2")
                
        rplot_start("test/output/hky2/hky_deriv2.pdf")
        rplot("plot", x, y, t="l", ylim=[min(dy2 + dy + y), max(dy2 + dy + y)])
        rp.lines(x, dy, col="red")
        rp.lines(x, dy2, col="blue")
        rplot_end(True)