def create_tbg_neural_efficacies(physiological_params, condition_defs, labels): """ Create neural efficacies from a truncated bi-Gaussian mixture. Ars: - physiological_params (dict (<param_name> : <param_value>): parameters of the physiological model - condition_defs (list of pyhrf.Condition): list of condition definitions. Each item should have the following fields (moments of the mixture): - m_act (0<=float<eff_max): mean of activating component - v_act (0<float): variance of activating component - v_inact (0<float): variance of non-activating component - labels (np.array((nb_cond, nb_vox), int)): binary activation states Return: np.array(np.array((nb_cond, nb_vox), float)) -> the generated neural efficacies TODO: settle how to relate brls and prls to neural efficacies """ eff_max = physiological_params['eps_max'] eff = [] for ic,c in enumerate(condition_defs): labels_c = labels[ic] mask_activ = np.where(labels_c) eff_c = truncnorm.rvs(0, eff_max, loc=0., scale=c.v_inact**.5, size=labels_c.size) # truncnorm -> loc is mean, scale is std_dev eff_c[mask_activ] = truncnorm.rvs(0, eff_max, loc=c.m_act, scale=c.v_act**.5, size=labels_c.sum()) eff.append(eff_c) return np.vstack(eff)
def __init__( self, bus_stop_positions=np.arange(0, 30, 3), passenger_arrival_times=lambda: np.random.exponential(10.0), hop_in_time=lambda: truncnorm.rvs(-1, 8, loc=0.3, scale=0.2), hop_out_time=lambda: truncnorm.rvs(-1, 8, loc=0.3, scale=0.2), nb_stops_to_dest=lambda: np.round(truncnorm.rvs(-1, 4, loc=4, scale=3)), bus_speed=lambda: truncnorm.rvs(-2, 2, loc=0.83, scale=0.1), nb_buses=50, time_between_buses=lambda: 25, stats_time=5.0): # Define stops self.bus_stop_positions = bus_stop_positions self.stops = [BusStop(position=pos, index=i, arrival_func=passenger_arrival_times) for i, pos in enumerate(self.bus_stop_positions)] # last stop, no one hops in self.stops[-1].next_arrival_time = lambda: np.Inf self.hop_in_time = hop_in_time self.hop_out_time = hop_out_time self.nb_stops_to_dest = nb_stops_to_dest self.bus_speed = bus_speed self.nb_buses = nb_buses self.time_between_buses = time_between_buses self.stats = None self.stats_time = stats_time
def _sample_v_single_sell(I, W, Y, mu_V, sigma_V): """ Sample V from a truncated normal for a sell-side quote. Note ---- This function should be reasonably efficient as it samples directly from a truncated normal distribution. Parameters ---------- I : int {2: 'Done', 1: 'TradedAway', 0: 'NotTraded'} The status of the trade. W : ndarray Other dealers quotes. Y : float Quote price mu_V : float Mean of V distribution. sigma_V : Standard deviation of V distribution. Returns ------- V : float Sampled value of V. """ # Done case: Y > max(V, W) if I == 2: V_upper = (Y - mu_V) / sigma_V V = truncnorm.rvs(-np.inf, V_upper, loc=mu_V, scale=sigma_V) # Traded away case: max(W) > max(V, Y) elif I == 1: C = np.max(W) V_upper = (C - mu_V) / sigma_V V = truncnorm.rvs(-np.inf, V_upper, loc=mu_V, scale=sigma_V) # Not traded case: V > max(Y, W) elif I == 0: C = np.max(W) if len(W) > 0 else -np.inf m = np.maximum(Y, C) V_lower = (m - mu_V) / sigma_V V = truncnorm.rvs(V_lower, np.inf, loc=mu_V, scale=sigma_V) else: raise ValueError("Unknown value (%s) for I" % I) return V
def sample_truncated_gaussian_vector(data, uncertainties, bounds=None): ''' Samples a Gaussian distribution subject to boundaries on the data :param numpy.ndarray data: Vector of N data values :param numpy.ndarray uncertainties: Vector of N data uncertainties :param int number_bootstraps: Number of bootstrap samples :param tuple bounds: (Lower, Upper) bound of data space ''' nvals = len(data) if bounds: #if bounds[0] or (fabs(bounds[0]) < 1E-12): if bounds[0] is not None: lower_bound = (bounds[0] - data) / uncertainties else: lower_bound = -np.inf #if bounds[1] or (fabs(bounds[1]) < 1E-12): if bounds[1] is not None: upper_bound = (bounds[1] - data) / uncertainties else: upper_bound = np.inf sample = truncnorm.rvs(lower_bound, upper_bound, size=nvals) else: sample = np.random.normal(0., 1., nvals) return data + uncertainties * sample
def generate(self): """ generates the positions """ # The first walker carries the inital parameter values # samples = numpy.row_stack([self.sampler.paramValues,[self.sampler.paramValues+numpy.random.normal(size= # self.sampler.paramCount)*self.sampler.paramWidths for i in range((self.sampler.nwalkers-1))]]) walkers_pos = numpy.zeros((self.sampler.nwalkers-1,self.sampler.paramCount)) print self.sampler.paramMins print self.sampler.paramMaxs print self.sampler.paramValues print self.sampler.paramWidths for j in range(len(walkers_pos)): for i in range(self.sampler.paramCount): a= (self.hlow[i] - self.sampler.paramValues[i]) / self.sampler.paramWidths[i] b= (self.hupp[i] - self.sampler.paramValues[i]) / self.sampler.paramWidths[i] pos = truncnorm.rvs(a,b,loc=self.sampler.paramValues[i], scale=self.sampler.paramWidths[i],size=1)[0] # while numpy.isinf(pos): # pos = truncnorm.rvs(self.sampler.paramMins[i],self.sampler.paramMaxs[i], # loc=self.sampler.paramValues[i], # scale=self.sampler.paramWidths[i],size=1) # print # print self.sampler.paramMins[i],self.sampler.paramMaxs[i] # print self.sampler.paramValues[i],self.sampler.paramWidths[i] walkers_pos[j,i] = pos return numpy.row_stack([self.sampler.paramValues,walkers_pos])
def fit(self, X): """ Fit a model given data. :param X: array-like, shape = (n_samples, n_features) :return: """ # Initialize RBM parameters self.n_visible_units = X.shape[1] if self.activation_function == 'sigmoid': self.W = np.random.randn(self.n_hidden_units, self.n_visible_units) / np.sqrt(self.n_visible_units) self.c = np.random.randn(self.n_hidden_units) / np.sqrt(self.n_visible_units) self.b = np.random.randn(self.n_visible_units) / np.sqrt(self.n_visible_units) self._activation_function_class = SigmoidActivationFunction elif self.activation_function == 'relu': self.W = truncnorm.rvs(-0.2, 0.2, size=[self.n_hidden_units, self.n_visible_units]) / np.sqrt( self.n_visible_units) self.c = np.full(self.n_hidden_units, 0.1) / np.sqrt(self.n_visible_units) self.b = np.full(self.n_visible_units, 0.1) / np.sqrt(self.n_visible_units) self._activation_function_class = ReLUActivationFunction else: raise ValueError("Invalid activation function.") if self.optimization_algorithm == 'sgd': self._stochastic_gradient_descent(X) else: raise ValueError("Invalid optimization algorithm.") return self
def make_noisy_probs(exact, noise_type, noise): """Noisify probabilities Args: exact - 2D np.array, rows are options, cols are ppl model_params: dict Outputs: 2d np.aray, rows are options, cols are ppl """ if noise_type == "noiseless": return exact elif noise_type == "binomial": num_hypothetical_trials = noise num_successes = nr.binomial(num_hypothetical_trials, exact[0]) noisy0 = num_successes / num_hypothetical_trials return np.array([noisy0, 1 - noisy0]) elif noise_type == "beta": alpha_beta = jmutils.beta_shape(exact[0], noise) noisy0 = nr.beta(*alpha_beta) return np.array([noisy0, 1 - noisy0]) elif noise_type == "truncnorm": scale = noise noisy0 = truncnorm.rvs(-exact[0] / scale, (1 - exact[0]) / scale, loc=exact[0], scale=scale) return np.array([noisy0, 1 - noisy0]) elif noise_type == "log_odds": lo = np.log(exact[0] / exact[1]) noisy_lo = nr.normal(lo, noise) given_1 = 1 / (math.exp(noisy_lo) + 1) return np.array([1 - given_1, given_1]) else: print("Error: meta noise_type not specified correctly")
def get_guess(self,nwalkers): """ pick sensible starting ranges for the guess parameters T0, period, impact paramter, rp/rs, ecosw and esinw """ rho_unc = 0.001 zpt_unc = 1.E-8 T0_unc = 0.0002 per_unc = 0.00005 b_unc = 0.001 rprs_unc = 0.0001 ecosw_unc = 0.001 esinw_unc = 0.001 p0 = np.zeros([nwalkers,2+self.nplanets*6]) rho = self.fit_sol[0] zpt = self.fit_sol[1] start,stop = (0.0001 - rho) / rho_unc, (30.0 - rho) / rho_unc p0[...,0] = truncnorm.rvs(start,stop ,loc=rho,scale=rho_unc,size=nwalkers) p0[...,1] = np.random.normal(loc=zpt,scale=zpt,size=nwalkers) for i in xrange(self.nplanets): T0,per,b,rprs,ecosw,esinw = self.fit_sol[i*6+2:i*6 + 8] b = 0.0 ecosw = 0.0 esinw = 0.0 p0[...,i*6+2] = np.random.normal(T0,T0_unc,size=nwalkers) p0[...,i*6+3] = np.random.normal(per,per_unc,size=nwalkers) start,stop = (0.0 - b) / b_unc, (0.5 - b) / b_unc p0[...,i*6+4] = truncnorm.rvs(start,stop ,loc=b,scale=b_unc,size=nwalkers) start,stop = (0.0 - rprs) / rprs_unc, (0.5 - rprs) / rprs_unc p0[...,i*6+5] = truncnorm.rvs(start,stop ,loc=rprs,scale=rprs_unc,size=nwalkers) start,stop = (0.0 - ecosw) / ecosw_unc, (0.5 - ecosw) / ecosw_unc p0[...,i*6+6] = truncnorm.rvs(start,stop ,loc=ecosw,scale=ecosw_unc,size=nwalkers) start,stop = (0.0 - esinw) / esinw_unc, (0.5 - esinw) / esinw_unc p0[...,i*6+7] = truncnorm.rvs(start,stop ,loc=esinw,scale=esinw_unc,size=nwalkers) return p0
def sample(self, box): bottomcorner, topcorner = box assert len(bottomcorner) == len(topcorner) assert len(bottomcorner) == self.dim self.counter += 1 s = np.empty(self.dim) for dim in range(self.dim): assert bottomcorner[dim] < topcorner[dim], "({0}, {1})".format(bottomcorner, topcorner) s[dim] = truncnorm.rvs((bottomcorner[dim]-self.mu[dim])/self.sigma[dim], (topcorner[dim]-self.mu[dim])/self.sigma[dim], loc=self.mu[dim], scale=self.sigma[dim]) return s
def Rstat2M(mean, std, unit='Earth', sample_size=1e3, grid_size=1e3, classify = 'No'): """ Forecast the mean and standard deviation of mass given the mean and standard deviation of the radius. Parameters --------------- mean: float Mean (average) of radius. std: float Standard deviation of radius. unit: string (optional) Unit of the radius. Options are 'Earth' and 'Jupiter'. sample_size: int (optional) Number of radius samples to draw with the mean and std provided. grid_size: int (optional) Number of grid in the mass axis when sampling mass from radius. The more the better results, but slower process. Returns --------------- mean: float Predicted mean of mass in the input unit. std: float Predicted standard deviation of mass. """ # unit if unit == 'Earth': pass elif unit == 'Jupiter': mean = mean * rearth2rjup std = std * rearth2rjup else: print "Input unit must be 'Earth' or 'Jupiter'. Using 'Earth' as default." # draw samples radius = truncnorm.rvs( (0.-mean)/std, np.inf, loc=mean, scale=std, size=sample_size) if classify == 'Yes': mass = Rpost2M(radius, 'Earth', grid_size, classify='Yes') else: mass = Rpost2M(radius, 'Earth', grid_size) if mass is None: return None if unit=='Jupiter': mass = mass / mearth2mjup m_med = np.median(mass) onesigma = 34.1 m_up = np.percentile(mass, 50.+onesigma, interpolation='nearest') m_down = np.percentile(mass, 50.-onesigma, interpolation='nearest') return m_med, m_up - m_med, m_med - m_down
def rprior(size, hyperparameters): """ returns untransformed parameters """ eps = 1 / gamma.rvs(hyperparameters["eps_shape"], scale = hyperparameters["eps_scale"], size = size) w = 1 / gamma.rvs(hyperparameters["w_shape"], scale = hyperparameters["w_scale"], size = size) n0 = truncnorm.rvs((hyperparameters["n0_a"] - hyperparameters["n0_mean"]) / hyperparameters["n0_sd"], \ (hyperparameters["n0_b"] - hyperparameters["n0_mean"]) / hyperparameters["n0_sd"], size = size, \ loc = hyperparameters["n0_mean"], scale = hyperparameters["n0_sd"]) r = random.exponential(scale = hyperparameters["r_scale"], size = size) K = truncnorm.rvs((hyperparameters["K_a"] - hyperparameters["K_mean"]) / hyperparameters["K_sd"], \ (hyperparameters["K_b"] - hyperparameters["K_mean"]) / hyperparameters["K_sd"], size = size, \ loc = hyperparameters["K_mean"], scale = hyperparameters["K_sd"]) theta = random.exponential(scale = hyperparameters["theta_scale"], size = size) parameters = zeros((6, size)) parameters[0, :] = eps parameters[1, :] = w parameters[2, :] = log(n0) parameters[3, :] = r parameters[4, :] = K parameters[5, :] = theta return parameters
def normalize_dither_compositions(mat, num_subjects, type): arr_isocomp_bysub = [[]] * num_subjects for t in range(num_subjects): iso_name = [""] * len(mat[0]) before = [0] * len(mat[0]) after = [0] * len(mat[0]) for i in range(len(mat[0])): iso_name[i] = mat[0][i] before[i] = mat[1][i] + truncnorm.rvs(loc=0, scale=0.8, a=-mat[1][i], b=1 - mat[1][i], size=1)[0] if type == "null": after[i] = mat[1][i] + truncnorm.rvs(loc=0, scale=0.8, a=-mat[1][i], b=1 - mat[1][i], size=1)[0] else: after[i] = mat[2][i] + truncnorm.rvs(loc=0, scale=0.8, a=-mat[2][i], b=1 - mat[2][i], size=1)[0] for i in range(len(mat[0])): before[i] = before[i] / sum(before) after[i] = after[i] / sum(after) arr_isocomp_bysub[t] = [iso_name, before, after] return arr_isocomp_bysub
def _sample_w_single(I, V, Y, mu_W, sigma_W, l): if l == 0: return norm.rvs(0, 1, size=0) # Done case: Y < min(V, W) <=> V < Y & W < Y if I == 2: W_lower = (Y - mu_W) / sigma_W W = truncnorm.rvs(W_lower, np.inf, loc=mu_W, scale=sigma_W, size=l) # Traded away case: min(W) < min(Y, V) = m (Working) elif I == 1: m = np.minimum(Y, V) W = mintruncnorm_rvs(m, mu_W, sigma_W, shape=(1, l))[0, :] # Not traded case: min(W) > V (and Y > V) elif I == 0: W_lower = (V - mu_W) / sigma_W W = truncnorm.rvs(W_lower, np.inf, loc=mu_W, scale=sigma_W, size=l) return W
def _sample_v_single(I, W, Y, mu_V, sigma_V): # Done case: V < Y (and W > V) if I == 2: V_lower = (Y - mu_V) / sigma_V V = truncnorm.rvs(V_lower, np.inf, loc=mu_V, scale=sigma_V) # Traded away case: V > min(W) (and min(W) < Y) elif I == 1: C = np.min(W) V_lower = (C - mu_V) / sigma_V V = truncnorm.rvs(V_lower, np.inf, loc=mu_V, scale=sigma_V) # Not traded case: V < min(min(W), Y) elif I == 0: C = np.min(W) if len(W) > 0 else np.inf m = np.minimum(Y, C) V_upper = (m - mu_V) / sigma_V V = truncnorm.rvs(-np.inf, V_upper, loc=mu_V, scale=sigma_V) return V
def truncnorm_proposal(current, scale, upper=1, lower=0): #a, b = (myclip_a - my_mean) / my_std, (myclip_b - my_mean) / my_std proposed = truncnorm.rvs((lower - current) / scale, (upper - current) / scale, loc=current, scale=scale) log_fwd_prob = truncnorm.logpdf(proposed, (lower - current) / scale, (upper - current) / scale, loc=current, scale=scale) log_back_prob = truncnorm.logpdf(current, (lower - proposed) / scale, (upper - proposed) / scale, loc=proposed, scale=scale) log_back_fwd = log_back_prob - log_fwd_prob return proposed, log_back_fwd
def generatePopulation(rands = None): ''' >>>print "testing 123" testing 123 ''' population = [] sex = numpy.random.binomial(1,0.5,args.population_size) #0 = female, 1 = male # print sex std = 1 if args.mutation_host_male == 0: persis1 = [args.male_persistence_trait]*args.population_size persis2 = [args.male_persistence_trait]*args.population_size else: persis1 = truncnorm.rvs(a = (0 - args.male_persistence_trait)/std, b = numpy.inf, loc = args.male_persistence_trait, scale = std, size = args.population_size) persis2 = truncnorm.rvs(a = (0 - args.male_persistence_trait)/std, b = numpy.inf, loc = args.male_persistence_trait, scale = std, size = args.population_size) # print "persis1", persis1 # print "persis2", persis2 if args.mutation_host_female == 0: resis1 = [args.female_resistance_trait]*args.population_size resis2 = [args.female_resistance_trait]*args.population_size else: resis1 = truncnorm.rvs(a = (0 - args.female_resistance_trait)/std, b = numpy.inf, loc = args.female_resistance_trait, scale = std, size = args.population_size) resis2 = truncnorm.rvs(a = (0 - args.female_resistance_trait)/std, b = numpy.inf, loc = args.female_resistance_trait, scale = std, size = args.population_size) # print "resis1", resis1 # print "resis2", resis2 for i in range(args.population_size): population.append(Individual(sex[i], persis1[i], persis2[i], resis1[i], resis2[i], 0, 0)) # print population[i] # print "runningPop", population # #put half of the TEs on each haplotype # num_of_tes_haplotype1 = numpy.random.poisson(args.initial_te_number/2) # num_of_tes_haplotype2 = numpy.random.poisson(args.initial_te_number/2) # #assign each TE a position # haplotype1 = sorted(random.sample(range(0,args.genome_size), num_of_tes_haplotype1)) # haplotype2 = sorted(random.sample(range(0,args.genome_size), num_of_tes_haplotype2)) # #calculate the fitness of this individual and put it into the population # population.append(calculateFitness(Individual(0.0, haplotype1, haplotype2))) #print "population", population return population
def truncnorm_rvs(lower, upper, loc, scale, shape): a = np.empty(shape) try: m, n = shape if np.isinf(lower).any(): for i in xrange(m): a[i] = truncnorm.rvs( lower, upper[i], loc=loc, scale=scale, size=n) elif np.isinf(upper).any(): for i in xrange(m): a[i] = truncnorm.rvs( lower[i], upper, loc=loc, scale=scale, size=n) else: for i in xrange(m): a[i] = truncnorm.rvs( lower[i], upper[i], loc=loc, scale=scale, size=n) except ValueError: m = shape[0] if np.isinf(lower).any(): for i in xrange(m): a[i] = truncnorm.rvs(lower, upper[i], loc=loc, scale=scale) elif np.isinf(upper).any(): for i in xrange(m): a[i] = truncnorm.rvs(lower[i], upper, loc=loc, scale=scale) else: for i in xrange(m): a[i] = truncnorm.rvs(lower[i], upper[i], loc=loc, scale=scale) return a
def get_a(a_low=1.0e1, a_high=4.41e7, num_sys=1, alpha=-1.6, prob='log_flat'): """ Generate a set of orbital separations from a power law Parameters ---------- a_low : float Lower separation limit a_high : float Upper mass limit (Default: 10 pc) num_sys : int Number of systems to randomly Generate prob : string Probability distribution (options: 'log_flat', 'raghavan', 'power_law') Returns ------- a : ndarray Random separations (ndarray) """ if prob == 'log_flat': C_a = 1.0 / (np.log(a_high) - np.log(a_low)) tmp_y = uniform(size=num_sys) return a_low*np.exp(tmp_y/C_a) elif prob == 'raghavan': mu_P_orb = 5.03 sigma_P_orb = 2.28 # Assume a system mass of 2 Msun P_orb_low = np.log10(a_to_P(1.0, 1.0, a_low)) P_orb_high = np.log10(a_to_P(1.0, 1.0, a_high)) # Set limits for truncnorm a, b = (P_orb_low - mu_P_orb) / sigma_P_orb, (P_orb_high - mu_P_orb) / sigma_P_orb P_orb = 10.0**(truncnorm.rvs(a, b, loc=mu_P_orb, scale=sigma_P_orb, size=num_sys)) return P_to_a(1.0, 1.0, P_orb) elif prob == 'power_law': if alpha == -1: return get_a(a_low=a_low, a_high=a_high, num_sys=num_sys, prob='log_flat') # Scale so 50% of binaries have a > 100 AU a_low, a_high = 100.0 * c.AU_to_cm / c.Rsun_to_cm, 4.41e7 C_a = 0.5 / (a_high**(alpha + 1.0) - a_low**(alpha + 1.0)) a_low = ((4.41e7)**(alpha + 1.0) - 1.0 / C_a) ** (1.0 / (alpha + 1.0)) tmp_y = uniform(size=num_sys) return (tmp_y/C_a + a_low**(alpha + 1.0))**(1.0/(alpha + 1.0)) else: print("You must provide a valid probability distribution") print("Options are 'log_flat', 'raghavan', or 'power_law'") return
def Mstat2R(mean, std, unit='Earth', sample_size=1000, classify='No'): """ Forecast the mean and standard deviation of radius given the mean and standard deviation of the mass. Assuming normal distribution with the mean and standard deviation truncated at the mass range limit of the model. Parameters ---------- mean: float Mean (average) of mass. std: float Standard deviation of mass. unit: string (optional) Unit of the mass. Options are 'Earth' and 'Jupiter'. sample_size: int (optional) Number of mass samples to draw with the mean and std provided. Returns ------- mean: float Predicted mean of radius in the input unit. std: float Predicted standard deviation of radius. """ # unit if unit == 'Earth': pass elif unit == 'Jupiter': mean = mean * mearth2mjup std = std * mearth2mjup else: print("Input unit must be 'Earth' or 'Jupiter'. " "Using 'Earth' as default.") # draw samples mass = truncnorm.rvs((mlower-mean)/std, (mupper-mean)/std, loc=mean, scale=std, size=sample_size) if classify == 'Yes': radius = Mpost2R(mass, unit='Earth', classify='Yes') else: radius = Mpost2R(mass, unit='Earth') if unit == 'Jupiter': radius = radius / rearth2rjup r_med = np.median(radius) onesigma = 34.1 r_up = np.percentile(radius, 50.+onesigma, interpolation='nearest') r_down = np.percentile(radius, 50.-onesigma, interpolation='nearest') return r_med, r_up - r_med, r_med - r_down
def sample(self,m): """ Samples m samples from the current TruncatedGaussian distribution. :param m: Number of samples to draw. :type m: int. :rtype: natter.DataModule.Data :returns: A Data object containing the samples """ a,b = (self.param['a']-self.param['mu'])/self.param['sigma'],(self.param['b']-self.param['mu'])/self.param['sigma'] return Data(truncnorm.rvs(a,b,loc=self.param['mu'],scale=self.param['sigma'],size=m),'%i samples from %s' % (m,self.name))
def sample_mu(current_partition, move, time_elapsed, params): group = move['new_group']; # new random mu new_mu = truncnorm.rvs((0.0-params[0])/10.0, (255.0-params[0])/10.0, params[0], 10.0) # pretty random... # sample both probs mu_log_prob = move_probability(current_partition, move, params[0], params[1], params[2], params[3], params[4])[group] new_mu_log_prob = move_probability(current_partition, move, new_mu, params[1], params[2], params[3], params[4])[group] diff = exp(new_mu_log_prob - mu_log_prob) # choose one prob = mu_log_prob if diff > 0.0 or random.uniform(0.0, 1.0) <= exp(diff): params[0] = new_mu prob = new_mu_log_prob return [params, prob]
def sample_var(current_partition, move, time_elapsed, params): group = move['new_group']; # new random var new_var = truncnorm.rvs((0.0-params[2])/8.0, (256.0*256.0-params[2])/8.0, params[1], 8.0) # pretty random... # sample both probs var_log_prob = move_probability(current_partition, move, params[0], params[1], params[2], params[3], params[4])[group] new_var_log_prob = move_probability(current_partition, move, params[0], params[1], new_var, params[3], params[4])[group] diff = new_var_log_prob - var_log_prob # choose one prob = var_log_prob if diff > 0.0 or random.uniform(0.0, 1.0) <= exp(diff): params[2] = new_var prob = new_var_log_prob return [params, prob]
def __init__(self, M1_vals, T2_vals, N_samp=10000, gamma=0.4, cache=False, low_q=0.0, high_q=1.0): """Initialize the orbit prior object Parameters: =========== - M1_vals: numpy array, or float The primary star masses - T2_vals: numpy array of same shape as M1_vals, or float The companion star temperatures - N_samp: The number of random samples to take for computing the mass-ratio distribution samples - gamma: The mass-ratio distribution power-law exponent - cache: boolean Should we cache the empirical prior to make lookups faster? If the input q changes, this will give THE WRONG ANSWER! - low_q: float, default=0.0 What is the lowest mass ratio in the sample? (Used for normalized the PDF) - high_q: float, default=1.0 What is the highest mass ratio in the sample? (Used for normalizing the PDF) Returns: ========= None """ M1_vals = np.atleast_1d(M1_vals) T2_vals = np.atleast_1d(T2_vals) # Estimate the mass-ratio prior M1_std = np.maximum(0.5, 0.2 * M1_vals) a, b = (1.5 - M1_vals) / M1_std, np.ones_like(M1_vals) * np.inf M1_samples = np.array( [truncnorm.rvs(a=a[i], b=b[i], loc=M1_vals[i], scale=M1_std[i], size=N_samp) for i in range(M1_vals.size)]) T2_samples = np.array([np.random.normal(loc=T2_vals[i], scale=200, size=N_samp) for i in range(T2_vals.size)]) M2_samples = teff2mass(T2_samples) q_samples = M2_samples / M1_samples self.empirical_q_prior = [gaussian_kde(q_samples[i, :]) for i in range(q_samples.shape[0])] self.gamma = gamma self._cache_empirical = cache self._cache = None self.low_q = low_q self.high_q = high_q
def trunc_norm_generation(mean, std, a, b, file_path): f = open(file_path, 'w') ave_feats = int(round(mean)) chars_per_feat = compute_ave_chars_per_feat(ave_feats) count = 0 feature_samples = truncnorm.rvs(a, b, size=num_samples) for i in range(num_samples): count += 1 if count % 100000 == 0: print "Processing line", str(count), "for the generated dataset." label = randint(0, num_classes - 1) feature_sample = int(round(feature_samples[i]*std+mean)) feature_labels = sample(range(1, num_features + 1), feature_sample) feature_labels.sort() features = [str(feature_labels[i]) + ':' + get_random_data_point() for i in range(feature_sample)] line_to_write = str(label) + ' ' + ' '.join(features) + '\n' f.write(line_to_write)
def lightcurve_scaled_to_mag(redshifts, model, mag=(-18, 0.1), mag_dist_trunc=None, r_v=2., ebv_rate=0.11, t_scale=None, cosmo=Planck15, n_templates=1, **kwargs): """ """ out = {} if n_templates > 1: out['template_index'] = np.random.randint(0, n_temp, len(redshifts)) model_kw = [{'template_index': k} for k in out['template_index']] elif n_templates == 1: model_kw = [{} for z in redshifts] # Amplitude amp = [] for z, model_kw_ in zip(redshifts, model_kw): if mag_dist_trunc is None: mabs = np.random.normal(mag[0], mag[1]) else: mabs = truncnorm.rvs(mag_dist_trunc[0], mag_dist_trunc[1], mag[0], mag[1]) if t_scale is None: model.set(z=z, **model_kw_) model.set_source_peakabsmag(mabs, 'bessellb', 'vega', cosmo=cosmo) amp.append(model.get('amplitude')) else: model.set(z=0, amplitude=1, **model_kw_) mag_abs = np.random.normal(mag[0], mag[1]) mag_current = model.bandmag('sdssr', 'ab', 1) dm = mag_current - mag_abs amp.append(10**(0.4*(dm-cosmo.distmod(z).value))) out['amplitude'] = np.array(amp) out['hostr_v'] = r_v * np.ones(len(redshifts)) out['hostebv'] = np.random.exponential(ebv_rate, len(redshifts)) return out
def sample_disp(current_partition, move, time_elapsed, params, last_probs): group = move['new_group']; # new random disp new_disp = truncnorm.rvs((0.0-params[4])/4.0, (1000.0-params[4])/4.0, params[4], 4.0) # pretty random... # sample the new probabailities all_new_disp_log_prob = move_probability(current_partition, move, params[0], params[1], params[2], params[3], new_disp) disp_log_prob = last_probs[group] new_disp_log_prob = all_new_disp_log_prob[group] # choose one diff = new_disp_log_prob - disp_log_prob prob = disp_log_prob all_probs = last_probs if diff > 0.0 or random.uniform(0.0, 1.0) <= exp(diff): all_probs = all_new_disp_log_prob params[4] = new_disp prob = new_disp_log_prob return [params, prob, all_probs]
def build_training_data(self): for dom in self.bases_data: num0 = len(self.bases_data[dom]['main'][0]) num1 = len(self.bases_data[dom]['plasm'][0]) seq_len = self.seqLenCut num_bases = len(self.basesSet) num_features = len(self.globFeatureL) # clever list-->numpy conversion, Thorsten's idea XhotAll = np.zeros([num0 + num1, seq_len, num_bases], dtype=np.float32) XfloatAll = np.zeros([num0 + num1, num_features], dtype=np.float32) #XnameAll=[] YAll = np.zeros([num0 + num1], dtype=np.float32) YAll[num0:] = np.ones( num1 ) # before randomization all plasmids are in 2nd half of array #print(dom,'wq0',XhotAll.shape,XfloatAll.shape) off = {'main': 0, 'plasm': num0} for role in self.mr12: seqL, gloftL = self.bases_data[dom][role] numSeq = len(seqL) print(' build_training_data:', dom, role, ' numSeq=%d ...' % numSeq) self.add_scaff_to_1hot(XhotAll, XfloatAll, off[role], seqL, gloftL) #print('tt dom=',dom,' role=',role,' len=',len(seqL)) print('build_training_data for dom:', dom, 'Xs,Xf,Y:', XhotAll.shape, XfloatAll.shape, YAll.shape, 'SNR=%.3f' % (num1 / num0), 'done') if self.trainNoise > 0: sigNoise = self.trainNoise if dom == 'val': sigNoise = 0.01 print(' add noise ', sigNoise, ' to gloft in', dom) XfloatAll += truncnorm.rvs(-2, 2, loc=0, scale=sigNoise, size=XfloatAll.shape) self.data[dom] = [XhotAll, XfloatAll, YAll]
def _sample_from_gmm( self, parzen_estimator: _ParzenEstimator, low: float, high: float, q: Optional[float] = None, size: Tuple = (), ) -> np.ndarray: weights = parzen_estimator.weights mus = parzen_estimator.mus sigmas = parzen_estimator.sigmas weights, mus, sigmas = map(np.asarray, (weights, mus, sigmas)) if low >= high: raise ValueError( "The 'low' should be lower than the 'high'. " "But (low, high) = ({}, {}).".format(low, high) ) active = np.argmax(self._rng.multinomial(1, weights, size=size), axis=-1) trunc_low = (low - mus[active]) / sigmas[active] trunc_high = (high - mus[active]) / sigmas[active] samples = np.full((), fill_value=high + 1.0, dtype=np.float64) while (samples >= high).any(): samples = np.where( samples < high, samples, truncnorm.rvs( trunc_low, trunc_high, size=size, loc=mus[active], scale=sigmas[active], random_state=self._rng, ), ) if q is None: return samples else: return np.round(samples / q) * q
def action_stay_at_bower(bird_id, current_time): global birds my_bird = birds[bird_id] # generate the length of the stay time_spent_at_bower = RBSB_tau_std * truncnorm.rvs( RBSB_tau_norm_range[0], RBSB_tau_norm_range[1]) + RBSB_tau_mean time_action_ends = current_time + time_spent_at_bower # generate the ticket generate_ticket(start_time=current_time, end_time=time_action_ends, length_activity=time_spent_at_bower, owner=bird_id, action="staying at bower", target=-1) # update the bird: my_bird["current_state"] = "staying at bower" my_bird["action_starts"] = current_time my_bird["action_ends"] = time_action_ends my_bird["staying_time_data"] += np.array( [1, time_spent_at_bower, time_spent_at_bower * time_spent_at_bower])
def _truncated_gaussian_sample(mu, sigma, lower_bound): """Returns samples from a truncated gaussian sample as defined in Bayesian Optimization With Censored Response Data (Frank Hutter, Holger Hoos, and Kevin Leyton-Brown). Args: mu (float): The mean of the distribution. sigma (positive float): The scale of the distribution. lower_bound (float): The lower bound value. n_samples (int): The number of samples Returns: float: the value of the density function at point x. """ normed_lower_bound = (lower_bound - mu) / sigma return truncnorm.rvs(a=normed_lower_bound, b=np.Inf, loc=mu, scale=sigma, size=1)
def make_xform(self): translate_time = self.rng.normalvariate(0, self.translate_time_stdev) translate_flux = self.rng.normalvariate(0, self.translate_flux_stdev) time_trunc = math.log( self.max_time_dilation) - self.log_dilate_time_stdev flux_trunc = math.log( self.max_flux_dilation) - self.log_dilate_flux_stdev dilations = truncnorm.rvs( [-time_trunc, -flux_trunc], [time_trunc, flux_trunc], 0, [self.log_dilate_time_stdev, self.log_dilate_flux_stdev], random_state=self.rng.getrandbits(32)) return xform.LinearBandDataXform( translate_time, translate_flux, dilate_time=math.exp(dilations[0]), dilate_flux=math.exp(dilations[1]), check_positive=True, )
def vect_sample_f(self): for i in range(self.size_f): f_temp = None for j in range(self.num_objectives): #sample = truncnorm.rvs(-1.96, 1.96, loc=self.mean_values[i,j], scale=self.stddev_values[i,j], size=self.n_samples) sample = truncnorm.rvs(-3, 3, loc=self.mean_values[i, j], scale=self.stddev_values[i, j], size=self.n_samples) #sample = np.random.normal(self.mean_values[i,j], self.stddev_values[i,j], self.n_samples) sample = np.reshape(sample, (1, 1, self.n_samples)) if f_temp is None: f_temp = sample else: f_temp = np.hstack((f_temp, sample)) if self.f_samples is None: self.f_samples = f_temp else: self.f_samples = np.vstack((self.f_samples, f_temp))
def xavier_normal(incoming, outgoing): """ Xavier normal initialization. Used for Neural Network weight initialization. Generally used with sigmoid, softmax, or tanh activation functions. Parameters ---------- incoming : int Shape from the incoming layer (i.e. number of inputs from the previous layer) outgoing : int Shape outgoing from the current layer (i.e. number of outputs from the current layer) Returns ------- W : numpy array The initialized weights. """ W = truncnorm.rvs(size=(incoming, outgoing), a=-2, b=2, scale=np.sqrt(2/(incoming + outgoing))) return W
def PMCMC_u(pi,xi,delta,alpha, eta, NT, T, y, x, beta,sigma_v_sqr,u): H = 5000-1 # particle numbers # sample u from N(mu_u, V_u) V_u = np.exp(np.dot(pi, xi)) mu_u = np.dot(pi, delta) myclip_a = 0 my_mean = mu_u my_std = V_u** 0.5 a, b = (myclip_a - my_mean) / my_std, np.inf * np.ones([NT,]) u_particles = truncnorm.rvs(a,b,loc = my_mean, scale = my_std, size = (H,NT)) u_particles = np.concatenate([u_particles, u.reshape(-1,1).T], axis=0) # calculate weight w = norm.pdf((y-np.dot(x, beta)-u_particles-np.kron(alpha,np.ones([T,]))-np.kron(eta,np.ones(T,)))/(sigma_v_sqr**0.5)) w = w/w.sum(axis=0) new_u = np.zeros([NT,]) for i in range(NT): new_u[i,] = np.random.choice(u_particles[:,i],p=w[:,i]) return new_u
def gen_datasets(n=6, num_rankings=1000, num_datasets=20, B=1.5, random_state=8080): theta = truncnorm.rvs(-B, B, loc=0, scale=1, size=n, random_state=random_state) theta = np.array(theta) theta -= theta.mean() print(f'theta: {theta}') p = np.exp(theta) p /= p.sum() data = [gen_O_J_v2(num_rankings, n, p) for idx in range(num_datasets)] return theta, data
def truncated_noise_sample( batch_size: int = 1, dim_z: int = 128, truncation: float = 1.0, seed: Optional[int] = None, ) -> np.ndarray: """Create a truncated noise vector. Params: batch_size: batch size. dim_z: dimension of z truncation: truncation value to use seed: seed for the random generator Output: array of shape (batch_size, dim_z) """ state = None if seed is None else np.random.RandomState(seed) values = truncnorm.rvs( -2, 2, size=(batch_size, dim_z), random_state=state ).astype(np.float32) return truncation * values
def trunc_norm(mu, sigma, n_gen=1, lower=0, upper=np.inf): """Draw from a truncated normal distribution. Args: mu (number): Mu sigma (number): Sigma n_gen (number): Number to generate lower (number): Lower limit upper (number): Upper limit Returns: array: Numpy of required length """ if sigma == 0: return np.full(n_gen, mu) left = (lower - mu) / sigma right = (upper - mu) / sigma d = truncnorm.rvs(left, right, loc=mu, scale=sigma, size=n_gen) return d
def lecun_normal(incoming, outgoing): """ Lecun normal initialization. Used for Neural Network weight initialization. Generally used with a selu activation function. Parameters ---------- incoming : int Shape from the incoming layer (i.e. number of inputs from the previous layer) outgoing : int Shape outgoing from the current layer (i.e. number of outputs from the current layer) Returns ------- W : numpy array The initialized weights. """ W = truncnorm.rvs(size=(incoming, outgoing), a=-2, b=2, scale=np.sqrt(1/incoming)) return W
def save_image(self,epoch): """ 这是保存生成图片的函数 :param images: 图片集 :param epoch:周期数 :return: """ rows, cols = 5, 5 noise = truncnorm.rvs(-1, 1, size=(rows * cols, self.config.generator_input_dim[0])) images = self.generotor_model.predict(noise) fig, axs = plt.subplots(rows, cols) cnt = 0 for i in range(rows): for j in range(cols): axs[i, j].imshow(images[cnt, :, :, 0], cmap='gray') axs[i, j].axis('off') cnt += 1 fig.savefig(os.path.join(self.config.result_path,"mnist-{0:0>5}.png".format(epoch)), dpi=300) plt.close()
def _init_weights(self): if self.active_func == 'sigmoid': self.W = np.random.randn(self.n_visible, self.n_hidden) / np.sqrt( self.n_visible) self.bias_v = np.random.randn(self.n_visible) / np.sqrt( self.n_visible) self.bias_h = np.random.randn(self.n_hidden) / np.sqrt( self.n_visible) self.active_func_class = SigmoidActivationFunction elif self.active_func == 'relu': self.W = truncnorm.rvs( -0.2, 0.2, size=[self.n_visible, self.n_hidden]) / np.sqrt( self.n_visible) self.bias_v = np.full(self.n_visible, 0.1) / np.sqrt( self.n_visible) self.bias_h = np.full(self.n_hidden, 0.1) / np.sqrt(self.n_visible) self.active_func_class = ReLUActivationFunction else: raise ValueError('unknown active func') self.errors = []
def trunc_norm_generation(mean, std, a, b, file_path): f = open(file_path, 'w') ave_feats = int(round(mean)) chars_per_feat = compute_ave_chars_per_feat(ave_feats) count = 0 feature_samples = truncnorm.rvs(a, b, size=num_samples) for i in range(num_samples): count += 1 if count % 100000 == 0: print "Processing line", str(count), "for the generated dataset." label = randint(0, num_classes - 1) feature_sample = int(round(feature_samples[i] * std + mean)) feature_labels = sample(range(1, num_features + 1), feature_sample) feature_labels.sort() features = [ str(feature_labels[i]) + ':' + get_random_data_point() for i in range(feature_sample) ] line_to_write = str(label) + ' ' + ' '.join(features) + '\n' f.write(line_to_write)
def setUp(self): size = 1000 np.random.seed(42) # Binary Data self.binary_data = np.random.randint(0, 2, size=10000) # Truncated Normal a, b, loc, scale = -1.0, 0.5, 0.0, 1.0 self.truncated_data = truncnorm.rvs(a, b, loc=loc, scale=scale, size=10000) # Mixture of Normals mask = np.random.normal(size=size) > 0.5 mode1 = np.random.normal(size=size) * mask mode2 = np.random.normal(size=size, loc=10) * (1.0 - mask) self.bimodal_data = mode1 + mode2
def initiate(min_type, max_type, min_rate, max_rate, dim1, vector_size): np.random.seed(49) # populate each element (territory) with a random bird (syllable syll) # low (inclusive), high(exclusive), discrete uniform distribution bird_matrix = np.random.randint(min_type, max_type, size=[dim1, dim1], dtype='int') # assign a random syllable rate to each bird (territory) # low (inclusive), high(exclusive), continuous uniform distribution # rate_matrix = np.random.uniform(min_rate, max_rate, size=[dim1, dim1]) mu, sigma = np.mean([min_rate, max_rate]), 5 rate_matrix = truncnorm.rvs( (min_rate - mu) / sigma, (max_rate - mu) / sigma, loc=mu, scale=sigma, size=[dim1, dim1], ) # initiate vectors for tracking info about entire population current_bps = np.zeros( vector_size, dtype='int') # no. birds per syllable at one time step actual_lifetimes = np.zeros(vector_size, dtype='int') # no. yrs syllable has existed unique_bps = np.zeros( vector_size, dtype='int' ) # no. of individual birds that have ever had that syllable syll # initiate vectors for tracking info about only the sampled population sample_bps = np.zeros(vector_size, dtype='int') sample_unique_bps = np.zeros(vector_size, dtype='int') first_sampled = np.zeros(vector_size, dtype='int') # first iter syllable was sampled last_sampled = np.zeros( vector_size, dtype='int' ) # last iter syllable was sampled + 1 (so can subtract to get lifespans) return bird_matrix, rate_matrix, current_bps, actual_lifetimes, \ unique_bps, sample_bps, sample_unique_bps, first_sampled, \ last_sampled
def __init__(self, n_visible_units=100, n_hidden_units=100, activation_function='sigmoid', optimization_algorithm='sgd', learning_rate=1e-3, n_epochs=10, contrastive_divergence_iter=1, batch_size=32, verbose=True): self.n_visible_units = n_visible_units self.n_hidden_units = n_hidden_units self.activation_function = activation_function self.optimization_algorithm = optimization_algorithm self.learning_rate = learning_rate self.n_epochs = n_epochs self.contrastive_divergence_iter = contrastive_divergence_iter self.batch_size = batch_size self.verbose = verbose # Initialize RBM parameters # self.n_visible_units = X.shape[1] if self.activation_function == 'sigmoid': self.W = np.random.randn(self.n_hidden_units, self.n_visible_units) / np.sqrt( self.n_visible_units) self.c = np.random.randn(self.n_hidden_units) / np.sqrt( self.n_visible_units) self.b = np.random.randn(self.n_visible_units) / np.sqrt( self.n_visible_units) self._activation_function_class = SigmoidActivationFunction elif self.activation_function == 'relu': self.W = truncnorm.rvs( -0.2, 0.2, size=[self.n_hidden_units, self.n_visible_units ]) / np.sqrt(self.n_visible_units) self.c = np.full(self.n_hidden_units, 0.1) / \ np.sqrt(self.n_visible_units) self.b = np.full(self.n_visible_units, 0.1) / \ np.sqrt(self.n_visible_units) self._activation_function_class = ReLUActivationFunction else: raise ValueError("Invalid activation function.")
def fit(self, X): """ Fit a model given data. :param X: array-like, shape = (n_samples, n_features) :return: """ # Initialize RBM parameters self.n_visible_units = X.shape[1] if self.activation_function == 'sigmoid': ########### MODIFIED BY YACONG ################### # self.W = np.random.randn(self.n_hidden_units, self.n_visible_units) / np.sqrt(self.n_visible_units) # self.c = np.random.randn(self.n_hidden_units) / np.sqrt(self.n_visible_units) # self.b = np.random.randn(self.n_visible_units) / np.sqrt(self.n_visible_units) self.W = np.random.rand(self.n_hidden_units, self.n_visible_units) / np.sqrt(self.n_visible_units) self.c = np.random.rand(self.n_hidden_units) / np.sqrt(self.n_visible_units) self.b = np.random.rand(self.n_visible_units) / np.sqrt(self.n_visible_units) self.W -= 0.5 self.W *= 2 self.b -= 0.5 self.b *= 2 self.c -= 0.5 self.c *= 2 ########### MODIFIED BY YACONG ################### self._activation_function_class = SigmoidActivationFunction elif self.activation_function == 'relu': self.W = truncnorm.rvs(-0.2, 0.2, size=[self.n_hidden_units, self.n_visible_units]) / np.sqrt( self.n_visible_units) self.c = np.full(self.n_hidden_units, 0.1) / np.sqrt(self.n_visible_units) self.b = np.full(self.n_visible_units, 0.1) / np.sqrt(self.n_visible_units) self._activation_function_class = ReLUActivationFunction else: raise ValueError("Invalid activation function.") if self.optimization_algorithm == 'sgd': self._stochastic_gradient_descent(X) else: raise ValueError("Invalid optimization algorithm.") return self
def to_gmfs(shakemap, crosscorr, site_effects, trunclevel, num_gmfs, seed, imts=None): """ :returns: an array of GMFs of shape (R, N, E, M) """ std = shakemap['std'] if imts is None: imts = std.dtype.names val = { imt: numpy.log(shakemap['val'][imt]) - std[imt]**2 / 2. for imt in imts } imts = [imt.from_string(name) for name in imts] dmatrix = geo.geodetic.distance_matrix(shakemap['lon'], shakemap['lat']) spatial_corr = spatial_correlation_array(dmatrix, imts) stddev = [std[str(imt)] for imt in imts] spatial_cov = spatial_covariance_array(stddev, spatial_corr) cross_corr = cross_correlation_matrix(imts, crosscorr) M, N = spatial_corr.shape[:2] mu = numpy.array([ numpy.ones(num_gmfs) * val[str(imt)][j] for imt in imts for j in range(N) ]) # mu has shape (M * N, E) L = cholesky(spatial_cov, cross_corr) # shape (M * N, M * N) Z = truncnorm.rvs(-trunclevel, trunclevel, loc=0, scale=1, size=(M * N, num_gmfs), random_state=seed) # Z has shape (M * N, E) gmfs = numpy.exp(numpy.dot(L, Z) + mu) / PCTG if site_effects: gmfs = amplify_gmfs(imts, shakemap['vs30'], gmfs) * 0.8 return gmfs.reshape((1, M, N, num_gmfs)).transpose(0, 2, 3, 1)
def to_gmfs(shakemap, spatialcorr, crosscorr, site_effects, trunclevel, num_gmfs, seed, imts=None): """ :returns: (IMT-strings, array of GMFs of shape (R, N, E, M) """ N = len(shakemap) # number of sites std = shakemap['std'] if imts is None or len(imts) == 0: imts = std.dtype.names else: imts = [imt for imt in imts if imt in std.dtype.names] val = {imt: numpy.log(shakemap['val'][imt]) - std[imt] ** 2 / 2. for imt in imts} imts_ = [imt.from_string(name) for name in imts] M = len(imts_) cross_corr = cross_correlation_matrix(imts_, crosscorr) mu = numpy.array([numpy.ones(num_gmfs) * val[str(imt)][j] for imt in imts_ for j in range(N)]) dmatrix = geo.geodetic.distance_matrix( shakemap['lon'], shakemap['lat']) spatial_corr = spatial_correlation_array(dmatrix, imts_, spatialcorr) stddev = [std[str(imt)] for imt in imts_] for im, std in zip(imts_, stddev): if std.sum() == 0: raise ValueError('Cannot decompose the spatial covariance ' 'because stddev==0 for IMT=%s' % im) spatial_cov = spatial_covariance_array(stddev, spatial_corr) L = cholesky(spatial_cov, cross_corr) # shape (M * N, M * N) if trunclevel: Z = truncnorm.rvs(-trunclevel, trunclevel, loc=0, scale=1, size=(M * N, num_gmfs), random_state=seed) else: Z = norm.rvs(loc=0, scale=1, size=(M * N, num_gmfs), random_state=seed) # Z has shape (M * N, E) gmfs = numpy.exp(numpy.dot(L, Z) + mu) / PCTG if site_effects: gmfs = amplify_gmfs(imts_, shakemap['vs30'], gmfs) if gmfs.max() > MAX_GMV: logging.warning('There are suspiciously large GMVs of %.2fg', gmfs.max()) return imts, gmfs.reshape((M, N, num_gmfs)).transpose(1, 2, 0)
def make_script(faces, length): total_time = 0.0 script = [] # (frame_index, sample_length, total_length) while total_time < length: # Get a frame frame_index = int(truncnorm.rvs(0,1)*len(faces)) # Decide the sample's length sample_length = random.random()/4 # Decide the total movie's length repetitions = int(random.random()*10) total_time += (sample_length*repetitions) script.append((faces[frame_index][0], sample_length, repetitions)) return script
def create_scenario(self): scenario = {'meal': {'time': [], 'amount': []}} # Probability of taking each meal # [breakfast, snack1, lunch, snack2, dinner, snack3] time_lb = np.array([5, 10, 16]) * 60 time_ub = np.array([10, 16, 22]) * 60 time_mu = np.array([7.5, 13, 19]) * 60 time_sigma = np.array([60, 60, 60]) * self.time_std_multiplier amount = [45, 70, 80] for tlb, tub, tbar, tsd, mbar in zip(time_lb, time_ub, time_mu, time_sigma, amount): tmeal = np.round(truncnorm.rvs(a=(tlb - tbar) / tsd, b=(tub - tbar) / tsd, loc=tbar, scale=tsd, random_state=self.random_gen)) scenario['meal']['time'].append(tmeal) scenario['meal']['amount'].append(mbar) return scenario
def x_season_generation(n, electricity_data, kappa, sigma_s_param, len_initialization=1000): """x_season_generation : simulation of the x_season according to the law described in the article""" #Initialization x = np.zeros(n) nu = truncnorm.rvs(a=0, b=math.inf, loc=0, scale=sigma_s_param, size=1)[0] sigma_s_current = nu error = truncnorm.rvs(a=0, b=math.inf, loc=0, scale=sigma_s_current, size=1)[0] s_current = error for i in range(1, len_initialization, 1): nu = truncnorm.rvs(a=-sigma_s_current / sigma_s_param, b=math.inf, loc=0, scale=sigma_s_param, size=1)[0] sigma_s_current += nu error = truncnorm.rvs(a=-s_current / sigma_s_current, b=math.inf, loc=0, scale=sigma_s_current, size=1)[0] s_current += error for i in range(0, n, 1): nu = truncnorm.rvs(a=-sigma_s_current / sigma_s_param, b=math.inf, loc=0, scale=sigma_s_param, size=1)[0] sigma_s_current += nu error = truncnorm.rvs(a=-s_current / sigma_s_current, b=math.inf, loc=0, scale=sigma_s_current, size=1)[0] s_current += error if (i == 0): print("s = ", s_current) print("sigma_s = ", sigma_s_current) x[i] = s_current * kappa[electricity_data.df.Day_type[ i]] # on multiplie s_n par la valeur de kappa au jour n return (x)
def __init__(self, field, config): super().__init__() embedding = field.get_embedding_from_glove(config.embedding) self.embedding = nn.Embedding( num_embeddings=config.embedding.vocab_size + 4, embedding_dim=config.embedding.dim, padding_idx=0, _weight=embedding) self.context_encoder = get_instance(config.context_encoder) self.candidates_encoder = get_instance(config.candidates_encoder) transform_mat = torch.Tensor(truncnorm.rvs(-2, 2, size=( config.candidates_encoder.code_size, config.context_encoder.code_size))) transform_bias = torch.zeros(1) self.transform_mat = torch.nn.Parameter(transform_mat) self.transform_bias = torch.nn.Parameter(transform_bias) self.config = config
def get_z_vector(shape, mode, limit=1, **kwargs): if mode == 'normal': z = torch.randn(*shape, **kwargs) * limit elif mode == 'clamped_normal': # Clamp between -truncation, truncation z = torch.clamp(torch.randn(*shape, **kwargs), -limit, limit) elif mode == 'truncated_normal': # Resample if any point lands outside -limit, limit values = truncnorm.rvs(-2, 2, size=shape).astype(np.float32) z = limit * torch.from_numpy(values).to(kwargs['device']) # raise NotImplementedError() elif mode == 'rectified_normal': # Max(N(0,1), 0) raise NotImplementedError() elif mode == 'uniform': z = 2 * limit * torch.rand(shape, **kwargs) - limit elif mode == 'zero': z = torch.zeros(*shape, **kwargs) else: raise NotImplementedError() return z
def sigma_random(self, a: float, b: float, loc: Optional[float] = None, strength: Optional[float] = None): """Returns a value in [a, b] by perturbating loc with a given strength.""" self._calls += 1 assert a <= b, "a must precede b" assert strength is None or 0 <= strength <= 1, "strength must be in [0, 1]" assert (loc is None and strength is None) or ( loc is not None and strength is not None), "loc and strength should be specified together (either both or neither)" assert loc is None or a <= loc <= b, "loc must be in [a, b]" if strength is None: val = self._generator.random() val = val * (b - a) + a elif strength == 0: val = loc else: std = Randy._strength_to_sigma(strength) clip_a, clip_b = (a - loc) / std, (b - loc) / std val = truncnorm.rvs(clip_a, clip_b, loc=loc, scale=std, random_state=self._generator) return val
def clip_gaussian(self): np.random.seed(self.seed) self.x_samples_rs = np.zeros( (self.num_rs_mc, self.x.shape[1] * self.x.shape[2] * self.x.shape[3])) x_flat = self.x.flatten() x_train_mean_flat = self.x_train_mean.flatten() for i_sample in range(x_flat.shape[0]): self.x_samples_rs[:, i_sample] = truncnorm.rvs( (-x_train_mean_flat[i_sample] - x_flat[i_sample]) / self.sigma, (1 - x_train_mean_flat[i_sample] - x_flat[i_sample]) / self.sigma, loc=x_flat[i_sample], scale=self.sigma, random_state=None, size=self.num_rs_mc) self.x_samples_rs = self.x_samples_rs.reshape(self.num_rs_mc, self.x.shape[1], self.x.shape[2], self.x.shape[3])
def randnorm(size=1): """Random number generator that follows a truncated normal distribution. This is the defalut random number generator used by the program. It generates normally distributed values ranging from -2 to +2 with unit standard deviation. The state of the random number generator is controlled by the ``np.random.RandomState`` instance. Parameters ---------- size : int Number of random values to compute Returns ------- rvs : ndarray or scalar Random variates of given `size`. """ return truncnorm.rvs(-STD_LIM, STD_LIM, scale=STD_SCALE, size=size)
def truncated_normal(x: T.Tensor, a=-1, b=1, mean=0., std=1.): """ Initializes a tensor from a truncated normal distribution. :param x: a :class:`torch.Tensor`. :param a: lower bound of the truncated normal. :param b: higher bound of the truncated normal. :param mean: mean of the truncated normal. :param std: standard deviation of the truncated normal. :return: ``None``. """ values = truncnorm.rvs(a, b, loc=mean, scale=std, size=list(x.shape)) with T.no_grad(): x.data.copy_(T.tensor(values))
def sample_var_conf(current_partition, move, time_elapsed, params, last_probs): group = move['new_group']; # new random var_conf new_var_conf = truncnorm.rvs((0.0-params[3])/4.0, (500.0-params[3])/4.0, params[3], 4.0) # pretty random... # sample both probs all_new_var_conf_log_prob = move_probability(current_partition, move, params[0], params[1], params[2], new_var_conf, params[4]) var_conf_log_prob = last_probs[group] new_var_conf_log_prob = all_new_var_conf_log_prob[group] # choose one diff = new_var_conf_log_prob - var_conf_log_prob prob = var_conf_log_prob all_probs = last_probs rand = random.uniform(0.0, 1.0) if diff > 0.0 or rand <= exp(diff): print "picking", new_var_conf_log_prob, "over", var_conf_log_prob, "with diff", diff, "and rand", rand all_probs = all_new_var_conf_log_prob params[3] = new_var_conf prob = new_var_conf_log_prob return params, prob, all_probs
def _sample_from_gmm( self, parzen_estimator, # type: _ParzenEstimator low, # type: float high, # type: float q=None, # type: Optional[float] size=(), # type: Tuple ): # type: (...) -> np.ndarray weights = parzen_estimator.weights mus = parzen_estimator.mus sigmas = parzen_estimator.sigmas weights, mus, sigmas = map(np.asarray, (weights, mus, sigmas)) if low >= high: raise ValueError( "The 'low' should be lower than the 'high'. " "But (low, high) = ({}, {}).".format(low, high) ) active = np.argmax(self._rng.multinomial(1, weights, size=size), axis=-1) trunc_low = (low - mus[active]) / sigmas[active] trunc_high = (high - mus[active]) / sigmas[active] while True: samples = truncnorm.rvs( trunc_low, trunc_high, size=size, loc=mus[active], scale=sigmas[active], random_state=self._rng, ) if (samples < high).all(): break if q is None: return samples else: return np.round(samples / q) * q
def DoOffspringVars( Bearpairs, Femalepercent, sourcePop, size_mean, transmissionprob, gen, sizecall, M_mature, F_mature, Mmat_slope, Mmat_int, Fmat_slope, Fmat_int, Mmat_set, Fmat_set, noOffspring, size_std, inheritans_classfiles, ): """ DoOffspringVars() This function assigns the age (0), sex, and size of each offspring. """ # Create empty variable for storing individual offspring information offspring = [] # Only if pairing occured if Bearpairs[0][0] != -9999: # Error check if len(noOffspring) != len(Bearpairs): print("Offspring mismatch with Bearpairs.") sys.exit(-1) count = 0 # Loop through each mate pair # ---------------------------- for i in xrange(len(Bearpairs)): # ------------------------------------------------------- # Get parent's information for multiple classfiles # if inheritans_classfiles is random use this information # ------------------------------------------------------- mothers_file = Bearpairs[i][0]["classfile"] mothers_natalP = int(mothers_file.split("_")[0].split("P")[1]) mothers_theseclasspars = int(mothers_file.split("_")[1].split("CV")[1]) fathers_file = Bearpairs[i][1]["classfile"] fathers_natalP = int(fathers_file.split("_")[0].split("P")[1]) fathers_theseclasspars = int(fathers_file.split("_")[1].split("CV")[1]) # --------------------- # Get Hindex # --------------------- offspring_hindex = Bearpairs[i][0]["hindex"] / 2.0 + Bearpairs[i][1]["hindex"] / 2.0 # And then loop through each offspring from that mate pair # -------------------------------------------------------- for j in xrange(noOffspring[i]): # ------------------------ # Mother's patch location # ------------------------ patchindex = int(Bearpairs[i][0][sourcePop]) - 1 # ------------------------ # Get classfile assignment # ------------------------ randno = rand() # Random number if inheritans_classfiles == "random": if randno < 0.5: natalP = fathers_natalP theseclasspars = fathers_theseclasspars else: natalP = mothers_natalP theseclasspars = mothers_theseclasspars else: # Hindex draw if randno <= offspring_hindex: # Use 1.0 files natalP = 0 theseclasspars = 0 else: # Use 0.0 files natalP = 0 theseclasspars = 1 # -------------------------- # Assign sex here # -------------------------- # Case for Wright Fisher or not if Femalepercent != "WrightFisher": # Select sex of the jth offspring - select a random number randsex = int(100 * rand()) # If that random number is less the Femalepercent, assign it to be a female if randsex < int(Femalepercent): offsex = 0 # If the random number is greater than the Femalepercent, assign it to be a male else: offsex = 1 # Special case for WrightFisher else: offsex = int(2 * rand()) # -------------------------- # Assign infection here # -------------------------- # If parent has infection if Bearpairs[i][0]["infection"] == 1 or Bearpairs[i][1]["infection"] == 1: # Get a random number randinfection = rand() # If random flip is less than transmissionprob if randinfection < transmissionprob: # Then append infection status to offspring infect = 1 # If offspring does not get infected else: infect = 0 # If offspring does not get infected. else: # Then append infection status to offspring infect = 0 # -------------------------- # Assign ID here # -------------------------- mother_name = Bearpairs[i][0]["name"] mother_name = mother_name.split("_") name = ( "Age0_" + "F" + Bearpairs[i][0][sourcePop] + "_P" + Bearpairs[i][0][sourcePop] + "_Y" + str(gen) + "_UO" + str(count) ) # Unique ID is needed for sorting later, however, this unique name can get large, check length and reset. check = name.split("_")[-1] if len(check) > 80: print("Too many offspring, recheck fecundity values.") sys.exit(-1) # Assign ID: 'Age0_Y{}_P{}_M{}_O{} id = name # -------------------------- # Assign size here # -------------------------- mu, sigma = size_mean[natalP][theseclasspars][0], size_std[natalP][theseclasspars][0] # Case here for sigma == 0 if sigma != 0: lower, upper = 0, np.inf sizesamp = truncnorm.rvs((lower - mu) / sigma, (upper - mu) / sigma, loc=mu, scale=sigma) else: sizesamp = mu # -------------------------- # Assign maturity here # -------------------------- # Assign maturity: age or size switch, then male or female mature switch if sizecall == "age": if offsex == 0: # Female check if Fmat_set == "N": # Use prob value matval = F_mature[natalP][theseclasspars][0] else: # Use set age if int(Fmat_set) == 0: # Age of offspring is 0 matval = 1.0 else: matval = 0.0 else: # Male check if Mmat_set == "N": # Use prob value in agevars matval = M_mature[natalP][theseclasspars][0] else: # Use set age if int(Mmat_set) == 0: # Age of offspring is 0 matval = 1.0 else: matval = 0.0 else: # If size control if offsex == 0: # Female check if Fmat_set == "N": # Use equation - size matval = np.exp(Fmat_int + Fmat_slope * sizesamp) / ( 1 + np.exp(Fmat_int + Fmat_slope * sizesamp) ) else: # Use set size if sizesamp >= int(Fmat_set): matval = 1.0 else: matval = 0.0 else: # Male check if Mmat_set == "N": # Use equation - size matval = np.exp(Mmat_int + Mmat_slope * sizesamp) / ( 1 + np.exp(Mmat_int + Mmat_slope * sizesamp) ) else: # Use set size if sizesamp >= int(Mmat_set): matval = 1.0 else: matval = 0.0 randmat = rand() if randmat < matval: mature = 1 else: mature = 0 # -------------------------- # REcord information # -------------------------- # And then recd new information of offspring [Mothergenes,Fathergenes,natalpop,emipop,immipop,emicd,immicd,age0,sex,size,mature,newmature,infection,id,capture,recapture,layeggs,Mothers Hindex, Fathers Hindex, ClassVars File] recd = ( Bearpairs[i][0]["genes"], Bearpairs[i][1]["genes"], Bearpairs[i][0][sourcePop], "NA", "NA", -9999, -9999, 0, offsex, sizesamp, mature, mature, infect, id, 0, 0, 0, Bearpairs[i][0]["hindex"], Bearpairs[i][1]["hindex"], "P" + str(natalP) + "_CV" + str(theseclasspars), ) offspring.append(recd) count = count + 1 # For unique naming tracking # If there was not a pairing else: offspring.append([]) # Variables returned return offspring