def test_plot_cauchyrv(self): def true_alpha_density_ratio(sample): return cauchy.pdf(sample, 0, 1. / 8) / ( alpha * cauchy.pdf(sample, 0, 1. / 8) + (1 - alpha) * cauchy.pdf(sample, 0, 1. / 2)) def estimated_alpha_density_ratio(sample): return densratio_obj.compute_density_ratio(sample) np.random.seed(1) x = cauchy.rvs(size=200, loc=0, scale=1. / 8) y = cauchy.rvs(size=200, loc=0, scale=1. / 2) alpha = 0 densratio_obj = densratio(x, y, alpha=alpha) sample_points = np.linspace(-1, 3, 400) plt.plot(sample_points, true_alpha_density_ratio(sample_points), 'b-', label='True Alpha-Relative Density Ratio') plt.plot(sample_points, estimated_alpha_density_ratio(sample_points), 'r-', label='Estimated Alpha-Relative Density Ratio') plt.title( "Alpha-Relative Density Ratio - Cauchy Random Variables (alpha={:03.2f})" .format(alpha)) plt.legend() plt.show() assert True
def generate_point(distribution, noise, cluster_number, number_of_clusters): max_value = number_of_clusters * 10 overlap = (noise * np.random.uniform(low=0, high=1)) min = (10 * cluster_number) - (overlap * 10) max = 10 + (overlap * 10) + (10 * cluster_number) if distribution == 'g': point = [ np.random.normal(loc=(max + min) / 2, scale=(max - min) / 3), np.random.normal(loc=(max + min) / 2), np.random.normal(loc=(max + min) / 2, scale=(max - min) / 3), np.random.randint(max_value), np.random.randint(4) * (max_value / 4), np.random.randint(2) * max_value, 12345, cluster_number * 10 ] elif distribution == 'c': point = [ cauchy.rvs(loc=(max + min) / 2, scale=(max - min) / 100), cauchy.rvs(loc=(max + min) / 2, scale=(max - min) / 100), cauchy.rvs(loc=(max + min) / 2, scale=(max - min) / 100), np.random.randint(max_value), np.random.randint(4) * (max_value / 4), np.random.randint(2) * max_value, 12345, cluster_number * 10 ] else: point = [ np.random.uniform(low=(10 * cluster_number), high=(10 * cluster_number) + 10), np.random.uniform(low=min, high=max), np.random.uniform(low=min, high=max), np.random.randint(max_value), np.random.randint(4) * (max_value / 4), np.random.randint(2) * max_value, 12345, cluster_number * 10 ] return point
def line(N, width, line="MnKa"): """ Simulate Ka/Kb Line Parameters (and their default values): N: desired number of pulse width: width (FWHM) of gaussian (voigt) profile line: line to simulate (Default: MnKa) Return (e): e: simulated data """ if width == 0: # Simulate by Cauchy (Lorentzian) e = np.array([]) fs = np.asarray(Constants.FS[line]) for f in fs[fs.T[2].argsort()]: e = np.concatenate((e, cauchy.rvs(loc=f[0], scale=Analysis.fwhm2gamma(f[1]), size=int(f[2]*N)))) e = np.concatenate((e, cauchy.rvs(loc=f[0], scale=Analysis.fwhm2gamma(f[1]), size=int(N-len(e))))) else: # Simulate by Voigt pdf = lambda E: Analysis.line_model(E, 0, width, line=line) Ec = np.array(Constants.FS[line])[:,0] _Emin = np.min(Ec) - width*50 _Emax = np.max(Ec) + width*50 e = random(pdf, N, _Emin, _Emax) return e
def line(N, width, line="MnKa"): """ Simulate Ka/Kb Line Parameters (and their default values): N: desired number of pulse width: width (FWHM) of gaussian (voigt) profile line: line to simulate (Default: MnKa) Return (e): e: simulated data """ if width == 0: # Simulate by Cauchy (Lorentzian) fs = np.asarray(Constants.FS[line]) renorm = fs.T[2].sum()**-1 e = np.concatenate([ cauchy.rvs(loc=f[0], scale=Analysis.fwhm2gamma(f[1]), size=int(f[2]*renorm*N)) for f in fs ]) if N - len(e) > 0: e = np.concatenate((e, cauchy.rvs(loc=f[0], scale=Analysis.fwhm2gamma(f[1]), size=int(N-len(e))))) else: # Simulate by Voigt pdf = lambda E: Analysis.line_model(E, 0, width, line=line) Ec = np.array(Constants.FS[line])[:,0] _Emin = np.min(Ec) - width*50 _Emax = np.max(Ec) + width*50 e = random(pdf, N, _Emin, _Emax) return e[:N]
def Ridgway_generator(d, seed, cpam): np.random.seed(seed) X = cauchy.rvs(scale=cpam, size=(d, d)) a = cauchy.rvs(scale=cpam, size=d) Sigma = X.T @ X normalizer = np.diag(1 / np.sqrt(np.diag(Sigma))) Sigma = normalizer @ Sigma @ normalizer a = normalizer @ a return Sigma, a
def generate(self, num_instances): self.num_instances = num_instances N0, N1 = self.generate_instances_numbers() S0 = cauchy.rvs(loc=self.mu0, scale=self.scale0, size=N0) #np.random.normal(self.mu0, self.sigma0, N0) S1 = cauchy.rvs(loc=self.mu1, scale=self.scale1, size=N1) #np.random.normal(self.mu1, self.sigma1, N1) X = np.array(np.concatenate((S0, S1), axis=0)) y = np.asarray([0] * N0 + [1] * N1) return X, y
def get_system_noise(self): """v_t vector""" data_noise = np.zeros((self.ydim, self.PARTICLES_NUM), dtype=np.float64) for i in xrange(self.ydim): data_noise[i,:] = cauchy.rvs(loc=[0]*self.PARTICLES_NUM, scale=np.power(10,self.particles[self.ydim]), size=self.PARTICLES_NUM) data_noise[data_noise==float("-inf")] = -1e308 data_noise[data_noise==float("inf")] = 1e308 parameter_noises = np.zeros((self.pdim, self.PARTICLES_NUM), dtype=np.float64) for i in xrange(self.pdim): parameter_noises[i,:] = cauchy.rvs(loc=0, scale=self.nois_sh_parameters[i], size=self.PARTICLES_NUM) return np.vstack((data_noise, parameter_noises))
def selectF(muF): # randomly generate new F based on a Cauchy distribution newF = cauchy.rvs(loc=muF, scale=0.1) # if it is smaller than 0, try to generate a new F value that is not # smaller than 0 if newF <= 0: while newF <= 0: newF = cauchy.rvs(loc=muF, scale=0.1) # when the newly calculated F value is greater than 1 -> return 1 if newF > 1: return 1 else: return newF
def CauchyBoxplotTukey(): tips, result, count = [], [], 0 for size in sizes: for i in range(NUMBER_OF_REPETITIONS): distribution = cauchy.rvs(size=size) distribution.sort() count += count_out(distribution) result.append(count / (size * NUMBER_OF_REPETITIONS)) distribution = cauchy.rvs(size=size) distribution.sort() tips.append(distribution) DrawBoxplot(tips, CAUCHY) printAnswer(result) return
def selectF(mF): # get a random index ri in the F memory ri = np.random.randint(0, mF.shape[0]) # get random new F by a chauchy distribution where mF[ri] is the centre newF = cauchy.rvs(loc=mF[ri], scale=0.1) # if it is smaller than 0, try to generate a new F value that is not # smaller than 0 if newF <= 0: while newF <= 0: newF = cauchy.rvs(loc=mF[ri], scale=0.1) # when the newly calculated F value is greater than 1 -> return 1 if newF > 1: return 1 else: return newF
def __init__(self, d, gamma=1, D=50, metric='rbf', device=torch.device('cpu')): ''' d: dimension of input D: number of Fourier features metric: 'rbf' or 'laplace' ''' self.D = D # Sample frequencies if metric == 'rbf': self.w = np.sqrt(2 * gamma) * torch.empty( (D, d), device=device).normal_().double() elif metric == 'laplace': self.w = cauchy.rvs(scale=gamma, size=(D, d)) self.w = torch.from_numpy(self.w).double().to(device) self.w_inv = torch.pinverse(self.w) # Sample offsets self.b = 2 * np.pi * torch.empty( (D, ), device=device, dtype=torch.double).random_(0, to=1)
def cauchy_distribution(select_size, asked=rvs, x=0): if asked == rvs: return cauchy.rvs(size=select_size) elif asked == pdf: return cauchy.pdf(x) elif asked == cdf: return cauchy.cdf(x)
def randwalk(lgth, stiff, start=[0., 0.], step=1., adrift=0., anoise=.2, dist="const"): """generates a 2d random walk with variable angular correlation and optional constant (+noise) angular drift. Arguments: walk length "lgth", stiffness factor "stiff" - consecutive orientations are derived by adding stiff*(random number in [-1,1]). Parameters "adrift" and "anoise" add a constant drift angle adrift modulated by a noise factor adrift*(random number in [-1,1]). The dist parameter accepts non-constant step length distributions (right now, only cauchy/gaussian distributed random variables)""" rw = [ list(start) ] #user provided initial coordinates: make (reasonably) sure it's a nested list type. ang = [0] #step lengths are precalculated for each step to account for if dist == "cauchy": steps = cauchy.rvs(size=lgth) elif dist == "norm": steps = norm.rvs(size=lgth) else: steps = array([step] * lgth) #Overkill for constant step length ;) #first generate angular progression vila cumulative sum of increments (random/stiff + drift terms) angs = cumsum(stiff * uniform.rvs(size=lgth, loc=-1, scale=2) + adrift * (1. + anoise * uniform.rvs(size=lgth, loc=-1, scale=2))) #x/y trace via steplength and angle for each step, some array reshuffling (2d conversion and transposition) rw = concatenate([ concatenate([array(start[:1]), cumsum(steps * sin(angs)) + start[0]]), concatenate([array(start)[1:], cumsum(steps * cos(angs)) + start[1]]) ]).reshape(2, -1).transpose() return rw, angs
def solve(n): x_cauchy = cauchy.rvs(size=n * M, loc=a, scale=s).reshape(M, n) x_median = np.apply_along_axis(np.median, 1, x_cauchy) x_mad = np.apply_along_axis(mad, 1, x_cauchy) mu = -x_median / x_mad ka = 1 / x_mad ci_par = a_ci(ci_parametric(mu, gamma), x_median, x_mad) ci_non_par = a_ci(ci_non_parametric(mu, gamma), x_median, x_mad) x_med = mean(x_median) empirical_cl_non_par, empirical_cl_par, sd_width_non_par, sd_width_par, width_non_par, width_par = \ test_ci(ci_non_par, ci_par, x_med) print("LOCATION | N = {0:5}; Non-parametric: width = {1} ({2}); empirical CL = {3}".format( n, mean(width_non_par), sd_width_non_par, empirical_cl_non_par)) print("LOCATION | N = {0:5}; Parametric: width = {1} ({2}); empirical CL = {3}".format( n, mean(width_par), sd_width_par, empirical_cl_par)) # --------------------------------- ci_par = s_ci(ci_parametric(ka, gamma), x_mad) ci_non_par = s_ci(ci_non_parametric(ka, gamma), x_mad) mad_ = np.exp(mean(np.log(np.abs(x_cauchy - x_med)))) empirical_cl_non_par, empirical_cl_par, sd_width_non_par, sd_width_par, width_non_par, width_par = \ test_ci(ci_non_par, ci_par, mad_) print("SCALE | N = {0:5}; Non-parametric: width = {1} ({2}); empirical CL = {3}".format( n, mean(width_non_par), sd_width_non_par, empirical_cl_non_par)) print("SCALE | N = {0:5}; Parametric: width = {1} ({2}); empirical CL = {3}".format( n, mean(width_par), sd_width_par, empirical_cl_par)) print print
async def iot_handler(websocket, path): await websocket.send(motd) # mode_query = "What kind of sensor would you like? (temperature,occupancy)" # await websocket.send(mode_query) # mode = await websocket.recv() mode = "all" rooms = get_simulated_rooms() while True: await asyncio.sleep(erlang.rvs(1, 0, size=1)) room = random.choice(list(rooms.keys())) dat = {"time": datetime.now().isoformat()} if mode.startswith(("all", "tem")): dat["temperature"] = cauchy.rvs(loc=rooms[room]["loc"], scale=rooms[room]["scale"], size=1).tolist() if mode.startswith(("all", "occ")): dat["occupancy"] = poisson.rvs(rooms[room]["occ"], size=1).tolist() if mode.startswith(("all", "co")): dat["co2"] = gamma.rvs(rooms[room]["co"], size=1).tolist() await websocket.send(json.dumps({room: dat}))
async def iot_handler(websocket, path): """ generate simulated data for each room and sensor """ await websocket.send(motd) mode = "all" rooms = get_simulated_rooms() print("IoT simulator connected to", websocket.remote_address) while True: await asyncio.sleep(erlang.rvs(1, 0, size=1).item()) room = random.choice(list(rooms.keys())) dat = {"time": datetime.now().isoformat()} if mode.startswith(("all", "tem")): dat["temperature"] = cauchy.rvs( loc=rooms[room]["loc"], scale=rooms[room]["scale"], size=1 ).tolist() if mode.startswith(("all", "occ")): dat["occupancy"] = poisson.rvs(rooms[room]["occ"], size=1).tolist() if mode.startswith(("all", "co")): dat["co2"] = gamma.rvs(rooms[room]["co"], size=1).tolist() try: await websocket.send(json.dumps({room: dat})) except websockets.exceptions.ConnectionClosedOK: print("Closing connection to", websocket.remote_address) break
def gen_free_energy(distribution, spread=2.0, location=0.0, size=1): """ Generate example free energies from a specified distribution Parameters ---------- distribution: str the distribution from which to draw samples size: int number of samples to generate spread: list or float the spread/scale parameter of the distribution, e.g. standard deviation for gaussian location: list of float the center of the distribution, e.d. Returns ------- numpy.ndarray samples of free energy with dimentions equal to spread and location """ if distribution.lower() == 'gaussian': f_true = np.random.normal(loc=location, scale=spread, size=size) elif distribution.lower() == 'cauchy': from scipy.stats import cauchy f_true = cauchy.rvs(loc=location, scale=spread, size=size) else: raise Exception( 'The distribution must be either "gaussain" or "cauchy".') return f_true
def rvs(cls, alpha: float, beta: float, gamma=1., delta=0., pm=1, size: Numeric = 1): cls._check_parameters(alpha, beta, gamma, pm) delta, gamma = cls._parameterize(alpha, delta, gamma, beta, pm) if np.isclose(alpha, 1) and np.isclose(beta, 0): z = cauchy.rvs(size=size) else: theta = np.pi * (np.random.uniform(size=size) - 0.5) w = np.random.standard_exponential(size) bt = beta * tanpi2(alpha) t0 = min(max(-PI2, np.arctan(bt) / alpha), PI2) at = alpha * (theta + t0) c = (1 + bt**2)**(1 / (2 * alpha)) z = (c * np.sin(at) * (np.cos(theta)**(-1 / alpha)) * ((np.cos(theta - at) / w)**((1 - alpha) / alpha)) - bt) return z * gamma + delta
def mod_cauchy(loc,scale,size,cut): samples=cauchy.rvs(loc=loc,scale=scale,size=3*size) samples=samples[abs(samples)<cut] if len(samples)>=size: samples=samples[:size] else: samples=mod_cauchy(loc,scale,size,cut) ##Only added for the very unlikely case that there are not enough samples after the cut. return samples
def distributions(size): n = norminvgauss.rvs(1, 0, size=size) l = laplace.rvs(size=size, scale=1 / m.sqrt(2), loc=0) p = poisson.rvs(10, size=size) c = cauchy.rvs(size=size) u = uniform.rvs(size=size, loc=-m.sqrt(3), scale=2 * m.sqrt(3)) counted_distributions = [n, l, p, c, u] return counted_distributions
def generate_panel(N, T, k, q, d, lam): beta = [] rho = [] # cov_rho = [] for l in range(k): beta.append(generate_beta(d - 1)) temp_rho = generate_rho(q, lam) rho.append(temp_rho) panel1 = np.array([[[1.0 for fea in range(d)] for t in range(T)] for i in range(N)]) panel2 = np.array([[[1.0 for fea in range(d)] for t in range(T)] for i in range(N)]) mean = [0 for i in range(d - 2)] cov = np.identity(d - 2) # mean vector of each individual mean_individual = [] cluster = np.random.randint(0, k, N) for i in range(N): temp = np.random.multivariate_normal(mean, cov, 1)[0] temp = temp / np.sqrt(square_sum(temp)) * np.random.uniform(0, 5) mean_individual.append(temp) #for i in range(N): # l2 = square_sum(mean_individual[i]) # norm = random.uniform(0, 10) # for dim in range(d-1): # mean_individual[i][dim] *= math.sqrt(norm / l2) # model of each individual mean_error = [0 for i in range(T)] for i in range(N): # Gaussian error mean_i = np.random.multivariate_normal( mean_individual[i], cov * square_sum(mean_individual[i]), T) error_i = [] error_basic = np.random.normal(0, 1, T) for t in range(T): error_i.append(error_basic[t]) for tt in range(min(t - 1, q)): error_i[t] += rho[cluster[i]][tt] * error_i[t - tt - 1] for t in range(T): panel1[i][t][0:-2] = mean_i[t] panel1[i][t][d - 1] = np.dot(panel1[i][t][0:d - 1], beta[cluster[i]]) + error_i[t] # Cauchy error error_i = [] error_basic = cauchy.rvs( 0, 2, T ) # errors drawn from Cauchy distribution with x0 = 0 and gamma = 2 for t in range(T): error_i.append(error_basic[t]) for tt in range(min(t - 1, q)): error_i[t] += rho[cluster[i]][tt] * error_i[t - tt - 1] for t in range(T): panel2[i][t][0:-2] = mean_i[t] panel2[i][t][d - 1] = np.dot(panel2[i][t][0:d - 1], beta[cluster[i]]) + error_i[t] return panel1, panel2
def _generate_uniform_planes(self, size, input_dim): """ Generate uniformly distributed hyperplanes and return it as a 2D numpy array. """ if (self.lazy): self.uniform_planes = cauchy.rvs( size=[size, self.hash_size, self.points.shape[1]]) else: self.uniform_planes = np.random.randn(size, self.hash_size, input_dim)
def cauchyFunc(): for i in range(len(size)): n = size[i] fig, ax = plt.subplots(1, 1) ax.set_title("Распределение Коши, n = " + str(n)) x = np.linspace(cauchy.ppf(0.01), cauchy.ppf(0.99), 100) ax.plot(x, cauchy.pdf(x), 'b-', lw=5, alpha=0.6) r = cauchy.rvs(size=n) ax.hist(r, density=True, histtype='stepfilled', alpha=0.2) plt.show()
def generate_signals(self, sig, noise_type = 4): ''' generate noises for given sig and noise type ''' # TODO: add different noises if noise_type == 0: #gaussian noise = np.random.normal(size=sig.shape) elif noise_type == 1: # cauchy from scipy.stats import cauchy noise = cauchy.rvs(size=sig.shape) elif noise_type == 2: # poisson noise = np.random.poisson(size=sig.shape) elif noise_type == 3: # speckle: Multiplicative noise using out = image + n*image,where n is gaussion noise with specified mean & variance. noise = np.random.randn(size=sig.shape)*sig else:# mix of 0 & 1 from scipy.stats import cauchy noise = np.random.normal(size=sig.shape) + cauchy.rvs(size=sig.shape) return noise
def generate_data(room: T.Dict[str, float]) -> T.Dict[str, T.Union[str, float]]: """ generate simulated data """ return { "time": datetime.now().isoformat(), "temperature": cauchy.rvs(loc=room["loc"], scale=room["scale"], size=1).tolist(), "occupancy": poisson.rvs(room["occ"], size=1).tolist(), "co2": gamma.rvs(room["co"], size=1).tolist(), }
def get_system_noise(self): """v_t vector""" data_noise = np.zeros((self.ydim, self.PARTICLES_NUM), dtype=np.float64) for i in xrange(self.ydim): data_noise[i, :] = cauchy.rvs(loc=[0] * self.PARTICLES_NUM, scale=np.power( 10, self.particles[self.ydim]), size=self.PARTICLES_NUM) data_noise[data_noise == float("-inf")] = -1e308 data_noise[data_noise == float("inf")] = 1e308 parameter_noises = np.zeros((self.pdim, self.PARTICLES_NUM), dtype=np.float64) for i in xrange(self.pdim): parameter_noises[i, :] = cauchy.rvs( loc=0, scale=self.nois_sh_parameters[i], size=self.PARTICLES_NUM) return np.vstack((data_noise, parameter_noises))
def cauchyNumbers(): for size in sizes: fig, ax = plt.subplots(1, 1) ax.hist(cauchy.rvs(size=size), histtype='stepfilled', alpha=0.5, color='blue', density=True) x = np.linspace(cauchy().ppf(0.01), cauchy().ppf(0.99), 100) ax.plot(x, cauchy().pdf(x), '-') ax.set_title('CauchyNumbers n = ' + str(size)) ax.set_xlabel('CauchyNumbers') ax.set_ylabel('density') plt.grid() plt.show() return
def random_growth(self): """ Cauchy distribution with barriers at -30 and 15 percent. """ growth = cauchy.rvs(size=1, loc=self.config['growth_mu'], scale=self.config['growth_sigma'])[0] if growth < -0.30: return -0.30 elif growth > 0.15: return 0.15 else: return growth
def dg_cauchy(n=150, num_groups=40, group_size=40, p=1600, ro=0.5, num_non_zero_coef_per_group=8, num_non_zero_groups=7, loc=0, scale=3): """ Generate a dataset formed by groups of variables. All the groups have the same size. num_non_zero_coef_per_group num_non_zero_groups :param n: number of observations :param num_groups: number of groups :param group_size: size of the groups :param p: number of variables :param ro: correlation between variables inside a group :param num_non_zero_coef_per_group: controls how many significant variables we want :param num_non_zero_groups: controls how many groups we want to store significant variables :param loc: location parameter for cauchy error :param scale: scale parameter for cauchy error :return: x, y index If num_non_zero_coef_per_group=8 and num_non_zero_groups=7 we will have 8 groups with 7 significant variables, and num_groups-num_non_zero_coef_per_group groups with all non-significant variables The error distribution is a student t with 3 degrees of freedom """ p = num_groups * group_size group_levels = np.arange(1, (num_groups + 1), 1) num_non_zeros_per_group = [0] * num_groups num_non_zeros_per_group[0:num_non_zero_groups] = [ num_non_zero_coef_per_group ] * num_non_zero_groups group_index = np.repeat(group_levels, [group_size] * num_groups, axis=0) s = np.zeros((p, p)) for level in group_levels: s[(level - 1) * group_size:level * group_size, (level - 1) * group_size:level * group_size] = ro np.fill_diagonal(s, 1) x = np.random.multivariate_normal(np.repeat(0, p), s, n) betas = np.zeros(p) for i in range(num_groups): betas[(i * group_size):((i + 1) * group_size)] = np.arange( 1, group_size + 1, 1) for i in range(num_groups): betas[((group_levels[i] - 1) * group_size + num_non_zeros_per_group[i]):group_levels[i] * group_size] = 0 e = cauchy.rvs(loc=loc, scale=scale, size=n) y = np.dot(x, betas) + e data = dict(x=x, y=y, true_beta=betas, index=group_index) logger.debug('Function finished without errors') return data
def selection(mu, sigma, size, distribution): if distribution == Distribution.NORMAL: return norm.rvs(mu, sigma, size) elif distribution == Distribution.CAUCHY: return cauchy.rvs(mu, sigma, size) elif distribution == Distribution.LAPLACE: return laplace.rvs(mu, sigma, size) elif distribution == Distribution.POISSON: return poisson.rvs(mu, size=size) elif distribution == Distribution.UNIFORM: return uniform.rvs(mu, sigma, size) else: return None
def make_positive_cauchy_rv(N, mean, var): """ Makes a list of N POSITIVE random variates drawn from a cauchy dist with given mean and var (or whatever pars define the dist). """ omega = [] for i in range(N): temp = -1 while temp < 0: temp = float(cauchy.rvs(size=1, scale=var_omega, loc=mean_omega)) omega.append(temp) return omega
def dataGeneration(self, x1=-5, x2=5, N=100): c = Cauchy_regression1.c d = Cauchy_regression1.d np.random.seed() x = np.random.uniform(x1, x2, N) s_rand = np.zeros(N) for i in range(len(x)): gamma = abs(c * x[i] + d) s_rand[i] = cauchy.rvs(loc=0, scale=gamma, size=1) y = self.a * x + self.b + s_rand return np.append(x, y).reshape(2, N).transpose()
def Cauchy(): for s in size: density = cauchy() histogram = cauchy.rvs(size=s) fig, ax = plt.subplots(1, 1) ax.hist(histogram, density=True, alpha=0.6) x = np.linspace(density.ppf(0.01), density.ppf(0.99), 100) ax.plot(x, density.pdf(x), LINE_TYPE, lw=1.5) ax.set_xlabel("CAUCHY") ax.set_ylabel("DENSITY") ax.set_title("SIZE: " + str(s)) plt.grid() plt.show()
def make_positive_cauchy_rv(N, mean, var): """ Makes a list of N POSITIVE random variates drawn from a cauchy dist with given mean and var (or whatever pars define the dist). """ omega = [] for i in range(N): temp = -1 while temp < 0: temp = float(cauchy.rvs(size = 1, scale = var_omega, loc = mean_omega)) omega.append(temp) return omega
def test_train(self): gsm = GSM(1, 10) gsm.train(laplace.rvs(size=[1, 10000]), max_iter=100, tol=-1) p = kstest(gsm.sample(10000).flatten(), laplace.cdf)[1] # test whether GSM faithfully reproduces Laplace samples self.assertTrue(p > 0.0001) gsm = GSM(1, 6) gsm.train(cauchy.rvs(size=[1, 10000]), max_iter=100, tol=-1) # test for stability of training self.assertTrue(not any(isnan(gsm.scales)))
def rvs(cls, alpha: float, beta: float, gamma=1., delta=0., pm=1, size: Numeric = 1): cls._check_parameters(alpha, beta, gamma, pm) delta, gamma = cls._form_parameters(alpha, delta, gamma, beta, pm) if np.isclose(alpha, 1) and np.isclose(beta, 0): z = cauchy.rvs(size=size) else: theta = np.pi * (np.random.uniform(size=size) - 0.5) w = np.random.standard_exponential(size) bt = beta * tanpi2(alpha) t0 = min(max(-PI2, np.arctan(bt) / alpha), PI2) at = alpha * (theta + t0) c = (1 + bt ** 2) ** (1 / (2 * alpha)) z = c * np.sin(at) \ * (np.cos(theta - at) / w) ** (1 / alpha - 1) \ / np.cos(theta) ** (1 / alpha) \ - bt return z * gamma + delta
N = 1000 x = 4 Inf = float("inf") # scipy.integrateでの積分 h1 = lambda t: t * norm(loc=x).pdf(t) * cauchy.pdf(t) h2 = lambda t: norm(loc=x).pdf(t) * cauchy.pdf(t) num = scipy.integrate.quad(h1, -Inf, Inf)[0] den = scipy.integrate.quad(h2, -Inf, Inf)[0] I = num / den print("scipy.integrate:", I) # (1) コーシー分布からサンプリングするモンテカルロ積分の収束テスト # 分子も分母も同じサンプルを使用すると仮定 theta = cauchy.rvs(size=N) num = theta * norm(loc=x).pdf(theta) den = norm(loc=x).pdf(theta) # 分母に0がくるサンプルを削除 num = num[den != 0] den = den[den != 0] Ndash = len(num) y = num / den estint = (np.cumsum(num) / np.arange(1, Ndash + 1)) / \ (np.cumsum(den) / np.arange(1, Ndash + 1)) esterr = np.sqrt(np.cumsum((y - estint) ** 2)) / np.arange(1, Ndash + 1) plt.subplot(2, 1, 1) plt.plot(estint, color='red', linewidth=2)
def initialize(self, X=None, method='data'): """ Initializes parameter values with more sensible values. @type X: array_like @param X: data points stored in columns @type method: string @param method: type of initialization ('data', 'gabor' or 'random') """ if self.noise: L = self.A[:, :self.num_visibles] if method.lower() == 'data': # initialize features with data points if X is not None: if X.shape[1] < self.num_hiddens: raise ValueError('Number of data points to small.') else: # whitening matrix val, vec = eig(cov(X)) # whiten data X_ = dot(dot(diag(1. / sqrt(val)), vec.T), X) # sort by norm in whitened space indices = argsort(sqrt(sum(square(X_), 0)))[::-1] # pick 25% largest data points and normalize X_ = X_[:, indices[:max([X.shape[1] / 4, self.num_hiddens])]] X_ = X_ / sqrt(sum(square(X_), 0)) # pick first basis vector at random A = X_[:, [randint(X_.shape[1])]] for _ in range(self.num_hiddens - 1): # pick vector with large angle to all other vectors A = hstack([ A, X_[:, [argmin(max(abs(dot(A.T, X_)), 0))]]]) # orthogonalize and unwhiten A = dot(sqrtmi(dot(A, A.T)), A) A = dot(dot(vec, diag(sqrt(val))), A) self.A = A elif method.lower() == 'gabor': # initialize features with Gabor filters if self.subspaces[0].dim > 1 and not mod(self.num_hiddens, 2): for i in range(self.num_hiddens / 2): G = gaborf(self.num_visibles) self.A[:, 2 * i] = real(G) self.A[:, 2 * i + 1] = imag(G) else: for i in range(len(self.subspaces)): self.A[:, i] = gaborf(self.num_visibles, complex=False) elif method.lower() == 'random': # initialize with Gaussian white noise self.A = randn(num_visibles, num_hiddens) elif method.lower() in ['laplace', 'student', 'cauchy', 'exponpow']: if method.lower() == 'laplace': # approximate multivariate Laplace with GSM samples = randn(self.subspaces[0].dim, 10000) samples = samples / sqrt(sum(square(samples), 0)) samples = laplace.rvs(size=[1, 10000]) * samples elif method.lower() == 'student': samples = randn(self.subspaces[0].dim, 50000) samples = samples / sqrt(sum(square(samples), 0)) samples = t.rvs(2., size=[1, 50000]) * samples elif method.lower() == 'exponpow': exponent = 0.8 samples = randn(self.subspaces[0].dim, 200000) samples = samples / sqrt(sum(square(samples), 0)) samples = gamma(1. / exponent, 1., (1, 200000))**(1. / exponent) * samples else: samples = randn(self.subspaces[0].dim, 100000) samples = samples / sqrt(sum(square(samples), 0)) samples = cauchy.rvs(size=[1, 100000]) * samples if self.noise: # ignore first subspace gsm = GSM(self.subspaces[1].dim, self.subspaces[1].num_scales) gsm.train(samples, max_iter=200, tol=1e-8) for m in self.subspaces[1:]: m.scales = gsm.scales.copy() else: # approximate distribution with GSM gsm = GSM(self.subspaces[0].dim, self.subspaces[0].num_scales) gsm.train(samples, max_iter=200, tol=1e-8) for m in self.subspaces: m.scales = gsm.scales.copy() else: raise ValueError('Unknown initialization method \'{0}\'.'.format(method)) if self.noise: # don't initialize noise covariance self.A[:, :self.num_visibles] = L
def random(self,n): if hasattr(n,'__len__'): n = len(n) return np.mod(cauchy.rvs(loc=self.p[-1],scale=self.p[0]/TWOPI,size=n),1)
# posterior theta_post = theta_post_unnorm / Z # approximate the posterior distribution theta_mean = trapz(theta * theta_post, theta) theta_var = trapz((theta-theta_mean)**2 * theta_post, theta) theta_post_approx = norm(theta_mean, np.sqrt(theta_var)) # get samples from approximate posterior theta_samples = theta_post_approx.rvs(size=N_SAMP) # sample the posterior predictive distribution for y6 y6_samples = np.empty(N_SAMP, dtype=float) for ith in xrange(N_SAMP): y6_samples[ith] = cauchy.rvs(loc=theta_samples[ith], scale=1) # plot fig, axes = plt.subplots(1, 3) plt.sca(axes[0]) plt.plot(theta, theta_post, label='actual') plt.plot(theta, theta_post_approx.pdf(theta), label='approx') plt.legend() plt.xlabel('theta') plt.ylabel('P(theta | y1...y5)') plt.sca(axes[1]) plt.hist(theta_samples) plt.xlabel('theta') plt.sca(axes[2]) plt.hist(y6_samples) plt.xlabel('y6')
def sample_episodes(numepisodes, physics): episodes = [] total_events, num_reasonable_events = 0, 0 for epinum in xrange(numepisodes): events = [] detections = [] assocs = [] # first generate all the events numevents = poisson.rvs( physics.lambda_e * 4 * pi * physics.R ** 2 * physics.T ) for evnum in xrange(numevents): # longitude is uniform from -180 to 180 evlon = uniform.rvs(-180, 360) # sin(latitude) is uniform from -1 to 1 evlat = degrees(arcsin(uniform.rvs(-1, 2))) # magnitude has an exponential distribution as per Gutenberg-Richter law while True: evmag = expon.rvs(physics.mu_m, physics.theta_m) # magnitude saturates at some maximum value, # re-sample if we exceed the max if evmag > physics.gamma_m: continue else: break # time is uniform evtime = uniform.rvs(0, physics.T) event = Event(evlon, evlat, evmag, evtime) events.append(event) truedets = [] #print ("event mag %f" % event.mag) # for each event generate its set of true detections for stanum, station in enumerate(STATIONS): dist = compute_distance((station.lon, station.lat), (event.lon, event.lat)) sta_to_ev_az = compute_azimuth((station.lon, station.lat), (event.lon, event.lat)) detprob = logistic(physics.mu_d0[stanum] + physics.mu_d1[stanum] * event.mag + physics.mu_d2[stanum] * dist) #print ("stanum %d dist %f detprob %f" % (stanum, dist, detprob)) # is detected ? if bernoulli.rvs(detprob): dettime = laplace.rvs(event.time + compute_travel_time(dist) + physics.mu_t[stanum], physics.theta_t[stanum]) # Note: the episode only has detections within the first T # seconds. Late arriving detections will not be available. if dettime < physics.T: degdiff = laplace.rvs(physics.mu_z[stanum], physics.theta_z[stanum]) detaz = (sta_to_ev_az + degdiff + 360) % 360 detslow = laplace.rvs(compute_slowness(dist) + physics.mu_s[stanum], physics.theta_s[stanum]) while True: # resample if the detection amplitude is infinite try: detamp = exp(norm.rvs(physics.mu_a0[stanum] + physics.mu_a1[stanum] * event.mag + physics.mu_a2[stanum] * dist, physics.sigma_a[stanum])) except FloatingPointError: continue # disallow zero or infinite amplitudes if detamp == 0 or isinf(detamp): continue break truedets.append(len(detections)) detections.append(Detection(stanum, dettime, detaz, detslow, detamp)) assocs.append(truedets) total_events += 1 if len(truedets) >= 2: num_reasonable_events += 1 # now generate the false detections for stanum in xrange(len(STATIONS)): numfalse = poisson.rvs(physics.lambda_f[stanum] * physics.T) for dnum in xrange(numfalse): dettime = uniform.rvs(0, physics.T) detaz = uniform.rvs(0, 360) detslow = uniform.rvs(compute_slowness(180), compute_slowness(0) - compute_slowness(180)) while True: # resample if the detection amplitude is infinite try: detamp = exp(cauchy.rvs(physics.mu_f[stanum], physics.theta_f[stanum])) except FloatingPointError: continue # disallow zero or infinite amplitudes if detamp == 0 or isinf(detamp): continue break detections.append(Detection(stanum, dettime, detaz, detslow, detamp)) episodes.append(Episode(events, detections, assocs)) print ("{:d} events generated".format(total_events)) print ("{:.1f} % events have at least two detections" .format(100 * num_reasonable_events / total_events)) return episodes
SF = [] CR = [] F = [] p = [] u = [] alldeltaf = [] deltaf = [] for var1 in range(0, N): r = random.randrange(0, N) cr = rand_normal(MCR[var1], math.sqrt(0.1)) if cr > 1: cr = 1 if cr < 0: cr = 0 CR.append(copy.deepcopy(cr)) f = cauchy.rvs() while True: if f > 1: f = 1.0 break if f > 0: break f = cauchy.rvs() # print f F.append(copy.deepcopy(f)) #コーシー乱数発生よく分かっておらず そもそもなんでコーシー乱数? p = random.uniform(2.0/NP, 0.2) # generate_trial_vector(x, P, A, p, N, CR, F) u.append(generate_trial_vector(population[var1], population, A, p, N, CR[var1], F[var1]))#current to pbest/1/bin test_count = 0 # print len(u)