def create_flat_hrg(p_in): """Creates a "flat" random network with well defined groups but no hierarchy.""" number_of_nodes = 128 number_of_groups = 4 group_size = 32 number_of_edges = 1024 network = UndirectedNetwork(number_of_nodes) prob_within = number_of_edges * p_in / (number_of_groups * choose(group_size, 2)) prob_without = number_of_edges * (1 - p_in) / (group_size * group_size * choose(number_of_groups, 2)) within_edges = 0 without_edges = 0 # connect nodes within modules for group in xrange(0, number_of_groups): for i in xrange(group * group_size, (group + 1) * group_size): for j in xrange(group * group_size, i): if random.random() < prob_within: network.add_edge(i, j) within_edges += 1 # connect modules together for i in xrange(0, number_of_groups): for j in xrange(0, i): for u in xrange(i * group_size, (i + 1) * group_size): for v in xrange(j * group_size, (j + 1) * group_size): if random.random() < prob_without: network.add_edge(u, v) without_edges += 1 return network
def negbin(self, k, r, p): ''' sample from negative binomial distribution ''' a = choose(k+r-1, k) b = p**k c = (1.0 - p)**r return a * b * c
def binom(s, p, n): """Binomial Distribution Calculate the binomial sampling probability (not very efficient, but not much effieciency is needed with small samples). """ prob = (choose(n, s) * p**s * (1 - p)**(n - s)) return prob
def binom(s, p, n): """Binomial Distribution Calculate the binomial sampling probability (not very efficient, but not much effieciency is needed with small samples). """ prob = (choose(n, s) * p**s * (1-p)**(n-s)) return prob
def combine_exact(self): # Hypothesis Test: # H0(i->j): i can be an ancestor of j # HA(i-*>j): i cannot be an ancestor of j statistic = np.zeros(self.dim) for index in range(self.size): # H0 specifies that all sampled diff values have + means --> # all the observed positive values are not any different than H+ null # hypothesis --> they contribute a multiplicative value of 1 to the LR # statistic # the observed negative values are where the ratio difference lies, and those # will be forced to be generated by mean of 0 under H+ mask = (self.data[index] < 0).astype(int) # sum up the (-2ln(x) ) for x corresponding each of # the terms in likelihood ratio # put zero where elements where the delta is missing and # is filled with place holder statistic = statistic + \ ((self.data[index]/ self.sigma[index])**2)* \ mask * self.isfilled[index] # sum over isfilled attribute to get the total number # of samples available for comparison of a pair of mutations totalSamples = sum(self.isfilled) # get the list of existing number of available samples across # all pairs of mutations countRange = map(int, list(np.unique(totalSamples))) # start by a blank input for pvalue self.pvalue = np.zeros(self.dim) for value in countRange: # for each value in existing set of available samples for a pair # assign the p-value for such pairs using exact test formulation # for that total sample count mask = (totalSamples == value).astype(int) if value == 0: # if no samples available for a pair of mutations # use pvalue of 1 to indicate lack of rejection # (insufficient information) self.pvalue += mask continue n = value pvalue = np.zeros(self.dim) for k in range(1, 1+n): pvalue += (1-chi2.cdf(statistic,k)) * \ choose(n, k, exact = True) / (2.0 ** n) self.pvalue += pvalue * mask
def absolute_magnetization_x(self, input_state): """ calculates the absolute magnization in the x direction by taking the overlap with every possible combination of N spins """ probs = np.zeros(self.N + 1) for state in self.possible_spin_states: total_up = self.N - state.sum() qstate = self.state_to_tensor(state) overlap = qstate.dag() * input_state probability = np.abs(overlap.diag()) ** 2 probs[int(total_up)] += probability ns = np.arange(self.N + 1) m_x = probs * np.abs(self.N - 2 * ns) / float(self.N) m_x = m_x.sum() scaling = choose(self.N, ns) * np.abs(self.N - 2 * ns) / float(self.N * 2 ** self.N) scaling = scaling.sum() m_x_bar = (scaling - m_x) / (scaling - 1) return m_x_bar
def absolute_magnetization_x(self, input_state): ''' calculates the absolute magnization in the x direction by taking the overlap with every possible combination of N spins ''' probs = np.zeros(self.N + 1) for state in self.possible_spin_states: total_up = self.N - state.sum() qstate = self.state_to_tensor(state) overlap = qstate.dag() * input_state probability = np.abs(overlap.diag())**2 probs[int(total_up)] += probability ns = np.arange(self.N + 1) m_x = probs * np.abs(self.N - 2 * ns) / float(self.N) m_x = m_x.sum() scaling = choose(self.N, ns) * np.abs(self.N - 2 * ns) / float( self.N * 2**self.N) scaling = scaling.sum() m_x_bar = (scaling - m_x) / (scaling - 1) return m_x_bar
[3,0,1] ] r = 3 """ """ rows and cols of square grid """ # SQUARE GRID r = 2 Ks = [4, 8, 16] for k in Ks: m = k**2 n = m square_grid = np.reshape(range(m), (k, k)) H = [row.tolist() for row in square_grid] + [col.tolist() for col in square_grid.T] N_per_support = int((k - 1) * choose(m, k)) N = len(H) * N_per_support num_trials = 100 Cs = np.zeros((num_trials, 2)) # 0:C1, 1:C2 #pcntiles = np.zeros((num_trials, 2)) # 0:C1, 1:C2 for i in range(num_trials): print("Trial %d" % i) A = np.random.randn(n, m) A = np.dot(A, np.diag(1. / np.linalg.norm(A, axis=0))) # normalize # Xs = [np.random.randn(k, N_per_support) for S in H] # Xs = [np.dot(X, np.diag(1. / np.linalg.norm(X, axis=0))) for X in Xs] # normalize Cs[i, 1] = C2(A, H, r) # Cs[i, 0] = Cs[i,1] / C1_denom(A, Xs, H, num_rand_ksets = 10) #Cs[i,0] = Cs[i,1] / L_k( np.dot(A[:,H[0]], Xs[0]), k, n_samples = 10 ) # print('%1.3f' % c2) # if (i > 1000) & (i % 100 == 0):