def find_service_time(self, n, c): """ Finds the service time function """ if self.mu[c][n][0] == 'Uniform': return lambda : uniform(self.mu[c][n][1], self.mu[c][n][2]) if self.mu[c][n][0] == 'Deterministic': return lambda : self.mu[c][n][1] if self.mu[c][n][0] == 'Triangular': return lambda : triangular(self.mu[c][n][1], self.mu[c][n][2], self.mu[c][n][3]) if self.mu[c][n][0] == 'Exponential': return lambda : expovariate(self.mu[c][n][1]) if self.mu[c][n][0] == 'Gamma': return lambda : gammavariate(self.mu[c][n][1], self.mu[c][n][2]) if self.mu[c][n][0] == 'Normal': return lambda : gauss(self.mu[c][n][1], self.mu[c][n][2]) if self.mu[c][n][0] == 'Lognormal': return lambda : lognormvariate(self.mu[c][n][1], self.mu[c][n][2]) if self.mu[c][n][0] == 'Weibull': return lambda : weibullvariate(self.mu[c][n][1], self.mu[c][n][2]) if self.mu[c][n][0] == 'Custom': P, V = zip(*self.parameters[self.mu[c][n][1]]) probs = list(P) cum_probs = [sum(probs[0:i+1]) for i in range(len(probs))] values = list(V) return lambda : self.custom_pdf(cum_probs, values) return False
def sample(self, scale=1): csample = [random.gammavariate(a, 1) for a in self.alphas] for aidx, (sampprob, alpha) in enumerate(zip(csample, self.alphas)): while sampprob == 0: sampprob = random.gammavariate(alpha, 1) csample[aidx] = sampprob return scale * scipy.array(csample) / sum(csample)
def createPatches(self,vCovarianceParams,aBreedGamma,bBreedGamma,aFeedGamma,bFeedGamma,aInitialPopulation,aSize): cNumPatches = np.random.poisson(vCovarianceParams[3]) vPatches = [] for i in range(0,cNumPatches): cNumBreedRand = random.gammavariate(aBreedGamma,bBreedGamma) cNumFeedRand = random.gammavariate(aFeedGamma,bFeedGamma) vPatches.append(self.addPatch(vCovarianceParams,cNumBreedRand,cNumFeedRand,mosquitoGroup(aInitialPopulation,aInitialPopulation,0,0,0,0,0,0,0,0),aSize)) return vPatches
def generate_raw_data(gene_ids, path): """Generate random DE data.""" de_data = {} header = ['test_id', 'gene_id', 'gene', 'locus', 'sample_1', 'sample_2', 'status', 'value_1', 'value_2', 'log2(fold_change)', 'test_stat', 'p_value', 'q_value', 'significant'] with gzip.open(gene_ids, mode='rt') as gene_ids: all_genes = [line.strip() for line in gene_ids] n_of_genes = len(all_genes) de_data['test_id'] = all_genes de_data['gene_id'] = all_genes de_data['gene'] = all_genes de_data['locus'] = ['chr20:463337-524482'] * n_of_genes de_data['sample_1'] = ['control'] * n_of_genes de_data['sample_2'] = ['case'] * n_of_genes de_data['status'] = ['OK'] * n_of_genes de_data['value_1'] = [random.gammavariate(1, 100) for _ in all_genes] de_data['value_2'] = [random.gammavariate(1, 100) for _ in all_genes] de_data['log2(fold_change)'] = [random.uniform(-10, 10) for _ in all_genes] de_data['test_stat'] = [random.uniform(-3, 3) for _ in all_genes] de_data['p_value'] = [random.uniform(0, 1) for _ in all_genes] de_data['q_value'] = [random.uniform(0, 1) for _ in all_genes] de_data['significant'] = [random.choice(['yes', 'no']) for _ in all_genes] rows = zip(de_data['test_id'], de_data['gene_id'], de_data['gene'], de_data['locus'], de_data['sample_1'], de_data['sample_2'], de_data['status'], de_data['value_1'], de_data['value_2'], de_data['log2(fold_change)'], de_data['test_stat'], de_data['p_value'], de_data['q_value'], de_data['significant']) with gzip.open(os.path.join(path, 'de_raw.tab.gz'), 'wt') as raw_df: writer = csv.writer(raw_df, delimiter=str('\t'), lineterminator='\n') writer.writerow(header) for row in rows: writer.writerow(row) with open(os.path.join(path, 'de_json.json'), 'w') as json_file: de_data_std = { 'stat': de_data['test_stat'], 'logfc': de_data['log2(fold_change)'], 'pvalue': de_data['p_value'], 'fdr': de_data['q_value'], 'gene_id': de_data['gene_id'] } json.dump(de_data_std, json_file, indent=4, sort_keys=True) rows = zip(de_data_std['gene_id'], de_data_std['logfc'], de_data_std['fdr'], de_data_std['pvalue'], de_data_std['stat']) with gzip.open(os.path.join(path, 'de_file.tab.gz'), 'wt') as de_file: writer = csv.writer(de_file, delimiter=str('\t'), lineterminator='\n') writer.writerow(['gene_id', 'logfc', 'fdr', 'pvalue', 'stat']) for row in rows: writer.writerow(row)
def createInputs(filename, N, S, dS, B, dB): # create input file for blimit.py out = open(filename, 'w') out.write('#----------------------------------------------------------\n') out.write('# Format:\n') out.write('# M number of bins\n') out.write('# N1 N2 ...\n') out.write('# K number of sampled points\n') out.write('# S1 S2 ...\n') out.write('# B1 B2 ...\n') out.write('# : :\n') out.write('# The item is the number of bins. This is followed\n') out.write('# by the observed counts, the signal counts, then\n') out.write('# the background counts\n') out.write('#\n') out.write('# In order to account for uncertainties whatever their origin\n') out.write('# repeat the last pair of lines with different random samplings\n') out.write('# of signal and background counts.\n') out.write('#----------------------------------------------------------\n') out.write('# number of bins\n') out.write('\t1\n') out.write('# observed counts\n') out.write('\t%d\n' % int(N)) if dS == 0 and dB == 0: out.write('# number of points\n') out.write('\t1\n') out.write('# signals or effective luminosities (eff x Lumi)\n') out.write('\t%10.4e\n' % S) out.write('# backgrounds\n') out.write('\t%10.4e\n' % B) else: ntrial = 400 gamma_S, beta_S = computeGammaConstants(S, dS) gamma_B, beta_B = computeGammaConstants(B, dB) out.write('# number of sampled points\n') out.write('\t%d\n' % ntrial) for ii in xrange(ntrial): jj = ii+1 if dS > 0: sig = gammavariate(gamma_S, beta_S) else: sig = 0.0 out.write('# %4d signal\n' % jj) out.write('\t%10.3f\n' % sig) if dB > 0: bkg = gammavariate(gamma_B, beta_B) else: bkg = 0.0 out.write('# %4d background\n' % jj) out.write('\t%10.3f\n' % bkg) out.close()
def __init__(self,anumPatches,aSize,aInitialPopulation,aBreedGamma,bBreedGamma,aFeedGamma,bFeedGamma,vCovarianceParams): self.vPatches = [] if vCovarianceParams[0] == 0: for i in range(0,anumPatches): cNumBreedRand = random.gammavariate(aBreedGamma,bBreedGamma) cNumFeedRand = random.gammavariate(aFeedGamma,bFeedGamma) self.vPatches.append(patch(randomLocation(aSize),cNumBreedRand,cNumFeedRand,mosquitoGroup(aInitialPopulation,aInitialPopulation,0,0,0,0,0,0,0,0))) self.numPatches = anumPatches else: self.vPatches = randomCovariancePatches(aSize,vCovarianceParams,aBreedGamma,bBreedGamma,aFeedGamma,bFeedGamma,aInitialPopulation) self.numPatches = len(self.vPatches) self.Size = aSize
def generateHits(thread, signal, pulse, eventType): if eventType != 'trues' and eventType != 'dark': return # skip those with 0 rate (disable) if _conf['eventRate'][eventType] == 0: return channel = thread.status.getChannel() fileName = _conf['directory'] + _conf['filename'][eventType].format(channel=int(channel)) if _conf['enableSaving']: f = file(fileName, 'w') t = float(0) while True: saveThisEvent = True t_wait = random.gammavariate(1.0, 1/_conf['eventRate'][eventType]) progress = int(round(t / _runTime * 100)) if progress > thread.status.getProgress(): thread.status.setProgress(progress) time.sleep(0.001) # give the output some time to print # time of the next event t_next = t + t_wait if t_next > _runTime: break # generate pulse (maximum at 5 fC (=MIP)) if eventType == 'trues': fC = random.gammavariate(_conf['chargeDistAlpha'], _conf['chargeDistBeta']) t_thr, length = pulse.addEvent(signal, fC, t_next) if fC < 2: saveThisEvent = False else: fC = 0.01; # calculated integral, done with WolframAlpha (http://wolfr.am/1jJtxac) t_thr, length = addDarkPulse(signal, t_next) # write to file if _conf['enableSaving'] and saveThisEvent: f.write("{0:.12f} {1:06.3f}\n".format(t_thr, fC)) # time for the next event if length > t_wait: t += length # fix pile-up t += t_wait if _conf['enableSaving']: f.close()
def getCvalue(self, hyperparameters): #The c value used to compute myopic-VPI. See Dearden et al. 1998. #a gamma distribution with one parameter corresponds to gamma(a,1) mu = hyperparameters[0]; lambdu = hyperparameters[1]; alpha = hyperparameters[2]; beta = hyperparameters[3]; num1 = (alpha * random.gammavariate(alpha+0.5, 1) * math.sqrt(beta)); denom1 = ((alpha-0.5)*random.gammavariate(alpha,1)*random.gammavariate(0.5,1)*alpha*math.sqrt(2*lambdu)); prod1 = (1.0 + mu**2/(2*alpha)); pow1 = -1.0 * alpha+0.5; c = (num1/denom1) * prod1**pow1; return c;
def Dirichlet(n=1000, alphas=[1,1,1]): ''' Generate a Dirichlet Distribution; Default: #points is 1000; plot 3-dim space; 2-dim simplex with uniform distribution; ''' dim = len(alphas) samples = np.array([([None] * dim) for row in xrange(n)]) for i in range(n): for j in range(dim): alpha = alphas[j] samples[i, j] = random.gammavariate(alpha, 1) samples[i, :] = samples[i, :]/sum(samples[i, :]) if dim==3: fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.scatter(samples[:, 0], samples[:, 1] , samples[:, 2]) plt.show() return samples
def tally(candidates, vote_counts, nballots): ncandidates = len(candidates) gvariates = [gammavariate(1 + vote_counts[i], 1) for i in xrange(ncandidates)] gvsum = float(sum(gvariates)) result = tuple(vote_counts[i] + (nballots - sum(vote_counts)) \ * gvariates[i] / gvsum for i in xrange(ncandidates)) return result.index(max(result))
def pux_sampling(x, A, C, invC, sampleNr): h = size(x, 0) w = size(x, 1) s2 = 0.5; # sigma squared x = x.reshape(h*w) zarr = zeros(size(x)) ident = identity(size(x)) c = zeros(sampleNr) m = zeros((sampleNr, h*w)) s = zeros((sampleNr, h*w, h*w)) zsum = 0 nsum = 0 acat = dot(A, dot(C, A.T)) ata = dot(A.T, A) for i in range(0, sampleNr): z = random.gammavariate(2,2) c[i] = multi_norm(x, zarr, s2*ident + z**2*acat) zsum += z*c[i] nsum += c[i] s[i] = linalg.inv(invC + z/s2*ata) m[i] = z/s2*dot(s[i]*A.T,x) c = c/sum(c); # normalization return [c, m, s, zsum/nsum]
def find_distributions(self, n, c, source): """ Finds distribution functions """ if source[c][n] == 'NoArrivals': return lambda : 'Inf' if source[c][n][0] == 'Uniform': return lambda : uniform(source[c][n][1], source[c][n][2]) if source[c][n][0] == 'Deterministic': return lambda : source[c][n][1] if source[c][n][0] == 'Triangular': return lambda : triangular(source[c][n][1], source[c][n][2], source[c][n][3]) if source[c][n][0] == 'Exponential': return lambda : expovariate(source[c][n][1]) if source[c][n][0] == 'Gamma': return lambda : gammavariate(source[c][n][1], source[c][n][2]) if source[c][n][0] == 'Lognormal': return lambda : lognormvariate(source[c][n][1], source[c][n][2]) if source[c][n][0] == 'Weibull': return lambda : weibullvariate(source[c][n][1], source[c][n][2]) if source[c][n][0] == 'Custom': P, V = zip(*self.parameters[source[c][n][1]]) probs = list(P) cum_probs = [sum(probs[0:i+1]) for i in range(len(probs))] values = list(V) return lambda : self.custom_pdf(cum_probs, values) if source[c][n][0] == 'Empirical': if isinstance(source[c][n][1], str): empirical_dist = self.import_empirical_dist(source[c][n][1]) return lambda : choice(empirical_dist) return lambda : choice(source[c][n][1]) return False
def sample_parameters_given_hyper(self, gen_seed=0): """ Samples a Gaussian parameter given the current hyperparameters. Inputs: gen_seed: integer used to seed the rng """ if type(gen_seed) is not int: raise TypeError("gen_seed should be an int") random.seed(gen_seed) hypers = self.get_hypers() s = hypers['s'] r = hypers['r'] nu = hypers['nu'] m = hypers['mu'] rho = random.gammavariate(nu/2.0, s) mu = random.normalvariate(m, (r/rho)**.5) assert(rho > 0) params = {'mu': mu, 'rho': rho} return params
def rand_student_t(df, mu=0, std=1): """return random number distributed by student's t distribution with `df` degrees of freedom with the specified mean and standard deviation. """ x = random.gauss(0, std) y = 2.0*random.gammavariate(0.5*df, 2.0) return x / (math.sqrt(y/df)) + mu
def generateGammaRVs(aShape, bScale, count, histDelta): rVec = numpy.zeros(count) rMax = 0. for ii in range(count): rVec[ii] = random.gammavariate(aShape, bScale) if (rMax < rVec[ii]): rMax = rVec[ii] # build the histogram ... deltaR = histDelta rMinHist = 0. rMaxHist = rMax + 2. * deltaR numBins = (rMaxHist - rMinHist) / deltaR numBins = int(numBins) + 2 rHist = numpy.zeros(numBins) print ' making histogram ... ', deltaR, rMaxHist for ii in range(count): iBin = int((rVec[ii] - rMinHist) / deltaR + 0.0001) rHist[iBin] += 1 for iBin in range(numBins): rHist[iBin] /= float(count) return (rVec, rHist)
def main(args): request_id = 0 fake = Faker() fake.seed(0) with open(filename, "w+") as f: f.write("request id|client name|room type|request type|start date|end date|#adults|#children\n") for i in range(0, number_of_lines): request_id += 1 client_name = fake.name() room_type = random.choice(data.rooms.keys()) request_type = random.choice(["wedding", "party", "conference"]) if "conference" in room_type else random.choice(["holiday", "business"]) start_date = data.random_date_between(datetime(2016, 1, 1).date(), datetime(2016, 3, 31)) end_date = start_date + timedelta(1 + int(random.gammavariate(2, 2))) num_adults = max(1, int(random.betavariate(2, 5) * 10)) num_children = int(random.betavariate(1, 5) * 10) if request_type == "conference": num_adults = max(1, int(random.normalvariate(25, 9))) num_children = 0 elif request_type == "wedding": num_adults = max(2, int(random.normalvariate(25, 9))) num_children = max(0, int(random.normalvariate(25, 12))) elif request_type == "party": num_adults = max(1, int(random.normalvariate(25, 9))) num_children = max(0, int(random.normalvariate(25, 12))) elif request_type == "business": num_children /= 2 f.write("{}|{}|{}|{}|{}|{}|{}|{}\n".format(request_id, client_name, room_type, request_type, start_date, end_date, num_adults, num_children))
def WstawBalony(self, N): for n in range(N): balon = Balon(0, 0) x = random.uniform(0 + balon.NW.x(), WIDTH_GAME - balon.NE.x()) y = random.gammavariate(1,1) balon.UstawPozycje(x,y) self.lista_enemy.append(balon)
def WstawStacje(self, N): for n in range(N): stacja = Stacja(0, 0) x = random.uniform(0 + stacja.NW.x(), WIDTH_GAME - stacja.NE.x()) y = random.gammavariate(1,1) stacja.UstawPozycje(x,y) self.lista_friend.append(stacja)
def WstawStatki(self, N): for n in range(N): statek = Statek(1 / 2 * WIDTH_GAME, 0) x = random.uniform(30 + statek.NW.x(), WIDTH_GAME - statek.NE.x() - 40) y = random.gammavariate(1,1) statek.UstawPozycje(x,y) self.lista_enemy.append(statek)
def randomly_fragment(sequence, max_pieces, \ alpha = 0.1, beta = 100000, \ min_length = 1000, max_length = 200000): """ randomly fragment genome and return random subset of fragments """ shuffled = [] # break into pieces of random length while sequence is not False: s = int(random.gammavariate(alpha, beta)) if s <= min_length or s >= max_length: continue if len(sequence) < s: seq = sequence[0:] else: seq = sequence[0:s] sequence = sequence[s:] shuffled.append(seq) if sequence == []: break # shuffle pieces random.shuffle(shuffled) # subset fragments subset, total = [], 0 for fragment in shuffled: length = len(fragment) if total + length <= max_pieces: subset.append(fragment) total += length else: diff = max_pieces - total subset.append(fragment[0:diff]) break return subset
def WstawMysliwce(self, N): for n in range(N): mysliwiec = Mysliwiec(0, 0) x = random.uniform(0 + mysliwiec.NW.x(), WIDTH_GAME - mysliwiec.NE.x()) y = random.gammavariate(1,1) mysliwiec.UstawPozycje(x,y) self.lista_enemy.append(mysliwiec)
def observations_r(obs, shape = 10): """ Parameters ---------- x A realization of the hidden variable shape The shape parameter of the added noise Returns ------- xb, xd The path of the hidden variables inc, splits Noisy observations """ nb_cells = len(obs['incr']) nb_splits = len(obs['splits']) xb = [None]*nb_cells xd = [None]*nb_splits xb[0] = 1.0 for ii in range(nb_splits): xd[ii] = xb[ii] + obs['incr'][ii] xb[2*ii+1+obs['delta'][ii]] = xd[ii]*obs['splits'][ii] xb[2*ii+2-obs['delta'][ii]] = xd[ii]-xb[2*ii+1+obs['delta'][ii]] growth_rate = [gammavariate(shape, 1.0/(x+sys.float_info.epsilon)) for x in xb] x_hat = [shape/gg for gg in growth_rate] return {'x_hat': x_hat }
def random_parameter_set(k,psi=1.0): """Draws a parameter set from a Dirichlet distribution. k is the number of states psi is the Dirichlet meta-parameter""" pr = [ random.gammavariate(psi,1) for i in xrange(k) ] pr_sum = sum(pr) return [ p/pr_sum for p in pr ]
def getRandomReturnRate(): """ This function generates and returns a random rate of return for your retirement fund. The rate will be given with two digits after the decimal point (i.e. 4.56 for 4.56%) The average rate will be near 11.5 (the historical average for the S&P 500) The standard deviation will also be near the historical standard deviation for the S&P 500 """ x = gauss(11.5,20) y = 2.0*gammavariate(1,2.0) ans = x/(y/2)**(1/2) while ans > 50 or ans < -50: x = gauss(11.5,20) y = 2.0*gammavariate(1,2.0) ans = x/(y/2)**(1/2) return round(x,2)
def sample(x): if bProbabilistic: try: return cache[x] except: cache[x] = random.gammavariate(1/.5625, .5625 * x) return cache[x] return x
def sample_sigma_from_inverse_gamma_distri_of_after(mu_now): mu_in_sigma_distribution_after=(n_1+1.0)/2.0 sigma_in_sigma_distribution_after=((n_1_times_S_1)+m_1*((mu_now-mu_1)**2))/2.0 #ちょっとこの関数には妙な感じがするので、別の方法で #sigma_now=stats.invgamma.rvs(sigma_in_sigma_distribution_after, loc=mu_in_sigma_distribution_after) #sigma_now=math.sqrt(sigma_now) sigma_now=(1.0/random.gammavariate(mu_in_sigma_distribution_after, 1.0/sigma_in_sigma_distribution_after)) return sigma_now
def random_pr(k, psi=1.0): """k is number of states""" pr = [random.gammavariate(psi, 1) for i in xrange(k)] # pr = [ -math.log(random.random()) for i in xrange(k) ] pr_sum = sum(pr) pr = [p / pr_sum for p in pr] return pr
def gibbs(N=50000,thin=1000): x=0 y=0 print "Iter x y" for i in range(N): for j in range(thin): x=random.gammavariate(3,1.0/(y*y+4)) y=random.gauss(1.0/(x+1),1.0/math.sqrt(2*x+2))
def bot_reap_task(dimensions, bot_id, bot_version): """Reaps a TaskToRun if one is available. The process is to find a TaskToRun where its .queue_number is set, then create a TaskRunResult for it. Returns: tuple of (TaskRequest, TaskRunResult) for the task that was reaped. The TaskToRun involved is not returned. """ assert bot_id q = task_to_run.yield_next_available_task_to_dispatch(dimensions) # When a large number of bots try to reap hundreds of tasks simultaneously, # they'll constantly fail to call reap_task_to_run() as they'll get preempted # by other bots. So randomly jump farther in the queue when the number of # failures is too large. failures = 0 to_skip = 0 total_skipped = 0 for request, to_run in q: if to_skip: to_skip -= 1 total_skipped += 1 continue run_result = _reap_task(to_run.key, request, bot_id, bot_version, dimensions) if not run_result: failures += 1 # Every 3 failures starting on the very first one, jump randomly ahead of # the pack. This reduces the contention where hundreds of bots fight for # exactly the same task while there's many ready to be run waiting in the # queue. if (failures % 3) == 1: # TODO(maruel): Choose curve that makes the most sense. The tricky part # is finding a good heuristic to guess the load without much information # available in this content. When 'failures' is high, this means a lot # of bots are reaping tasks like crazy, which means there is a good flow # of tasks going on. On the other hand, skipping too much is useless. So # it should have an initial bump but then slow down on skipping. to_skip = min(int(round(random.gammavariate(3, 1))), 30) continue # Try to optimize these values but do not add as formal stats (yet). logging.info("failed %d, skipped %d", failures, total_skipped) pending_time = run_result.started_ts - request.created_ts stats.add_run_entry( "run_started", run_result.key, bot_id=bot_id, dimensions=request.properties.dimensions, pending_ms=_secs_to_ms(pending_time.total_seconds()), user=request.user, ) return request, run_result if failures: logging.info("Chose nothing (failed %d, skipped %d)", failures, total_skipped) return None, None
def GenerateLDASample(): #Load the word list words=[] fid = open('C:\\Users\\Arvind\\Desktop\\data\\words.txt','rU') for line in fid: words.append(line.split()[1]) fid.close() #Load the alpha file alpha=[] fid = open('C:\\Users\\Arvind\\Desktop\\models\\model65536_50.alpha','rU') for line in fid: for word in line.split(): alpha.append(float(word)) fid.close() #Load the beta file beta=[] fid = open('C:\\Users\\Arvind\\Desktop\\models\\model65536_50.beta','rU') for line in fid: tmpvec=[] for word in line.split(): tmpvec.append(float(word)) beta.append(tmpvec) fid.close() #Sample theta from dirichlet(alpha) using the logic mentioned at http://en.wikipedia.org/wiki/Dirichlet_distribution#Random_number_generation tmptheta=[rand.gammavariate(alphai, 1) for alphai in alpha] sumtmptheta = sum(tmptheta) theta = [] for tmpthetai in tmptheta: theta.append(tmpthetai/sumtmptheta) AbstractLength = 1000 AbstractVec = [] for pos in range(AbstractLength): #For each word position, sample a topic z from multinomial(theta) randomUniformNumber = rand.uniform(0, 1) topic = -1 for index in range(len(alpha)): if randomUniformNumber < sum(alpha[0:index+1]): topic = index break if topic == -1: topic = len(alpha)-1 #Then, sample the word at that position from multinomial(beta[z]) betaz = [betaprime[topic] for betaprime in beta] randomUniformNumber = rand.uniform(0, 1) wordid = -1 for index in range(len(betaz)): if randomUniformNumber < sum(betaz[0:index+1]): wordid = index break if wordid == -1: wordid = len(betaz)-1 AbstractVec.append(words[wordid]) print ' '.join(AbstractVec)
def grow_sustained(self, t): """Sample segment elongation rate from "branch/elongate phase" gamma distribution, and apply to ``self.elongated_len``.""" p = self.dendrite.parameters rate = random.gammavariate(p['gamma_be'], p['beta_be']) + p['alpha_be'] self.elongated_len = rate * (t - self.created - 1)
def gen_dirichlet_random_from_gamma(params): sample = [random.gammavariate(a, 1) for a in params] sample = [v/sum(sample) for v in sample] return sample
def gamma(alpha, beta): while True: yield random.gammavariate(alpha, beta)
def dataGenerator(self, powerLevel): pi = math.pi lambda1 = 0.005 #arrival rate per sample lambda2 = 0.005 #survival rate per sample kParam1 = 2 #k-parameter for Erlang/gamma distribution (ON) kParam2 = 2 #k-parameter for Erlang/gamma distribution (OFF) var1 = lambda1 #variance parameter for log-normal distribution (ON) var2 = lambda2 #variance parameter for log-normal distribution (OFF) N = 300 #number of samples occupancy = [0] * N stateTrans = [] #tracks alternating states [1,0,1,0,1,0,...] intTimes = [] #tracks intervals upTimes = [] downTimes = [] intTimesSeq = [] #counts and tracks intervals upDist = "lnorm" #'exp', 'erl', or 'lnorm' downDist = "lnorm" #'exp', 'erl', or 'lnorm' #process initialized to "on" totalTime = 0 #tracks total time generated by the ARP seqState = 1 #tracks next state to generate while totalTime < N: #generates on sequence if seqState: #generates random on period if upDist == "exp": period = math.ceil(random.expovariate(lambda1)) elif upDist == "erl": period = math.ceil( random.gammavariate(kParam1, 1 / lambda1)) #assumes k=2 elif upDist == "lnorm": trueMu = math.log(((1 / lambda1)**2) / math.sqrt((1 / var1) + (1 / lambda1)**2)) trueSig = math.sqrt( math.log((1 / var1) / ((1 / lambda1)**2) + 1)) period = math.ceil(random.lognormvariate(trueMu, trueSig)) #period = 5 if (totalTime + period) > N: #makes sure total time isn't exceeded occupancy[totalTime:N] = [1] * (N - totalTime) else: #appends proper sequence of 1s occupancy[totalTime:totalTime + period] = [1] * period #tracks state transitions and on/off durations stateTrans.append(1) intTimes.append(period) upTimes.append(period) intTimesSeq.append(list(range(1, period + 1))) seqState = 0 #generates off sequence else: #generates random off period if downDist == "exp": period = math.ceil(random.expovariate(lambda2)) elif downDist == "erl": period = math.ceil( random.gammavariate(kParam2, 1 / lambda2)) #assumes k=2 elif downDist == "lnorm": period = math.ceil(random.lognormvariate(lambda2, var2)) #period = 10 if (totalTime + period) > N: #makes sure total time isn't exceeded occupancy[totalTime:N] = [0] * (N - totalTime) else: #appends proper sequence of 1s occupancy[totalTime:totalTime + period] = [0] * period #tracks state transitions and on/off durations stateTrans.append(0) intTimes.append(period) downTimes.append(period) intTimesSeq.append(list(range(1, period + 1))) seqState = 1 totalTime += period seqSize = len(stateTrans) #total number of on and off states traffic_intensity = sum(occupancy) / N #measures traffic intensity #measures mean signal interarrival mean_int = sum(intTimes[0:seqSize - (seqSize % 2)]) / ((seqSize - (seqSize % 2)) / 2) actual_int = 1 / lambda1 + 1 / lambda2 #calculates theoretical interarrival #reactive predictor "accuracy/error" predicted = occupancy[0:N - 1] #theoretical accuracy based on lambda parameters theoAcc = 1 - (2 / actual_int - 1 / N) #accuracy based on measured mean interarrival expAcc = 1 - (2 / mean_int - 1 / N) #observed accuracy """ result = [0]*(N-1) for i in range(N-1): if predicted[i]==occupancy[i+1]: result[i]=1 obsAcc = sum(result)/(N-1) """ obsAcc = sum([predicted[i] == occupancy[i + 1] for i in range(N - 1)]) / (N - 1) ###input RF signal generation### dLen = 10 #length of the energy detector fs = 100e6 time = [i / fs for i in range(N * dLen)] powerLvl = powerLevel #power in dBm amp = math.sqrt( (10**(powerLvl / 10)) / 1000 * (2 * 50)) #sinusoid amplitude noiseVar = 1e-7 #noisefloor variance (1e-7 places noisefloor around -100 dBm) noisefloor = [ math.sqrt(noiseVar) * random.gauss(0, 1) for i in range(N * dLen) ] sineWave = [ amp * cmath.exp(1j * 2 * pi * (10e6) * time[i]) for i in range(N * dLen) ] #sine wave at 10 MHz #SNR of the signal SNR = 10 * math.log10( (sum([abs(sineWave[i])**2 for i in range(N * dLen)]) / (dLen * N)) / (sum([abs(noisefloor[i])**2 for i in range(N * dLen)]) / (dLen * N))) #Modulates the sine wave with the occupancy state where each state has dLen samples occSwitch = [occupancy[math.floor(i / dLen)] for i in range(N * dLen)] inputRF = [ sineWave[i] * occSwitch[i] + noisefloor[i] for i in range(N * dLen) ] P_fa = 0.01 #probability of false alarm thresh = noiseVar / np.sqrt(dLen) * special.erfinv(P_fa) + noiseVar #Calculates total average power over a sliding window totalAvgPwr = np.zeros((1, dLen * N - dLen + 1)) pwrStates = np.zeros((dLen, dLen * N - dLen + 1)) t = dLen * N - dLen + 1 for i in range(t): totalAvgPwr.itemset(i, sum(np.abs(inputRF[i:i + dLen - 1])**2) / dLen) for k in range(t, dLen): pwrStates.itemset((i, k - 1), k) #Observed states based on energy detector obsState = totalAvgPwr > thresh plt.plot(np.array([i for i in range(dLen * N - dLen + 1)]), 10 * np.log10(thresh * np.ones(np.size(totalAvgPwr))) - 30) #energy detector threshold #plt.plot(t,s,dLen*N-dLen+1) plt.title('ARP Simulator') plt.xlabel('Samples') plt.ylabel('Total Average Power (dBm)') plt.show() plt.subplot(2, 1, 2) #plt.subplot(2,1,2) plt.plot(inputRF[dLen:dLen * N]) plt.title('ARP Simulator') plt.xlabel('Samples') plt.ylabel('Amplitude (V)') plt.show() self.saveDataSet(zip(inputRF[dLen:dLen * N]))
def gamma(alpha, rate, cap=None): return capfunc(random.gammavariate(alpha, rate), cap)
class Invader(sge.dsp.Object): gene_props = { 'scale': { 'min': 1, 'max': 7, 'gen': lambda: random.gammavariate(4, 0.5) + 1 }, 'alpha': { 'min': 5, 'max': 255, 'gen': lambda: random.randint(20, 255) }, 'xvelocity': { 'min': 0.01, 'max': 5, 'gen': lambda: random.gammavariate(2, 0.4) }, 'yvelocity': { 'min': 0.01, 'max': 5, 'gen': lambda: random.gammavariate(2, 0.3) }, 'x_prob_change_dir': { 'min': 0.01, 'max': 0.07, 'gen': lambda: random.uniform(0.0, 0.05) }, 'y_prob_change_dir': { 'min': 0.0, 'max': 0.07, 'gen': lambda: random.uniform(0.0, 0.05) }, } @staticmethod def _generate_gen(name): v = Invader.gene_props[name]['gen']() max_v = Invader.gene_props[name]['max'] min_v = Invader.gene_props[name]['min'] if v < min_v: return min_v elif v > max_v: return max_v else: return v def __init__(self, **kwargs): # Generate random values and update with the ones provided in kwargs self.attributes = { k: self._generate_gen(k) for k in self.gene_props.keys() } self.attributes.update(kwargs) #print self.attributes self.genes = self.attributes super(Invader, self).__init__(sge.game.width / 2., sge.game.height / 2. - 80, sprite=sge.gfx.Sprite(name='invader'), image_blend=sge.gfx.Color('white'), checks_collisions=False) self.xvelocity = self.attributes.get('xvelocity') self.yvelocity = self.attributes.get('yvelocity') blend = int(self.attributes.get('alpha')) scale = self.attributes.get('scale') self.bbox_width = (self.sprite.width * scale) self.bbox_height = (self.sprite.height * scale) self.image_blend = sge.gfx.Color([blend, blend, blend]) self.image_xscale = scale self.image_yscale = scale self.fitness = 0 def event_step(self, time_passed, delta_mult): self.fitness += 1 # Change directions if random.random() <= self.attributes.get('x_prob_change_dir'): self.xvelocity = -self.xvelocity if random.random() <= self.attributes.get('y_prob_change_dir'): self.yvelocity = -self.yvelocity # Bouncing off the edges and the wall if self.bbox_left < 0: self.bbox_left = 0 self.xvelocity = abs(self.xvelocity) elif self.bbox_right > sge.game.current_room.width: self.bbox_right = sge.game.current_room.width self.xvelocity = -abs(self.xvelocity) if self.bbox_top < 0: self.bbox_top = 0 self.yvelocity = abs(self.yvelocity) if self.bbox_bottom > game.RESY - (game.WALL_YOFFSET + game.WALL_HEIGHT): self.bbox_bottom = game.RESY - (game.WALL_YOFFSET + game.WALL_HEIGHT) self.yvelocity = -abs(self.yvelocity) def compare_fitness(self, other): if not isinstance(other, Invader): raise ValueError('Incomparable types') return self.fitness.__cmp__(other.fitness)
m = sum(counts.values()) for lo, hi in zip(points, uppers): c = sum(counts[k] for k in counts if lo <= k <= hi) if by * c < m: print('FAIL', '{}/{} > {}'.format(m, by, c), (lo, hi)) else: print('pass', '{}/{} <= {}'.format(m, by, c), (lo, hi)) if __name__ == '__main__': import math, random # test data - would have liked poisson but random has poisson not, # while gamma seems to have, vaguely-ish, a desired kind of shape data = Counter(math.ceil(random.gammavariate(3, 2)) for k in range(1000)) print('Data (1000 ceiling(Gamma(3, 2)):') print(*('{:-2} observed {:-3} times'.format(k, c) for k, c in sorted(data.items())), sep='\n', end='\n\n') print('Quartile points:', *quantiles(data)) quantile_report(data) print() print('Quintile points:', *quantiles(data, by=5)) quantile_report(data, by=5) print() print('Decile points:', *quantiles(data, by=10)) quantile_report(data, by=10) if True:
def create_character(job_override=None): name_syllables = [ 'al', 'ben', 'cor', 'dan', 'fan', 'fer', 'frey', 'gra', 'gar', 'ger', 'hin', 'har', 'par', 'pen', 'pul', 'ser', 'star', 'ston', 'nikov', 'wray', 'chill', 'chan', 'lon', 'and', 'drak', 'crat', 'yon' ] name = ''.join([ name_syllables[rand.randint(0, len(name_syllables) - 1)] for _ in range(0, rand.randint(2, 4)) ]) name = name.capitalize() races = [ 'human', 'elf', 'dwarf', 'half-elf', 'gnome', 'halfling', 'half-orc', 'orc', 'goblin' ] race_probs = [100, 10, 10, 20, 5, 5, 3, 1, 1] race = rand.choices(races, weights=race_probs)[0] gender = 'male' if rand.random() < 0.5 else 'female' age = math.floor(rand.gammavariate(5, 8)) if race == 'elf': age *= 10 elif race == 'dwarf': age *= 3 elif race == 'goblin': age //= 3 relationships = [ 'single', 'courting', 'engaged', 'married', 'widowed', 'divorced' ] if age < 13: relationship = 'single' elif age < 18: relationship = relationships[rand.randint(0, 1)] elif age < 25: relationship = relationships[rand.randint(0, 4)] else: relationship = relationships[rand.randint(0, len(relationships) - 1)] children = 0 if relationship in ['single', 'courting', 'engaged'] else min( rand.randint(0, 6), age - 18 // 2) jobs = [ 'butcher', 'baker', 'farmer', 'grocer', 'glassblower', 'blacksmith', 'tanner', 'clothier', 'leatherworker', 'florist', 'shepherd', 'hunter', 'woodsman', 'lumberjack', 'construction worker', 'priest', 'guard', 'guard', 'soldier', 'clerk', 'nobleman', 'barkeep', 'clockmaker', 'silversmith', 'potter', 'cafe proprietor', 'groundskeeper', 'Dolora thug', 'Corela thug', 'Straka thug' ] nested_jobs = NestedChoices.load_from_string_list('random_jobs', jobs) job = nested_jobs.gen_choices()[0] wealths = [ 'dirt poor', 'dirt poor', 'poor', 'poor', 'getting by', 'getting by', 'getting by', 'well-off', 'well-off', 'rich' ] wealth = rand.choice(wealths) classes = [ 'commoner', 'guard', 'acolyte', 'mage', 'fighter', 'ranger', 'rogue', 'bard', 'sorcerer', 'wizard' ] class_probs = [50, 25, 10, 5, 4, 2, 1, 1, 1, 1] npc_class = rand.choices(classes, weights=class_probs)[0] level = math.floor(rand.gammavariate(1, 3) + 1) desires = [ 'wealth', 'power', 'love', 'fame', 'peace and quiet', 'adventure', 'security', 'an easy life', 'food', 'drink', 'friends', 'their family', 'revenge', 'piety', 'arcane knowledge' ] desire_probs = [ 100, 50, 50, 30, 100, 30, 50, 50, 30, 50, 40, 70, 10, 25, 5 ] desire = rand.choices(desires, weights=desire_probs)[0] event_choices = NestedChoices.load_from_file('npc_events.txt') event_choices.register_subtable(nested_jobs) events = event_choices.gen_choices(params={ 'num': rand.randint(1, 3), 'uniqueness_level': 1 }) char = f'{name}, a {age} year(s) old {gender} {race}.\n' char += f'They are {relationship}, and ' + ( 'don\'t have' if children == 0 else f'have {children}') + ' children.\n' if job_override: char += f'They run a {job_override}' else: char += f'They are a {job}' char += f', and are {wealth}.\n' char += f'Their greatest desire in life is {desire}.\n' char += f'They are a level {level} {npc_class}.\n' for event in events: char += f'\n{event}.' return char
import csv import random b = open('test.csv', 'w') a = csv.writer(b) x = range(1, 100) y = random.gammavariate(2, 3) data = [['Me', 'You'], [x, y]] matrix = [[ random.gauss(2, 3) for x in range(1), random.gammavariate(1, 2) for x in range(2) ] for x in range(100)] a.writerows(matrix) b.close()
def passanger_arrival_time(): return -0.5 + random.gammavariate(3.72, 1.55)
def service_time(): return 7.5 + random.gammavariate(2.45, 4.47)
def GAMMA(index, alpha: float = 1.0, beta: float = 1.0): return random.gammavariate(alpha, beta)
def _sampleValue(self): return random.gammavariate(self._params[0], self._params[1])
def grow_initial(self): """Sample from initial segment length gamma distribution, and apply to ``self.initial_len``.""" p = self.dendrite.parameters self.initial_len = random.gammavariate(p['gamma_in'], p['beta_in']) + p['alpha_in']
def gen_size(mid_size): """Interesting non-guassian distribution, to get a few very large files. Found via guessing on Wikipedia. Module 'random' says it's threadsafe. """ return int(random.gammavariate(3, 2) * mid_size / 4)
def random_options(self, preproc=False): cmd = " --zero-exit-status " if random.choice([True, False]): cmd += "--maple %d " % random.choice([0, 0, 0, 1]) cmd += "--reconf %d " % random.choice([3, 6, 7, 12, 13, 14]) # cmd += "--undef %d " % random.choice([0, 1]) cmd += " --reconfat %d " % random.randint(0, 2) cmd += "--burst %d " % random.choice( [0, 100, random.randint(0, 10000)]) cmd += "--ml %s " % random.randint(0, 10) cmd += "--restart %s " % random.choice(["geom", "glue", "luby"]) cmd += "--adjustglue %f " % random.choice([0, 0.5, 0.7, 1.0]) cmd += "--gluehist %s " % random.randint(1, 500) cmd += "--updateglueonanalysis %s " % random.randint(0, 1) cmd += "--otfhyper %s " % random.randint(0, 1) # cmd += "--clean %s " % random.choice(["size", "glue", "activity", # "prconf"]) cmd += "--cacheformoreminim %d " % random.choice([0, 1, 1, 1, 1]) cmd += "--stampformoreminim %d " % random.choice([0, 1, 1, 1, 1]) cmd += "--alwaysmoremin %s " % random.randint(0, 1) cmd += "--rewardotfsubsume %s " % random.randint(0, 100) cmd += "--bothprop %s " % random.randint(0, 1) cmd += "--probemaxm %s " % random.choice([0, 10, 100, 1000]) cmd += "--cachesize %s " % random.randint(10, 100) cmd += "--cachecutoff %s " % random.randint(0, 2000) cmd += "--elimstrgy %s " % random.choice( ["heuristic", "calculate"]) cmd += "--elimcplxupd %s " % random.randint(0, 1) cmd += "--occredmax %s " % random.randint(0, 100) cmd += "--extscc %s " % random.randint(0, 1) cmd += "--distill %s " % random.randint(0, 1) cmd += "--recur %s " % random.randint(0, 1) cmd += "--compsfrom %d " % random.randint(0, 2) cmd += "--compsvar %d " % random.randint(20000, 500000) cmd += "--compslimit %d " % random.randint(0, 3000) cmd += "--implicitmanip %s " % random.randint(0, 1) cmd += "--occsimp %s " % random.randint(0, 1) cmd += "--occirredmaxmb %s " % random.randint(0, 10) cmd += "--occredmaxmb %s " % random.randint(0, 10) cmd += "--skipresol %d " % random.choice([1, 1, 1, 0]) cmd += "--implsubsto %s " % random.choice([0, 10, 1000]) cmd += "--sync %d " % random.choice([100, 1000, 6000, 100000]) cmd += "-m %0.12f " % random.gammavariate(0.4, 2.0) # gammavariate gives us sometimes very low values, sometimes large if options.sqlite: cmd += "--sql 2 " cmd += "--sqlrestfull %d " % random.choice([0, 1]) cmd += "--sqlresttime %d " % random.choice([0, 1]) # the most buggy ones, don't turn them off much, please if random.choice([True, False]): opts = [ "scc", "varelim", "comps", "strengthen", "probe", "intree", "stamp", "cache", "otfsubsume", "renumber", "savemem", "moreminim", "gates", "bva", "gorshort", "gandrem", "gateeqlit", "schedsimp", "presimp", "elimcoststrategy" ] opts.extend(self.extra_options_if_supported) for opt in opts: cmd += "--%s %d " % (opt, random.randint(0, 1)) def create_rnd_sched(string_list): opts = string_list.split(",") opts = [a.strip(" ") for a in opts] opts = list(set(opts)) if options.verbose: print("available schedule options: %s" % opts) sched = [] for _ in range(int(random.gammavariate(12, 0.7))): sched.append(random.choice(opts)) if "autodisablegauss" in self.extra_options_if_supported and options.test_gauss: sched.append("occ-gauss") return sched cmd += self.add_schedule_options(create_rnd_sched, preproc) return cmd
def main(): # First, grab and interpret command line arguments parser = argparse.ArgumentParser( description="Command line arguments for Simulate") parser.add_argument("-v", "--verbose", help="Triggers verbose mode", action="store_true") parser.add_argument("-d", "--distribution", default="normal", choices=["normal", "gamma"], help="Distribution option") parser.add_argument( "-p1", "--parameter1", default=3.0, type=float, help="Shape parameter (only used if distribution choice is gamma)") parser.add_argument( "-p2", "--parameter2", default=1.5, type=float, help="Scale parameter (only used if distribution choice is gamma)") parser.add_argument("-s", "--size", required=True, type=int, nargs="+", help="Specify population size(s)") parser.add_argument("-l", "--loci", required=True, type=int, nargs="+", help="Loci with effects") parser.add_argument("-n", "--number", required=True, default=3000, type=int, help="Number of loci per population or sub-population") parser.add_argument("-e", "--effect", required=True, type=float, nargs="+", help="Effect size(s) for the loci specified") parser.add_argument("-i", "--heritability", default=0.2, type=restricted_float, help="Heritability coefficient for population") parser.add_argument("-m", "--mean", default=2.0, type=float, nargs="+", help="Mean(s) for population phenotype(s)") parser.add_argument("-g", "--gen", default=5, type=int, help="Number of generations for population to evolve") parser.add_argument("-r", "--rrate", default=0.0, type=restricted_float, help="Recombination rate for given population") parser.add_argument("-f", "--filename", default="my", type=str, help="Prefix for output file set") args = parser.parse_args() verbose = args.verbose if verbose: print "Verbose mode" distribution = args.distribution if verbose: print "Simulation will occur with " + distribution + " distribution" parameter1 = args.parameter1 if verbose and distribution == "gamma": print "Gamma distrbution will occur with alpha parameter:", parameter1 parameter2 = args.parameter2 if verbose and distribution == "gamma": print "Gamma distribution will occur with beta parameter", parameter2 individuals = args.size if verbose: print "Population size(s) set at", individuals loci = args.loci if verbose: print "Loci positions per individual set as", loci number = args.number if verbose: print "Number of loci per population set as", number global effects effects = args.effect if verbose: print "Effects for loci per individual are", effects heritability = args.heritability mean = args.mean if len(mean) == 1 and len(individuals) > 1: mean = numpy.array(mean) mean = numpy.repeat(mean, len(individuals), axis=0) mean = list(mean) if verbose: print "Population mean(s) set as", mean gen = args.gen if verbose: print "Number of generations to evolve set as", gen rrate = args.rrate if verbose: print "Recombination rate set as", rrate filename = args.filename if verbose: "File will be saved as", filename ## Start quantitative trait simulation via simuPOP if verbose: print "Creating population..." pop = sim.Population(size=individuals, loci=int(number), infoFields=["qtrait"]) if verbose: print "Evolving population..." type(gen) pop.evolve( initOps=[sim.InitSex(), sim.InitGenotype(prop=[0.7, 0.3])], matingScheme=sim.RandomMating(ops=sim.Recombinator(rates=rrate)), postOps=[ sim.PyQuanTrait(loci=loci, func=additive_model, infoFields=["qtrait"]) ], gen=gen) if verbose: print "Coalescent process complete. Population evolved with", pop.numSubPop( ), "sub-populations." genotypes = list() for i in pop.individuals(): genotypes.append(i.genotype()) phenotypes = list() for i in pop.individuals(): phenotypes.append(i.qtrait) # fun() obtains the heritability equation set to zero for various settings of sigma (standard deviation) def fun(sigma, h): x_exact = list() count = 0 for i in phenotypes: current_mean = mean[pop.subPopIndPair(count)[0]] x_exact.append(current_mean + i) count += 1 x_random = list() count = 0 for each in phenotypes: current_mean = mean[pop.subPopIndPair(count)[0]] x_random.append(random.normalvariate(current_mean + each, sigma)) count += 1 r = pearsonr(x_exact, x_random)[0] return r - math.sqrt(h) if verbose: print "Building polynomial model for variance tuning..." # Polyfit fits a polynomial model in numpy to the values obtained from the fun() function points = list() for i in drange(0, max(effects) * 10, 0.001): points.append(i) y_points = list() for i in points: y_points.append(fun(i, heritability)) z = numpy.polyfit(x=points, y=y_points, deg=3) p = numpy.poly1d(z) # Netwon's method finds the polynomial model's roots def newton(p): xn = 100 p_d = p.deriv() count = 0 while abs(p(xn)) > 0.01: if count > 1000: print "Unable to converge after 1000 iterations...\nPlease choose different settings." sys.exit() count += 1 xn = xn - p(xn) / p_d(xn) if xn < 0.0: xn = 0.0 if verbose: print "Estimated variance of phenotypes for specified heriability: ", xn return xn if verbose: print "Using Newton's method to find polynomial roots..." # Files are saved to the specified location estimated_variance = newton(p) new_phenotypes = list() count = 0 for each in phenotypes: current_mean = mean[pop.subPopIndPair(count)[0]] if distribution == "normal": new_phenotypes.append( random.normalvariate(current_mean + each, estimated_variance)) elif distribution == "gamma": new_phenotypes.append( random.gammavariate( (current_mean + each) / parameter2, numpy.sqrt(estimated_variance / parameter1))) count += 1 f = open(filename + "_qtrait.txt", "w") f.write("\n".join(map(lambda x: str(x), new_phenotypes))) f.close() numpy.savetxt(filename + "_kt_ote.txt", numpy.column_stack((loci, numpy.array(effects))), fmt='%i %10.7f') saveCSV(pop, filename + "_genomes.csv") # Call the convert.R script to convert the output into usable PLINK files # Will probably need to change this line to something more generalizable in the near future os.system("Rscript convert.R " + filename) print "\n\n"
def sampleG(self): grand = random.gammavariate(self.shape, self.scale ) return grand
def generator(self): if self.typ == "三角分布": if None in [self.low, self.high]: return False else: try: self.value = random.triangular(self.low, self.high) except: self.value = 999 elif self.typ == "均匀分布": if None in [self.low, self.high]: return False else: try: test = random.uniform(self.low, self.high) self.value = test except: self.value = 999 elif self.typ == "正态分布": if None in [self.mu, self.sigma]: return False else: try: self.value = random.normalvariate(self.mu, self.sigma) except: self.value = 999 elif self.typ == "高斯分布": if None in [self.mu, self.sigma]: return False else: try: self.value = random.gauss(self.mu, self.sigma) except: self.value = 999 elif self.typ == "beta分布": if None in [self.alpha, self.beta]: return False else: try: self.value = random.betavariate(self.alpha, self.beta) except: self.value = 999 elif self.typ == "指数分布": if None in [self.lambd]: return False else: try: self.value = random.expovariate(self.lambd) except: self.value = 999 elif self.typ == "伽马分布": if None in [self.alpha, self.beta]: return False else: try: self.value = random.gammavariate(self.alpha, self.beta) except: self.value = 999 elif self.typ == "对数正态分布": if None in [self.mu, self.sigma]: return False else: try: self.value = random.lognormvariate(self.mu, self.sigma) except: self.value = 999 else: return False
def test_gammavariate_alpha_between_zero_and_one(self, random_mock): # #3: 0 < alpha < 1. # This is the most complex region of code to cover, # as there are multiple if-else statements. Let's take a look at the # source code, and determine the values that we need accordingly: # # while 1: # u = random() # b = (_e + alpha)/_e # p = b*u # if p <= 1.0: # <=== (A) # x = p ** (1.0/alpha) # else: # <=== (B) # x = -_log((b-p)/alpha) # u1 = random() # if p > 1.0: # <=== (C) # if u1 <= x ** (alpha - 1.0): # <=== (D) # break # elif u1 <= _exp(-x): # <=== (E) # break # return x * beta # # First, we want (A) to be True. For that we need that: # b*random() <= 1.0 # r1 = random() <= 1.0 / b # # We now get to the second if-else branch, and here, since p <= 1.0, # (C) is False and we take the elif branch, (E). For it to be True, # so that the break is executed, we need that: # r2 = random() <= _exp(-x) # r2 <= _exp(-(p ** (1.0/alpha))) # r2 <= _exp(-((b*r1) ** (1.0/alpha))) _e = random._e _exp = random._exp _log = random._log alpha = 0.35 beta = 1.45 b = (_e + alpha) / _e epsilon = 0.01 r1 = 0.8859296441566 # 1.0 / b r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha))) # These four "random" values result in the following trace: # (A) True, (E) False --> [next iteration of while] # (A) True, (E) True --> [while loop breaks] random_mock.side_effect = [r1, r2 + epsilon, r1, r2] returned_value = random.gammavariate(alpha, beta) self.assertAlmostEqual(returned_value, 1.4499999999997544) # Let's now make (A) be False. If this is the case, when we get to the # second if-else 'p' is greater than 1, so (C) evaluates to True. We # now encounter a second if statement, (D), which in order to execute # must satisfy the following condition: # r2 <= x ** (alpha - 1.0) # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0) # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0) r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False r2 = 0.9445400408898141 # And these four values result in the following trace: # (B) and (C) True, (D) False --> [next iteration of while] # (B) and (C) True, (D) True [while loop breaks] random_mock.side_effect = [r1, r2 + epsilon, r1, r2] returned_value = random.gammavariate(alpha, beta) self.assertAlmostEqual(returned_value, 1.5830349561760781)
res = [random.betavariate(alpha,beta) for _ in range(1, SAMPLE_SIZE)] plt.hist(res, buckets) # 第五张图 指数分布 plt.subplot(625) plt.xlabel("random.expovariate") lambd = 1.0 /((SAMPLE_SIZE + 1) /2.) res = [random.expovariate(lambd) for _ in range(1, SAMPLE_SIZE)] plt.hist(res, buckets) #第六张图 gamma分布 plt.subplot(626) plt.xlabel("random.gammavariate") alpha = 1 beta = 10 res = [random.gammavariate(alpha,beta) for _ in range(1, SAMPLE_SIZE)] plt.hist(res, buckets) #第七张图 对数正态分布 plt.subplot(627) plt.xlabel("random.lognormvariate") mu = 1 sigma = 0.5 res = [random.lognormvariate(mu, sigma) for _ in range(1,SAMPLE_SIZE)] plt.hist(res,buckets) #第八张图 正态分布 plt.subplot(628) plt.xlabel("random.normalvariate") mu = 1 sigma = 0.5
def Dirichlet(alpha): sample = [gammavariate(a, 1) for a in alpha] sample = [v / sum(sample) for v in sample] return sample
def test_gammavariate_full_code_coverage(self, random_mock): # There are three different possibilities in the current implementation # of random.gammavariate(), depending on the value of 'alpha'. What we # are going to do here is to fix the values returned by random() to # generate test cases that provide 100% line coverage of the method. # #1: alpha > 1.0: we want the first random number to be outside the # [1e-7, .9999999] range, so that the continue statement executes # once. The values of u1 and u2 will be 0.5 and 0.3, respectively. random_mock.side_effect = [1e-8, 0.5, 0.3] returned_value = random.gammavariate(1.1, 2.3) self.assertAlmostEqual(returned_value, 2.53) # #2: alpha == 1: first random number less than 1e-7 to that the body # of the while loop executes once. Then random.random() returns 0.45, # which causes while to stop looping and the algorithm to terminate. random_mock.side_effect = [1e-8, 0.45] returned_value = random.gammavariate(1.0, 3.14) self.assertAlmostEqual(returned_value, 2.507314166123803) # #3: 0 < alpha < 1. This is the most complex region of code to cover, # as there are multiple if-else statements. Let's take a look at the # source code, and determine the values that we need accordingly: # # while 1: # u = random() # b = (_e + alpha)/_e # p = b*u # if p <= 1.0: # <=== (A) # x = p ** (1.0/alpha) # else: # <=== (B) # x = -_log((b-p)/alpha) # u1 = random() # if p > 1.0: # <=== (C) # if u1 <= x ** (alpha - 1.0): # <=== (D) # break # elif u1 <= _exp(-x): # <=== (E) # break # return x * beta # # First, we want (A) to be True. For that we need that: # b*random() <= 1.0 # r1 = random() <= 1.0 / b # # We now get to the second if-else branch, and here, since p <= 1.0, # (C) is False and we take the elif branch, (E). For it to be True, # so that the break is executed, we need that: # r2 = random() <= _exp(-x) # r2 <= _exp(-(p ** (1.0/alpha))) # r2 <= _exp(-((b*r1) ** (1.0/alpha))) _e = random._e _exp = random._exp _log = random._log alpha = 0.35 beta = 1.45 b = (_e + alpha) / _e epsilon = 0.01 r1 = 0.8859296441566 # 1.0 / b r2 = 0.3678794411714 # _exp(-((b*r1) ** (1.0/alpha))) # These four "random" values result in the following trace: # (A) True, (E) False --> [next iteration of while] # (A) True, (E) True --> [while loop breaks] random_mock.side_effect = [r1, r2 + epsilon, r1, r2] returned_value = random.gammavariate(alpha, beta) self.assertAlmostEqual(returned_value, 1.4499999999997544) # Let's now make (A) be False. If this is the case, when we get to the # second if-else 'p' is greater than 1, so (C) evaluates to True. We # now encounter a second if statement, (D), which in order to execute # must satisfy the following condition: # r2 <= x ** (alpha - 1.0) # r2 <= (-_log((b-p)/alpha)) ** (alpha - 1.0) # r2 <= (-_log((b-(b*r1))/alpha)) ** (alpha - 1.0) r1 = 0.8959296441566 # (1.0 / b) + epsilon -- so that (A) is False r2 = 0.9445400408898141 # And these four values result in the following trace: # (B) and (C) True, (D) False --> [next iteration of while] # (B) and (C) True, (D) True [while loop breaks] random_mock.side_effect = [r1, r2 + epsilon, r1, r2] returned_value = random.gammavariate(alpha, beta) self.assertAlmostEqual(returned_value, 1.5830349561760781)
def grow_only(self, dt): """Sample segment elongation rate from "elongate phase" gamma distribution, and add to ``self.elongated_len``.""" p = self.dendrite.parameters rate = random.gammavariate(p['gamma_e'], p['beta_e']) + p['alpha_e'] self.elongated_len += rate * dt
def gen_degree_value(length, alpha=10, beta=1): deg_seq = [int(rnd.gammavariate(alpha, beta)) for i in xrange(length)] if sum(deg_seq) % 2 == 1: deg_seq[-1] += 1 return deg_seq
def one(o): return o.final(random.gammavariate(o.a, o.b))
def randslen(): # Ceil ensures that we don't end up with zero length. return int(ceil(gammavariate(3.08095294271, 8.01572810369) + 0.0))
def _fix(self): return int(round(random.gammavariate(self.alpha, self.beta)))
def generateFreqEnergy(self, lambda1, lambda2, numberOfSamples): pi = m.pi tic.clock() kParam1 = 2 #k-parameter for Erlang/gamma distribution (ON) kParam2 = 2 #k-parameter for Erlang/gamma distribution (OFF) vScale1 = 1 #scales variance relative to lambda1 (optional) vScale2 = 1 #scales variance relative to lambda2 (optional) var1 = vScale1 * lambda1 #variance parameter for log-normal distribution (ON) var2 = vScale2 * lambda2 #variance parameter for log-normal distribution (OFF) N = numberOfSamples #number of samples occupancy = [0] * N stateTrans = [] #tracks alternating states [1,0,1,0,1,0,...] intTimes = [] #tracks intervals upTimes = [] downTimes = [] intTimesSeq = [] #counts and tracks intervals upDist = "lnorm" #'exp', 'erl', or 'lnorm' downDist = "lnorm" #'exp', 'erl', or 'lnorm' #process initialized to "on" totalTime = 0 #tracks total time generated by the ARP seqState = 1 #tracks next state to generate while totalTime < N: #generates on sequence if seqState: #generates random on period if upDist == "exp": period = m.ceil(rnd.expovariate(lambda1)) elif upDist == "erl": period = m.ceil(rnd.gammavariate(kParam1, 1 / lambda1)) #assumes k=2 elif upDist == "lnorm": trueMu = m.log(((1 / lambda1)**2) / m.sqrt((1 / var1) + (1 / lambda1)**2)) trueSig = m.sqrt(m.log((1 / var1) / ((1 / lambda1)**2) + 1)) period = m.ceil(rnd.lognormvariate(trueMu, trueSig)) #period = 5 if (totalTime + period) > N: #makes sure total time isn't exceeded occupancy[totalTime:N] = [1] * (N - totalTime) else: #appends proper sequence of 1s occupancy[totalTime:totalTime + period] = [1] * period #tracks state transitions and on/off durations stateTrans.append(1) intTimes.append(period) upTimes.append(period) intTimesSeq.append(list(range(1, period + 1))) seqState = 0 #generates off sequence else: #generates random off period if downDist == "exp": period = m.ceil(rnd.expovariate(lambda2)) elif downDist == "erl": period = m.ceil(rnd.gammavariate(kParam2, 1 / lambda2)) #assumes k=2 elif downDist == "lnorm": period = m.ceil(rnd.lognormvariate(lambda2, var2)) #period = 10 if (totalTime + period) > N: #makes sure total time isn't exceeded occupancy[totalTime:N] = [0] * (N - totalTime) else: #appends proper sequence of 1s occupancy[totalTime:totalTime + period] = [0] * period #tracks state transitions and on/off durations stateTrans.append(0) intTimes.append(period) downTimes.append(period) intTimesSeq.append(list(range(1, period + 1))) seqState = 1 totalTime += period seqSize = len(stateTrans) #total number of on and off states traffic_intensity = sum(occupancy) / N #measures traffic intensity #measures mean signal interarrival mean_int = sum(intTimes[0:seqSize - (seqSize % 2)]) / ((seqSize - (seqSize % 2)) / 2) actual_int = 1 / lambda1 + 1 / lambda2 #calculates theoretical interarrival #reactive predictor "accuracy/error" predicted = occupancy[0:N - 1] #theoretical accuracy based on lambda parameters theoAcc = 1 - (2 / actual_int - 1 / N) #accuracy based on measured mean interarrival expAcc = 1 - (2 / mean_int - 1 / N) #observed accuracy obsAcc = sum([predicted[i] == occupancy[i + 1] for i in range(N - 1)]) / (N - 1) ###input RF signal generation### dLen = 100 #length of the energy detector fs = 100e6 time = np.linspace(0, N * dLen / fs, N * dLen) powerLvl = -40 #power in dBm amp = m.sqrt( (10**(powerLvl / 10)) / 1000 * (2 * 50)) #sinusoid amplitude noiseVar = 1e-7 #noisefloor variance (1e-7 places noisefloor around -100 dBm) noisefloor = m.sqrt(noiseVar) * np.random.randn(N * dLen) sineWave = amp * np.exp(1j * 2 * pi * (10e6) * time) #sine wave at 10 MHz #SNR of the signal SNR = 10 * np.log10((sum(np.abs(sineWave)**2) / (dLen * N)) / (sum(np.abs(noisefloor)**2) / (dLen * N))) #Modulates the sine wave with the occupancy state where each state has dLen samples occSwitch = np.repeat(occupancy, dLen) inputRF = sineWave * occSwitch + noisefloor P_fa = 0.01 #probability of false alarm #energy detector threshold thresh = noiseVar / m.sqrt(dLen) * (-norm.ppf(P_fa)) + noiseVar #calculates total average power over a sliding window totalAvgPwr = np.zeros((dLen * N - dLen + 1)) trueState = np.zeros((dLen * N - dLen + 1)) #pwrStates = np.zeros((dLen, dLen*N-dLen+1)) for i in range(dLen * N - dLen + 1): totalAvgPwr[i] = sum(np.abs(inputRF[i:i + dLen])**2) / dLen #pwrStates[:,i] = np.arange(i,i+dLen) trueState[i] = int(sum(occSwitch[i:i + dLen]) > 0) return totalAvgPwr
def getRain(self): return random.gammavariate(self._alpha, self._beta)