def sample_ext_length(lamb=1/250.0,min_seq_length=75,alpha=None): """Given a read that extends for (at least) min_seq_length, sample the distance it extends past the end of the sequenced read. If the position of the TF is inside the sequenced portion, then the endpoint of the fragment is distributed as Exp(lamb). If the position of the TF is outside the sequenced portion, however, then the endpoint is distributed as Exp(lamb) + Exp(lamb) = Gamma(lamb,2). We therefore model the distribution of the fragment endpoint as a mixture model: endpoint ~ alpha*Exp(lamb) + (1-alpha)*Gamma(lamb,2) where the mixing parameter alpha is the probability that the TF resides within the sequenced portion. We assume that this probability can be described as: <sequenced_length/total_length>, where the expectation is taken with respect to the total length of the fragment, which is distributed exponentially. """ if alpha is None: alpha = sum(min_seq_length/float(min_seq_length+ell)*lamb*exp(-lamb*ell) for ell in xrange(100000)) alpha = 0.3 if random.random() < alpha: # TF resides in binding_site ext_length = nprand.geometric(lamb) else: ext_length = nprand.geometric(lamb) + nprand.geometric(lamb) return ext_length
def __init__(self,source): self.source = source #System idnum distance = random.geometric(p=0.2,size=1) #At present, all warp gates go FORWARD self.destination = source+distance[0] #Determine the initial sequence self.chart_sequence = [] draws = random.geometric(p=0.5,size=int(log2(distance)+log2(abs(self.destination))))
def generateRandomInt(): """Generates a random integer. Uses a geometric distribution. Returns: An integer that is chosen that has a 50% chance of being 0 or greater, and a 50% chance of being negative. """ if random() > 0.5: return geometric(p=0.5) else: return -(1 + geometric(p=0.5))
def span_corrupting(tokenized_sent: List[str], max_length: int = 5, prob: float = 0.25): max_length = min(len(tokenized_sent) // 2 + 1, max_length) span_length = geometric(prob) while span_length > max_length or span_length < 2: span_length = int(geometric(prob)) start_point = randint(0, len(tokenized_sent) - span_length + 1) return tokenized_sent[:start_point + 1] + tokenized_sent[start_point + span_length:]
def _mutate_i(self, individual): eta = np.array(individual[self._id_eta]) if len(self._id_eta) == 1: eta = max(1, eta * exp(self.tau_i * randn())) p = 1 - (eta / self.N_i) / (1 + np.sqrt(1 + (eta / self.N_i) ** 2)) individual[self.id_i] = np.array(individual[self.id_i]) + \ geometric(p, self.N_i) - geometric(p, self.N_i) else: eta = eta * exp(self.tau_i * randn() + self.tau_p_i * randn(self.N_i)) eta[eta > 1] = 1 p = 1 - (eta / self.N_i) / (1 + np.sqrt(1 + (eta / self.N_i) ** 2)) individual[self.id_i] = np.array(individual[self.id_i]) + \ np.array([geometric(p_) - geometric(p_) for p_ in p]) individual[self._id_eta] = eta
def loi_geom(nb_val, param1, barres, pas): # Loi pratique (valeurs aleatoires) xp = loi.geometric(param1, size=nb_val) # Normalisation mini = 0 maxi = nb_val p = param1 # Loi theorique vec = np.arange(mini, maxi, pas) xt = loiT.geom.pmf(vec, p) # A COMPLETER... # Affichage plt.figure() plt.hist(xp, barres, density=True, label='resultat pratique') plt.plot( vec, xt, 'r', label='resultat theorique') # A MODIFIER dans le cas discret par 'or' plt.title('Loi Geometrique') # A COMPLETER... plt.xlabel('Intervalles') plt.ylabel('Probabilites') plt.legend() plt.show()
def generateRandomNonNegInt(): """Generates a random integer that is greater than or equal to 0. Uses a geometric distribution. Returns: A value chosen using a geometric distribution with p=5, starting at 0. """ return geometric(p=0.5)
def get_householdsizes(n, p=0.5, dist=None): """ Return a sample of the sizes of n households, either from a geometric distribution with parameter p, or from a fixed frequency distribution dist. :param n: int, number of households :param p: parameter of geometric distribution :param dist: frequencies of household sizes :return: """ if dist is None: households = npr.geometric(p, size=n) return households else: dist = np.array(dist) try: households = npr.choice(np.arange(1, len(dist) + 1), size=n, p=dist) except ValueError: dist = dist / dist.sum() households = npr.choice(np.arange(1, len(dist) + 1), size=n, p=dist) return households
def test_geometric(self): pymc3_random_discrete( Geometric, {'p': Unit}, size=500, # Test always fails with larger sample sizes. fails=50, # Be a bit more generous. ref_rand=lambda size, p=None: nr.geometric(p, size=size))
def compact(self): if (self.numCompaction%2==1 and self.alternate): self.offset = 1 - self.offset else: self.offset = int(random() < 0.5) self.sort() s = geometric(self.eps) while s+1 >= len(self)//2: s = self.k + geometric(self.eps) s1 = len(self)//2 - s s2 = len(self)//2 + s for i in range(s1+self.offset, s2, 2): yield self[i] del self[s1:s2] self.numCompaction += 1
def get_assignment_context(request): """ Get a random task of the specified type.""" worker_id = request.GET.get('worker_id') task_type = request.GET.get('task_type') eligible_tasks = ( InternalCrowdInterface.get_eligible_tasks(worker_id).filter( task_type=task_type).order_by('create_time')) # Pick a random task, biased towards older tasks. task_index = min( geometric(ORDER_WEIGHT) - 1, eligible_tasks.count() - 1) # generate a random assignment id for this assignment. assignment_id = uuid.uuid4() return { 'task_id': (eligible_tasks[task_index].task_id if task_index >= 0 else None), 'worker_id': worker_id, 'is_accepted': True, 'assignment_id': assignment_id, }
def _get_genome_amounts_geometric_fix(num_real_genomes, max_genome_amount): """ Get amounts of genomes by original genome @param num_real_genomes: exact number of real genomes @type num_real_genomes: int | long @param max_genome_amount: Total number of genomes @type max_genome_amount: int | long @return: List of integers representing amount of strains @rtype: list[int] """ assert isinstance(num_real_genomes, (int, long)) assert isinstance(max_genome_amount, (int, long)) final_amounts = [1] * num_real_genomes index = 0 while index < len(final_amounts): if sum(final_amounts) >= max_genome_amount: break final_amounts[index] += min(1 + np_random.geometric(0.3), 10) index += 1 final_amounts[index - 1] -= sum(final_amounts) - max_genome_amount return final_amounts
def atm2bank(atm, bank): #handle hanshake bank.sendall(atm.recv(4096)) #handle msg bank.sendall(atm.recv(4096)) # which packet to drop? if DISTRIBUTION == 'G': commit_num = geometric(1 - pow(10.0, -3 / float(MAX_COMMIT))) elif DISTRIBUTION == 'U': commit_num = random_integers(MAX_COMMIT - 1) else: commit_num = StrongRandom().randint(MIN_COMMIT, MAX_COMMIT) # print "here" logger.info('[+] Nombre commits guessed: {0}'.format(commit_num)) pck = 1 # commit protocol while True: b = recv(atm) if not b: break if pck != commit_num: bank.sendall(b) else: logger.debug("Attacking!!!") pck += 1 return
def test_likelihood_kernel(self): eig_vals = 1 + rndm.geometric(p=0.5, size=self.rank) eig_vecs, _ = qr(rndm.randn(self.N, self.rank), mode='economic') dpp = FiniteDPP(kernel_type='likelihood', projection=False, **{'L': (eig_vecs * eig_vals).dot(eig_vecs.T)}) for size in self.sizes: for mode in ('GS', 'GS_bis', 'KuTa12'): dpp.flush_samples() for _ in range(self.nb_samples): dpp.sample_exact_k_dpp(size, mode) self.check_right_cardinality(dpp, dpp.list_of_samples) for mode in ('AED', 'AD'): dpp.flush_samples() dpp.sample_mcmc_k_dpp(size, **{'nb_iter': self.nb_samples}) self.check_right_cardinality(dpp, dpp.list_of_samples[0])
def _get_genome_amounts_geometric(probability, max_genome_amount): """ Get amounts of genomes by original genome @param probability: Proportion of simulated original genomes @type probability: int | long | float @param max_genome_amount: Total number of genomes @type max_genome_amount: int | long @return: List of integers representing amount of strains @rtype: list[int] """ assert isinstance(probability, (int, long, float)) assert 0 <= probability <= 1 assert isinstance(max_genome_amount, (int, long)) final_amounts = [] while sum(final_amounts) < max_genome_amount: if random.uniform(0, 1) < probability: final_amounts.append(1) else: amount = min(1 + np_random.geometric(0.3), 10) final_amounts.append(amount) final_amounts[-1] -= sum(final_amounts) - max_genome_amount return final_amounts
def _mutate_i(self, individual): eta = np.array(individual[self._id_eta]) x = np.array(individual[self.id_i]) if len(self._id_eta) == 1: eta = max(1, eta * exp(self.tau_i * randn())) p = 1 - (eta / self.N_i) / (1 + np.sqrt(1 + (eta / self.N_i) ** 2)) x_ = x + geometric(p, self.N_i) - geometric(p, self.N_i) else: eta = eta * exp(self.tau_i * randn() + self.tau_p_i * randn(self.N_i)) eta[eta > 1] = 1 p = 1 - (eta / self.N_i) / (1 + np.sqrt(1 + (eta / self.N_i) ** 2)) x_ = x + np.array([geometric(p_) - geometric(p_) for p_ in p]) # TODO: implement the same step-size repairing method here x_ = boundary_handling(x_, self.bounds_i[:, 0], self.bounds_i[:, 1]) individual[self._id_eta] = eta individual[self.id_i] = x_
def set_vars(self): if "num_rounds" not in self.session.vars: self.session.vars["num_rounds"] = min( geometric(0.5) + Constants.base_rounds - 1, Constants.num_rounds) # -1 because of geometric vs first success definition self.session.vars['timed_out'] = False
def make_splits(G,lamb): splits = [] i = 0 while i < G: splits.append(i) i += nprand.geometric(lamb) splits.append(G) return splits
def test_random(self): npr.seed(1) mean = 30 for ii in range(1000): length = npr.geometric(1/(mean-2)) + 2 binary1 = npr.rand(length) < 0.5 gray = binary2gray(binary1) binary2 = gray2binary(gray) nptest.assert_equal(binary1, binary2)
def map_otus_to_genomes(profile, per_rank_map, ranks, max_rank, mu, sigma, max_strains, debug, no_replace, max_genomes): unmatched_otus = [] otu_genome_map = {} warnings = [] sorted_otus = sort_by_abundance(profile) genome_set_size = 0 for otu in sorted_otus: if genome_set_size >= max_genomes and no_replace: #cancel if no genomes are available anymore break lin, abundances = profile[otu] lineage = transform_lineage(lin, ranks, max_rank) if len(lineage) == 0: warnings.append("No matching NCBI ID for otu %s, scientific name %s" % (otu, lin[-1].split("__")[-1])) unmatched_otus.append(otu) lineage_ranks = ncbi.get_rank(lineage) for tax_id in lineage: # lineage sorted ascending rank = lineage_ranks[tax_id] if ranks.index(rank) > ranks.index(max_rank): warnings.append("Rank %s of OTU %s too high, no matching genomes found" % (rank, otu)) warnings.append("Full lineage was %s, mapped from BIOM lineage %s" % (lineage, lin)) unmatched_otus.append(otu) break genomes = per_rank_map[rank] if tax_id not in genomes: warnings.append("For OTU %s no genomes have been found on rank %s with ID %s" % (otu, rank, tax_id)) continue # warning will appear later if rank is too high available_genomes = genomes[tax_id] strains_to_draw = max((np_rand.geometric(2./max_strains) % max_strains),1) if len(available_genomes) >= strains_to_draw: used_indices = np_rand.choice(len(available_genomes),strains_to_draw,replace=False) used_genomes = set([available_genomes[i] for i in used_indices]) else: used_genomes = set(available_genomes) # if not enough genomes: use all genome_set_size += len(used_genomes) # how many genomes are used log_normal_vals = np_rand.lognormal(mu,sigma, len(used_genomes)) sum_log_normal = sum(log_normal_vals) i = 0 for path, genome_id in used_genomes: otu_id = otu + "." + str(i) otu_genome_map[otu_id] = (tax_id, genome_id, path, []) # taxid, genomeid, http path, abundances per sample relative_abundance = log_normal_vals[i]/sum_log_normal i += 1 for abundance in abundances: # calculate abundance per sample current_abundance = relative_abundance * abundance otu_genome_map[otu_id][-1].append(current_abundance) if (no_replace): # sampling without replacement: for new_rank in per_rank_map: for taxid in per_rank_map[new_rank]: if (path, genome_id) in per_rank_map[new_rank][taxid]: per_rank_map[new_rank][taxid].remove((path,genome_id)) break # genome(s) found: we can break if len(warnings) > 0: _log.warning("Some OTUs could not be mapped") if debug: for warning in warnings: _log.warning(warning) return otu_genome_map, unmatched_otus, per_rank_map
def new_infection(self, node_count, generation, household, household_dict, serial_interval=None): """ Adds a new infection to the graph along with the following attributes: t - when they were infected offspring - how many offspring they produce Inputs:: G - the network object time - the time when the new infection happens node_count - how many nodes are currently in the network """ # Symptom onset time symptom_onset_time = self.time + self.incubation_period() # When a node reports it's infection if npr.binomial(1, self.infection_reporting_prob) == 1: will_report = True time_of_reporting = symptom_onset_time + npr.geometric( self.reporting_delay_par) else: will_report = False time_of_reporting = float('Inf') # We assign each node a recovery period of 21 days, after 21 days the probability of causing a new infections is 0, due to the generation time distribution recovery_time = self.time + 21 # Give the node the required attributes self.G.add_node(node_count) new_node_dict = { "time_infected": self.time, "generation": generation, "household": household, "contact_traced": False, "isolated": household_dict[household]["isolated"], "symptom_onset": symptom_onset_time, "outside_house_contacts_made": 0, "had_contacts_traced": False, "spread_to": [], "serial_interval": serial_interval, "recovered": False, "recovery_time": recovery_time, "will_report_infection": will_report, "reporting_time": time_of_reporting, "has_trace_app": self.has_contact_tracing_app() } self.G.nodes[node_count].update(new_node_dict) # Updates to the household dictionary # Each house now stores a the ID's of which nodes are stored inside the house, so that quarantining can be done at the household level nodes_in_house = household_dict[household]['nodes'] nodes_in_house.append(node_count) household_dict[household].update({'nodes': nodes_in_house})
def reads_from_ps(ps,mfl,min_seq_len,num_reads): """Given a vector of chromosomal occupancies and mean fragment length, return specified number of reads""" reads = [] lamb = 1.0/mfl G = len(ps) sampler = inverse_cdf_sampler(ps) while len(reads) < num_reads: L,R = nprand.geometric(lamb),nprand.geometric(lamb) #can be optimized to gamma(2,lamb) if L + R < min_seq_len: continue i = sampler() start,stop = (i - L) % G, (i + R)%G strand = "+" if random.random() < 0.5 else "-" if strand == "+": reads.append((strand,start,(start+min_seq_len)%G)) else: reads.append((strand,(stop-min_seq_len)%G,stop)) return reads
def take_random_sample(file_name, prob): reader = csv.reader( open(file_name, 'r'), delimiter = '\t', ) p = 0.01 skip = random.geometric(p) lines = [] spam_period = 10000 for line in reader: skip -= 1 if skip > 1: continue lines.append(line) if len(lines) % spam_period == 0: print len(lines) skip = random.geometric(p) pickle.dump(lines, open('training_sample.pickle', 'w'))
def do_serve(self, serve_pdu): assert isinstance(serve_pdu, Pdu) err_p = random.random() dice = random.random() if dice < err_p: error = True else: error = False duration = random.geometric(0.1) return duration, error
def infect(self, disease): """Infect this person with the given disease. This will change the person's state to INFECTED and calculate the time periods of this person's infection Arguments: disease {Disease} -- The disease that this person shall be infected with. """ self.disease = disease self.status = SIR_Status.INFECTED self.infection = Infection(self, disease) self.survival_days = geometric(self.infection.daily_mortality)
def initial_walk_states(initial, actions, num_walks, p_stop=.1): #randint(1, 10) states = set() for i in range(num_walks): walk = initial_random_walk(initial, actions, steps=geometric(p_stop)) #walk = initial_random_walk(initial, actions, steps=randint(1, 10)) for state in walk: states.add(state) #states.add(walk[-1]) return states
def _mutate_i(self, individual): eta = np.asarray(individual[self._id_eta].tolist(), dtype="float") x = np.asarray(individual[self.id_i], dtype="int") if len(self._id_eta) == 1: eta = eta * exp(self.tau_i * randn()) else: eta = eta * exp(self.tau_i * randn() + self.tau_p_i * randn(self.N_i)) eta[eta > 1] = 1 p = 1 - (eta / self.N_i) / (1 + np.sqrt(1 + (eta / self.N_i)**2.0)) x_ = x + geometric(p) - geometric(p) # Interval Bounds Treatment x_ = np.asarray(handle_box_constraint(x_, self.bounds_i[:, 0], self.bounds_i[:, 1]), dtype="int") individual[self.id_i] = x_ individual[self._id_eta] = eta
def randomise(_epsilon, _sensitivity): _gamma = 1 / (1 + np.exp(_epsilon / 2)) _sensitivity = 1 sign = -1 if random() < 0.5 else 1 geometric_rv = geometric(1 - np.exp(-_epsilon)) - 1 unif_rv = random() binary_rv = 0 if random() < _gamma / ( _gamma + (1 - _gamma) * np.exp(-_epsilon)) else 1 return sign *((1-binary_rv)*((geometric_rv+ _gamma * unif_rv) * _sensitivity) + \ binary_rv* ((geometric_rv +_gamma + (1-_gamma) * unif_rv) * _sensitivity))
def density_from_ps(ps,mfl,min_seq_len,num_reads): """ This function combines reads_from_ps and density_from_reads. For some reason, slightly slower than generating and mapping reads separately... """ G = len(ps) lamb = 1.0/mfl fwd_map = np.zeros(G) rev_map = np.zeros(G) reads_so_far = 0 sampler = inverse_cdf_sampler(ps) problematic_reads = 0 while reads_so_far < num_reads: L,R = nprand.geometric(lamb),nprand.geometric(lamb) #can be optimized to gamma(2,lamb) if L + R < min_seq_len: continue reads_so_far += 1 i = sampler() start,stop = (i - L) % G, (i + R)%G if random.random() < 0.5: strand = "+" stop = (start + min_seq_len) % G else: strand = "-" start = (stop - min_seq_len) % G if start < stop: #i.e. if read doesn't wrap around if strand == "+": fwd_map[start:stop] += 1 else: rev_map[start:stop] += 1 else: problematic_reads += 1 if strand == "+": fwd_map[start:G] += 1 fwd_map[0:stop] += 1 else: rev_map[start:G] += 1 rev_map[0:stop] += 1 print "problematic reads:",problematic_reads return fwd_map,rev_map
def __init__(self,arrival): self.arrival = arrival self.bags = npr.geometric(p=.8,size=1) self.total_time = 0 #adjust when leave station system_time - station time #need to adjust for arrival time self.checkin_time = 0 self.security_time = 0 self.gate_time = 0 #total_system_time - system time when leave security self.is_first_class = False
def centered_geometric(p, n=1, size=1): """ Returns a sample from the geometric law of parameter p, whose variance has been reduced by taking the mean of n experiences. The std of the returned sample is s/sqrt(n) where s is the original std, i.e. s = sqrt(p(1-p)). """ if isinstance(size, int): size = [size] size = [n] + list(size) return np.sum(rd.geometric(p, size), axis=0) / n
def geometric(list): n = len(list) - 1 p = 1e-4 #4.3e-4 #find_zero(lambda(x): fun_to_zero(x,n,f),1.0/(n+1),1.0) i = nr.geometric(p) - 1 if i > n: i = n return list[i]
def randomise(self, value): self.check_inputs(value) sign = -1 if random() < 0.5 else 1 geometric_rv = geometric(1 - np.exp(- self._epsilon)) - 1 unif_rv = random() binary_rv = 0 if random() < self._gamma / (self._gamma + (1 - self._gamma) * np.exp(- self._epsilon)) else 1 return value + sign * ((1 - binary_rv) * ((geometric_rv + self._gamma * unif_rv) * self._sensitivity) + binary_rv * ((geometric_rv + self._gamma + (1 - self._gamma) * unif_rv) * self._sensitivity))
def geometric(list): n = len(list)-1 p = 1e-4 #4.3e-4 #find_zero(lambda(x): fun_to_zero(x,n,f),1.0/(n+1),1.0) i = nr.geometric(p)-1 if i > n: i = n return list[i]
def permute_values(self): ''' Selects a value permutation for the whole game according the prior distribution. ''' orig_perm = list(range(13))[::-1] prop_perm = [] seed = geometric(p=0.25, size=13) - 1 for s in seed: pop_i = len(orig_perm) - 1 - (s % len(orig_perm)) prop_perm.append(orig_perm.pop(pop_i)) return prop_perm
def test_data_gen_non_markovian_1_par(): npr.seed(1) simulation = SIR_Selke(200, 0.008, 0.9, 5, infection_period_distribution=npr.geometric) test_var = simulation.inf_periods npr.seed(1) test_var2 = npr.geometric(0.9, 200) assert all(test_var2 == test_var)
def _mutate_i(self, X): n_point = X.shape[0] eta = np.array(X[:, self._id_eta], dtype=float).reshape(n_point, -1) X_ = np.array(X[:, self.id_i], dtype=int) if len(self._id_eta) == 1: eta *= exp(self.tau_i * randn(n_point, 1)) else: eta *= exp(self.tau_i * randn(n_point, 1) + \ self.tau_p_i * randn(n_point, self.N_i)) eta[eta > 1] = 1 p = 1 - (eta / self.N_i) / (1 + np.sqrt(1 + (eta / self.N_i)**2.)) _X = X_ + geometric(p) - geometric(p) # Interval Bounds Treatment _X = handle_box_constraint(_X, self.bounds_i[:, 0], self.bounds_i[:, 1]) _X = _X.astype(int) X[:, self.id_i] = _X X[:, self._id_eta] = eta
def compact(self): assert (len(self) >= self.capacity()) if (self.numCompaction % 2 == 1 and self.alternate): self.offset = 1 - self.offset else: self.offset = int(random() < 0.5) self.sort() lastItem = None s = 0 # where the compaction starts; default is 0 for self.schedule == 'alwaysAll' secsToCompact = 0 # choose a part to compact according to the selected schedule if self.schedule == 'deterministic' or self.schedule == 'randomized': if self.schedule == 'randomized': while True: secsToCompact = geometric(0.5) if (secsToCompact <= self.numSections): break else: #if self.schedule == 'deterministic' secsToCompact = trailing_zeros(self.numCompaction) s = int( 2 / 3 * self.capacity() ) - secsToCompact * self.sectionSize # 1/3 of capacity always compacted # make the number of sections larger if self.numCompaction > 2**self.numSections: self.numSections *= 2 # basically, doubling strategy on log_2 (number of compactions) elif self.schedule == 'alwaysHalf': s = int(self.capacity() / 2) #TODO randomizedSimple: set s uniformly and randomly in [0.25 * capacity(), 0.75 * capacity()], or sth like that assert (s < len(self) - 1) if ((len(self) - s) % 2 == 1): s += 1 for i in range(s + self.offset, len(self), 2): yield self[i] print( f"compacting {s}:\t secsToComp {secsToCompact}\t height {self.height}\t capacity {self.capacity()}\t size {len(self)}\t secSize {self.sectionSize}\t numSecs {self.numSections}" ) self[s:] = [] print(f"compaction done: size {len(self)}") self.numCompaction += 1
def select_pred_words_effectiveness(self, numWords=None): assert self.resp_words is not None # Add Predictive Words if self._pred_data_tickers is None: self.__add_pred_data_tickers() self.create_data_sorting_array() # Select How Many Words if numWords is None: numWords = geometric(PRED_COUNT_GEOMETRIC_PARAM) + PRED_COUNT_FLOOR # Select Words count = 0 chosen = {} resp_word_names = [str(w) for w in self.resp_words] min_date = max([w.min_date for w in self.resp_words]) max_date = min([w.max_date for w in self.resp_words]) while (len(chosen) < numWords) and (count < numWords * 10): count += 1 # Avoid Infinite Loops # Select Words / Create Data Handle id_ = weighted_choice(self._pred_data_eff_array["prob"].iteritems(), self._total_prob) ticker = retrieve_DataSeriesTicker(self.hndl_DB.cursor, id_) hndl_Data = EMF_DataSeries_Handle(self.hndl_DB, ticker=ticker) # Select Words / Create Trans Handle hndl_Trns = EMF_Transformation_Handle(choice(self._pred_trns_ptrns)) # Select Words / Create Trans Handle / Add Parameters to Trans Handle hndl_Trns.parameters = self.__create_random_trns_params_pred() # Select Words / Create Word Handle hndl_Word = EMF_WordSeries_Handle(self.hndl_DB, hndl_Data, hndl_Trns) # Select Words / Ensure Word Validity # Select Words / Ensure Word Validity / Make sure Word Isn't Response Word wordName = str(hndl_Word) if wordName in resp_word_names: continue # Select Words / Ensure Word Validity / Make sure Word Isn't Already Taken if wordName in chosen: continue # Select Words / Ensure Word Validity / Make sure Dates Don't Conflict min_challenger = hndl_Word.min_date if min_challenger >= max_date: continue max_challenger = hndl_Word.max_date if max_challenger <= min_date: continue min_date = max(min_date, min_challenger) max_date = min(max_date, max_challenger) # Select Words / Add Word to Set chosen[wordName] = hndl_Word log.info("WORDSELECT: Predictive Words: Chose {0}".format(wordName)) # TEST: Delete log.info("WORDSELECT: Predictive Words: Chose {0}".format(chosen.keys())) self._pred_words = chosen.values() return self.pred_words
def test_instanciation_from_kernel(self): rank, N = 6, 10 eig_vals = 1 + rndm.geometric(p=0.5, size=rank) eig_vecs, _ = qr(rndm.randn(N, rank), mode='economic') dpp = FiniteDPP(kernel_type='likelihood', projection=False, **{'L': (eig_vecs * eig_vals).dot(eig_vecs.T)}) dpp.compute_K() K = (eig_vecs * (eig_vals / (1.0 + eig_vals))).dot(eig_vecs.T) self.assertTrue(np.allclose(dpp.K, K))
def select_pred_words_random(self, numWords=None): assert self.resp_words is not None # Add Predictive Words if self._pred_data_tickers is None: self.__add_pred_data_tickers() # Select How Many Words if numWords is None: numWords = geometric(PRED_COUNT_GEOMETRIC_PARAM) + PRED_COUNT_FLOOR log.info("WORDSELECT: Predictive Words: Choosing {0} words".format(numWords)) # Select Words count = 0 chosen = {} resp_word_names = [ str(w) for w in self.resp_words ] # TODO: Can make this a one-time calculation to avoid repitition min_date = max([w.min_date for w in self.resp_words]) max_date = min([w.max_date for w in self.resp_words]) while (len(chosen) < numWords) and (count < numWords * 10): count += 1 # Avoid Infinite Loops # Select Words / Create Data Handle hndl_Data = EMF_DataSeries_Handle(self.hndl_DB, ticker=choice(self._pred_data_tickers)) # Select Words / Create Trans Handle hndl_Trns = EMF_Transformation_Handle(choice(self._pred_trns_ptrns)) # Select Words / Create Trans Handle / Add Parameters to Trans Handle hndl_Trns.parameters = self.__create_random_trns_params_pred() # Select Words / Create Word Handle hndl_Word = EMF_WordSeries_Handle(self.hndl_DB, hndl_Data, hndl_Trns) # Select Words / Ensure Word Validity # Select Words / Ensure Word Validity / Make sure Word Isn't Response Word wordName = str(hndl_Word) if wordName in resp_word_names: continue # Select Words / Ensure Word Validity / Make sure Word Isn't Already Taken if wordName in chosen: continue # Select Words / Ensure Word Validity / Make sure Dates Don't Conflict min_challenger = hndl_Word.min_date if min_challenger >= max_date: continue max_challenger = hndl_Word.max_date if max_challenger <= min_date: continue min_date = max(min_date, min_challenger) max_date = min(max_date, max_challenger) # Select Words / Add Word to Set chosen[wordName] = hndl_Word log.info("WORDSELECT: Predictive Words: Chose {0}".format(chosen.keys())) self._pred_words = chosen.values() return self.pred_words
def random_integer_partition(n, min_i): p = [] x = float(exp(-3.14/sqrt(6.0*float(n)))) good = False while not good: z = [0 for i in range(min_i-1)] z = z+[geometric(1-pow(x,i+1))-1 for i in range(min_i-1, n)] sum = 0 for i in range(min_i-1, n): sum = sum + (i+1)*z[i] good = sum==n p = [i+1 for i in range(n) for _ in range(z[i])] return p
def select_pred_words_random(self, numWords=None): assert self._resp_word is not None # Add Predictive Words if self._pred_data_tickers is None: self.__add_pred_data_tickers() # Select How Many Words if numWords is None: numWords = geometric(PRED_COUNT_GEOMETRIC_PARAM) + PRED_COUNT_FLOOR log.info('WORDSELECT: Predictive Words: Choosing {0} words'.format(numWords)) # Select Words count = 0 chosen = {} min_date = self.resp_data_min_date if min_date is None: min_ = maxint max_date = self.resp_data_max_date if max_date is None: max_ = -maxint-1 while (len(chosen) < numWords) and (count < numWords*10): count += 1 # Avoid Infinite Loops # Select Words / Create Data Handle ticker = choice(self._resp_data_tickers) hndl_Data = EMF_DataSeries_Handle(self.hndl_DB, ticker=ticker) # Select Words / Create Trans Handle hndl_Trns = EMF_Transformation_Handle(choice(self._pred_trns_ptrns)) # Select Words / Create Trans Handle / Add Parameters to Trans Handle hndl_Trns.parameters = self.__create_random_trns_params() # Select Words / Create Word Handle hndl_Word = EMF_WordSeries_Handle(self.hndl_DB, hndl_Data, hndl_Trns) # Select Words / Ensure Word Validity # Select Words / Ensure Word Validity / Make sure Word Isn't Response Word wordName = str(hndl_Word) if wordName == str(self._resp_word): continue # Select Words / Ensure Word Validity / Make sure Dates Don't Conflict min_challenger = hndl_Word.min_date if min_challenger >= max_date: continue max_challenger = hndl_Word.max_date if max_challenger <= min_date: continue min_date = max(min_date, min_challenger) max_date = min(max_date, max_challenger) # Select Words / Add Word to Set chosen[wordName] = hndl_Word log.info('WORDSELECT: Predictive Words: Chose {0}'.format(wordName)) # TEST: Delete log.info('WORDSELECT:Predictive Words: Chose {0}'.format(chosen.keys())) self._pred_words = chosen.values() return self.pred_words
def mapped_reads_from_chip_ps_np(ps,mean_frag_length,cells=10000): G = len(ps) genome = np.zeros(G) lamb = 1.0/mean_frag_length for cell in (xrange(cells)): print cell config = rfd_xs_np(ps) config_len = len(config) idx = 0 l = 0 r = 0 while l < G and idx < config_len: #print l,r,idx,config[idx] l,r = r,r+nprand.geometric(lamb) if l <= config[idx] < r: #print "got hit" genome[l:r] += 1 while idx < config_len and config[idx] < r: idx += 1 return genome
def get_assignment_context(request): """ Get a random task of the specified type.""" worker_id = request.GET.get('worker_id') task_type = request.GET.get('task_type') eligible_tasks = (InternalCrowdInterface.get_eligible_tasks(worker_id) .filter(task_type=task_type) .order_by('create_time')) # Pick a random task, biased towards older tasks. task_index = min(geometric(ORDER_WEIGHT) - 1, eligible_tasks.count() - 1) # generate a random assignment id for this assignment. assignment_id = uuid.uuid4() return { 'task_id': (eligible_tasks[task_index].task_id if task_index >= 0 else None), 'worker_id': worker_id, 'is_accepted': True, 'assignment_id': assignment_id, }
def handle(self): if not self.secureRequest: return try: data = self.secureRequest.recv() logger.info('[+] {0} wrote: {1}'.format(self.client_address[0], data)) if DISTRIBUTION == 'G': commit_num = geometric(1 - pow(10.0, -3 / float(MAX_COMMIT))) elif DISTRIBUTION == 'U': commit_num = random_integers(MAX_COMMIT - 1) else: commit_num = StrongRandom().randint(MIN_COMMIT, MAX_COMMIT) self.secureRequest.send(commit_num, True) self.secureRequest.commit(commit_num) logger.info('Banks says OK') except Exception as e: # Error in communication logger.critical('Bank says Failure')
def getInstance(self): dist = self.dist p = self.params small_correction = random.random() * 0.001 if dist == 'exponential': return random.exponential(p[0]) + small_correction elif dist == 'normal': return random.normal(p[0],p[1]) + small_correction elif dist == 'uniform': return random.uniform(p[0],p[1]) + small_correction elif dist == 'poisson': return random.poisson(p[0]) + small_correction elif dist == 'binomial': return random.binomial(p[0],p[1]) + small_correction elif dist == 'geometric': return random.geometric(p[0]) + small_correction elif dist == 'weibull': return random.weibull(p[0]) + small_correction elif dist == 'gamma': return random.gamma(p[0],p[1]) + small_correction elif dist == 'beta': return random.beta(p[0],p[1]) + small_correction elif dist == 'lognormal': return random.lognormal(p[0],p[1]) + small_correction
def _sample_temp_hypers(self, th_curr, #current temporal hypers (for some k) w, #current GP (for some k) Ztime, #counts for Z log_py_curr = None, #if not none, do pseudo_marg prop_scale = .025, #proposal scale for MH steps jitter = 1e-6, #jitter for gram mat sig2_bias = 25 #variance of bias term for W ): """ note that conditioned on Z, fitting W and cov hypers of W is like a simple Poisson regression proglem """ ## ## If no log_py_curr estimate, do Whitened slice sample ## if log_py_curr is None: nu = self._tkern.whiten_process(w[1:], th_curr, self._grids[-1]) def whitened_log_like(th, nu=nu): ll_prior = self._tkern.hyper_prior_lnpdf(th) if ll_prior < -1e50: return -np.inf logW = w[0] + np.log(self._dt) + \ self._tkern.gen_prior(th, self._grids[-1], nu=nu) ll = np.sum( logW*Ztime - np.exp(logW) ) return ll + ll_prior th_curr = slice_sample(th_curr, whitened_log_like) w[1:] = self._tkern.gen_prior(th_curr, self._grids[-1], nu=nu) return th_curr, w ## ## otherwise Sample covariance hypers with PSEUDOMARGINAL method ## #gen proposal, stop early if it's out of bounds for nn in range(1): th_prop = th_curr.copy() for i,hyper_name in enumerate(self._tkern.hyper_names()): if hyper_name=="Period": # special proposal for periodic params jump_prop=False if npr.rand() < .5: jump_prop=True factor = npr.geometric(p=.5) + 1 if npr.rand() < .5: factor = 1./factor th_prop[i] = factor * th_curr[i] + .05*npr.randn() else: th_prop[i] = th_curr[i] + prop_scale*npr.randn() else: th_prop[i] = th_curr[i] + prop_scale*npr.randn() #prior prob of hypers, stop early if it's no bueno log_prior_prop = self._tkern.hyper_prior_lnpdf(th_prop) if log_prior_prop < -1e20: continue #return th_curr, w, log_py_curr #likelihood function def bias_ll(y, f, cK_inv, grad=False, hess=False, hessp=False, p=None): return poisson_log_like(y, f, cK_inv, f0=np.log(self._dt), grad=grad, hess=hess, hessp=hessp, p=p) #compute current log marginal like (important, because Z changes) tgrid = self._grids[-1] N = len(tgrid) K_curr = ( self._tkern.K(tgrid, tgrid, th_curr) + #temp covariance jitter*np.eye(N) + #jitter for num stability sig2_bias*np.ones((N,N)) ) #prior uncertainty from bias #compute log marginal like K_prop = ( self._tkern.K(tgrid, tgrid, th_prop) + #temp covariance jitter*np.eye(N) + #jitter for num stability sig2_bias*np.ones((N,N)) ) #prior uncertainty from bias log_py_curr = approx_log_marg_like( Ztime, K_curr, like_func=bias_ll ) log_py_prop = approx_log_marg_like( Ztime, K_prop, like_func = bias_ll ) #print "period: ", th_prop #print "length: ", ell_prop #print log_py_prop - log_py_curr rat = ( log_py_prop - log_py_curr + log_prior_prop - self._tkern.hyper_prior_lnpdf(th_curr) ) if np.log(npr.rand()) < rat: print "accepting temporal proposal" self._num_accepts += 1 th_curr = th_prop log_py_curr = log_py_prop return th_curr, w, log_py_curr
def __init__(self, arrival, is_first_class): self.arrival = arrival self.is_first_class = is_first_class self.bags = npr.geometric(p=.6,size=1) self.total_time = 0 self.flight_time = None
def game_loop(self): '''This is called every game tick. You call this in a loop until it returns false, which means you hit a tree trunk, fell off the bottom of the screen, or jumped off the top of the screen. It calls the action and reward callbacks.''' # Render the background. self.screen.blit(self.background_img, (self.iter,0)) if self.iter < self.background_img.get_width() - self.screen_width: self.screen.blit(self.background_img, (self.iter+self.background_img.get_width(),0)) # Perhaps generate a new tree. if self.next_tree <= 0: self.next_tree = self.tree_img.get_width() + int(npr.geometric(1.0/self.tree_mean)) self.trees.append( { 'x': self.screen_width+1, 'y': int((0.3 + npr.rand()*0.65)*(self.screen_height-self.tree_gap)), 's': False }) # Process input events. for event in pg.event.get(): if event.type == pg.QUIT: sys.exit() elif self.action_fn is None and event.type == pg.KEYDOWN: self.vel = npr.poisson(self.impulse) self.hook = self.screen_width # Perhaps take an action via the callback. if self.action_fn is not None and self.action_fn(self.get_state()): self.vel = npr.poisson(self.impulse) self.hook = self.screen_width # Eliminate trees that have moved off the screen. self.trees = filter(lambda x: x['x'] > -self.tree_img.get_width(), self.trees) # Monkey dynamics self.monkey_loc -= self.vel self.vel -= self.gravity # Current monkey bounds. monkey_top = self.monkey_loc - self.monkey_img.get_height()/2 monkey_bot = self.monkey_loc + self.monkey_img.get_height()/2 # Move trees to the left, render and compute collision. self.next_tree -= self.horz_speed edge_hit = False tree_hit = False pass_tree = False for tree in self.trees: tree['x'] -= self.horz_speed # Render tree. self.screen.blit(self.tree_img, (tree['x'], self.tree_offset)) # Render gap in tree. self.screen.blit(self.background_img, (tree['x'], tree['y']), (tree['x']-self.iter, tree['y'], self.tree_img.get_width(), self.tree_gap)) if self.iter < self.background_img.get_width() - self.screen_width: self.screen.blit(self.background_img, (tree['x'], tree['y']), (tree['x']-(self.iter+self.background_img.get_width()), tree['y'], self.tree_img.get_width(), self.tree_gap)) trunk_left = tree['x'] + 215 trunk_right = tree['x'] + 290 trunk_top = tree['y'] trunk_bot = tree['y'] + self.tree_gap # Compute collision. if (((trunk_left < (self.monkey_left+15)) and (trunk_right > (self.monkey_left+15))) or ((trunk_left < self.monkey_right) and (trunk_right > self.monkey_right))): #pg.draw.rect(self.screen, (255,0,0), (trunk_left, trunk_top, trunk_right-trunk_left, trunk_bot-trunk_top), 1) #pg.draw.rect(self.screen, (255,0,0), (self.monkey_left+15, monkey_top, self.monkey_img.get_width()-15, monkey_bot-monkey_top), 1) if (monkey_top < trunk_top) or (monkey_bot > trunk_bot): tree_hit = True # Keep score. if not tree['s'] and (self.monkey_left+15) > trunk_right: tree['s'] = True self.score += 1 pass_tree = True if self.sound: self.blop_snd.play() # Monkey swings down on a vine. if self.vel < 0: pg.draw.line(self.screen, (92,64,51), (self.screen_width/2+20, self.monkey_loc-25), (self.hook,0), 4) # Render the monkey. self.screen.blit(self.monkey_img, (self.monkey_left, monkey_top)) # Fail on hitting top or bottom. if monkey_bot > self.screen_height or monkey_top < 0: edge_hit = True # Render the score score_text = self.font.render("Score: %d" % (self.score), 1, (230, 40, 40)) self.screen.blit(score_text, score_text.get_rect()) if self.text is not None: text = self.font.render(self.text, 1, (230, 40, 40)) textpos = text.get_rect() self.screen.blit(text, (self.screen_width-textpos[2],0,textpos[2],textpos[3])) # Render the display. pg.display.update() # If failed, play sound and exit. Also, assign rewards. if edge_hit: if self.sound: ch = self.screech_snd.play() while ch.get_busy(): pg.time.delay(500) if self.reward_fn is not None: self.reward_fn(self.edge_penalty) if self.action_fn is not None: self.action_fn(self.get_state()) return False if tree_hit: if self.sound: ch = self.screech_snd.play() while ch.get_busy(): pg.time.delay(500) if self.reward_fn is not None: self.reward_fn(self.tree_penalty) if self.action_fn is not None: self.action_fn(self.get_state()) return False if self.reward_fn is not None: if pass_tree: self.reward_fn(self.tree_reward) else: self.reward_fn(0.0) # Wait just a bit. pg.time.delay(self.tick_length) # Move things. self.hook -= self.horz_speed self.iter -= self.horz_speed if self.iter < -self.background_img.get_width(): self.iter += self.background_img.get_width() return True
def newcontent(self,n=0,t=-1): xpos,xposc = uniform(0.,1.,(2,self.M-n)) self.xpos[n:] = xpos self.xspeed[n:] = (xposc-xpos)/self.N self.born[n:] = t+cumsum(geometric(self.rate,(self.M-n,)))
def generate_fragments(T, xs, m): ''' Generate m random fragments from the transcripts T. The model of random fragmentation used here is very simple: we simple choose two random break-points, as follows, 1. Choose a transcript t randomly, with probability proportional to its relative abundance times its length. 2. Choose a start breakpoint uniformly from within the mature mRNA. 3. Choose an end breakpoint by drawing a geometric variate and adding it to the start breakpoint. If this ends up outside the mRNA, reject, and try again from step 1. ''' stderr.write('generating fragments ...\n') ws = np.array([t.exonic_length() for t in T]) * xs ws /= sum(ws) np.cumsum(ws, out = ws) fs = np.empty((m, 3), dtype = int) i = 0 while i < m: # choose a random transcript j = ws.searchsorted(random()) l = T[j].exonic_length() # choose start and end points start = random_integers(0, l - 1) end = start + geometric(args.frag_pr) # reject improper fragments (end breakpoint outside the mRNA) if end >= l: continue # reject fragments lost in size selection fraglen = end - start + 1 if fraglen < args.size_low + normal(args.size_noise): continue if fraglen > args.size_high + normal(args.size_noise): continue # reject fragments too short to sequence if fraglen < args.readlen: continue fs[i] = (j, start, end) i += 1 if (i % 100000) == 0: stderr.write('\t{0}\n'.format(i)) stderr.write('done. ({0} fragments)\n'.format(m)) return fs
def test_geometric(self): pymc3_random_discrete(Geometric, {'p':Unit}, size=500,# Test always fails with larger sample sizes. fails=50,# Be a bit more generous. ref_rand=lambda size, p=None: nr.geometric(p, size=size))
count += 1; print("-"*50) print("Score table:") print(result) print("\nRanking:") total = np.zeros(strlen, dtype=int) for i in range(strlen): total[i] = np.sum(result[i]) ranking = np.argsort(total)[::-1] for i in range(strlen): s = ranking[i] print("{0}. \"{1}\"".format(i+1, self.strategies[s].__name__)) print("total points: {0}, average points per game: {1}, average points per stage: {2}" .format( total[s], total[s]/(strlen-1), total[s]/((strlen-1)*self.ts_length) )) if __name__ == '__main__': payoff = np.array([[2, 0], [3, 1]]) discount_v = 0.9999 playtimes = geometric(1 - discount_v) strategies = [grim_trigger, random_strategy, allC, allD, alternate, two_one, tit_for_tat, probability] game = RepeatedMatrixGame(payoff, strategies, playtimes) game.play()
def randArity(self, minArity = 0, p = 0.4): return minArity + nrandom.geometric(p) - 1
def chip_spec_deprecated(G,config,mean_frag_length): lamb = 1.0/mean_frag_length raw_frags = [(max(0,i-nprand.geometric(lamb)),min(G,i+nprand.geometric(lamb))) for i in config]
keyfunc = kwds.get('key', lambda a: a) fill_val = kwds.get('fill_value', 0.0) args = [a.copy() for a in args] argsort = sorted(enumerate(args[0]), key=compose(keyfunc,itemgetter(1))) indexsort = [index for index, item in argsort] args = [a.take(indexsort) for a in args] # calculate groups g_mask = keyfunc(args[0]) g_set = unique1d(g_mask) g_max = max([g_mask[g_mask==g].shape[0] for g in g_set]) g_args = [fill_val * ones((len(g_set), g_max), dtype=a.dtype) for a in args] for gix, gval in enumerate(g_set): for ga, a in izip(g_args, args): b = a[g_mask==gval] ga[gix,:len(b)] = b return tuple(g_args) if __name__ == "__main__": from numpy import arange, set_printoptions, random set_printoptions(precision=2, suppress=True, linewidth=60); b = arange(100, 200) c = agroupby(b, key=lambda x: x%10) print c a = random.geometric(0.01, 20) b = a + 20 c, d = agroupby(a, b, key=lambda x: x%10) print c print d
def generate_geometric_distribution(S,pr=None): if not pr: pr = random() return ([geometric(pr) for _ in xrange(S)])
def _rvs(self, p): return mtrand.geometric(p, size=self._size)