Beispiel #1
0
def shut_times_between_burst_mean(mec):
    """
    Calculate the mean length of the gap between bursts (Eq. 3.86, CH82).

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    m : float
        The mean shut time between bursts.
    """

    pA = qml.pinf(mec.Q)[:mec.kA]
    end = np.dot(-mec.QAA, endBurst(mec))
    start = pA / np.dot(pA, end)
    uA = np.ones((mec.kA, 1))

    GAF, GFA = qml.iGs(mec.Q, mec.kA, mec.kF)
    invQFF = -nplin.inv(mec.QFF)
    invQBB = -nplin.inv(mec.QBB)
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)

    m1 = np.dot(np.dot(mec.QAF, invQFF), GFA)
    m2 = np.dot(np.dot(mec.QAB, invQBB), GBA)
    m = np.dot(np.dot(start, m1 - m2), uA)[0]

    return m
Beispiel #2
0
def openings_cond_distr_depend_on_start_state(mec, r):
    """
    The distribution of openings per burst coditional on starting state.

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.
    r : int
        Number of openings per burst.

    Returns
    -------
    vecPr : array_like, shape (kA, 1)
        Probability of seeing r openings per burst depending on starting state.
    """

    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    GG = np.dot(GAB, GBA)
    if r == 1:
        interm = np.eye(mec.kA)
    elif r == 2:
        interm = GG
    else:
        interm = GG
        for i in range(2, r):
            interm = np.dot(interm, GG)
    vecPr = np.dot(interm, endBurst(mec))
    vecPr = vecPr.transpose()
    return vecPr
Beispiel #3
0
def openings_distr_components(mec):
    """
    Calculate coeficients for geometric ditribution P(r)- probability of
    seeing r openings (Eq. 3.9 CH82):
    P(r) = sum(W * rho^(r-1))
    where w
    wm = phiB * Am * endB (Eq. 3.10 CH82)
    and rho- eigenvalues of GAB * GBA.

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.
    r : int
        Number of openings per burst.

    Returns
    -------
    rho : ndarray, shape (kA,)
    w : ndarray, shape (kA,)
    """

    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    GG = np.dot(GAB, GBA)
    rho, A = qml.eigs(GG)
    w = np.dot(np.dot(phiBurst(mec), A), endBurst(mec)).transpose()[0]
    return rho, w
Beispiel #4
0
def openings_distr(mec, r):
    """
    The distribution of openings per burst (Eq. 3.5, CH82).
    P(r) = phiB * (GAB * GBA)^(r-1) * eB

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.
    r : int
        Number of openings per burst.

    Returns
    -------
    Pr : float
        Probability of seeing r openings per burst.
    """

    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    GG = np.dot(GAB, GBA)
    if r == 1:
        interm = np.eye(mec.kA)
    elif r == 2:
        interm = GG
    else:
        interm = GG
        for i in range(2, r):
            interm = np.dot(interm, GG)
    Pr = np.dot(np.dot(phiBurst(mec), interm), endBurst(mec))
    return Pr
Beispiel #5
0
def open_time_total_pdf_components(mec):
    """
    Eq. 3.23, CH82

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    eigs : ndarray, shape(k, 1)
        Time constants.
    w : ndarray, shape(k, 1)
        Component amplitudes.
    """

    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    VAA = mec.QAA + np.dot(mec.QAB, GBA)
    eigs, A = qml.eigs(-VAA)
    uA = np.ones((mec.kA, 1))

    w = np.zeros(mec.kA)
    for i in range(mec.kA):
        w[i] = np.dot(np.dot(np.dot(phiBurst(mec), A[i]), (-VAA)), uA)

    return eigs, w
Beispiel #6
0
def shut_time_total_pdf_components_2more_openings(mec):
    """
    Calculate time constants and amplitudes for a PDF of total shut time 
    per burst (Eq. 3.40, CH82) for bursts with at least 2 openings.

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    eigs : ndarray, shape(k, 1)
        Time constants.
    w : ndarray, shape(k, 1)
        Component amplitudes.
    """

    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    WBB = mec.QBB + np.dot(mec.QBA, GAB)
    eigs, A = qml.eigs(-WBB)
    norm = 1 - np.dot(phiBurst(mec), endBurst(mec))[0]

    w = np.zeros(mec.kB)
    for i in range(mec.kB):
        w[i] = np.dot(np.dot(np.dot(np.dot(phiBurst(mec), GAB),
            A[i]), (mec.QBA)), endBurst(mec)) / norm

    return eigs, w
Beispiel #7
0
def length_mean(mec):
    """
    Calculate the mean burst length (Eq. 3.19, CH82).
    m = PhiB * (I - GAB * GBA)^(-1) * (-QAA^(-1)) * \
        (I - QAB * (QBB^(-1)) * GBA) * uA

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    m : float
        The mean burst length.
    """

    uA = np.ones((mec.kA, 1))
    I = np.eye(mec.kA)
    invQAA = -1 * nplin.inv(mec.QAA)
    invQBB = nplin.inv(mec.QBB)
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    interm1 = nplin.inv(I - np.dot(GAB, GBA))
    interm2 = I - np.dot(np.dot(mec.QAB, invQBB), GBA)
    m = (np.dot(np.dot(np.dot(np.dot(phiBurst(mec), interm1), invQAA),
        interm2), uA)[0])
    return m
Beispiel #8
0
def shut_times_inside_burst_pdf_components(mec):
    """
    Calculate time constants and amplitudes for a PDF of all gaps within
    bursts (Eq. 3.75, CH82).

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    eigs : ndarray, shape(k, 1)
        Time constants.
    w : ndarray, shape(k, 1)
        Component amplitudes.
    """

    uA = np.ones((mec.kA, 1))
    eigs, A = qml.eigs(-mec.QBB)
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    interm = nplin.inv(np.eye(mec.kA) - np.dot(GAB, GBA))
    norm = openings_mean(mec) - 1

    w = np.zeros(mec.kB)
    for i in range(mec.kB):
        w[i] = np.dot(np.dot(np.dot(np.dot(np.dot(np.dot(phiBurst(mec), interm),
            GAB), A[i]), (-mec.QBB)), GBA), uA) / norm

    return eigs, w
Beispiel #9
0
def first_opening_length_pdf_components(mec):
    """
    Calculate time constants and amplitudes for an ideal (no missed events)
    pdf of first opening in a burst with 2 or more openings.

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    eigs : ndarray, shape(k, 1)
        Time constants.
    w : ndarray, shape(k, 1)
        Component amplitudes.
    """

    uA = np.ones((mec.kA, 1))
    eigs, A = qml.eigs(-mec.QAA)
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    GG = np.dot(GAB, GBA)
    norm = np.dot(np.dot(phiBurst(mec), GG), uA)[0]

    w = np.zeros(mec.kA)
    for i in range(mec.kA):
        w[i] = np.dot(np.dot(np.dot(np.dot(phiBurst(mec),
            A[i]), (-mec.QAA)), GG), uA) / norm

    return eigs, w
Beispiel #10
0
def HJC_adjacent_mean_open_to_shut_time_pdf(sht, tres, Q, QAA, QAF, QFF, QFA):
    """
    Calculate theoretical HJC (with missed events correction) mean open time
    given previous/next gap length (continuous function; CHS96 Eq.3.5). 

    Parameters
    ----------
    sht : array of floats
        Shut time interval.
    tres : float
        Time resolution.
    Q : array, shape (k,k)
        Q matrix.
    QAA, QAF, QFF, QFA : array_like
        Submatrices of Q.

    Returns
    -------
    mp : ndarray of floats
        Mean open time given previous gap length.
    mn : ndarray of floats
        Mean open time given next gap length.
    """
    
    kA, kF = QAA.shape[0], QFF.shape[0]
    uA = np.ones((kA))[:,np.newaxis]
    uF = np.ones((kF))[:,np.newaxis]
    expQFF = qml.expQt(QFF, tres)
    expQAA = qml.expQt(QAA, tres)
    GAF, GFA = qml.iGs(Q, kA, kF)
    eGAF = qml.eGs(GAF, GFA, kA, kF, expQFF)
    eGFA = qml.eGs(GFA, GAF, kF, kA, expQAA)
    phiA = qml.phiHJC(eGAF, eGFA, kA)
    phiF = qml.phiHJC(eGFA, eGAF, kF)
    DARS = qml.dARSdS(tres, QAA, QFF, GAF, GFA, expQFF, kA, kF)
    eigs, A = qml.eigs(-Q)
    Feigvals, FZ00, FZ10, FZ11 = qml.Zxx(Q, eigs, A, kA, QAA, QFA, QAF, expQAA, False)
    Froots = asymptotic_roots(tres, QFF, QAA, QFA, QAF, kF, kA)
    FR = qml.AR(Froots, tres, QFF, QAA, QFA, QAF, kF, kA)
    Q1 = np.dot(np.dot(DARS, QAF), expQFF)
    col1 = np.dot(Q1, uF)
    row1 = np.dot(phiA, Q1)
    
    mp = []
    mn = []
    for t in sht:
        eGFAt = qml.eGAF(t, tres, Feigvals, FZ00, FZ10, FZ11, Froots,
                    FR, QFA, expQAA)
        denom = np.dot(np.dot(phiF, eGFAt), uA)[0]
        nom1 = np.dot(np.dot(phiF, eGFAt), col1)[0]
        nom2 = np.dot(np.dot(row1, eGFAt), uA)[0]
        mp.append(nom1 / denom)
        mn.append(nom2 / denom)
    
    return np.array(mp), np.array(mn)
Beispiel #11
0
def HJC_dependency(top, tsh, tres, Q, QAA, QAF, QFF, QFA):
    """
    Calculate normalised joint distribution (CHS96, Eq. 3.22) of an open time
    and the following shut time as proposed by Magleby & Song 1992. 
    
    Parameters
    ----------
    top, tsh : array_like of floats
        Open and shut tims.
    tres : float
        Time resolution.
    Q : array, shape (k,k)
        Q matrix. 
    QAA, QAF, QFF, QFA : array_like
        Submatrices of Q.

    Returns
    -------
    dependency : ndarray
    """
    
    kA, kF = QAA.shape[0], QFF.shape[0]
    uA = np.ones((kA))[:,np.newaxis]
    uF = np.ones((kF))[:,np.newaxis]
    expQFF = qml.expQt(QFF, tres)
    expQAA = qml.expQt(QAA, tres)
    GAF, GFA = qml.iGs(Q, kA, kF)
    eGAF = qml.eGs(GAF, GFA, kA, kF, expQFF)
    eGFA = qml.eGs(GFA, GAF, kF, kA, expQAA)
    phiA = qml.phiHJC(eGAF, eGFA, kA)
    phiF = qml.phiHJC(eGFA, eGAF, kF)
    eigs, A = qml.eigs(-Q)
    Feigvals, FZ00, FZ10, FZ11 = qml.Zxx(Q, eigs, A, kA, QAA, QFA, QAF, expQAA, False)
    Froots = asymptotic_roots(tres, QFF, QAA, QFA, QAF, kF, kA)
    FR = qml.AR(Froots, tres, QFF, QAA, QFA, QAF, kF, kA)
    Aeigvals, AZ00, AZ10, AZ11 = qml.Zxx(Q, eigs, A, kA, QFF, QAF, QFA, expQFF, True)
    Aroots = asymptotic_roots(tres, QAA, QFF, QAF, QFA, kA, kF)
    AR = qml.AR(Aroots, tres, QAA, QFF, QAF, QFA, kA, kF)

    dependency = np.zeros((top.shape[0], tsh.shape[0]))
    
    for i in range(top.shape[0]):
        eGAFt = qml.eGAF(top[i], tres, Aeigvals, AZ00, AZ10, AZ11, Aroots,
                AR, QAF, expQFF)
        fo = np.dot(np.dot(phiA, eGAFt), uF)[0]
        
        for j in range(tsh.shape[0]):
            eGFAt = qml.eGAF(tsh[j], tres, Feigvals, FZ00, FZ10, FZ11, Froots,
                FR, QFA, expQAA)
            fs = np.dot(np.dot(phiF, eGFAt), uA)[0]
            fos = np.dot(np.dot(np.dot(phiA, eGAFt), eGFAt), uA)[0]
            dependency[i, j] = (fos - (fo * fs)) / (fo * fs)
    return dependency
Beispiel #12
0
def exact_mean_open_shut_time(mec, tres):
    """
    Calculate exact mean open or shut time from HJC probability density
    function.

    Parameters
    ----------
    tres : float
        Time resolution (dead time).
    QAA : array_like, shape (kA, kA)
    QFF : array_like, shape (kF, kF)
    QAF : array_like, shape (kA, kF)
        QAA, QFF, QAF - submatrices of Q.
    kA : int
        A number of open states in kinetic scheme.
    kF : int
        A number of shut states in kinetic scheme.
    GAF : array_like, shape (kA, kB)
    GFA : array_like, shape (kB, kA)
        GAF, GFA- transition probabilities

    Returns
    -------
    mean : float
        Apparent mean open/shut time.
    """
    GAF, GFA = qml.iGs(mec.Q, mec.kA, mec.kF)
    expQFF = qml.expQt(mec.QFF, tres)
    expQAA = qml.expQt(mec.QAA, tres)
    eGAF = qml.eGs(GAF, GFA, mec.kA, mec.kF, expQFF)
    eGFA = qml.eGs(GFA, GAF, mec.kF, mec.kA, expQAA)

    phiA = qml.phiHJC(eGAF, eGFA, mec.kA)
    phiF = qml.phiHJC(eGFA, eGAF, mec.kF)
    QexpQF = np.dot(mec.QAF, expQFF)
    QexpQA = np.dot(mec.QFA, expQAA)
    DARS = qml.dARSdS(tres, mec.QAA, mec.QFF, GAF, GFA, expQFF, mec.kA, mec.kF)
    DFRS = qml.dARSdS(tres, mec.QFF, mec.QAA, GFA, GAF, expQAA, mec.kF, mec.kA)
    uF, uA = np.ones((mec.kF, 1)), np.ones((mec.kA, 1))
    # meanOpenTime = tres + phiA * DARS * QexpQF * uF
    meanA = tres + np.dot(phiA, np.dot(np.dot(DARS, QexpQF), uF))[0]
    meanF = tres + np.dot(phiF, np.dot(np.dot(DFRS, QexpQA), uA))[0]

    return meanA, meanF
Beispiel #13
0
def exact_GAMAxx(mec, tres, open):
    """
    Calculate gama coeficients for the exact open time pdf (Eq. 3.22, HJC90).

    Parameters
    ----------
    tres : float
    mec : dcpyps.Mechanism
        The mechanism to be analysed.
    open : bool
        True for open time pdf and False for shut time pdf.

    Returns
    -------
    eigen : ndarray, shape (k,)
        Eigenvalues of -Q matrix.
    gama00, gama10, gama11 : ndarrays
        Constants for the exact open/shut time pdf.
    """

    expQFF = qml.expQt(mec.QII, tres)
    expQAA = qml.expQt(mec.QAA, tres)
    GAF, GFA = qml.iGs(mec.Q, mec.kA, mec.kI)
    eGAF = qml.eGs(GAF, GFA, mec.kA, mec.kI, expQFF)
    eGFA = qml.eGs(GFA, GAF, mec.kI, mec.kA, expQAA)
    eigs, A = qml.eigs_sorted(-mec.Q)

    if open:
        phi = qml.phiHJC(eGAF, eGFA, mec.kA)
        eigen, Z00, Z10, Z11 = qml.Zxx(mec.Q, eigs, A, mec.kA,
            mec.QII, mec.QAI, mec.QIA, expQFF, open)
        u = np.ones((mec.kI,1))
    else:
        phi = qml.phiHJC(eGFA, eGAF, mec.kI)
        eigen, Z00, Z10, Z11 = qml.Zxx(mec.Q, eigs, A, mec.kA,
            mec.QAA, mec.QIA, mec.QAI, expQAA, open)
        u = np.ones((mec.kA, 1))

    gama00 = (np.dot(np.dot(phi, Z00), u)).T[0]
    gama10 = (np.dot(np.dot(phi, Z10), u)).T[0]
    gama11 = (np.dot(np.dot(phi, Z11), u)).T[0]

    return eigen, gama00, gama10, gama11
Beispiel #14
0
def open_time_mean(mec):
    """
    Calculate the mean total open time per burst (Eq. 3.26, CH82).

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    m : float
        The mean total open time per burst.
    """

    uA = np.ones((mec.kA, 1))
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    VAA = mec.QAA + np.dot(mec.QAB, GBA)
    m = np.dot(np.dot(phiBurst(mec), -nplin.inv(VAA)), uA)[0]
    return m
Beispiel #15
0
def shut_time_total_mean(mec):
    """
    Calculate the mean total shut time per burst (Eq. 3.41, CH82).

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    m : float
        The mean total shut time per burst.
    """

    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    WBB = mec.QBB + np.dot(mec.QBA, GAB)
    invW = - nplin.inv(WBB)
    uA = np.ones((mec.kA, 1))
    m = np.dot(np.dot(np.dot(np.dot(phiBurst(mec), GAB),
            invW), (GBA)), uA)[0]
    return m
Beispiel #16
0
def phiBurst(mec):
    """
    Calculate the start probabilities of a burst (Eq. 3.2, CH82).
    phiB = (pCinf * (QCB * GBA + QCA)) / (pCinf * (QCB * GBA + QCA) * uA)

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    phiB : array_like, shape (1, kA)
    """

    uA = np.ones((mec.kA, 1))
    pC = qml.pinf(mec.Q)[mec.kE:mec.kG]
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    nom = np.dot(pC, (np.dot(mec.QCB, GBA) + mec.QCA))
    denom = np.dot(nom, uA)
    phiB = nom / denom
    return phiB
Beispiel #17
0
def openings_mean(mec):
    """
    Calculate the mean number of openings per burst (Eq. 3.7, CH82).
    mu = phiB * (I - GAB * GBA)^(-1) * uA

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    mu : float
        The mean number ofopenings per burst.
    """

    uA = np.ones((mec.kA,1))
    I = np.eye(mec.kA)
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    interm = nplin.inv(I - np.dot(GAB, GBA))
    mu = np.dot(np.dot(phiBurst(mec), interm), uA)[0]
    return mu
Beispiel #18
0
def endBurst(mec):
    r"""
    Calculate the end vector for a burst (Eq. 3.4, CH82).

    .. math::

       \bs{e}_\text{b} = (\bs{I}-\bs{G}_\cl{AB} \bs{G}_\cl{BA}) \bs{u}_\cl{A}

    Parameters
    ----------
    mec : dcpyps.Mechanism
        The mechanism to be analysed.

    Returns
    -------
    eB : array_like, shape (kA, 1)
    """

    uA = np.ones((mec.kA, 1))
    I = np.eye(mec.kA)
    GAB, GBA = qml.iGs(mec.Q, mec.kA, mec.kB)
    eB = np.dot((I - np.dot(GAB, GBA)), uA)
    return eB
Beispiel #19
0
def HJClik(theta, opts):
    """
    Calculate likelihood for a series of open and shut times using HJC missed
    events probability density functions (first two dead time intervals- exact
    solution, then- asymptotic).

    Lik = phi * eGAF(t1) * eGFA(t2) * eGAF(t3) * ... * eGAF(tn) * uF
    where t1, t3,..., tn are open times; t2, t4,..., t(n-1) are shut times.

    Gaps > tcrit are treated as unusable (e.g. contain double or bad bit of
    record, or desens gaps that are not in the model, or gaps so long that
    next opening may not be from the same channel). However this calculation
    DOES assume that all the shut times predicted by the model are present
    within each group. The series of multiplied likelihoods is terminated at
    the end of the opening before an unusable gap. A new series is then
    started, using appropriate initial vector to give Lik(2), ... At end
    these are multiplied to give final likelihood.

    Parameters
    ----------
    theta : array_like
        Guesses.
    bursts : dictionary
        A dictionary containing lists of open and shut intervals.
    opts : dictionary
        opts['mec'] : instance of type Mechanism
        opts['tres'] : float
            Time resolution (dead time).
        opts['tcrit'] : float
            Ctritical time interval.
        opts['isCHS'] : bool
            True if CHS vectors should be used (Eq. 5.7, CHS96).

    Returns
    -------
    loglik : float
        Log-likelihood.
    newrates : array_like
        Updated rates/guesses.
    """
    # TODO: Errors.

    mec = opts['mec']
    conc = opts['conc']
    tres = opts['tres']
    tcrit = opts['tcrit']
    is_chsvec = opts['isCHS']
    bursts = opts['data']

    mec.theta_unsqueeze(np.exp(theta))
    mec.set_eff('c', conc)

    GAF, GFA = qml.iGs(mec.Q, mec.kA, mec.kF)
    expQFF = qml.expQt(mec.QFF, tres)
    expQAA = qml.expQt(mec.QAA, tres)
    eGAF = qml.eGs(GAF, GFA, mec.kA, mec.kF, expQFF)
    eGFA = qml.eGs(GFA, GAF, mec.kF, mec.kA, expQAA)
    phiF = qml.phiHJC(eGFA, eGAF, mec.kF)
    startB = qml.phiHJC(eGAF, eGFA, mec.kA)
    endB = np.ones((mec.kF, 1))

    eigen, A = qml.eigs(-mec.Q)
    Aeigvals, AZ00, AZ10, AZ11 = qml.Zxx(mec.Q, eigen, A, mec.kA, mec.QFF,
        mec.QAF, mec.QFA, expQFF, True)
    Aroots = asymptotic_roots(tres,
        mec.QAA, mec.QFF, mec.QAF, mec.QFA, mec.kA, mec.kF)
    AR = qml.AR(Aroots, tres, mec.QAA, mec.QFF, mec.QAF, mec.QFA, mec.kA, mec.kF)
    Feigvals, FZ00, FZ10, FZ11 = qml.Zxx(mec.Q, eigen, A, mec.kA, mec.QAA,
        mec.QFA, mec.QAF, expQAA, False)
    Froots = asymptotic_roots(tres,
        mec.QFF, mec.QAA, mec.QFA, mec.QAF, mec.kF, mec.kA)
    FR = qml.AR(Froots, tres, mec.QFF, mec.QAA, mec.QFA, mec.QAF, mec.kF, mec.kA)

    if is_chsvec:
        startB, endB = qml.CHSvec(Froots, tres, tcrit,
            mec.QFA, mec.kA, expQAA, phiF, FR)

    loglik = 0
    for ind in range(len(bursts)):
        burst = bursts[ind]
        grouplik = startB
        for i in range(len(burst)):
            t = burst[i]
            if i % 2 == 0: # open time
                eGAFt = qml.eGAF(t, tres, Aeigvals, AZ00, AZ10, AZ11, Aroots,
                AR, mec.QAF, expQFF)
            else: # shut
                eGAFt = qml.eGAF(t, tres, Feigvals, FZ00, FZ10, FZ11, Froots,
                FR, mec.QFA, expQAA)
            grouplik = np.dot(grouplik, eGAFt)
            if grouplik.max() > 1e50:
                grouplik = grouplik * 1e-100
                #print 'grouplik was scaled down'
        grouplik = np.dot(grouplik, endB)
        try:
            loglik += log(grouplik[0])
        except:
            print ('HJClik: Warning: likelihood has been set to 0')
            print ('likelihood=', grouplik[0])
            print ('rates=', mec.unit_rates())
            loglik = 0
            break

    newrates = np.log(mec.theta())
    return -loglik, newrates
Beispiel #20
0
def printout_correlations(mec, output=sys.stdout, eff='c'):
    """

    """

    str = ('\n\n*************************************\n' +
        'CORRELATIONS\n')
    
    kA, kI = mec.kA, mec.kI
    str += ('kA, kF = {0:d}, {1:d}\n'.format(kA, kI))
    GAF, GFA = qml.iGs(mec.Q, kA, kI)
    rGAF, rGFA = np.rank(GAF), np.rank(GFA)
    str += ('Ranks of GAF, GFA = {0:d}, {1:d}\n'.format(rGAF, rGFA))
    XFF = np.dot(GFA, GAF)
    rXFF = np.rank(XFF)
    str += ('Rank of GFA * GAF = {0:d}\n'.format(rXFF))
    ncF = rXFF - 1
    eigXFF, AXFF = qml.eigs(XFF)
    str += ('Eigenvalues of GFA * GAF:\n')
    str1 = ''
    for i in range(kI):
        str1 += '\t{0:.5g}'.format(eigXFF[i])
    str += str1 + '\n'
    XAA = np.dot(GAF, GFA)
    rXAA = np.rank(XAA)
    str += ('Rank of GAF * GFA = {0:d}\n'.format(rXAA))
    ncA = rXAA - 1
    eigXAA, AXAA = qml.eigs(XAA)
    str += ('Eigenvalues of GAF * GFA:\n')
    str1 = ''
    for i in range(kA):
        str1 += '\t{0:.5g}'.format(eigXAA[i])
    str += str1 + '\n'
    phiA, phiF = qml.phiA(mec).reshape((1,kA)), qml.phiF(mec).reshape((1,kI))
    varA = corr_variance_A(phiA, mec.QAA, kA)
    varF = corr_variance_A(phiF, mec.QII, kI)
    
    #   open - open time correlations
    str += ('\n OPEN - OPEN TIME CORRELATIONS')
    str += ('Variance of open time = {0:.5g}\n'.format(varA))
    SDA = sqrt(varA)
    str += ('SD of all open times = {0:.5g} ms\n'.format(SDA * 1000))
    n = 50
    SDA_mean_n = SDA / sqrt(float(n))
    str += ('SD of means of {0:d} open times if'.format(n) + 
        'uncorrelated = {0:.5g} ms\n'.format(SDA_mean_n * 1000))
    covAtot = 0
    for i in range(1, n):
        covA = corr_covariance_A(i+1, phiA, mec.QAA, XAA, kA)
        ro = correlation_coefficient(covA, varA, varA)
        covAtot += (n - i) * ro * varA
    vtot = n * varA + 2. * covAtot
    actSDA = sqrt(vtot / (n * n))
    str += ('Actual SD of mean = {0:.5g} ms\n'.format(actSDA * 1000))
    pA = 100 * (actSDA - SDA_mean_n) / SDA_mean_n
    str += ('Percent difference as result of correlation = {0:.5g}\n'.
        format(pA))
    v2A = corr_limit_A(phiA, mec.QAA, AXAA, eigXAA, kA)
    pmaxA = 100 * (sqrt(1 + 2 * v2A / varA) - 1)
    str += ('Limiting value of percent difference for large n = {0:.5g}\n'.
        format(pmaxA))
    str += ('Correlation coefficients, r(k), for up to lag k = 5:\n')
    for i in range(5):
        covA = corr_covariance_A(i+1, phiA, mec.QAA, XAA, kA)
        ro = correlation_coefficient(covA, varA, varA)
        str += ('r({0:d}) = {1:.5g}\n'.format(i+1, ro))

    # shut - shut time correlations
    str += ('\n SHUT - SHUT TIME CORRELATIONS\n')
    str += ('Variance of shut time = {0:.5g}\n'.format(varF))
    SDF = sqrt(varF)
    str += ('SD of all shut times = {0:.5g} ms\n'.format(SDF * 1000))
    n = 50
    SDF_mean_n = SDF / sqrt(float(n))
    str += ('SD of means of {0:d} shut times if'.format(n) +
        'uncorrelated = {0:.5g} ms\n'.format(SDF_mean_n * 1000))
    covFtot = 0
    for i in range(1, n):
        covF = corr_covariance_A(i+1, phiF, mec.QII, XFF, kI)
        ro = correlation_coefficient(covF, varF, varF)
        covFtot += (n - i) * ro * varF
    vtotF = 50 * varF + 2. * covFtot
    actSDF = sqrt(vtotF / (50. * 50.))
    str += ('Actual SD of mean = {0:.5g} ms\n'.format(actSDF * 1000))
    pF = 100 * (actSDF - SDF_mean_n) / SDF_mean_n
    str += ('Percent difference as result of correlation = {0:.5g}\n'.
        format(pF))
    v2F = corr_limit_A(phiF, mec.QII, AXFF, eigXFF, kI)
    pmaxF = 100 * (sqrt(1 + 2 * v2F / varF) - 1)
    str += ('Limiting value of percent difference for large n = {0:.5g}\n'.
        format(pmaxF))
    str += ('Correlation coefficients, r(k), for up to k = 5 lags:\n')
    for i in range(5):
        covF = corr_covariance_A(i+1, phiF, mec.QII, XFF, kI)
        ro = correlation_coefficient(covF, varF, varF)
        str += ('r({0:d}) = {1:.5g}\n'.format(i+1, ro))

    # open - shut time correlations 
    str += ('\n OPEN - SHUT TIME CORRELATIONS\n')
    str += ('Correlation coefficients, r(k), for up to k= 5 lags:\n')
    for i in range(5):
        covAF = corr_covariance_AF(i+1, phiA, mec.QAA, mec.QII, XAA, GAF, kA, kI)
        ro = correlation_coefficient(covAF, varA, varF)
        str += ('r({0:d}) = {1:.5g}\n'.format(i+1, ro))
    return str
Beispiel #21
0
def printout_distributions(mec, tres, eff='c'):
    """

    """

    str = '\n*******************************************\n'
    GAI, GIA = qml.iGs(mec.Q, mec.kA, mec.kI)
    # OPEN TIME DISTRIBUTIONS
    open = True
    # Ideal pdf
    eigs, w = ideal_dwell_time_pdf_components(mec.QAA,
        qml.phiA(mec))
    str += 'IDEAL OPEN TIME DISTRIBUTION\n'
    str += pdfs.expPDF_printout(eigs, w)

    # Asymptotic pdf
    #roots = asymptotic_roots(mec, tres, open)
    roots = asymptotic_roots(tres,
        mec.QAA, mec.QII, mec.QAI, mec.QIA, mec.kA, mec.kI)
    #areas = asymptotic_areas(mec, tres, roots, open)
    areas = asymptotic_areas(tres, roots,
        mec.QAA, mec.QII, mec.QAI, mec.QIA, mec.kA, mec.kI, GAI, GIA)
    str += '\nASYMPTOTIC OPEN TIME DISTRIBUTION\n'
    str += 'term\ttau (ms)\tarea (%)\trate const (1/sec)\n'
    for i in range(mec.kA):
        str += ('{0:d}'.format(i+1) +
        '\t{0:.5g}'.format(-1.0 / roots[i] * 1000) +
        '\t{0:.5g}'.format(areas[i] * 100) +
        '\t{0:.5g}\n'.format(- roots[i]))
    areast0 = np.zeros(mec.kA)
    for i in range(mec.kA):
        areast0[i] = areas[i] * np.exp(- tres * roots[i])
    areast0 = areast0 / np.sum(areast0)
    str += ('Areas for asymptotic pdf renormalised for t=0 to\
    infinity (and sum=1), so areas can be compared with ideal pdf.\n')
    for i in range(mec.kA):
        str += ('{0:d}'.format(i+1) +
        '\t{0:.5g}\n'.format(areast0[i] * 100))
    mean = exact_mean_time(tres,
            mec.QAA, mec.QII, mec.QAI, mec.kA, mec.kI, GAI, GIA)
    str += ('Mean open time (ms) = {0:.5g}\n'.format(mean * 1000))

    # Exact pdf
    eigvals, gamma00, gamma10, gamma11 = exact_GAMAxx(mec, tres, open)
    str += ('\nEXACT OPEN TIME DISTRIBUTION\n')
    str += ('eigen\tg00(m)\tg10(m)\tg11(m)\n')
    for i in range(mec.k):
        str += ('{0:.5g}'.format(eigvals[i]) +
        '\t{0:.5g}'.format(gamma00[i]) +
        '\t{0:.5g}'.format(gamma10[i]) +
        '\t{0:.5g}\n'.format(gamma11[i]))

    str += ('\n\n*******************************************\n')
    # SHUT TIME DISTRIBUTIONS
    open = False
    # Ideal pdf
    eigs, w = ideal_dwell_time_pdf_components(mec.QII, qml.phiF(mec))
    str += ('IDEAL SHUT TIME DISTRIBUTION\n')
    str += pdfs.expPDF_printout(eigs, w)

    # Asymptotic pdf
    #roots = asymptotic_roots(mec, tres, open)
    roots = asymptotic_roots(tres,
        mec.QII, mec.QAA, mec.QIA, mec.QAI, mec.kI, mec.kA)
    #areas = asymptotic_areas(mec, tres, roots, open)
    areas = asymptotic_areas(tres, roots,
        mec.QII, mec.QAA, mec.QIA, mec.QAI, mec.kI, mec.kA, GIA, GAI)
    str += ('\nASYMPTOTIC SHUT TIME DISTRIBUTION\n')
    str += ('term\ttau (ms)\tarea (%)\trate const (1/sec)\n')
    for i in range(mec.kI):
        str += ('{0:d}'.format(i+1) +
        '\t{0:.5g}'.format(-1.0 / roots[i] * 1000) +
        '\t{0:.5g}'.format(areas[i] * 100) +
        '\t{0:.5g}\n'.format(- roots[i]))
    areast0 = np.zeros(mec.kI)
    for i in range(mec.kI):
        areast0[i] = areas[i] * np.exp(- tres * roots[i])
    areast0 = areast0 / np.sum(areast0)
    str += ('Areas for asymptotic pdf renormalised for t=0 to\
    infinity (and sum=1), so areas can be compared with ideal pdf.\n')
    for i in range(mec.kI):
        str += ('{0:d}'.format(i+1) +
        '\t{0:.5g}\n'.format(areast0[i] * 100))
    mean = exact_mean_time(tres,
            mec.QII, mec.QAA, mec.QIA, mec.kI, mec.kA, GIA, GAI)
    str += ('Mean shut time (ms) = {0:.6f}\n'.format(mean * 1000))

    # Exact pdf
    eigvals, gamma00, gamma10, gamma11 = exact_GAMAxx(mec, tres, open)
    str += ('\nEXACT SHUT TIME DISTRIBUTION\n' + 
        'eigen\tg00(m)\tg10(m)\tg11(m)\n')
    for i in range(mec.k):
        str += ('{0:.5g}'.format(eigvals[i]) +
        '\t{0:.5g}'.format(gamma00[i]) +
        '\t{0:.5g}'.format(gamma10[i]) +
        '\t{0:.5g}\n'.format(gamma11[i]))

    # Transition probabilities
    pi = transition_probability(mec.Q)
    str += ('\nProbability of transitions regardless of time:\n')
    for i in range(mec.k):
        str1 = '['
        for j in range(mec.k):
            str1 += '{0:.4g}\t'.format(pi[i,j])
        str1 += ']\n'
        str += str1

    # Transition frequency
    f = transition_frequency(mec.Q)
    str += ('\nFrequency of transitions (per second):\n')
    for i in range(mec.k):
        str1 = '['
        for j in range(mec.k):
            str1 += '{0:.4g}\t'.format(f[i,j])
        str1 += ']\n'
        str += str1
        
    return str
Beispiel #22
0
def printout_occupancies(mec, tres):
    """
    """

    str = ('\n\n\n*******************************************\n\n' +
        'Open\tEquilibrium\tMean life\tMean latency (ms)\n' +
        'state\toccupancy\t(ms)\tto next shutting\n' +
        '\t\t\tgiven start in this state\n')

    pinf = qml.pinf(mec.Q)

    for i in range(mec.k):
        if i == 0:
            mean_life_A = ideal_subset_mean_life_time(mec.Q, 1, mec.kA)
            str += ('Subset A ' +
                '\t{0:.5g}'.format(np.sum(pinf[:mec.kA])) +
                '\t{0:.5g}\n'.format(mean_life_A * 1000))
        if i == mec.kA:
            mean_life_B = ideal_subset_mean_life_time(mec.Q, mec.kA + 1, mec.kE)
            str += ('\nShut\tEquilibrium\tMean life\tMean latency (ms)\n' +
                'state\toccupancy\t(ms)\tto next opening\n' +
                '\t\t\tgiven start in this state\n' +
                'Subset B ' +
                '\t{0:.5g}'.format(np.sum(pinf[mec.kA : mec.kE])) +
                '\t{0:.5g}\n'.format(mean_life_B * 1000))
        if i == mec.kE:
            mean_life_C = ideal_subset_mean_life_time(mec.Q, mec.kE + 1, mec.kG)
            str += ('\nSubset C ' +
                '\t{0:.5g}'.format(np.sum(pinf[mec.kE : mec.kG])) +
                '\t{0:.5g}\n'.format(mean_life_C * 1000))
        if i == mec.kG:
            mean_life_D = ideal_subset_mean_life_time(mec.Q, mec.kG + 1, mec.k)
            str += ('\nSubset D ' +
                '\t{0:.5g}'.format(np.sum(pinf[mec.kG : mec.k])) +
                '\t{0:.5g}\n'.format(mean_life_D * 1000))

        mean = ideal_mean_latency_given_start_state(mec, i+1)
        str += ('{0:d}'.format(i+1) +
            '\t{0:.5g}'.format(pinf[i]) +
            '\t{0:.5g}'.format(-1 / mec.Q[i,i] * 1000) +
            '\t{0:.5g}\n'.format(mean * 1000))

    expQFF = qml.expQt(mec.QFF, tres)
    expQAA = qml.expQt(mec.QAA, tres)
    GAF, GFA = qml.iGs(mec.Q, mec.kA, mec.kF)
    eGAF = qml.eGs(GAF, GFA, mec.kA, mec.kF, expQFF)
    eGFA = qml.eGs(GFA, GAF, mec.kF, mec.kA, expQAA)
    phiA = qml.phiHJC(eGAF, eGFA, mec.kA)
    phiF = qml.phiHJC(eGFA, eGAF, mec.kF)

    str += ('\n\nInitial vector for HJC openings phiOp =\n')
    for i in range(phiA.shape[0]):
        str += ('\t{0:.5g}'.format(phiA[i]))
    str += ('\nInitial vector for ideal openings phiOp =\n')
    phiAi = qml.phiA(mec)
    for i in range(phiA.shape[0]):
        str += ('\t{0:.5g}'.format(phiAi[i]))
    str += ('\nInitial vector for HJC shuttings phiSh =\n')
    for i in range(phiF.shape[0]):
        str += ('\t{0:.5g}'.format(phiF[i]))
    str += ('\nInitial vector for ideal shuttings phiSh =\n')
    phiFi = qml.phiF(mec)
    for i in range(phiF.shape[0]):
        str += ('\t{0:.5g}'.format(phiFi[i]))
    str += '\n'
    
    return str
Beispiel #23
0
    def test_openshut(self):

        # # # Initial HJC vectors.
        expQFF = qml.expQt(self.mec.QFF, self.tres)
        expQAA = qml.expQt(self.mec.QAA, self.tres)
        GAF, GFA = qml.iGs(self.mec.Q, self.mec.kA, self.mec.kF)
        eGAF = qml.eGs(GAF, GFA, self.mec.kA, self.mec.kF, expQFF)
        eGFA = qml.eGs(GFA, GAF, self.mec.kF, self.mec.kA, expQAA)
        phiA = qml.phiHJC(eGAF, eGFA, self.mec.kA)
        phiF = qml.phiHJC(eGFA, eGAF, self.mec.kF)

        self.assertAlmostEqual(phiA[0], 0.153966, 6)
        self.assertAlmostEqual(phiA[1], 0.846034, 6)
        self.assertAlmostEqual(phiF[0], 0.530369, 6)
        self.assertAlmostEqual(phiF[1], 0.386116, 6)
        self.assertAlmostEqual(phiF[2], 0.0835153, 6)
        
        # Ideal shut time pdf
        eigs, w = scl.ideal_dwell_time_pdf_components(self.mec.QFF,
            qml.phiF(self.mec))
        self.assertAlmostEqual(eigs[0], 0.263895, 6)
        self.assertAlmostEqual(eigs[1], 2062.93, 2)
        self.assertAlmostEqual(eigs[2], 19011.8, 1)
        self.assertAlmostEqual(w[0], 0.0691263, 6)
        self.assertAlmostEqual(w[1], 17.2607, 4)
        self.assertAlmostEqual(w[2], 13872.7, 1)

        # Asymptotic shut time pdf
        roots = scl.asymptotic_roots(self.tres,
            self.mec.QFF, self.mec.QAA, self.mec.QFA, self.mec.QAF,
            self.mec.kF, self.mec.kA)
        areas = scl.asymptotic_areas(self.tres, roots,
            self.mec.QFF, self.mec.QAA, self.mec.QFA, self.mec.QAF,
            self.mec.kF, self.mec.kA, GFA, GAF)
        mean = scl.exact_mean_time(self.tres,
                self.mec.QFF, self.mec.QAA, self.mec.QFA, self.mec.kF,
                self.mec.kA, GFA, GAF)
        self.assertAlmostEqual(-roots[0], 17090.2, 1)
        self.assertAlmostEqual(-roots[1], 2058.08, 2)
        self.assertAlmostEqual(-roots[2], 0.243565, 6)
        self.assertAlmostEqual(areas[0] * 100, 28.5815, 4)
        self.assertAlmostEqual(areas[1] * 100, 1.67311, 5)
        self.assertAlmostEqual(areas[2] * 100, 68.3542, 4)

        # Exact pdf
        eigvals, gamma00, gamma10, gamma11 = scl.exact_GAMAxx(self.mec,
            self.tres, False)
        self.assertAlmostEqual(gamma00[0], 0.940819, 6)
        self.assertAlmostEqual(gamma00[1], 117.816, 3)
        self.assertAlmostEqual(gamma00[2], 24.8962, 4)
        self.assertAlmostEqual(gamma00[3], 1.28843, 5)
        self.assertAlmostEqual(gamma00[4], 5370.18, 2)
        self.assertAlmostEqual(gamma10[0], 4.57792, 5)
        self.assertAlmostEqual(gamma10[1], 100.211, 3)
        self.assertAlmostEqual(gamma10[2], -5.49855, 4)
        self.assertAlmostEqual(gamma10[3], 0.671548, 6)
        self.assertAlmostEqual(gamma10[4], -99.9617, 4)
        self.assertAlmostEqual(gamma11[0], 0.885141, 6)
        self.assertAlmostEqual(gamma11[1], 43634.99, 1)
        self.assertAlmostEqual(gamma11[2], 718.068, 3)
        self.assertAlmostEqual(gamma11[3], -39.7437, 3)
        self.assertAlmostEqual(gamma11[4], -1.9832288e+06, 0)