I'm trying to make a matrix A (200, 200) where A[i][j] = A[j][i] follows Bernoulli distribution of probability p if the element is in a same cluster of index(i.e. 0-49, 50-99, 100-149, 150-199) and probability q if not.
So far I tried to implement it straightforwardly like below,
import numpy as np from scipy.stats import bernoulli def link_weight(p=0.05, q=0.04): A = np.zeros((200, 200)) for i in range(len(A)): for j in range(len(A)): if i == j: A[i][j] = 0 elif i < j: if i < 50: if j < 50: A[i][j] = bernoulli.rvs(p) A[j][i] = bernoulli.rvs(p) else: A[i][j] = bernoulli.rvs(q) A[j][i] = bernoulli.rvs(q) if 50 <= i <= 99: if 50 <= j <= 99: A[i][j] = bernoulli.rvs(p) A[j][i] = bernoulli.rvs(p) else: A[i][j] = bernoulli.rvs(q) A[j][i] = bernoulli.rvs(q) if 100 <= i <= 149: if 100 <= j <= 149: A[i][j] = bernoulli.rvs(p) A[j][i] = bernoulli.rvs(p) else: A[i][j] = bernoulli.rvs(q) A[j][i] = bernoulli.rvs(q) if 150 <= i <= 199: if 150 <= j <= 199: A[i][j] = bernoulli.rvs(p) A[j][i] = bernoulli.rvs(p) else: A[i][j] = bernoulli.rvs(q) A[j][i] = bernoulli.rvs(q) return A But I wanna make it more readable and faster. How can I prove this code by avoiding nested if/for loops?
bernoulli.rvs(p)is not equal tobernoulli.rvs(p).