This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
AAs = list('ACEDGFIHKMLNQPSRTWVY') | |
alphas = [0.05]*20 | |
sequence = '' | |
weights = np.random.dirichlet(alphas) | |
for i in range(100): | |
sequence += np.random.choice(AAs,p=weights) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import bisect | |
import itertools | |
def weighted_choice_b2(weights): | |
partsums = list(itertools.accumulate(weights)) | |
total = partsums[-1] | |
rnd = random.random() * total | |
return bisect.bisect_right(partsums, rnd) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def weighted_choice(weights): | |
totals = np.cumsum(weights) | |
norm = totals[-1] | |
throw = np.random.rand()*norm | |
return np.searchsorted(totals, throw) |