def draw_sample(output_dist, sample_temperature, sample_argmax, _rnd): if sample_argmax: output_dist = np.eye(256)[np.argmax(output_dist, axis=-1)] else: if sample_temperature is not None: output_dist = softmax(output_dist, sample_temperature) output_dist = output_dist / np.sum(output_dist + 1e-7) output_dist = random.multinomial(1, output_dist) return output_dist
def experiment(mean_b, n_max=POP_MAX, n0=INITIAL_CELLS): """ Simulate the fate of the cell population. Random variables are drawn from a multinomial law to determine the fate of the cells at each step, which is equivalent to drawing a random variable for each cell independently. Args: mean_b (float): value of the adaptation timing. n_max (int, optional): maximum population allowed, defaults to POP_MAX. n_0 (int, optional): initial population, defaults to INITIAL_CELLS. Returns: int : number of steps needed to fill the medium with healthy (repaired) cells """ damaged_cells = n0 repaired_cells = 0 adapted_cells = 0 dead_cells = 0 n = 0 while repaired_cells < n_max: a = alpha_sto(n) b = beta_sto(n, mean_b) # Fate of damaged cells draw = rd.multinomial(damaged_cells, [max(0,1-a-b-GAMMA_DAM), a, b, min(1-a-b,GAMMA_DAM)]) # Draw a multinomial law damaged_cells = draw[0] repaired_cells += draw[1] adapted_cells += draw[2] dead_cells += draw[3] # Fate of adapted cells draw = rd.multinomial(adapted_cells, [DELTA, GAMMA_AD, 1-DELTA-GAMMA_AD]) # Draw a multinomial law repaired_cells = min(n_max, repaired_cells + draw[0]) dead_cells += draw[1] adapted_cells = draw[2] # Reproduce cells is space is available if damaged_cells + repaired_cells + adapted_cells < n_max : r = r = reproduce_sto(damaged_cells, adapted_cells, repaired_cells) adapted_cells += r[0] repaired_cells += r[1] n+=1 return n
def generate_logistic_multinomial(X: spa.csr_matrix, W: np.array, b: np.array): X1 = np.c_[np.ones(len(X)), X] bW = np.c_[b, W] nu = np.dot(X1, bW.T) enu = np.exp(nu) enu_sum = enu.sum(axis=1) pi = enu / enu_sum[:, np.newaxis] Z = np.empty_like(pi) for i1 in range(len(pi)): Z[i1] = random.multinomial(1, pi[i1], 1) return Z
def generate_bsample(duplicate_counts,n): new_counts= []; new_counts.append(duplicate_counts[0]); new_counts.append(duplicate_counts[1]); for i in xrange(2,n): N = duplicate_counts[i][1]; if N < 2: new_counts.append(duplicate_counts[i]); continue; pvals = []; for j in xrange(2,len(duplicate_counts[i])): pvals.append(float(duplicate_counts[i][j])/N); #print >>sys.stderr, "PVALS",pvals,duplicate_counts[i] S = random.multinomial(N,pvals); new_counts.append(duplicate_counts[i]); for j in xrange(2,len(duplicate_counts[i])): new_counts[-1][j] = S[j-2]; print 'BOOTSTRAP-counts',i,new_counts[i]; return new_counts;
def generate_bsample(duplicate_counts, n): new_counts = [] new_counts.append(duplicate_counts[0]) new_counts.append(duplicate_counts[1]) for i in xrange(2, n): N = duplicate_counts[i][1] if N < 2: new_counts.append(duplicate_counts[i]) continue pvals = [] for j in xrange(2, len(duplicate_counts[i])): pvals.append(float(duplicate_counts[i][j]) / N) #print >>sys.stderr, "PVALS",pvals,duplicate_counts[i] S = random.multinomial(N, pvals) new_counts.append(duplicate_counts[i]) for j in xrange(2, len(duplicate_counts[i])): new_counts[-1][j] = S[j - 2] print 'BOOTSTRAP-counts', i, new_counts[i] return new_counts