Skip to content

Instantly share code, notes, and snippets.

View merenlin's full-sized avatar

Oxana Sachenkova merenlin

View GitHub Profile
@merenlin
merenlin / weighted_random_protein.py
Last active August 29, 2015 13:55
Non-uniform generation of a random protein sequence
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)
@merenlin
merenlin / weighted_choice_itertools.py
Last active August 29, 2015 13:55
Weighted choice using itertools
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)
@merenlin
merenlin / weighted_choice_numpy.py
Last active August 29, 2015 13:55
Weighted choice using numpy, old approach
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)