@@ -84,12 +84,14 @@ def num_of_nonzero_elements(my_vector):
8484 return counter
8585
8686
87- def normalize_markov_matrix (transition_matrix ):
87+ def normalize_markov_matrix (transition_matrix , reversible = False ):
8888 '''Transform a matrix of positive elements to a markov-like
8989 matrix by divding each row by the sum of the elements of the
9090 row.
9191 '''
9292 t_matrix = np .array (transition_matrix , dtype = np .float64 )
93+ if reversible :
94+ t_matrix = t_matrix .T + t_matrix
9395
9496 n_states = len (t_matrix )
9597 assert (n_states == len (t_matrix [0 ]))
@@ -320,6 +322,46 @@ def pseudo_nm_tmatrix(markovian_tmatrix, stateA, stateB):
320322 check_tmatrix (p_nm_tmatrix ) # just in case
321323 return p_nm_tmatrix
322324
325+ def confindence_interval_cdf (populations , totCounts , conf_interval = 0.95 , n_samples = 100000 ):
326+ counts = np .round (np .array (populations )* totCounts )
327+ partialCounts = sum (counts )
328+ myarray = list (counts )+ [totCounts - partialCounts ]
329+ s = np .random .dirichlet (myarray , n_samples )
330+
331+ s_cdf = []
332+
333+ for line in s :
334+ s_cdf .append (cdf (line ))
335+ s_cdf = np .array (s_cdf )
336+
337+ s = np .transpose (s )
338+ s_cdf = np .transpose (s_cdf )
339+
340+ minval = []
341+ maxval = []
342+ minvalcdf = []
343+ maxvalcdf = []
344+
345+ for line in s :
346+ sorted_line = np .sort (line )
347+ minval .append (sorted_line [int ( (1 - conf_interval )/ 2 * len (sorted_line ))])
348+ maxval .append (sorted_line [int ( (1 - (1 - conf_interval )/ 2 ) * len (sorted_line ))])
349+
350+ for line in s_cdf :
351+ sorted_line = np .sort (line )
352+ minvalcdf .append (sorted_line [int ( (1 - conf_interval )/ 2 * len (sorted_line ))])
353+ maxvalcdf .append (sorted_line [int ( (1 - (1 - conf_interval )/ 2 ) * len (sorted_line ))])
354+
355+ return minvalcdf [:- 1 ], maxvalcdf [:- 1 ]
356+
357+ def cdf (pmf ):
358+ mycdf = []
359+ tot = 0
360+ for element in pmf :
361+ tot += element
362+ mycdf .append (tot )
363+ return np .array (mycdf )
364+
323365
324366if __name__ == '__main__' :
325367 import mfpt
0 commit comments