Example #1
0
def test_dK_dtheta_5():
    n = 4
    theta = np.array([
        2.59193443e-02, 0.00000000e+00, 6.83797216e-07, 3.08837678e-03,
        0.00000000e+00, 2.56956907e-02, -1.48051536e+00, -1.51759911e+00,
        -1.34983215e+00, -1.22431771e+00
    ])
    size = len(theta)

    dK1 = np.zeros((size, n, n))
    dK2 = np.zeros((size, n, n))
    dK3 = np.zeros((size, n, n))

    for u in range(size):
        _ratematrix.dK_dtheta_ij(theta, n, u, A=None, out=dK1[u, :, :])
    for i in range(n):
        for j in range(n):
            _ratematrix.dK_dtheta_u(theta, n, i, j, out=dK2[:, i, j])
    for i in range(n):
        for j in range(n):
            dKij = np.zeros(size)
            _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKij)
            dK3[:, i, j] = dKij

    np.testing.assert_almost_equal(dK1, dK2)
    np.testing.assert_almost_equal(dK1, dK3)
    np.testing.assert_almost_equal(dK2, dK3)
def test_dK_dtheta_5():
    n = 4
    theta = np.array(
        [  2.59193443e-02,  0.00000000e+00,  6.83797216e-07,   3.08837678e-03,
           0.00000000e+00,  2.56956907e-02,  -1.48051536e+00,  -1.51759911e+00,
          -1.34983215e+00, -1.22431771e+00])
    size = len(theta)


    dK1 = np.zeros((size, n, n))
    dK2 = np.zeros((size, n, n))
    dK3 = np.zeros((size, n, n))

    for u in range(size):
        _ratematrix.dK_dtheta_ij(theta, n, u, A=None, out=dK1[u, :, :])
    for i in range(n):
        for j in range(n):
            _ratematrix.dK_dtheta_u(theta, n, i, j, out=dK2[:, i, j])
    for i in range(n):
        for j in range(n):
            dKij = np.zeros(size)
            _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKij)
            dK3[:, i, j] = dKij

    np.testing.assert_almost_equal(dK1, dK2)
    np.testing.assert_almost_equal(dK1, dK3)
    np.testing.assert_almost_equal(dK2, dK3)
 def grad(x, i, j):
     # gradient of the (i,j) entry of the rate matrix w.r.t. theta
     dKu = np.zeros((n, n))
     g = np.zeros(len(x))
     for u in range(len(x)):
         _ratematrix.dK_dtheta_ij(x, n, u, None, dKu)
         g[u] = dKu[i, j]
     return g
Example #4
0
 def grad(x, i, j):
     # gradient of the (i,j) entry of the rate matrix w.r.t. theta
     dKu = np.zeros((n, n))
     g = np.zeros(len(x))
     for u in range(len(x)):
         _ratematrix.dK_dtheta_ij(x, n, u, None, dKu)
         g[u] = dKu[i, j]
     return g
def test_dK_dtheta_2():
    # test function `dK_dtheta_A` to make sure that the part that hadamards
    # the matrix against A is correct.
    n = 4
    A = random.randn(4, 4)
    theta = example_theta(n)

    for u in range(len(theta)):
        dKu = np.zeros((n, n))
        _ratematrix.dK_dtheta_ij(theta, n, u, None, dKu)
        value1 = (dKu * A).sum()

        dKu = np.zeros((n, n))
        value2 = _ratematrix.dK_dtheta_ij(theta, n, u, A, dKu)
        value3 = _ratematrix.dK_dtheta_ij(theta, n, u, A)

        np.testing.assert_approx_equal(value1, value2)
        np.testing.assert_approx_equal(value1, value3)
Example #6
0
def test_dK_dtheta_2():
    # test function `dK_dtheta_A` to make sure that the part that hadamards
    # the matrix against A is correct.
    n = 4
    A = random.randn(4, 4)
    theta = example_theta(n)

    for u in range(len(theta)):
        dKu = np.zeros((n, n))
        _ratematrix.dK_dtheta_ij(theta, n, u, None, dKu)
        value1 = (dKu * A).sum()

        dKu = np.zeros((n, n))
        value2 = _ratematrix.dK_dtheta_ij(theta, n, u, A, dKu)
        value3 = _ratematrix.dK_dtheta_ij(theta, n, u, A)

        np.testing.assert_approx_equal(value1, value2)
        np.testing.assert_approx_equal(value1, value3)
Example #7
0
def test_dK_dtheta_3():
    # test dK_dtheta_ij vs dK_dtheta_u. both return slices of the same 3D
    # tensor, so by repeated calls to both functions we can build the whole
    # tensor using both approaches and check that they're equal.

    for n in [3, 4]:
        theta = example_theta(n)

        dKuij1 = np.zeros((len(theta), n, n))
        dKuij2 = np.zeros((len(theta), n, n))

        for u in range(len(theta)):
            _ratematrix.dK_dtheta_ij(theta, n, u, None, dKuij1[u])

        for i in range(n):
            for j in range(n):
                _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKuij2[:, i, j])

        np.testing.assert_array_almost_equal(dKuij1, dKuij2)
Example #8
0
def test_dK_dtheta_3():
    # test dK_dtheta_ij vs dK_dtheta_u. both return slices of the same 3D
    # tensor, so by repeated calls to both functions we can build the whole
    # tensor using both approaches and check that they're equal.

    for n in [3, 4]:
        theta = example_theta(n)

        dKuij1 = np.zeros((len(theta), n, n))
        dKuij2 = np.zeros((len(theta), n, n))

        for u in range(len(theta)):
            _ratematrix.dK_dtheta_ij(theta, n, u, None, dKuij1[u])

        for i in range(n):
            for j in range(n):
                _ratematrix.dK_dtheta_u(theta, n, i, j, out=dKuij2[:, i, j])

        np.testing.assert_array_almost_equal(dKuij1, dKuij2)
    def grad(theta, i):
        # gradient of the ith eigenvalue of K with respect to theta
        K = np.zeros((n, n))
        _ratematrix.build_ratemat(theta, n, K)
        w, V = scipy.linalg.eig(K)
        order = np.argsort(np.real(w))

        V = np.real(np.ascontiguousarray(V[:, order]))
        U = np.ascontiguousarray(scipy.linalg.inv(V).T)

        g = np.zeros(len(theta))

        for u in range(len(theta)):
            dKu = np.zeros((n, n))
            _ratematrix.dK_dtheta_ij(theta, n, u, None, dKu)
            out = np.zeros(n)
            temp = np.zeros(n)
            _ratematrix.dw_du(dKu, U, V, n, temp, out)
            g[u] = out[i]
        return g
Example #10
0
    def grad(theta, i):
        # gradient of the ith eigenvalue of K with respect to theta
        K = np.zeros((n, n))
        _ratematrix.build_ratemat(theta, n, K)
        w, V = scipy.linalg.eig(K)
        order = np.argsort(np.real(w))

        V = np.real(np.ascontiguousarray(V[:, order]))
        U = np.ascontiguousarray(scipy.linalg.inv(V).T)

        g = np.zeros(len(theta))

        for u in range(len(theta)):
            dKu = np.zeros((n, n))
            _ratematrix.dK_dtheta_ij(theta, n, u, None, dKu)
            out = np.zeros(n)
            temp = np.zeros(n)
            _ratematrix.dw_du(dKu, U, V, n, temp, out)
            g[u] = out[i]
        return g