Exemple #1
0
def get_response_content(fs):
    out = StringIO()
    np.set_printoptions(linewidth=200)
    # get the user defined variables
    n = fs.nstates
    # sample a random reversible CTMC rate matrix
    v = divtime.sample_distribution(n)
    S = divtime.sample_symmetric_rate_matrix(n)
    R = mrate.to_gtr_halpern_bruno(S, v)
    distn = mrate.R_to_distn(R)
    spectrum = scipy.linalg.eigvalsh(mrate.symmetrized(R))
    print >> out, "random reversible CTMC rate matrix:"
    print >> out, R
    print >> out
    print >> out, "stationary distribution:"
    print >> out, distn
    print >> out
    print >> out, "spectrum:"
    print >> out, spectrum
    print >> out
    Q = aggregate(R)
    distn = mrate.R_to_distn(Q)
    spectrum = scipy.linalg.eigvalsh(mrate.symmetrized(Q))
    print >> out, "aggregated rate matrix:"
    print >> out, Q
    print >> out
    print >> out, "stationary distribution:"
    print >> out, distn
    print >> out
    print >> out, "spectrum:"
    print >> out, spectrum
    print >> out
    return out.getvalue()
Exemple #2
0
def get_mutual_information_diff_b(R, t):
    """
    This is a more symmetrized version.
    Note that two of the three terms are probably structurally zero.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R * t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    G = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G[i, j] = 0
            for k in range(n):
                G[i, j] += U[i, k] * U[j, k] * math.exp(t * w[k])
    G_diff = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G_diff[i, j] = 0
            for k in range(n):
                G_diff[i, j] += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
    B = np.outer(U.T[-1], U.T[-1])
    term_a = np.sum(B * G_diff)
    term_b = np.sum(B * G_diff * np.log(G))
    term_c = -np.sum(B * G_diff * np.log(B))
    #print term_a
    #print term_b
    #print term_c
    return term_b
Exemple #3
0
def get_mutual_information_diff_c(R, t):
    """
    This is a more symmetrized version.
    Some structurally zero terms have been removed.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R * t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    B = np.outer(U.T[-1], U.T[-1])
    G = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G[i, j] = 0
            for k in range(n):
                G[i, j] += U[i, k] * U[j, k] * math.exp(t * w[k])
    G_diff = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G_diff[i, j] = 0
            for k in range(n):
                G_diff[i, j] += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
    return np.sum(B * G_diff * np.log(G))
Exemple #4
0
def get_asymptotic_variance_b(R, t):
    """
    Break up the sum into two parts and investigate each separately.
    The second part with only the second derivative is zero.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R*t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance
    accum_a = 0
    for i in range(n):
        for j in range(n):
            # define f
            f = p[i] * P[i, j]
            # define the first derivative of f
            f_dt = 0
            for k in range(n):
                f_dt += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            f_dt *= (p[i] * p[j])**.5
            accum_a -= (f_dt * f_dt) / f
    accum_b = 0
    for i in range(n):
        for j in range(n):
            # define the second derivative of f
            f_dtt = 0
            for k in range(n):
                f_dtt += U[i, k] * U[j, k] * w[k] * w[k] * math.exp(t * w[k])
            f_dtt *= (p[i] * p[j])**.5
            # accumulate the contribution of this entry to the expectation
            accum_b += f_dtt
    return - 1 / (accum_a + accum_b)
Exemple #5
0
def get_mutual_information_diff_zero(R):
    """
    Derivative of mutual information at time zero.
    Haha apparently this does not exist.
    """
    # get non-spectral summaries
    n = len(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    B = np.outer(U.T[-1], U.T[-1])
    G = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G[i, j] = 0
            for k in range(n):
                G[i, j] += U[i, k] * U[j, k]
    G_diff = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G_diff[i, j] = 0
            for k in range(n):
                G_diff[i, j] += U[i, k] * U[j, k] * w[k]
    print G
    print G_diff
    print B
    return np.sum(B * G_diff * np.log(G))
Exemple #6
0
def get_mutual_information_small_approx_d(R, t):
    """
    This is an approximation for small times.
    This uses all of the off-diagonal entries of the mutual information
    and also uses an approximation of the off-diagonal entries.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum_diag_a = 0
    accum_diag_b = 0
    accum_diag_c = 0
    accum_diag_d = 0
    for i in range(n):
        a = 0
        b = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        for k in range(n - 1):
            prefix = U[i, k] * U[i, k]
            b += prefix * math.exp(t * w[k])
        x1 = v[i] * v[i]
        x2 = v[i] * b
        y1 = math.log(a)
        y2 = -math.log(v[i])
        accum_diag_a += x1 * y1
        accum_diag_b += x1 * y2
        accum_diag_c += x2 * y1
        accum_diag_d += x2 * y2
    accum_a = 0
    accum_b = 0
    accum_c = 0
    accum_d = 0
    for i in range(n):
        for j in range(n):
            if i != j:
                prefix = (v[i] * v[j])**.5
                a = 0
                for k in range(n):
                    a += U[i, k] * U[j, k] * math.exp(t * w[k])
                b = 0
                for k in range(n - 1):
                    b += U[i, k] * U[j, k] * math.exp(t * w[k])
                x1 = v[i] * v[j]
                x2 = prefix * b
                y1 = math.log(a)
                y2 = -math.log(prefix)
                accum_a += x1 * y1
                accum_b += x1 * y2
                accum_c += x2 * y1
                accum_d += x2 * y2
    terms = [
        accum_diag_a, accum_diag_b, accum_diag_c, accum_diag_d, accum_a,
        accum_b, accum_c, accum_d
    ]
    for term in terms:
        print term
    return sum(terms)
Exemple #7
0
def get_mutual_information_stable(R, t):
    """
    This is a more stable function.
    @return: unscaled_result, log_of_scaling_factor
    """
    #FIXME under construction
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    P = np.zeros_like(R)
    accum = 0
    for i in range(n):
        for j in range(n):
            for k in range(n):
                a = (v[j] / v[i])**0.5
                b = U[i, k] * U[j, k]
                c = math.exp(t * w[k])
                P[i, j] += a * b * c
    # compute the unscaled part of log(X(i,j)/(X(i)*X(j)))
    for i in range(n):
        for j in range(n):
            if v[i] and P[i, j]:
                coeff = v[i] * P[i, j]
                numerator = P[i, j]
                denominator = v[j]
                # the problem is that the following log is nearly zero
                value = coeff * math.log(numerator / denominator)
                accum += np.real(value)
    return accum
Exemple #8
0
def get_asymptotic_variance_b(R, t):
    """
    Break up the sum into two parts and investigate each separately.
    The second part with only the second derivative is zero.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R * t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance
    accum_a = 0
    for i in range(n):
        for j in range(n):
            # define f
            f = p[i] * P[i, j]
            # define the first derivative of f
            f_dt = 0
            for k in range(n):
                f_dt += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            f_dt *= (p[i] * p[j])**.5
            accum_a -= (f_dt * f_dt) / f
    accum_b = 0
    for i in range(n):
        for j in range(n):
            # define the second derivative of f
            f_dtt = 0
            for k in range(n):
                f_dtt += U[i, k] * U[j, k] * w[k] * w[k] * math.exp(t * w[k])
            f_dtt *= (p[i] * p[j])**.5
            # accumulate the contribution of this entry to the expectation
            accum_b += f_dtt
    return -1 / (accum_a + accum_b)
Exemple #9
0
def get_mutual_information_small_approx_b(R, t):
    """
    This is an approximation for small times.
    Check a decomposition.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum_a = 0
    accum_b = 0
    accum_c = 0
    accum_d = 0
    for i in range(n):
        a = 0
        b = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        for k in range(n - 1):
            prefix = U[i, k] * U[i, k]
            b += prefix * math.exp(t * w[k])
        x1 = v[i] * v[i]
        x2 = v[i] * b
        y1 = math.log(a)
        y2 = -math.log(v[i])
        accum_a += x1 * y1
        accum_b += x1 * y2
        accum_c += x2 * y1
        accum_d += x2 * y2
    return accum_a + accum_b + accum_c + accum_d
Exemple #10
0
def get_asymptotic_variance_e(R, t):
    """
    Try to mitigate the damage of the aggressive approximation.
    The next step is to try to simplify this complicated correction.
    But I have not been able to do this.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R * t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance approximation
    accum = 0
    for k in range(n - 1):
        accum += w[k] * w[k] * math.exp(2 * t * w[k])
    accum_b = 0
    G_a = np.zeros_like(R)
    G_b = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            prefix = (p[i] * p[j])**-.5
            a = 0
            for k in range(n - 1):
                a += U[i, k] * U[j, k] * math.exp(t * w[k])
            b = 0
            for k in range(n - 1):
                b += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            suffix = a * b * b
            value = prefix * suffix
            accum_b += value
    return 1 / (accum - accum_b)
Exemple #11
0
def get_mutual_information_diff_b(R, t):
    """
    This is a more symmetrized version.
    Note that two of the three terms are probably structurally zero.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R*t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    G = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G[i, j] = 0
            for k in range(n):
                G[i, j] += U[i, k] * U[j, k] * math.exp(t * w[k])
    G_diff = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G_diff[i, j] = 0
            for k in range(n):
                G_diff[i, j] += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
    B = np.outer(U.T[-1], U.T[-1])
    term_a = np.sum(B * G_diff)
    term_b = np.sum(B * G_diff * np.log(G))
    term_c = -np.sum(B * G_diff * np.log(B))
    #print term_a
    #print term_b
    #print term_c
    return term_b
Exemple #12
0
def get_mutual_information_diff_c(R, t):
    """
    This is a more symmetrized version.
    Some structurally zero terms have been removed.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R*t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    B = np.outer(U.T[-1], U.T[-1])
    G = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G[i, j] = 0
            for k in range(n):
                G[i, j] += U[i, k] * U[j, k] * math.exp(t * w[k])
    G_diff = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G_diff[i, j] = 0
            for k in range(n):
                G_diff[i, j] += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
    return np.sum(B * G_diff * np.log(G))
Exemple #13
0
def get_mutual_information_small_approx_b(R, t):
    """
    This is an approximation for small times.
    Check a decomposition.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum_a = 0
    accum_b = 0
    accum_c = 0
    accum_d = 0
    for i in range(n):
        a = 0
        b = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        for k in range(n-1):
            prefix = U[i, k] * U[i, k]
            b += prefix * math.exp(t * w[k])
        x1 = v[i] * v[i]
        x2 = v[i] * b
        y1 = math.log(a)
        y2 = -math.log(v[i])
        accum_a += x1 * y1
        accum_b += x1 * y2
        accum_c += x2 * y1
        accum_d += x2 * y2
    return accum_a + accum_b + accum_c + accum_d
Exemple #14
0
def get_mutual_information_stable(R, t):
    """
    This is a more stable function.
    @return: unscaled_result, log_of_scaling_factor
    """
    #FIXME under construction
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    P = np.zeros_like(R)
    accum = 0
    for i in range(n):
        for j in range(n):
            for k in range(n):
                a = (v[j] / v[i])**0.5
                b = U[i, k] * U[j, k]
                c = math.exp(t * w[k])
                P[i, j] += a * b * c
    # compute the unscaled part of log(X(i,j)/(X(i)*X(j)))
    for i in range(n):
        for j in range(n):
            if v[i] and P[i, j]:
                coeff = v[i] * P[i, j]
                numerator = P[i, j]
                denominator = v[j]
                # the problem is that the following log is nearly zero
                value = coeff * math.log(numerator / denominator)
                accum += np.real(value)
    return accum
Exemple #15
0
def get_asymptotic_variance_e(R, t):
    """
    Try to mitigate the damage of the aggressive approximation.
    The next step is to try to simplify this complicated correction.
    But I have not been able to do this.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R*t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance approximation
    accum = 0
    for k in range(n-1):
        accum += w[k] * w[k] * math.exp(2 * t * w[k])
    accum_b = 0
    G_a = np.zeros_like(R)
    G_b = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            prefix = (p[i] * p[j]) ** -.5
            a = 0
            for k in range(n-1):
                a += U[i, k] * U[j, k] * math.exp(t * w[k])
            b = 0
            for k in range(n-1):
                b += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            suffix = a * b * b
            value = prefix * suffix
            accum_b += value
    return 1 / (accum - accum_b)
Exemple #16
0
def get_mutual_information_diff_zero(R):
    """
    Derivative of mutual information at time zero.
    Haha apparently this does not exist.
    """
    # get non-spectral summaries
    n = len(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    B = np.outer(U.T[-1], U.T[-1])
    G = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G[i, j] = 0
            for k in range(n):
                G[i, j] += U[i, k] * U[j, k]
    G_diff = np.zeros_like(R)
    for i in range(n):
        for j in range(n):
            G_diff[i, j] = 0
            for k in range(n):
                G_diff[i, j] += U[i, k] * U[j, k] * w[k]
    print G
    print G_diff
    print B
    return np.sum(B * G_diff * np.log(G))
Exemple #17
0
def get_mutual_information_small_approx_d(R, t):
    """
    This is an approximation for small times.
    This uses all of the off-diagonal entries of the mutual information
    and also uses an approximation of the off-diagonal entries.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum_diag_a = 0
    accum_diag_b = 0
    accum_diag_c = 0
    accum_diag_d = 0
    for i in range(n):
        a = 0
        b = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        for k in range(n-1):
            prefix = U[i, k] * U[i, k]
            b += prefix * math.exp(t * w[k])
        x1 = v[i] * v[i]
        x2 = v[i] * b
        y1 = math.log(a)
        y2 = -math.log(v[i])
        accum_diag_a += x1 * y1
        accum_diag_b += x1 * y2
        accum_diag_c += x2 * y1
        accum_diag_d += x2 * y2
    accum_a = 0
    accum_b = 0
    accum_c = 0
    accum_d = 0
    for i in range(n):
        for j in range(n):
            if i != j:
                prefix = (v[i] * v[j]) ** .5
                a = 0
                for k in range(n):
                    a += U[i, k] * U[j, k] * math.exp(t * w[k])
                b = 0
                for k in range(n-1):
                    b += U[i, k] * U[j, k] * math.exp(t * w[k])
                x1 = v[i] * v[j]
                x2 = prefix * b
                y1 = math.log(a)
                y2 = -math.log(prefix)
                accum_a += x1 * y1
                accum_b += x1 * y2
                accum_c += x2 * y1
                accum_d += x2 * y2
    terms = [
            accum_diag_a, accum_diag_b, accum_diag_c, accum_diag_d,
            accum_a, accum_b, accum_c, accum_d]
    for term in terms:
        print term
    return sum(terms)
Exemple #18
0
def get_mutual_information_b(R, t):
    """
    This uses some cancellation.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum_diag_a = 0
    accum_diag_b = 0
    accum_diag_c = 0
    for i in range(n):
        a = 0
        b = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        for k in range(n - 1):
            prefix = U[i, k] * U[i, k]
            b += prefix * math.exp(t * w[k])
        x1 = v[i] * v[i]
        x2 = v[i] * b
        y1 = math.log(a)
        y2 = -math.log(v[i])
        accum_diag_a += x1 * y1
        accum_diag_b += x1 * y2
        accum_diag_c += x2 * y1
    accum_a = 0
    accum_b = 0
    accum_c = 0
    for i in range(n):
        for j in range(n):
            if i != j:
                prefix = (v[i] * v[j])**.5
                a = 0
                for k in range(n):
                    a += U[i, k] * U[j, k] * math.exp(t * w[k])
                b = 0
                for k in range(n - 1):
                    b += U[i, k] * U[j, k] * math.exp(t * w[k])
                x1 = v[i] * v[j]
                x2 = prefix * b
                y1 = math.log(a)
                y2 = -math.log(prefix)
                accum_a += x1 * y1
                accum_b += x1 * y2
                accum_c += x2 * y1
    terms = [
        accum_diag_a, accum_diag_b, accum_diag_c, accum_a, accum_b, accum_c
    ]
    return sum(terms)
Exemple #19
0
def get_mutual_information_b(R, t):
    """
    This uses some cancellation.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum_diag_a = 0
    accum_diag_b = 0
    accum_diag_c = 0
    for i in range(n):
        a = 0
        b = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        for k in range(n-1):
            prefix = U[i, k] * U[i, k]
            b += prefix * math.exp(t * w[k])
        x1 = v[i] * v[i]
        x2 = v[i] * b
        y1 = math.log(a)
        y2 = -math.log(v[i])
        accum_diag_a += x1 * y1
        accum_diag_b += x1 * y2
        accum_diag_c += x2 * y1
    accum_a = 0
    accum_b = 0
    accum_c = 0
    for i in range(n):
        for j in range(n):
            if i != j:
                prefix = (v[i] * v[j]) ** .5
                a = 0
                for k in range(n):
                    a += U[i, k] * U[j, k] * math.exp(t * w[k])
                b = 0
                for k in range(n-1):
                    b += U[i, k] * U[j, k] * math.exp(t * w[k])
                x1 = v[i] * v[j]
                x2 = prefix * b
                y1 = math.log(a)
                y2 = -math.log(prefix)
                accum_a += x1 * y1
                accum_b += x1 * y2
                accum_c += x2 * y1
    terms = [
            accum_diag_a, accum_diag_b, accum_diag_c,
            accum_a, accum_b, accum_c]
    return sum(terms)
Exemple #20
0
def get_mutual_information_diff_approx_c(R, t):
    """
    This is an approximation for large times.
    It has been rewritten using orthogonality.
    It has also been rewritten using orthonormality.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for k in range(n - 1):
        accum += w[k] * math.exp(2 * t * w[k])
    return accum
Exemple #21
0
def get_mutual_information_diff_approx_c(R, t):
    """
    This is an approximation for large times.
    It has been rewritten using orthogonality.
    It has also been rewritten using orthonormality.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for k in range(n-1):
        accum += w[k]*math.exp(2*t*w[k])
    return accum
Exemple #22
0
def get_mutual_information_approx_b(R, t):
    """
    This is an approximation for large times.
    It has been rewritten using orthogonality.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        for j in range(n):
            for k in range(n - 1):
                accum += ((U[i, k] * U[j, k])**2) * math.exp(2 * t * w[k]) / 2
    return accum
Exemple #23
0
def get_mutual_information_approx_b(R, t):
    """
    This is an approximation for large times.
    It has been rewritten using orthogonality.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        for j in range(n):
            for k in range(n-1):
                accum += ((U[i,k]*U[j,k])**2) * math.exp(2*t*w[k]) / 2
    return accum
Exemple #24
0
def get_asymptotic_variance_d(R, t):
    """
    Use a very aggressive approximation.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R * t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance approximation
    accum = 0
    for k in range(n - 1):
        accum += w[k] * w[k] * math.exp(2 * t * w[k])
    return 1 / accum
Exemple #25
0
def get_asymptotic_variance_d(R, t):
    """
    Use a very aggressive approximation.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R*t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance approximation
    accum = 0
    for k in range(n-1):
        accum += w[k] * w[k] * math.exp(2 * t * w[k])
    return 1 / accum
Exemple #26
0
def get_mutual_information_small_approx_c(R, t):
    """
    This is an approximation for small times.
    This is an even more aggressive approximation.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        a = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        accum += -v[i] * math.log(v[i]) * a
    return accum
Exemple #27
0
def get_mutual_information_small_approx_c(R, t):
    """
    This is an approximation for small times.
    This is an even more aggressive approximation.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        a = 0
        for k in range(n):
            prefix = U[i, k] * U[i, k]
            a += prefix * math.exp(t * w[k])
        accum += - v[i] * math.log(v[i]) * a
    return accum
Exemple #28
0
def get_mutual_information_small_approx(R, t):
    """
    This is an approximation for small times.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        a = 0
        for k in range(n):
            a += (U[i, k]**2) * math.exp(t * w[k])
        accum += v[i] * a * math.log(a / v[i])
    #print [R[i, i] for i in range(n)]
    #print [sum(U[i, k] * U[i, k] * w[k] for k in range(n)) for i in range(n)]
    #print [sum(U[i, k] * U[i, k] for k in range(n)) for i in range(n)]
    return accum
Exemple #29
0
def get_mutual_information_small_approx(R, t):
    """
    This is an approximation for small times.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        a = 0
        for k in range(n):
            a += (U[i, k]**2) * math.exp(t * w[k])
        accum += v[i] * a * math.log(a / v[i])
    #print [R[i, i] for i in range(n)]
    #print [sum(U[i, k] * U[i, k] * w[k] for k in range(n)) for i in range(n)]
    #print [sum(U[i, k] * U[i, k] for k in range(n)) for i in range(n)]
    return accum
Exemple #30
0
def get_mutual_information_diff_approx(R, t):
    """
    This is an approximation for large times.
    It can be rewritten using orthogonality.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        for j in range(n):
            b = 0
            for k in range(n - 1):
                b += U[i, k] * U[j, k] * math.exp(t * w[k])
            c = 0
            for k in range(n - 1):
                c += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            accum += b * c
    return accum
Exemple #31
0
def get_mutual_information_diff_approx(R, t):
    """
    This is an approximation for large times.
    It can be rewritten using orthogonality.
    """
    n = len(R)
    v = mrate.R_to_distn(R)
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    accum = 0
    for i in range(n):
        for j in range(n):
            b = 0
            for k in range(n-1):
                b += U[i,k]*U[j,k]*math.exp(t*w[k])
            c = 0
            for k in range(n-1):
                c += U[i,k]*U[j,k]*w[k]*math.exp(t*w[k])
            accum += b * c
    return accum
Exemple #32
0
def get_asymptotic_variance_c(R, t):
    """
    Re-evaluate, this time throwing away the part that is structurally zero.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R * t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance
    accum = 0
    for i in range(n):
        for j in range(n):
            # define f
            f = p[i] * P[i, j]
            # define the first derivative of f
            f_dt = 0
            for k in range(n):
                f_dt += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            f_dt *= (p[i] * p[j])**.5
            accum -= (f_dt * f_dt) / f
    return -1 / accum
Exemple #33
0
def get_asymptotic_variance_c(R, t):
    """
    Re-evaluate, this time throwing away the part that is structurally zero.
    """
    # get non-spectral summaries
    n = len(R)
    P = scipy.linalg.expm(R*t)
    p = mrate.R_to_distn(R)
    # get spectral summaries
    S = mrate.symmetrized(R)
    w, U = np.linalg.eigh(S)
    # compute the asymptotic variance
    accum = 0
    for i in range(n):
        for j in range(n):
            # define f
            f = p[i] * P[i, j]
            # define the first derivative of f
            f_dt = 0
            for k in range(n):
                f_dt += U[i, k] * U[j, k] * w[k] * math.exp(t * w[k])
            f_dt *= (p[i] * p[j])**.5
            accum -= (f_dt * f_dt) / f
    return - 1 / accum
Exemple #34
0
def get_response_content(fs):
    out = StringIO()
    np.set_printoptions(linewidth=200)
    # get the user defined variables
    n = fs.nstates
    t = fs.divtime
    #h = fs.delta
    # sample a random rate matrix
    v = divtime.sample_distribution(n)
    S = divtime.sample_symmetric_rate_matrix(n)
    R = mrate.to_gtr_halpern_bruno(S, v)
    # get some properties of the rate matrix
    distn = mrate.R_to_distn(R)
    spectrum = np.linalg.eigvalsh(mrate.symmetrized(R))
    #spectrum, U = np.linalg.eigh(mrate.symmetrized(R))
    #spectrum = np.linalg.eigvals(R)
    # report some information about the mutual information curve
    mi = ctmcmi.get_mutual_information(R, t)
    mi_diff = ctmcmi.get_mutual_information_diff(R, t)
    mi_diff_b = ctmcmi.get_mutual_information_diff_b(R, t)
    mi_diff_c = ctmcmi.get_mutual_information_diff_c(R, t)
    print >> out, 'arbitrary large-ish divergence time:'
    print >> out, t
    print >> out
    print >> out, 'randomly sampled reversible rate matrix:'
    print >> out, R
    print >> out
    print >> out, 'stationary distribution:'
    print >> out, distn
    print >> out
    print >> out, 'spectrum of the rate matrix:'
    print >> out, spectrum
    print >> out
    print >> out, 'mutual information at t = %f:' % t
    print >> out, mi
    print >> out
    print >> out, 'mutual information at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_b(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_approx(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_approx_b(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f (ver. 3):' % t
    print >> out, ctmcmi.cute_MI_alternate(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f (ver. 4):' % t
    print >> out, ctmcmi.get_mutual_information_approx_c(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_small_approx(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_small_approx_b(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f (ver. 3):' % t
    print >> out, ctmcmi.get_mutual_information_small_approx_c(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f (ver. 4):' % t
    print >> out, ctmcmi.get_mutual_information_small_approx_d(R, t)
    print >> out
    print >> out, 'mutual information diff at t = %f:' % t
    print >> out, mi_diff
    print >> out
    print >> out, 'mutual information diff at t = %f (ver. 2):' % t
    print >> out, mi_diff_b
    print >> out
    print >> out, 'mutual information diff at t = %f (ver. 3):' % t
    print >> out, mi_diff_c
    print >> out
    print >> out, 'large t approximation of MI diff at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx(R, t)
    print >> out
    print >> out, 'large t approximation of MI diff at t = %f: (ver. 2)' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx_b(R, t)
    print >> out
    print >> out, 'large t approximation of MI diff at t = %f: (ver. 4)' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx_c(R, t)
    print >> out
    print >> out, 'log of mutual information at t = %f:' % t
    print >> out, math.log(mi)
    print >> out
    #print >> out, 'estimated derivative',
    #print >> out, 'of log of mutual information at t = %f:' % t
    #print >> out, (math.log(mi_c) - math.log(mi_a)) / (2*h)
    #print >> out
    print >> out, 'estimated derivative of log of MI',
    print >> out, 'at t = %f:' % t
    print >> out, mi_diff / mi
    print >> out
    print >> out, 'large t approximation of derivative of log of MI',
    print >> out, 'at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx(R,
            t) / ctmcmi.get_mutual_information_approx(R, t)
    print >> out
    print >> out, 'large t approximation of derivative of log of MI',
    print >> out, 'at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx_b(R,
            t) / ctmcmi.get_mutual_information_approx_b(R, t)
    print >> out
    print >> out, 'twice the relevant eigenvalue:'
    print >> out, 2 * spectrum[-2]
    print >> out
    print >> out
    #print >> out, 'estimated derivative',
    #print >> out, 'of mutual information at t = %f:' % t
    #print >> out, (mi_c - mi_a) / (2*h)
    #print >> out
    #print >> out, '(estimated derivative of mutual information) /',
    #print >> out, '(mutual information) at t = %f:' % t
    #print >> out, (mi_c - mi_a) / (2*h*mi_b)
    #print >> out
    return out.getvalue()
Exemple #35
0
def get_response_content(fs):
    out = StringIO()
    np.set_printoptions(linewidth=200)
    # get the user defined variables
    n = fs.nstates
    t = fs.divtime
    #h = fs.delta
    # sample a random rate matrix
    v = divtime.sample_distribution(n)
    S = divtime.sample_symmetric_rate_matrix(n)
    R = mrate.to_gtr_halpern_bruno(S, v)
    # get some properties of the rate matrix
    distn = mrate.R_to_distn(R)
    spectrum = np.linalg.eigvalsh(mrate.symmetrized(R))
    #spectrum, U = np.linalg.eigh(mrate.symmetrized(R))
    #spectrum = np.linalg.eigvals(R)
    # report some information about the mutual information curve
    mi = ctmcmi.get_mutual_information(R, t)
    mi_diff = ctmcmi.get_mutual_information_diff(R, t)
    mi_diff_b = ctmcmi.get_mutual_information_diff_b(R, t)
    mi_diff_c = ctmcmi.get_mutual_information_diff_c(R, t)
    print >> out, 'arbitrary large-ish divergence time:'
    print >> out, t
    print >> out
    print >> out, 'randomly sampled reversible rate matrix:'
    print >> out, R
    print >> out
    print >> out, 'stationary distribution:'
    print >> out, distn
    print >> out
    print >> out, 'spectrum of the rate matrix:'
    print >> out, spectrum
    print >> out
    print >> out, 'mutual information at t = %f:' % t
    print >> out, mi
    print >> out
    print >> out, 'mutual information at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_b(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_approx(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_approx_b(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f (ver. 3):' % t
    print >> out, ctmcmi.cute_MI_alternate(R, t)
    print >> out
    print >> out, 'large t approximation of MI at t = %f (ver. 4):' % t
    print >> out, ctmcmi.get_mutual_information_approx_c(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_small_approx(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_small_approx_b(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f (ver. 3):' % t
    print >> out, ctmcmi.get_mutual_information_small_approx_c(R, t)
    print >> out
    print >> out, 'small t approximation of MI at t = %f (ver. 4):' % t
    print >> out, ctmcmi.get_mutual_information_small_approx_d(R, t)
    print >> out
    print >> out, 'mutual information diff at t = %f:' % t
    print >> out, mi_diff
    print >> out
    print >> out, 'mutual information diff at t = %f (ver. 2):' % t
    print >> out, mi_diff_b
    print >> out
    print >> out, 'mutual information diff at t = %f (ver. 3):' % t
    print >> out, mi_diff_c
    print >> out
    print >> out, 'large t approximation of MI diff at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx(R, t)
    print >> out
    print >> out, 'large t approximation of MI diff at t = %f: (ver. 2)' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx_b(R, t)
    print >> out
    print >> out, 'large t approximation of MI diff at t = %f: (ver. 4)' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx_c(R, t)
    print >> out
    print >> out, 'log of mutual information at t = %f:' % t
    print >> out, math.log(mi)
    print >> out
    #print >> out, 'estimated derivative',
    #print >> out, 'of log of mutual information at t = %f:' % t
    #print >> out, (math.log(mi_c) - math.log(mi_a)) / (2*h)
    #print >> out
    print >> out, 'estimated derivative of log of MI',
    print >> out, 'at t = %f:' % t
    print >> out, mi_diff / mi
    print >> out
    print >> out, 'large t approximation of derivative of log of MI',
    print >> out, 'at t = %f:' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx(
        R, t) / ctmcmi.get_mutual_information_approx(R, t)
    print >> out
    print >> out, 'large t approximation of derivative of log of MI',
    print >> out, 'at t = %f (ver. 2):' % t
    print >> out, ctmcmi.get_mutual_information_diff_approx_b(
        R, t) / ctmcmi.get_mutual_information_approx_b(R, t)
    print >> out
    print >> out, 'twice the relevant eigenvalue:'
    print >> out, 2 * spectrum[-2]
    print >> out
    print >> out
    #print >> out, 'estimated derivative',
    #print >> out, 'of mutual information at t = %f:' % t
    #print >> out, (mi_c - mi_a) / (2*h)
    #print >> out
    #print >> out, '(estimated derivative of mutual information) /',
    #print >> out, '(mutual information) at t = %f:' % t
    #print >> out, (mi_c - mi_a) / (2*h*mi_b)
    #print >> out
    return out.getvalue()
Exemple #36
0
def get_response_content(fs):
    out = StringIO()
    np.set_printoptions(linewidth=200)
    # get the user defined variables
    n = fs.nstates
    # sample a random rate matrix
    v = divtime.sample_distribution(n)
    S = divtime.sample_symmetric_rate_matrix(n)
    R = mrate.to_gtr_halpern_bruno(S, v)
    # get some properties of the rate matrix and its re-symmetrization
    S = mrate.symmetrized(R)
    distn = mrate.R_to_distn(R)
    w, U = np.linalg.eigh(S)
    D = np.diag(U.T[-1])**2
    D_inv = np.diag(np.reciprocal(U.T[-1]))**2
    for t in (1.0, 2.0):
        P = scipy.linalg.expm(R*t)
        M = ndot(D**.5, scipy.linalg.expm(S*t), D**.5)
        M_star = ndot(D_inv**.5, scipy.linalg.expm(S*t), D_inv**.5)
        M_star_log = np.log(M_star)
        M_star_log_w, M_star_log_U = np.linalg.eigh(M_star_log)
        E = M * np.log(M_star)
        E_w, E_U = np.linalg.eigh(E)
        print >> out, 't:'
        print >> out, t
        print >> out
        print >> out, 'randomly sampled rate matrix R'
        print >> out, R
        print >> out
        print >> out, 'symmetrized matrix S'
        print >> out, S
        print >> out
        print >> out, 'stationary distribution diagonal D'
        print >> out, D
        print >> out
        print >> out, 'R = D^-1/2 S D^1/2'
        print >> out, ndot(D_inv**.5, S, D**.5)
        print >> out
        print >> out, 'probability matrix e^(R*t) = P'
        print >> out, P
        print >> out
        print >> out, 'P = D^-1/2 e^(S*t) D^1/2'
        print >> out, ndot(D_inv**.5, scipy.linalg.expm(S*t), D**.5)
        print >> out
        print >> out, 'pairwise distribution matrix M'
        print >> out, 'M = D^1/2 e^(S*t) D^1/2'
        print >> out, M
        print >> out
        print >> out, 'sum of entries of M'
        print >> out, np.sum(M)
        print >> out
        print >> out, 'M_star = D^-1/2 e^(S*t) D^-1/2'
        print >> out, M_star
        print >> out
        print >> out, 'entrywise logarithm logij(M_star)'
        print >> out, np.log(M_star)
        print >> out
        print >> out, 'Hadamard product M o logij(M_star) = E'
        print >> out, E
        print >> out
        print >> out, 'spectrum of M:'
        print >> out, np.linalg.eigvalsh(M)
        print >> out
        print >> out, 'spectrum of logij(M_star):'
        print >> out, M_star_log_w
        print >> out
        print >> out, 'corresponding eigenvectors of logij(M_star) as columns:'
        print >> out, M_star_log_U
        print >> out
        print >> out, 'spectrum of E:'
        print >> out, E_w
        print >> out
        print >> out, 'corresponding eigenvectors of E as columns:'
        print >> out, E_U
        print >> out
        print >> out, 'entrywise square roots of stationary distribution:'
        print >> out, np.sqrt(v)
        print >> out
        print >> out, 'sum of entries of E:'
        print >> out, np.sum(E)
        print >> out
        print >> out, 'mutual information:'
        print >> out, ctmcmi.get_mutual_information(R, t)
        print >> out
        print >> out
    return out.getvalue()
Exemple #37
0
def get_response_content(fs):
    out = StringIO()
    np.set_printoptions(linewidth=200)
    # get the user defined variables
    n = fs.nstates
    # sample a random rate matrix
    v = divtime.sample_distribution(n)
    S = divtime.sample_symmetric_rate_matrix(n)
    R = mrate.to_gtr_halpern_bruno(S, v)
    # get some properties of the rate matrix and its re-symmetrization
    S = mrate.symmetrized(R)
    distn = mrate.R_to_distn(R)
    w, U = np.linalg.eigh(S)
    D = np.diag(U.T[-1])**2
    D_inv = np.diag(np.reciprocal(U.T[-1]))**2
    for t in (1.0, 2.0):
        P = scipy.linalg.expm(R * t)
        M = ndot(D**.5, scipy.linalg.expm(S * t), D**.5)
        M_star = ndot(D_inv**.5, scipy.linalg.expm(S * t), D_inv**.5)
        M_star_log = np.log(M_star)
        M_star_log_w, M_star_log_U = np.linalg.eigh(M_star_log)
        E = M * np.log(M_star)
        E_w, E_U = np.linalg.eigh(E)
        print >> out, 't:'
        print >> out, t
        print >> out
        print >> out, 'randomly sampled rate matrix R'
        print >> out, R
        print >> out
        print >> out, 'symmetrized matrix S'
        print >> out, S
        print >> out
        print >> out, 'stationary distribution diagonal D'
        print >> out, D
        print >> out
        print >> out, 'R = D^-1/2 S D^1/2'
        print >> out, ndot(D_inv**.5, S, D**.5)
        print >> out
        print >> out, 'probability matrix e^(R*t) = P'
        print >> out, P
        print >> out
        print >> out, 'P = D^-1/2 e^(S*t) D^1/2'
        print >> out, ndot(D_inv**.5, scipy.linalg.expm(S * t), D**.5)
        print >> out
        print >> out, 'pairwise distribution matrix M'
        print >> out, 'M = D^1/2 e^(S*t) D^1/2'
        print >> out, M
        print >> out
        print >> out, 'sum of entries of M'
        print >> out, np.sum(M)
        print >> out
        print >> out, 'M_star = D^-1/2 e^(S*t) D^-1/2'
        print >> out, M_star
        print >> out
        print >> out, 'entrywise logarithm logij(M_star)'
        print >> out, np.log(M_star)
        print >> out
        print >> out, 'Hadamard product M o logij(M_star) = E'
        print >> out, E
        print >> out
        print >> out, 'spectrum of M:'
        print >> out, np.linalg.eigvalsh(M)
        print >> out
        print >> out, 'spectrum of logij(M_star):'
        print >> out, M_star_log_w
        print >> out
        print >> out, 'corresponding eigenvectors of logij(M_star) as columns:'
        print >> out, M_star_log_U
        print >> out
        print >> out, 'spectrum of E:'
        print >> out, E_w
        print >> out
        print >> out, 'corresponding eigenvectors of E as columns:'
        print >> out, E_U
        print >> out
        print >> out, 'entrywise square roots of stationary distribution:'
        print >> out, np.sqrt(v)
        print >> out
        print >> out, 'sum of entries of E:'
        print >> out, np.sum(E)
        print >> out
        print >> out, 'mutual information:'
        print >> out, ctmcmi.get_mutual_information(R, t)
        print >> out
        print >> out
    return out.getvalue()