def sample_pareto(self, shape, scale, lb, ub, ntrials): d = [] for i in range(ntrials): rand_sample = (random.pareto(shape) + 1) * scale while(rand_sample < lb or rand_sample > ub): rand_sample = (random.pareto(shape) + 1) * scale d.append(rand_sample) return d
def __init__(self, bias_stdev=0.2, eval_stdev=0.2, mode='pareto', frac=0.1, pareto_shape=1.4, gamma_shape=3): """Initializes the precision and bias of a user. Useful only for simulation.""" # Chooses the bias of the user self.true_bias = 0 if bias_stdev > 0: self.true_bias = npr.normal(scale=bias_stdev) # Chooses the variance of the user. if mode == 'bimodal': # 10% of the students are responsible for 90% of the trouble, # where 10% is the fraction. # This code keeps the standard deviation as specified, but explains # it via a bimodal distribution, with values s and s / frac. s = eval_stdev * eval_stdev * frac / (1.0 + frac - frac * frac) if npr.uniform() < frac: self.prec = (s / (frac * frac)) ** 0.5 else: self.prec = s ** 0.5 elif mode == 'pareto': # The mean of a pareto distribution of shape a is 1 / (a - 1) # Here, we use the pareto distribution to sample the variance. prec_sq = npr.pareto(pareto_shape) * eval_stdev * eval_stdev * (pareto_shape - 1.0) self.prec = prec_sq ** 0.5 else: # Gamma. prec_sq = npr.gamma(gamma_shape, scale=eval_stdev) self.prec = prec_sq * prec_sq # List of items it judged. self.items = [] # Dictionary mapping each item, to the grade assigned by the user. self.grade = {}
def distribution(n): """ Returns a Pareto distribution of n length """ d = random.pareto(1, n) distribution = list(d / d.sum(axis=0, keepdims=1)).sort(reverse=True) return distribution
def get_dist_num(args): dist = args[0] for i in range(len(args[1:])): args[i + 1] = float(args[1:][i]) if dist == 'EXP': return exponential(args[1]) elif dist == 'NOR': return normal(loc=args[1], scale=args[2]) # loc = média , scale = desvio elif dist == 'TRI': return triangular(args[1], args[2], args[3]) elif dist == 'UNI': return uniform(low=args[1], high=args[2]) elif dist == 'BET': return beta(args[1], args[2]) elif dist == 'WEI': return weibull(args[1]) elif dist == 'CAU': # CAU: Cauchy return 0 elif dist == 'CHI': return chisquare(args[1]) elif dist == 'ERL': # ERL: Erlang return 0 elif dist == 'GAM': return gamma(args[1], scale=args[2]) elif dist == 'LOG': return lognormal(mean=args[1], sigma=args[2]) elif dist == 'PAR': return pareto(args[1]) elif dist == 'STU': return standard_t(args[1])
def updateLocation(self): # Don't move if route not started yet if not self.route_started: return self.location # We have completed our route if self.route_step >= len(self.route): return self.location # We are waiting at a waypoint if self.seconds_to_wait > 0: self.seconds_to_wait = self.seconds_to_wait - 1 return self.location # Check if we are at our next stop next_waypoint = self.route[self.route_step] route_vector = (next_waypoint[0] - self.location[0], next_waypoint[1] - self.location[1]) route_magnitude = math.sqrt(route_vector[0]**2 + route_vector[1]**2) # We are close enough. Time to go to next place if route_magnitude < self.speed: # Wait time is pulled from a pareto distribution truncated between 30 and 10000 seconds self.seconds_to_wait = int(max(30, min(nprand.pareto(0.25), 10000))) self.route_step = self.route_step + 1 return self.location # Move forward along vector speed distance move_distance = self.speed * (1.0 / route_magnitude) self.location = (self.location[0] + move_distance * route_vector[0], self.location[1] + move_distance * route_vector[1]) return self.location
def __init__(self, agent_id,agent_type=None): # First two agent parameters are exogenous primitives self.disposition=randint(low=0,high=2) # Prior disposition to contribuiting self.wealth=pareto(3.) # Level of wealth # Next, endogenously generated variables # Set agent type if type(agent_id) is not int: raise ValueError("Agent IDs must be integers") else: self.my_id=agent_id # Agent identifier self.adj_list=list() # Agent's ego-network (adjacency network) # Set agent type, from one of five possible type: # 0 - Altruistic: Always sets c=.5(wealth) # 1 - Community: Sets c=m' such that m'+m_net=w, given m_net # 2 - Min-match: Sets c=min(m') for all of agent's neighbors # 3 - Max-match: Sets c=max(m') for all of agent's neighbors # 4 - Miserly: Sets c=\epsilon if agent_type is None: self.type=randint(low=0,high=5) else: if type(agent_type)is int and agent_type>=0 and agent_type<5: self.type=agent_type else: raise ValueError("Agent type must be an int between 0 and 4") # Finally, place holder for level of contribution and m_net self.contrib=None self.mnet=None
def _simulate_clicks(param_container, cookie_count, cookie_lifetime, cur_dev_state, cur_time, order_pref, session_count, obs_session_count, user, user_lifetime, attr_mat, satis_mat): """ Simulates the clicks in the current session :param cookie_count: Current user cookie count :param cookie_lifetime: Current cookie lifetime :param cur_dev_state: Current device :param cur_time: Time at which the query session starts :param order_pref: Item weights used to determine the item ordering :param session_count: Overall user session count :param obs_session_count: Session count as observed by the analyst :param user: Current user-id :param user_lifetime: current lifetime of the user :return: pandas dataframe containing the clicks and several other query session data for this query session """ # Draw the attraction and satisfaction parameters for all positions item_order = rand.choice(np.arange(param_container.items), param_container.list_size, replace=False, p=order_pref) real_att = np.array([ rand.binomial(1, x, 1) for x in attr_mat[user, item_order] ]).reshape(-1) real_satis = np.array([ rand.binomial(1, x, 1) for x in satis_mat[user, item_order] ]).reshape(-1) eval_vec = np.zeros(param_container.list_size + 1) eval_vec[0] = 1 obs_satis = np.zeros(param_container.list_size) click_vec = np.zeros(param_container.list_size) # If the user is satisfied by the query, simulate clicks (otherwise all zero) for k in range(param_container.list_size): click_vec[k] = real_att[k] * eval_vec[k] obs_satis[k] = real_satis[k] * click_vec[k] eval_vec[k + 1] = eval_vec[k] * (1 - obs_satis[k]) time_till_next = rand.pareto( param_container.inter_session_pareto_shape) - 1 # Add results to dictionary ses_sim_res = pd.DataFrame.from_dict( dict( zip(SDBNSimpleSimulator.PD_RES_NAMES, [ np.repeat(user, param_container.list_size), item_order, np.arange(param_container.list_size) + 1, click_vec, real_att, real_satis, obs_satis, eval_vec[0:param_container.list_size], np.repeat(session_count, param_container.list_size), np.repeat(obs_session_count, param_container.list_size), np.repeat(cur_time, param_container.list_size), np.repeat(time_till_next, param_container.list_size), np.repeat(cookie_count, param_container.list_size), np.repeat(cookie_lifetime, param_container.list_size), np.repeat(cur_dev_state, param_container.list_size), np.repeat(user_lifetime, param_container.list_size) ]))) return ses_sim_res, time_till_next
def f(alpha, x0fac, x1fac, CE, CI, sum_a): seed() CEonCI = CE / CI epsilonXCE = Jepsilon * CE b = x1fac * (pareto(alpha, CI) + 1) + x0fac tf0 = tim() # mean of ensembles a,b is CI,CE while (abs(sum_a - CEonCI * sum(b)) > epsilonXCE) and (tim() - tf0 < timeout): b1 = x1fac * (pareto(alpha, CI) + 1) + x0fac if abs(sum_a - CEonCI * sum(b1)) < abs(sum_a - CEonCI * sum(b)): b = b1 # print sum_a, sum(b), abs(sum_a-CEonCI*sum(b))/abs(sum_a) if tim() - tf0 > timeout: with nfailures_tightbalance.get_lock(): nfailures_tightbalance.value += 1 return b
def time_to_mutation_rate(tree): if not hasattr(GC, "NUMPY_SEEDED"): from numpy.random import seed as numpy_seed numpy_seed(seed=GC.random_number_seed) GC.random_number_seed += 1 GC.NUMPY_SEEDED = True t = read_tree_newick(tree) for node in t.traverse_preorder(): if node.edge_length is not None: node.edge_length *= pareto(a=GC.tree_rate_shape) return str(t)
def install_pareto(self): ''' Installs the file system with a Pareto distribution whith the specified shape ''' while opCounter<=osSettings.nOperations: fileSize = int(random.pareto(fsDist.shape)) #filesystem.create_file(fileSize) if fileSize>maxSize: #TODO: meter aqui la condicion de crear exitosamente el archivo maxSize = maxSize fsSize = fsSize + fileSize opCounter = opCounter +1
def ParetoDistribution(**kwargs): #num samples, shape, mode random.seed(supportedDistributions["Pareto"]["seed"]) a = kwargs["a"] m = kwargs["m"] size = kwargs["size"] hist_partitions = kwargs["hist_partitions"] samples = (random.pareto(a=a, size=size) + 1) * m count, hist_bins = numpy.histogram(samples, hist_partitions, density=True) fit = a*m**a / hist_bins**(a+1) hist_fitted = [max(count)*fitt/max(fit) for fitt in fit] return samples, hist_bins, hist_fitted, hist_partitions
def update(name, g, _g): _g = _g.ravel() target = getattr(self.rbm, name) k = len(_g) g_len = len(_g) #print "g_len: ", g_len #print "target shape: ", target.shape while k > g_len-1: k = int(rng.pareto(tau)) print g worst = g.argsort()[-k:][::-1][-1] target.flat[worst] += g.flat[worst] ## so currently crappy gradient descent _g[:] = g.ravel()
def matrix(C, R, alpha): matrix = [] for c in range(0, C): matrix.append([]) s = random.pareto(alpha) for r in range(0, R): matrix[-1].append(abs(s*random.normal())) Matrix = [] for r in range(0, R): Matrix.append([]) for c in range(0, C): Matrix[-1].append(matrix[c][r]) return Matrix
def survives(t, λ_α, λ_β, μ_α, μ_β, ρ): Δ = pareto(μ_α[0]) * μ_β[0] μ_α += 1 μ_β += Δ if Δ > t: if uniform(0., 1.) < ρ: return True Δ = t t_end = t - Δ s = negative_binomial(λ_α[0], λ_β[0] / (λ_β[0] + Δ)) λ_α += s λ_β += Δ for i in range(s): τ = uniform(t_end, t) if survives(τ, λ_α, λ_β, μ_α, μ_β, ρ): return True return False
def write_to_disk(self, path, nCol): if not os.path.exists(path): os.mkdir(path) V = data.euclidean_to_hypercube(self.G.T[:nCol].T) R = pareto(1, size=self.nSamp) * (np.ones(self.nSamp) + 0.3 * self.y) Z = (V.T * R).T Z_df = pd.DataFrame(Z, columns=['Z_{}'.format(i) for i in range(nCol)]) y_df = pd.DataFrame({'y': self.y}) z_path = os.path.join(path, 'ad_sim_m{}_c{}_x.csv'.format(self.nMix, nCol)) y_path = os.path.join(path, 'ad_sim_m{}_c{}_y.csv'.format(self.nMix, nCol)) Z_df.to_csv(z_path, index=False) y_df.to_csv(y_path, index=False) return
def __init__(self, bias_stdev=0.2, eval_stdev=0.2, mode='pareto', frac=0.1, pareto_shape=1.4, gamma_shape=3): """Initializes the precision and bias of a user. Useful only for simulation.""" # Chooses the bias of the user self.true_bias = 0 if bias_stdev > 0: self.true_bias = npr.normal(scale=bias_stdev) # Chooses the variance of the user. if mode == 'bimodal': # 10% of the students are responsible for 90% of the trouble, # where 10% is the fraction. # This code keeps the standard deviation as specified, but explains # it via a bimodal distribution, with values s and s / frac. s = eval_stdev * eval_stdev * frac / (1.0 + frac - frac * frac) if npr.uniform() < frac: self.prec = (s / (frac * frac))**0.5 else: self.prec = s**0.5 elif mode == 'pareto': # The mean of a pareto distribution of shape a is 1 / (a - 1) # Here, we use the pareto distribution to sample the variance. prec_sq = npr.pareto(pareto_shape) * eval_stdev * eval_stdev * ( pareto_shape - 1.0) self.prec = prec_sq**0.5 else: # Gamma. prec_sq = npr.gamma(gamma_shape, scale=eval_stdev) self.prec = prec_sq * prec_sq # List of items it judged. self.items = [] # Dictionary mapping each item, to the grade assigned by the user. self.grade = {}
def InitMatrices(params): """ The following parameters are required: strategies..a list with length = number of nodes in the credit network def_alpha...alpha parameter for default probability beta-distribution def_beta....beta parameter for default probability beta-distribution rate_alpha..alpha parameter for transaction rate pareto-distribution min_value...minimum for buy value uniform-distribution max_value...maximum for buy value uniform-distribution min_cost....minimum for sell cost uniform-distribution max_cost....maximum for sell cost uniform-distribution """ n = len(params["strategies"]) matrices = dict() matrices["DP"] = R.beta(params["def_alpha"], params["def_beta"], n) matrices["TR"] = R.pareto(params["rate_alpha"], [n]*2) fill_diagonal(matrices["TR"], 0) matrices["TR"] /= matrices["TR"].sum() matrices["BV"] = R.uniform(params["min_value"], params["max_value"], [n]*2) fill_diagonal(matrices["BV"], 0) matrices["SC"] = R.uniform(params["min_cost"], params["max_cost"], [n]*2) fill_diagonal(matrices["SC"], 0) return matrices
def pareto(size, params): try: return random.pareto(params['a'], size) except ValueError as e: exit(e)
def network(**kwargs): with nfailures_tightbalance.get_lock(): nfailures_tightbalance.value = 0 globals().update(kwargs) nest.ResetKernel() startbuild = tim() order = int(orderCE / (epsilon * 4)) #2500 NE = 4 * order # number of excitatory neurons NI = 1 * order # number of inhibitory neurons N_neurons = NE + NI # number of neurons in total # N_rec = 50 # record from 50 neurons CE = int(epsilon * NE) # number of excitatory synapses per neuron CI = int(epsilon * NI) # number of inhibitory synapses per neuron #C_tot = int(CI + CE) # total number of synapses per neuron neuron_params = { "C_m": 1.0, "tau_m": tauMem, "t_ref": 2.0, "E_L": 0.0, "V_reset": Vr, "V_m": 0.0, "V_th": theta } J_ex = J # amplitude of excitatory postsynaptic potential J_in = -g * J_ex # amplitude of inhibitory postsynaptic potential nu_th = theta / (J * CE * tauMem) nu_ex = eta * nu_th p_rate = 1000.0 * nu_ex * CE nest.SetKernelStatus({ "resolution": dt, "print_time": True, "overwrite_files": True, "local_num_threads": nthreads }) nest.SetDefaults("iaf_psc_delta", neuron_params) nest.SetDefaults("poisson_generator", {"rate": p_rate / CE}) nodes_ex = nest.Create("iaf_psc_delta", NE) nodes_in = nest.Create("iaf_psc_delta", NI) noise = nest.Create("poisson_generator", CE) espikes = nest.Create("spike_detector") ispikes = nest.Create("spike_detector") nest.SetStatus(espikes, [{ "label": "%s/alpha%.2fespikes" % (datafolder, alpha), "withtime": True, "withgid": True, "to_file": True }]) nest.SetStatus(ispikes, [{ "label": "%s/alpha%.2fispikes" % (datafolder, alpha), "withtime": True, "withgid": True, "to_file": True }]) nest.CopyModel("static_synapse", "excitatory", { "weight": J_ex, "delay": delay }) nest.CopyModel("static_synapse", "inhibitory", { "weight": J_in, "delay": delay }) A_alpha = gamma(1 + alpha) * numpy.sin(numpy.pi * alpha / 2) / numpy.pi D = 0.5 # pareto pdf = alpha*x1**alpha/(x-x0)**(alpha+1), defined for x > x0+x1 x1fac = (2 * A_alpha * D / alpha)**(1 / alpha) x0fac = 1 - x1fac * alpha / (alpha - 1) J_noise_ex = J_ex * (x1fac * (pareto(alpha, (NE, CE)) + 1) + x0fac) J_noise_in = J_ex * (x1fac * (pareto(alpha, (NI, CE)) + 1) + x0fac) # correlated amplitude populations: samples_ex = x1fac * (pareto(alpha, (NE + NI, CE)) + 1) + x0fac # print x0fac,x1fac with Pool_(nthreads) as p: samples_in = numpy.array( p.map(partial(f, alpha, x0fac, x1fac, CE, CI), numpy.sum(samples_ex, 1), 1)) J_ex_tot = J_ex * samples_ex J_in_tot = J_in * samples_in multimeter = nest.Create("multimeter") nest.SetStatus( multimeter, { "to_memory": False, "withtime": True, "record_from": ["V_m"], "to_file": True, "label": "%s/alpha%.2fV_m" % (datafolder, alpha) }) #"interval": 100.0, nest.Connect(multimeter, nodes_ex + nodes_in) # nodes_ex[:N_rec]+... nest.Connect(noise, nodes_ex, syn_spec={ "model": "excitatory", "weight": J_noise_ex }) nest.Connect(noise, nodes_in, syn_spec={ "model": "excitatory", "weight": J_noise_in }) nest.Connect(nodes_ex, espikes, syn_spec="excitatory") # nodes_ex[:N_rec] nest.Connect(nodes_in, ispikes, syn_spec="excitatory") # nodes_in[:N_rec] conn_params_ex = {'rule': 'fixed_indegree', 'indegree': CE} nest.Connect(nodes_ex, nodes_ex + nodes_in, conn_params_ex, syn_spec={ "model": "excitatory", "weight": J_ex_tot }) conn_params_in = {'rule': 'fixed_indegree', 'indegree': CI} nest.Connect(nodes_in, nodes_ex + nodes_in, conn_params_in, syn_spec={ "model": "inhibitory", "weight": J_in_tot }) endbuild = tim() nest.Simulate(simtime) endsimulate = tim() events_ex = nest.GetStatus(espikes, "n_events")[0] events_in = nest.GetStatus(ispikes, "n_events")[0] rate_ex = events_ex / simtime * 1000.0 / NE rate_in = events_in / simtime * 1000.0 / NI num_synapses = (nest.GetDefaults("excitatory")["num_connections"] + nest.GetDefaults("inhibitory")["num_connections"]) build_time = endbuild - startbuild sim_time = endsimulate - endbuild print("Number of tight balance failures: {0}".format( nfailures_tightbalance.value)) print("alpha : {0}".format(alpha)) print("Number of neurons : {0}".format(N_neurons)) print("Number of synapses: {0}".format(num_synapses)) print(" Exitatory : {0}".format(int(CE * N_neurons) + N_neurons)) print(" Inhibitory : {0}".format(int(CI * N_neurons))) print("Excitatory rate : %.2f Hz" % rate_ex) print("Inhibitory rate : %.2f Hz" % rate_in) print("Building time : %.2f s" % build_time) print("Simulation time : %.2f s" % sim_time)
tp = rg.get_shortest_paths(p_sp, t_sp)[0] while len(tp) > 1: ttl = len(tp) row.append(ttl) rg.delete_edges([tuple(tp[-2:])]) tp = rg.get_shortest_paths(p_sp, t_sp, mode='OUT')[0] if row != []: tl.append([mean(row), min(row), max(row)]) else: tl.append([1, 1, 1]) print(g.vs['name'][t_sp], tl[-1]) tl_dict = dict([[g.vs['name'][i], tl[i]] for i in range(len(g.vs))]) par_dist = [ int(round(i) + 1) for i in (pareto(a=1.02, size=100000)) if i <= 51 ] ####coextinction out = open('results_22_07_21.csv', 'w') out.write( 'net_n,extinct_status,species,vulnerability,plant_n,mean_tl,min_tl,max_tl,paths_to_bas,deg_in,deg_out\n' ) for net_n in range(1, 1001): f80 = [ j for j in csv.reader(open('./nets_80/' + str(net_n) + '.csv', 'r')) ] net = [i[1:][::-1] for i in f80[1:]] p_set = ['p' + str(i) for i in range(1300)] i_set = ['i' + str(i) for i in range(6000)] f_set = ['f' + str(i) for i in range(23)]
# sample a pareto distribution from numpy.random import pareto # define the distribution alpha = 1.1 n = 10 # generate the sample sample = pareto(alpha, n) print(sample)
timecost.append([mid_time-start_time,time.time()-mid_time]) #geometric start_time=time.time() a=dsg.geometric(0.4,times) mid_time=time.time() b=nr.geometric(0.4,times) timecost.append([mid_time-start_time,time.time()-mid_time]) #pareto start_time=time.time() a=dsg.pareto(1.25,times) mid_time=time.time() b=nr.pareto(1.25,times) timecost.append([mid_time-start_time,time.time()-mid_time]) timecost=np.array(timecost) timecost1+=timecost[:,0] timecost2+=timecost[:,1] bar_width = 0.4 index = np.arange(len(timecost)) rects1 = plt.bar(index, timecost1, bar_width, color='#0072BC', label='dsg') rects2 = plt.bar(index + bar_width, timecost2, bar_width, color='#ED1C24', label='numpy') plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.03), fancybox=True, ncol=5) plt.xticks(index + bar_width/2, func_name) plt.xticks(fontsize=9) plt.yticks(fontsize=9)
def draw_income(): return IncomeDist.x_min * (1. + pareto(IncomeDist.alpha))
def Pareto(self, alpha, sigma): return pareto(alpha) + sigma
from numpy import random import matplotlib.pyplot as plt import seaborn as sns x = random.pareto(scale=2, size=(2, 3)) print(x) sns.displot(x, hist=False, label="pareto") plt.show()
# distribution following pareto's law # i.e. 80-20 distribution (20% factors cause 80% outcome). # it has two parameters # a = shape parameter. # size = shape of returned array. from numpy import random import matplotlib.pyplot as plt import seaborn as sns arr1 = random.pareto(a=2, size=1000) print(arr1) # 20% out are similar and 80% out are similar sns.distplot(arr1, kde=False) plt.show()
def np_pareto_distribution(): x = random.pareto(a=2, size=(2, 3)) print(x) sns.distplot(random.pareto(a=2, size=1000), kde=False) plt.show()
def pare(df): """Pareto distribution.""" pareto(df)
# pareto_distribution # a distribution following pareto's law # 80-20 distribution (20 % factors cause 80% outcome) # It has two parameters. # a - shape parameter # size - The shape of the returned array # draw out a sample for pareto distribution with shape of 2 with size 2x3 from numpy import random x = random.pareto(a=2, size=(2, 3)) print(x) # visualization of pareto distribution # from numpy import random import matplotlib.pyplot as plt import seaborn as sns sns.distplot(random.pareto(a=2, size=1000), kde=False) plt.show()
def noise_sample(order): noise = npr.pareto(1, size=(2**order, 2**order)) return noise