def printout_adjacent(mec, t1, t2): """ """ str = ('\n*************************************\n' + ' OPEN TIMES ADJACENT TO SPECIFIED SHUT TIME RANGE\n') kA = mec.kA phiA = qml.phiA(mec).reshape((1,kA)) str += ('PDF of open times that precede shut times between {0:.3f}\ and {1:.3f} ms\n'.format(t1 * 1000, t2 * 1000)) eigs, w = adjacent_open_to_shut_range_pdf_components(t1, t2, mec.QAA, mec.QAF, mec.QFF, mec.QFA, phiA) str += pdfs.expPDF_printout(eigs, w) mean = adjacent_open_to_shut_range_mean(t1, t2, mec.QAA, mec.QAF, mec.QFF, mec.QFA, phiA) str += ('Mean from direct calculation (ms) = {0:.6f}\n'.format(mean * 1000)) return str
def likelihood(theta, opts): """ Calculate likelihood for a series of open and shut times using ideal probability density functions. """ mec = opts['mec'] conc = opts['conc'] bursts = opts['data'] #mec.set_rateconstants(np.exp(theta)) mec.theta_unsqueeze(np.exp(theta)) mec.set_eff('c', conc) startB = qml.phiA(mec) endB = np.ones((mec.kF, 1)) loglik = 0 for ind in bursts: burst = bursts[ind] grouplik = startB for i in range(len(burst)): t = burst[i] if i % 2 == 0: # open time GAFt = qml.iGt(t, mec.QAA, mec.QAF) else: # shut GAFt = qml.iGt(t, mec.QFF, mec.QFA) grouplik = np.dot(grouplik, GAFt) if grouplik.max() > 1e50: grouplik = grouplik * 1e-100 print ('grouplik was scaled down') grouplik = np.dot(grouplik, endB) loglik += log(grouplik[0]) newrates = np.log(mec.theta()) return -loglik, newrates
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
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
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