def _to_cartesian(self): if (self.verbose): print(" convert to cartesian coordinates ...") r = self.r r2 = r**2 R1 = random.rand(self.N) R2 = random.rand(self.N) self.x = (1 - 2 * R1) * self.r self.y = sqrt(r2 - self.x**2) * cos(2 * pi * R2) self.z = sqrt(r2 - self.x**2) * sin(2 * pi * R2) if (self.ani): R1 = random.rand(self.N) vphi = self.vt * cos(2 * pi * R1) vtheta = self.vt * sin(2 * pi * R1) theta = numpy.arccos(self.z / r) phi = numpy.arctan2(self.y, self.x) self.vx = self.vr * sin(theta) * cos(phi) + vtheta * cos( theta) * cos(phi) - vphi * sin(phi) self.vy = self.vr * sin(theta) * sin(phi) + vtheta * cos( theta) * sin(phi) + vphi * cos(phi) self.vz = self.vr * cos(theta) - vtheta * sin(theta) else: v2 = self.v**2 R1 = random.rand(self.N) R2 = random.rand(self.N) self.vx = (1 - 2 * R1) * self.v self.vy = sqrt(v2 - self.vx**2) * cos(2 * pi * R2) self.vz = sqrt(v2 - self.vx**2) * sin(2 * pi * R2)
def _to_cartesian(self): if (self.verbose): print " convert to cartesian coordinates ..." r = self.r r2 = r**2 R1 = random.rand(self.N) R2 = random.rand(self.N) self.x = (1-2*R1)*self.r self.y = sqrt(r2 - self.x**2)*cos(2*pi*R2) self.z = sqrt(r2 - self.x**2)*sin(2*pi*R2) if (self.ani): R1 = random.rand(self.N) vphi = self.vt*cos(2*pi*R1) vtheta = self.vt*sin(2*pi*R1) theta = numpy.arccos(self.z/r) phi = numpy.arctan2(self.y, self.x) self.vx = self.vr*sin(theta)*cos(phi) + vtheta*cos(theta)*cos(phi) - vphi*sin(phi) self.vy = self.vr*sin(theta)*sin(phi) + vtheta*cos(theta)*sin(phi) + vphi*cos(phi) self.vz = self.vr*cos(theta) - vtheta*sin(theta) else: v2 = self.v**2 R1 = random.rand(self.N) R2 = random.rand(self.N) self.vx = (1-2*R1)*self.v self.vy = sqrt(v2 - self.vx**2)*cos(2*pi*R2) self.vz = sqrt(v2 - self.vx**2)*sin(2*pi*R2)
def main(): train_sample_num = 1000 test_sample_num = 200 label_ratio = 1.32 data_name = "fake1" dim = 20 mean_pos = random.rand(dim) * 10 - 5 mean_neg = random.rand(dim) * 10 - 5 temp = random.rand(dim, dim) * 2 var_pos = np.dot(temp, temp.transpose()) temp = random.rand(dim, dim) * 2 var_neg = np.dot(temp, temp.transpose()) var_noise = random.rand(dim, dim) * 1.5 var_noise = np.dot(var_noise, var_noise.transpose()) data1 = np.random.multivariate_normal(mean_neg, var_neg, [int(train_sample_num * label_ratio)]) + np.random.multivariate_normal(np.zeros(dim), var_noise, [int(train_sample_num * label_ratio)]) data2 = np.random.multivariate_normal(mean_pos, var_pos, [train_sample_num]) + np.random.multivariate_normal(np.zeros(dim), var_noise, [train_sample_num]) data3 = np.random.multivariate_normal(mean_neg, var_neg, [int(test_sample_num * label_ratio)]) + np.random.multivariate_normal(np.zeros(dim), var_noise, [int(test_sample_num * label_ratio)]) data4 = np.random.multivariate_normal(mean_pos, var_pos, [test_sample_num]) + np.random.multivariate_normal(np.zeros(dim), var_noise, [test_sample_num]) #print(data3, data4) pickle.dump( { "train_set": np.vstack([data1, data2]), "test_set": np.vstack([data3, data4]), "train_label": np.vstack([np.zeros([int(train_sample_num * label_ratio), 1]), np.ones([train_sample_num, 1])]), "test_label": np.vstack([np.zeros([int(test_sample_num * label_ratio), 1]), np.ones([test_sample_num, 1])]) }, open('../data/' + data_name, 'wb') ) save_plot(data1, data2, data3, data4, data_name)
def _sample_k(self, jbin): # y(x) = y_j # cdf(x) = ycum_j + y_j*(x - x_j) # x = R/y_j + x_j N = self.N iter = 0 c = numpy.zeros(N, dtype='bool') + True if self.mod.multi: sta, end = self.Nstart[jbin], self.Nend[jbin] c[0:sta] = False c[end::] = False ycum = self.ycum[:, c] rtmp = self.r[c] ftmp = self.phihat[c] y = self.y[:, c] x = self.x[:, c] if self.mod.multi: c = numpy.zeros(self.Nj[jbin], dtype='bool') + True while (sum(c) > 0): nc = sum(c) ycum = ycum[:, c] rtmp = rtmp[c] ftmp = ftmp[c] y = y[:, c] x = x[:, c] R = random.rand(nc) * ycum[-1] xtmp = numpy.zeros(nc) P = numpy.zeros(nc) for j in range(self.nx): cr = (R >= ycum[j]) & (R < ycum[j + 1]) if (sum(cr) > 0): xtmp[cr] = (R[cr] - ycum[j, cr]) / y[j, cr] + x[j, cr] P[cr] = y[j, cr] R2 = random.rand(nc) * P f = self._pdf_k32(rtmp, ftmp, xtmp, jbin) c = (R2 > f) if (iter == 0): xsamp = xtmp[~c] r = rtmp[~c] else: xsamp = numpy.r_[xsamp, xtmp[~c]] r = numpy.r_[r, rtmp[~c]] iter += 1 # Assign final velocities and update r because of shuffling self.k = numpy.r_[self.k, xsamp**(2. / 3)] self.v = numpy.r_[self.v, sqrt(2 * xsamp**(2 / 3) * self.mod.s2)] self.rfinal = numpy.r_[self.rfinal, r]
def cftp(P, T=1, u=None): """Single draw with coupling from the past Parameters -------------- P : (n, n) ndarray Transition matrix T : int Number of iterations u : (t <= T, ) ndarray Draws from a uniform distribution Returns ----------- X : int State T : int Number of iterations until convergence """ if u is not None: m = len(u) ## If u is shorter than T then add new u if T - m > 0: u = sp.concatenate((random.rand(T - m), u)) ## If u is too long, then remove extra entries elif T - m < 0: u = u[:T] ## Otherwise T = m else: pass ## u is not defined, add new u else: u = random.rand(T) m = 0 ## Define state space from P S = sp.r_[0:P.shape[0]] ## P is defined ## Define map from P matrix def phi(S, u): return sp.unique(sp.array([multinomial(u, P[i, ]) for i in S])) ## Initialize X_T = S X = S ## I number them 0 to T because I append ## new u's to the front. for t in range(0, T): X = phi(X, u[t]) # If more than one state in X, call cftp again # Double the length of the chain if len(X) > 1: X, T = cftp(P, T*2, u) return X, T
def create_pos_matrix(size): M = 10 * random.rand(size, size) P = np.matmul(M, M.transpose()) eigval, eigvec = np.linalg.eig(P) while (min(eigval) < 0.5): M = 10 * random.rand(size, size) P = np.matmul(M, M.transpose()) eigval, eigvec = np.linalg.eig(P) return P
def randSymMat(size): """ Generates a random symmetric matrix of size M x M. """ A = random.rand(size, size) Q, R = np.linalg.qr(A) v = random.rand(size) D = np.diag(v) return np.dot(Q, np.dot(D, Q.T))
def split_dataset(fl, nofl, test_size=0.3, mode='random'): """ splits two datasets of flaring and non-flaring regions with given ratio of non-flaring (N) to flaring regions (P) into a training and a test set with same ratio of N/P Args: fl: 2D np array (Nobs, Nattr) of flaring regions nofl: 2D np array (Nobs, Nattr) of non-flaring regions test_size: float, the ratio of number of test observations to total number of observations mode: string, either 'random' or 'contiguous', indicates whether the fl, nofl datasets will be split randomly to form training and test sets or by preserving the order of the observations (for example first 7 in training set and next 3 in test set). Contiguous mode should be used for data that their ordering in non-arbitrary (e.g. nth observation may be correlated with n+1 observation). See also '3.1.3. A note on shuffling' at: http://scikit-learn.org/stable/modules/cross_validation.html Returns: train, test: 2D np arrays to be used for training and testing ytrain, ytest: 1D np arrays that contain the corresponding labels """ yfl = np.ones(np.shape(fl)[0]); ynofl = np.zeros(np.shape(nofl)[0]) Nfl = np.shape(fl)[0]; Nnofl = np.shape(nofl)[0] if mode == 'random': fltrain, fltest, ytrain, ytest = train_test_split(fl, yfl, test_size=test_size) nofltrain, nofltest, ynotrain, ynotest = train_test_split(nofl, ynofl, test_size=test_size) train = np.concatenate((fltrain, nofltrain), axis=0) test = np.concatenate((fltest, nofltest), axis=0) ytrain = np.concatenate((ytrain, ynotrain), axis=0) ytest = np.concatenate((ytest, ynotest), axis=0) return train, test, ytrain, ytest elif mode == 'contiguous': Nfltrain = int(round(Nfl*(1.0-test_size))); Nfltest = Nfl - Nfltrain Nnofltrain = int(round(Nnofl*(1.0-test_size))); Nnofltest = Nnofl - Nnofltrain fl = np.concatenate((fl, fl[0:Nfltrain-1]), axis=0) nofl = np.concatenate((nofl, nofl[0:Nnofltrain-1]), axis=0) index = int(round(Nfl*random.rand())) fltrain = fl[index:index+Nfltrain, :]; ytrain = np.ones(Nfltrain) # temp = index+Nnofltrain-Nnofl; start_index = max(0, temp) fltest = np.concatenate((fl[0:index, :], fl[index+Nfltrain:Nfl,:]), axis=0) ytest = np.ones(Nfltest) index = int(round(Nnofl*random.rand())) nofltrain = nofl[index:index+Nnofltrain, :]; ynotrain = np.zeros(Nnofltrain) # temp = index+Nnofltrain-Nnofl; start_index = max(0, temp) nofltest = np.concatenate((nofl[0:index, :], nofl[index+Nnofltrain:Nnofl,:]), axis=0) ynotest = np.zeros(Nnofltest) train = np.concatenate((fltrain, nofltrain), axis=0) test = np.concatenate((fltest, nofltest), axis=0) ytrain = np.concatenate((ytrain, ynotrain), axis=0) ytest = np.concatenate((ytest, ynotest), axis=0) return train, test, ytrain, ytest
def initParameters(numDim):# {{{ sig1 = random.rand(numDim,numDim) sig1 = np.dot(sig1,sig1.transpose()) sig2 = random.rand(numDim,numDim) sig2 = np.dot(sig2,sig2.transpose()) for i in range(len(sig2)): sig1[i][i] = 1 sig2[i][i] = 1 u1,u2 = random.rand(1,numDim)[0],random.rand(1,numDim)[0] pi1 = np.random.random() pi2 = 1 - pi1 return u1,u2,sig1,sig2,pi1,pi2# }}}
def generate_random_data(size): matrix = random.rand(size, size) matrix = np.dot(matrix, matrix.transpose()) identity_matrix = np.identity(size) * np.random.random_sample() matrix = matrix + identity_matrix inital_vector = random.rand(size) inital_decision = np.zeros(len(inital_vector), float) return matrix, inital_decision, inital_vector
def generate_clusters(n_points=100, n_clusters=5, n_dim=3, noise=0.15): X = np.zeros((n_points * n_clusters, n_dim)) y = np.zeros(n_points * n_clusters) for cluster in range(n_clusters): mean = (np.ones(n_dim) * (cluster + 1)) + (random.rand(n_dim)) random_mat = random.rand(n_dim, n_dim) cov = np.dot(random_mat, random_mat.transpose()) # pos semi-definite sample = np.random.multivariate_normal(mean, cov, n_points) sample += (noise * np.random.random_sample((n_points, n_dim))) X[cluster * n_points: (cluster + 1) * n_points] = sample y[cluster * n_points: (cluster + 1) * n_points] = cluster return X, y
def AdvanceTime(TimeList,ObservableList): #MyLattice.PrintState() #int(len(MyLattice.ExcitationList)) TranslateableLinks = MyLattice.TranslateableList AnnihilateableLinks = MyLattice.AnnihilateableList Norm = len(TranslateableLinks)*TranslateRate + len(AnnihilateableLinks)*AnnihilationRate + \ ((2*(MyLattice.Dimension)**2) - len(AnnihilateableLinks) - len(TranslateableLinks))*SpawnRate PTrans = len(TranslateableLinks)*TranslateRate/Norm PAnnih = len(AnnihilateableLinks)*AnnihilationRate/Norm PSpawn = ((2*(MyLattice.Dimension)**2) - len(AnnihilateableLinks) - len(TranslateableLinks))*SpawnRate/Norm print "Normalization: " + str(Norm) r = random.rand() DeltaTau = (-1./Norm)*log(r) if len(MyLattice.AnnihilateableList)>0: print MyLattice.AnnihilateableList print [str(l) + "\t" for l in [PTrans,PAnnih,PSpawn]] print r
def gm_params_generator(d, k, sparse_proba=None, alpha=5, min_center_dist=None): """ We generate centers in [-0.5, 0.5] and verify that they are separated enough alpha is the size of the grid """ # we scatter the unit square on k squares, the min distance is given by alpha/sqrt(k) if min_center_dist == None: min_center_dist = alpha / np.sqrt(k) centers = [alpha*(np.random.rand(1, d)[0]-0.5)] for i in range(k-1): center = alpha*(np.random.rand(1, d)[0]-0.5) distances = np.linalg.norm( np.array(centers) - np.array(center), axis=1) while len(distances[distances < min_center_dist]) > 0: center = alpha*(np.random.rand(1, d)[0]-0.5) distances = np.linalg.norm( np.array(centers) - np.array(center), axis=1) centers.append(center) # if sparse_proba is set : # generate covariance matrix with the possibility to set the sparsity on the precision matrix, # we multiply by 1/k^2 to avoid overlapping if sparse_proba == None: A = [random.rand(d, d) for _ in range(k)] cov = [alpha * 1e-2 / (k ** 2) * (np.diag(np.ones(d)) + np.dot(a, a.transpose())) for a in A] else: cov = np.array([np.linalg.inv(make_sparse_spd_matrix(d, alpha=sparse_proba)) for _ in range(k)]) p = np.random.randint(1000, size=(1, k))[0] weights = 1.0*p/p.sum() return weights, centers, cov
def generateSamples(N, means0, Covs0, m1, C1, class_prior): '''generate samples, shape(N, d)''' # sample dimension d = m1.size # preallocation N = np.int(N) X = np.zeros((N, d)) y = np.zeros(N) temp = random.rand(N) # class 0: GMM ind0 = (temp <= class_prior[0]) y[ind0] = 0 # components number M = Covs0.shape[0] weights = np.ones(M) / M X[ind0, :], dummy = generateGMM(d, ind0.sum(), weights, means0, Covs0) # class 1: Gaussian ind1 = (temp > class_prior[0]) y[ind1] = 1 X[ind1, :] = random.multivariate_normal(m1, C1, ind1.sum()) return X, y
def create_dataset(n_clusters, n_dim, n_samples, mean=None, std=None, seed=None): """Create n_clusters clusters of n_dim dimensions, n_samples of it.""" if seed is not None: np.random.seed(seed) if mean is None: means = [np.random.randint(-(i + 1), (i + 1), n_dim).astype(np.float32) for i in range(n_clusters)] if std is None: stds = [] for _ in range(n_clusters): a = np.diag(random.rand(n_dim)) stds.append(a) data_set = [] cluster_id = [] for i, mean, std in zip(range(n_clusters), means, stds): data_set.append(np.random.multivariate_normal(mean, std, n_samples)) cluster_id.append(np.ones(n_samples) * i) data_set = np.concatenate(data_set) cluster_id = np.concatenate(cluster_id) sample_idx = np.random.choice(data_set.shape[0], n_samples, replace=False) # Return correct amount of samples return data_set[sample_idx, :], cluster_id[sample_idx]
def even_time(start, stop, rate, jitter=0): """ Produce an evenly-distributed set of times which has some jitter. Parameters ---------- start : int The gps start time of the set. stop : int The gps end time of the set. rate : int The rate of events per year. jitter : float The number of seconds around which the time can "jitter". This has the effect of adding a small random time on to each time returned in the distribution, up to a maximum of half the jitter value, or taking away up to half the jitter value. Returns ------- times : ndarray A numpy array of gps times where injections should be made. """ expnum_exact = (stop-start) * rate / 365.0 / 24 / 3600 interval = (stop - start) / expnum_exact time = numpy.array(map(lambda i: start + i*interval + jitter *(random.rand()-0.5), range(1,int(expnum_exact)+1))) return time
def generate_random_positive_matrix(D): """ Generate a random semidefinite positive matrix :param D: Dimension :return: DxD matrix """ aux = random.rand(D, D) return np.dot(aux, aux.transpose())
def maximize_shadow(): random_bases = random.rand(low_dimension, high_dimension - 2) res_bh = basinhopping(negative_volume, random_bases, disp=True, niter=200) optimal_bases = add_columns( res_bh.x.reshape((low_dimension, high_dimension - 2))) orth_optimal_bases = orth(optimal_bases.T).T return (-res_bh.fun, orth_optimal_bases)
def main(): N = 10000 D = 10 mean, covar = moments_from_samples(random.rand(N), random.randn(D, N)) print("residual norm^2:", linalg.norm(mean)**2, linalg.norm(covar - eye(D))**2)
def PINVIT(mata, matm, pre, num=1, maxit=20, printrates=True, GramSchmidt=False): """preconditioned inverse iteration""" r = mata.CreateRowVector() Av = mata.CreateRowVector() Mv = mata.CreateRowVector() uvecs = [] for i in range(num): uvecs.append(mata.CreateRowVector()) vecs = [] for i in range(2 * num): vecs.append(mata.CreateRowVector()) for v in uvecs: r.FV().NumPy()[:] = random.rand(len(r.FV())) v.data = pre * r asmall = Matrix(2 * num, 2 * num) msmall = Matrix(2 * num, 2 * num) lams = num * [1] for i in range(maxit): for j in range(num): vecs[j].data = uvecs[j] r.data = mata * vecs[j] - lams[j] * matm * vecs[j] vecs[num + j].data = pre * r if GramSchmidt: Orthogonalize(vecs, matm) for j in range(2 * num): Av.data = mata * vecs[j] Mv.data = matm * vecs[j] for k in range(2 * num): asmall[j, k] = InnerProduct(Av, vecs[k]) msmall[j, k] = InnerProduct(Mv, vecs[k]) ev, evec = scipy.linalg.eigh(a=asmall, b=msmall) lams[:] = ev[0:num] if printrates: print(i, ":", lams) for j in range(num): uvecs[j][:] = 0.0 for k in range(2 * num): uvecs[j].data += float(evec[k, j]) * vecs[k] return lams, uvecs
def AdvanceTime(TimeList,ObservableList): PTrans, PAnnih, PSpawn, Norm, TranslateableLinks, AnnihilateableLinks, SpawnableLinks = Probabilities() r = random.rand() DeltaTau = (-1./Norm)*log(r) if (r < PTrans): RandLink = random.randint(len(TranslateableLinks)) LinkCoords = TranslateableLinks[RandLink] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] MyLattice.TranslateExcitation(MyLattice.ReturnExcitationIndex(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension), XDisplacement, YDisplacement) if (r >= PTrans and r < PTrans + PAnnih): RandLink = random.randint(len(AnnihilateableLinks)) LinkCoords = AnnihilateableLinks[RandLink] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] #if(LinkCoords[2]==MyLattice.Dimension or LinkCoords[0] == MyLattice.Dimension - 1): #print str(LinkCoords[2]) + "\t" + str(LinkCoords[0]) #print str(LinkCoords[3]) + "\t" + str(LinkCoords[1]) MyLattice.AnnihilateExcitation(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension) MyLattice.AnnihilateExcitation(LinkCoords[2]%MyLattice.Dimension,LinkCoords[3]%MyLattice.Dimension) MyLattice.CheckSector(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension, XDisplacement, YDisplacement) if (r >= PTrans + PAnnih): RandLink = random.randint(len(SpawnableLinks)) LinkCoords = SpawnableLinks[RandLink] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] #if(LinkCoords[2]==MyLattice.Dimension or LinkCoords[0] == MyLattice.Dimension - 1): RandLink = random.randint(len(AnnihilateableLinks)) LinkCoords = AnnihilateableLinks[RandLink] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] #if(LinkCoords[2]==MyLattice.Dimension or LinkCoords[0] == MyLattice.Dimension - 1): #print str(LinkCoords[2]) + "\t" + str(LinkCoords[0]) #print str(LinkCoords[3]) + "\t" + str(LinkCoords[1]) MyLattice.AnnihilateExcitation(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension) MyLattice.AnnihilateExcitation(LinkCoords[2]%MyLattice.Dimension,LinkCoords[3]%MyLattice.Dimension) MyLattice.CheckSector(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension, XDisplacement, YDisplacement) if (r >= PTrans + PAnnih): RandLink = random.randint(len(SpawnableLinks)) LinkCoords = SpawnableLinks[RandLink] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] #if(LinkCoords[2]==MyLattice.Dimension or LinkCoords[0] == MyLattice.Dimension - 1): #print str(LinkCoords[2]) + "\t" + str(LinkCoords[0]) #print str(LinkCoords[3]) + "\t" + str(LinkCoords[1]) MyLattice.SpawnExcitation(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension) MyLattice.SpawnExcitation(LinkCoords[2]%MyLattice.Dimension,LinkCoords[3]%MyLattice.Dimension) MyLattice.CheckSector(LinkCoords[0]%MyLattice.Dimension,LinkCoords[1]%MyLattice.Dimension, XDisplacement, YDisplacement) return DeltaTau, MyLattice.Sector
def solving_time(A,b): U = Function(V) t0=time.time() x0=random.rand(len(b.array())) x=jacobi_iter(A,b,x0,10**(-4)) t1=time.time() print #print sqrt(sum((x-xe)**2))/len(x) print return t1-t0
def draw(self): """ Draws the best arm with probability (1 - epsilon) Draws any arm at random with probility epsilon :return: The numerical index of the selected arm """ if random.rand() < self.epsilon: return random.choice(self.n_arms) else: return argmax(self._metric_fn())
def generate_data(n,numDim=2):# {{{ sig1 = random.rand(numDim,numDim) sig1 = np.dot(sig1,sig1.transpose()) sig2 = random.rand(numDim,numDim) sig2 = np.dot(sig2,sig2.transpose()) for i in range(len(sig2)): sig1[i][i] = 1 sig2[i][i] = 1 u1,u2 = random.rand(1,numDim)[0],random.rand(1,numDim)[0] pi1 = np.random.random() pi2 = 1 - pi1 original_params = [u1,sig1,pi1,u2,sig2,pi2] data = [] for i in range(n): obs_generator = np.random.random() if obs_generator > pi1: data.append(np.random.multivariate_normal(u1,sig1)) else: data.append(np.random.multivariate_normal(u2,sig2)) return data,original_params# }}}
def draw(self): """ Draws the best arm with probability (1 - epsilon * temp) Draws any arm with probability epsilon * temp :return: The numerical index of the selected arm """ temp = self._schedule_fn(self.total_draws) if random.rand() < self.epsilon * temp: return random.choice(self.n_arms) else: return argmax(self._metric_fn())
def AdvanceTime(TimeList,ObservableList): TranslateableLinks = MyLattice.TranslateableList AnnihilateableLinks = MyLattice.AnnihilateableList Norm = len(TranslateableLinks)*TranslateRate + len(AnnihilateableLinks)*AnnihilationRate + \ ((2*(MyLattice.Dimension)**2) - len(AnnihilateableLinks) - len(TranslateableLinks))*SpawnRate PTrans = len(TranslateableLinks)*TranslateRate/Norm PAnnih = len(AnnihilateableLinks)*AnnihilationRate/Norm PSpawn = ((2*(MyLattice.Dimension)**2) - len(AnnihilateableLinks) - len(TranslateableLinks))*SpawnRate/Norm r = random.rand() DeltaTau = (-1./Norm)*log(r) if (r < PTrans): RandLink = random.randint(len(TranslateableLinks)) LinkCoords = [TranslateableLinks[RandLink].x1,TranslateableLinks[RandLink].y1,TranslateableLinks[RandLink].x2,TranslateableLinks[RandLink].y2] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] MyLattice.TranslateExcitations(MyLattice.ReturnExcitationIndex(LinkCoords[0],LinkCoords[1]), XDisplacement, YDisplacement) if (r >= PTrans and r < PTrans + PAnnih): RandLink = random.randint(len(AnnihilateableLinks)) LinkCoords = [AnnihilateableLinks[RandLink].x1,AnnihilateableLinks[RandLink].y1,AnnihilateableLinks[RandLink].x2,AnnihilateableLinks[RandLink].y2] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] MyLattice.AnnihilateExcitations(LinkCoords[0],LinkCoords[1],LinkCoords[2],LinkCoords[3]) if (r >= PTrans + PAnnih): Occupancies = MyLattice.ReturnOccupancies() FoundCreatableLink = False RandX = 0 RandY = 0 RandDir = 0 while FoundCreatableLink == False: RandX = random.randint(MyLattice.Dimension) RandY = random.randint(MyLattice.Dimension) RandDir = random.randint(2) if(Occupancies[RandX,RandY]==0 and \ Occupancies[(RandX + RandDir)%MyLattice.Dimension,(RandY + (1-RandDir))%MyLattice.Dimension] == 0): FoundCreatableLink = True LinkCoords = [RandX,RandY,RandX + RandDir, RandY + (1-RandDir)] XDisplacement = LinkCoords[2] - LinkCoords[0] YDisplacement = LinkCoords[3] - LinkCoords[1] MyLattice.CreateExcitations(LinkCoords[0],LinkCoords[1],LinkCoords[2],LinkCoords[3]) return DeltaTau, MyLattice.Sector, len(MyLattice.ExcitationList)
def getThetaComplex(model, S): theta = [] if (model == 0): return theta else: mu = np.ones(model) * 5 A = random.rand(model, model) cov = np.dot(A, A.transpose()) * 1000 print("Mean for Model" + str(model)) print(mu) print("Varaince for Model" + str(model)) print(cov) return np.random.multivariate_normal(mu, cov, S)
def TrialMove(InitLat): InitialLattice = deepcopy(InitLat) InitialEnergy = 2*Je*len(InitialLattice.ExcitationList) i = 0 ListLength = len(InitialLattice.ExcitationList) while i < ListLength: RandomDirection = random.randint(0,5) if InitialLattice.MoveExcitation(i,RandomDirection) == False: #if there was a collision, and this collision killed all the excitations #exit the loop if len(InitialLattice.ExcitationList) == 0: i = 1 #else, you've just popped the excitation under consideration, so now i is #point to something else else: i = i #if you didn't collide with anything, continue iterating over excitations else: i += 1 #The number of excitations may have gone down, so we need to keep track of that: ListLength = len(InitialLattice.ExcitationList) for x in xrange(InitialLattice.Dimension): for y in xrange(InitialLattice.Dimension): SpawnPairUpDown = random.randint(0,2) SpawnPairLeftRight = random.randint(0,2) if SpawnPairUpDown == 1: InitialLattice.SpawnExcitation(x,y) InitialLattice.SpawnExcitation(x,(y+1)%InitialLattice.Dimension) InitialLattice.CheckSector(x,y,0,1) if SpawnPairLeftRight == 1: InitialLattice.SpawnExcitation(x,y) InitialLattice.SpawnExcitation((x+1)%InitialLattice.Dimension,y) InitialLattice.CheckSector(x,y,1,0) FinalEnergy = 2*Je*len(InitialLattice.ExcitationList) if(FinalEnergy <= InitialEnergy): return InitialLattice else: r = random.rand() print(exp(-(FinalEnergy-InitialEnergy)/Temperature)) if r < exp(-(FinalEnergy-InitialEnergy)/Temperature): return InitialLattice else: return InitLat
def generateGMM(d, N, alpha, means, Covs): alpha_cum = np.insert(np.cumsum(alpha), 0, 0) X = np.zeros((N, d)) temp = random.rand(N) gmm_label = np.zeros(N) for i in range(alpha.size): ind = (temp >= alpha_cum[i]) & (temp < alpha_cum[i + 1]) gmm_label[ind] = i + 1 X[ind, :] = random.multivariate_normal(means[i, :].flatten(), Covs[i, :, :], ind.sum()) return X, gmm_label
def getPriorCov(typPriorCov, matrixSize, maxOnSize, koef): assert (typPriorCov <= 6) #assert(matrixSize>=5) #assert(maxOnSize>=5) half = matrixSize / 2 if (typPriorCov == TypPriorCov.JEDNOTKOVA): priorCov = np.identity(matrixSize) #elif (typPriorCov == TypPriorCov.JEDNICKY): # priorCov = np.ones((matrixSize, matrixSize)) elif (typPriorCov == TypPriorCov.KOLEM_JEDNOTKOVE): priorCov = almost_diag(matrixSize, 1) + almost_diag( matrixSize, 2) + almost_diag(matrixSize, 3) priorCov = priorCov.dot(priorCov.transpose()) elif (typPriorCov == TypPriorCov.JEDNICKY_VLEVO_NAHORE or typPriorCov == TypPriorCov.JEDNICKY_VPRAVO_DOLE): jednicky = np.ones((maxOnSize, maxOnSize)) nulyPravo = np.zeros((maxOnSize, matrixSize - maxOnSize)) nulyDole = np.zeros((matrixSize - maxOnSize, matrixSize)) if (typPriorCov == TypPriorCov.JEDNICKY_VLEVO_NAHORE): dorovnano = np.hstack((jednicky, nulyPravo)) priorCov = np.vstack((dorovnano, nulyDole)) for i in range(0, maxOnSize): priorCov[i, i] += 1 for i in range(maxOnSize, matrixSize): priorCov[i, i] = 10e-30 else: dorovnano = np.hstack((nulyPravo, jednicky)) priorCov = np.vstack((nulyDole, dorovnano)) for i in range(0, maxOnSize): j = matrixSize - i - 1 priorCov[j, j] += 1 for i in range(maxOnSize, matrixSize): j = matrixSize - i - 1 priorCov[j, j] = 10e-30 #print priorCov.shape elif (typPriorCov == TypPriorCov.JEDNICKY_VPRAVO_DOLE): priorCov = np.vstack((np.zeros((maxOnSize, matrixSize)), np.hstack((np.zeros( (maxOnSize, maxOnSize - maxOnSize)), np.ones((maxOnSize, maxOnSize)))))) for i in range(0, matrixSize): priorCov = 1 elif (typPriorCov == TypPriorCov.RANDOM): priorCov = random.rand(matrixSize, matrixSize) priorCov = priorCov.dot(priorCov.transpose()) assert (np.linalg.inv(priorCov).shape == (matrixSize, matrixSize)) return priorCov * koef
def positive_definite_matrix(size=3, alpha=1.0): """ :param size: size of matrix :param alpha: arbitrary positive number :return: A positive definite matrix For alpha > 0 the eigenvalues should be >= alpha, which makes the returned matrix a PDM (I hope) """ M = random.rand(size, size) I = np.dot(np.identity(size), alpha) return np.dot(M, M.T) + I
def __init__(self, k, dim, init_mu=None, init_sigma=None, init_pi=None, colors=None): ''' Define a model with known number of clusters and dimensions. input: - k: Number of Gaussian clusters - dim: Dimension - init_mu: initial value of mean of clusters (k, dim) (default) random from uniform[-10, 10] - init_sigma: initial value of covariance matrix of clusters (k, dim, dim) (default) Identity matrix for each cluster - init_pi: initial value of cluster weights (k,) (default) equal value to all cluster i.e. 1/k - colors: Color valu for plotting each cluster (k, 3) (default) random from uniform[0, 1] ''' self.k = k self.dim = dim if (init_mu is None): init_mu = random.rand(k, dim) * 20 - 10 self.mu = init_mu if (init_sigma is None): init_sigma = np.zeros((k, dim, dim)) for i in range(k): init_sigma[i] = np.eye(dim) self.sigma = init_sigma if (init_pi is None): init_pi = np.ones(self.k) / self.k self.pi = init_pi if (colors is None): colors = random.rand(k, 3) self.colors = colors
def generate_data(num_dims, n_components): X = np.empty([0, num_dims]) all_means = [] for i in range(n_components): # Con esto aseguramos de generar matriz positiva semi definida cov = random.rand(num_dims, num_dims) cov = np.dot(cov, cov.transpose()) means = [] for j in range(num_dims): # Medias aleatorias means.append(random.uniform(-5, 5)) new_data = np.random.multivariate_normal(means, cov, random.randint(100, 400)) X = np.concatenate((X, new_data)) all_means.append(means) return X
def random_mean_and_matrix_semidefinite(self, random_means, random_covariances, array=True, num_points=1): ''' Definition: Parameters ---------- random_means: Matrix Have in each row a two column vector [(low = a,high = b)] Example: means = [[0,1],[2,3],[6,7]] Where each row indicates in which range the uniform random generator can takes values random_covariances: Matrix Is a square matrix, where each row has a column vector, who has inside a vector with two components Example: A matrix 3x3. covariance = [[[a11,b11],[a12,b12],[a13,b13]],[[a21,b21],[a22,b22],[a23,b23]],[[a31,b31],[a32,b32],[a33,b33]]] Where each row indicates in which range the uniform random generator can takes values a = [[[1,2],[-5,5],[-100,6]],[[135,683],[2,285],[-135,13]],[[58,135],[16,35],[5,68478]]] ''' mean = [] for random_mean in random_means: mean.append(random.uniform(low=random_mean[0], high=random_mean[1])) if array == True: covariance = random.rand( random_means.shape[0], random_means.shape[0]) * ( random_covariances[1] - random_covariances[0]) + random_covariances[0] covariance = covariance * covariance.transpose() else: covariance = [] for random_covariance in random_covariances: covariance.append([]) for points in random_covariance: covariance[-1].append( random.uniform(low=points[0], high=points[1])) covariance = np.array(covariance) #A*A' is a semedefinite matrix covariance = covariance * covariance.transpose() return [mean, multivariate_normal(mean, covariance, num_points)]
def random_data(N, kernel_type="rbf", sigma=0.1): """ input: N - number of data points to generate kernel - which kernel to use, for now only rbf is implemented output: X - data K - kernel matrix """ # choose a random dimension for the data dimension = 100 X = random.rand(N, dimension) # get a kernel matrix K = compute_kernel(X, kernel_type, sigma) return X, K
def getPriorCov(typPriorCov, matrixSize, maxOnSize, koef): assert(typPriorCov<=6) #assert(matrixSize>=5) #assert(maxOnSize>=5) half=matrixSize/2 if (typPriorCov == TypPriorCov.JEDNOTKOVA): priorCov = np.identity(matrixSize) #elif (typPriorCov == TypPriorCov.JEDNICKY): # priorCov = np.ones((matrixSize, matrixSize)) elif (typPriorCov == TypPriorCov.KOLEM_JEDNOTKOVE): priorCov = almost_diag(matrixSize,1)+almost_diag(matrixSize,2)+almost_diag(matrixSize,3) priorCov = priorCov.dot(priorCov.transpose()) elif (typPriorCov == TypPriorCov.JEDNICKY_VLEVO_NAHORE or typPriorCov == TypPriorCov.JEDNICKY_VPRAVO_DOLE): jednicky = np.ones((maxOnSize,maxOnSize)) nulyPravo = np.zeros((maxOnSize, matrixSize-maxOnSize)) nulyDole = np.zeros((matrixSize-maxOnSize,matrixSize)) if (typPriorCov==TypPriorCov.JEDNICKY_VLEVO_NAHORE): dorovnano = np.hstack((jednicky, nulyPravo)) priorCov = np.vstack((dorovnano, nulyDole)) for i in range(0,maxOnSize): priorCov[i,i]+=1 for i in range(maxOnSize,matrixSize): priorCov[i,i]=10e-30 else: dorovnano = np.hstack((nulyPravo,jednicky)) priorCov = np.vstack((nulyDole,dorovnano)) for i in range(0,maxOnSize): j=matrixSize-i-1 priorCov[j,j]+=1 for i in range(maxOnSize,matrixSize): j=matrixSize-i-1 priorCov[j,j]=10e-30 #print priorCov.shape elif (typPriorCov == TypPriorCov.JEDNICKY_VPRAVO_DOLE): priorCov = np.vstack((np.zeros((maxOnSize,matrixSize)),np.hstack((np.zeros((maxOnSize,maxOnSize-maxOnSize)),np.ones((maxOnSize,maxOnSize)))))) for i in range(0,matrixSize): priorCov=1 elif (typPriorCov == TypPriorCov.RANDOM): priorCov = random.rand(matrixSize,matrixSize) priorCov = priorCov.dot(priorCov.transpose()) assert (np.linalg.inv(priorCov).shape == (matrixSize, matrixSize)) return priorCov*koef
def _sample_r(self, m): # Sample radial distances from cumulative mass profile if (self.verbose): print " sample r ..." ran = random.rand(self.N) if not m.multi: self.r = numpy.interp(ran, m.mc/m.mc[-1], m.r) self.rmark = self.r if m.multi: self.r = numpy.zeros(self.N) for j in range(m.nmbin): s, e = self.Nstart[j], self.Nend[j] m.mcj[j][0] = 0.5*m.mcj[j][1] self.r[s:e] = numpy.interp(ran[s:e], m.mcj[j]/m.mcj[j][-1], m.r) # get dimensionless potential self.phihat = m.interp_phi(self.r)/m.s2 return
def dmat(pix): dm = sp.zeros(np * nt * ntm * npm) wdm = sp.zeros(np * nt) rpeff = sp.zeros(ntm * npm) rteff = sp.zeros(ntm * npm) zeff = sp.zeros(ntm * npm) weff = sp.zeros(ntm * npm) npairs = 0 npairs_used = 0 for p in pix: for d1 in data[p]: print("\rcomputing xi: {}%".format( round(counter.value * 100. / ndata, 3)), end="") with lock: counter.value += 1 order1 = d1.order r1 = d1.r_comov w1 = d1.we l1 = d1.ll z1 = d1.z r = random.rand(len(d1.neighs)) w = r > rej npairs += len(d1.neighs) npairs_used += w.sum() for d2 in sp.array(d1.neighs)[w]: same_half_plate = (d1.plate == d2.plate) and\ ( (d1.fid<=500 and d2.fid<=500) or (d1.fid>500 and d2.fid>500) ) order2 = d2.order ang = d1 ^ d2 r2 = d2.r_comov w2 = d2.we l2 = d2.ll z2 = d2.z fill_dmat(l1, l2, r1, r2, z1, z2, w1, w2, ang, wdm, dm, rpeff, rteff, zeff, weff, same_half_plate, order1, order2) setattr(d1, "neighs", None) return wdm, dm.reshape(np * nt, npm * ntm), rpeff, rteff, zeff, weff, npairs, npairs_used
def __generate_covariance(self, idx_1, idx_0, idx_e): # idx_1, idx_0, idx_e are indicator vectors of length p # this function generates a covariance matrix with block structure # according to intersections of idx_1, idx_0, idx_e # We assume the noise covariates are uncorrelated. cov = np.identity(self.p) for i1 in [False, True]: for i0 in [False, True]: for ie in [False, True]: if ~i1 & ~i0 & ~ie: # noise covariates are uncorrelated continue b = (idx_1 == i1) & (idx_0 == i0) & (idx_e == ie) k = np.sum(b) # block size if k == 0: continue idx_block = np.outer(b, b) A = random.rand(k, k) cov[idx_block] = np.dot(A, A.transpose()).ravel() self.cov = cov
def _sample_angles(self,N): if (self.verbose): print " sample angles ..." R = random.rand(N) if self.ani: # Anisotropic systems sample angles: cdf(q) = erfi(a*q)/erfi(a), a = sqrt(k)*p p = self.r/self.ra a = p*sqrt(self.k) if self.mod.multi: a *= sqrt(self.mod.s2/self.s2j) self.q = numpy.zeros(N) for j in range(N): self.q[j] = optimize.brentq(self._pdf_angle, 0, 1, args=(a[j], R[j])) else: # Isotropic: cdf(q) = q self.q = R self.vr = self.v*self.q*random.choice((-1,1),size=self.N) self.vt = self.v*sqrt(1-self.q**2)
def GenerateCholesky(matrixSize=7): #Generate a positive semi-definite 'covariance' matrix # (generate a random matrix, multiply it by its own transpose) A = random.rand(matrixSize, matrixSize) B = np.dot(A, A.transpose()) #Convert this random 'covariance' matrix into a correlation matrix B = np.asanyarray(B) std = np.sqrt(np.diag(B)) corr = B / np.outer(std, std) print 'random positive semi-define matrix for today is\n', corr #Calculate the Cholesky decomposition so we can throw # random vectors with correlations c = cholesky(corr, lower=True) return c
def maximize_shadow(): # generators = [0.378556, 0.326643, 0.100944, 0.489704 ] # generators = [0.711917, 0.203285] generators = random.rand(2, 1) # generators = [math.pi/4, math.pi/4] print "Initial generator is:" print generators print "Initial bases are:" print get_bases(generators) * math.sqrt(2) * 2 print "Initial Volume is:", shadow_volume(get_bases(generators)) bounds = Bounds([math.pi / 4, math.pi / 4], [0, 0]) res_bh = basinhopping(negative_volume, generators, disp=False, niter=50, accept_test=bounds) print "Optimal generator is:" print res_bh.x optimal_bases = get_bases(res_bh.x) return (-res_bh.fun, optimal_bases)
def TrialMove2(InitLat): InitialLattice = deepcopy(InitLat) InitialEnergy = 2*Je*len(InitialLattice.ExcitationList) if(len(InitialLattice.ExcitationList)!=0): RandomExcitation = random.randint(len(InitialLattice.ExcitationList)) RandomDirection = random.randint(0,5) InitialLattice.MoveExcitation(RandomExcitation,RandomDirection) SpawnOrientation = random.randint(0,3) #0 don't do anything, 1 updown, 2 leftright if(SpawnOrientation == 1): RandomX = random.randint(0,InitialLattice.Dimension) RandomY = random.randint(0,InitialLattice.Dimension) InitialLattice.SpawnExcitation(RandomX,RandomY) InitialLattice.SpawnExcitation(RandomX,(RandomY+1)%InitialLattice.Dimension) InitialLattice.CheckSector(RandomX,RandomY,0,1) if(SpawnOrientation == 2): RandomX = random.randint(0,InitialLattice.Dimension) RandomY = random.randint(0,InitialLattice.Dimension) InitialLattice.SpawnExcitation(RandomX,RandomY) InitialLattice.SpawnExcitation((RandomX+1)%InitialLattice.Dimension,RandomY) InitialLattice.CheckSector(RandomX,RandomY,1,0) FinalEnergy = 2*Je*len(InitialLattice.ExcitationList) if(FinalEnergy <= InitialEnergy): return InitialLattice else: r = random.rand() #print(exp(-(FinalEnergy-InitialEnergy)/Temperature)) if r < exp(-(FinalEnergy-InitialEnergy)/Temperature): return InitialLattice else: return InitLat
def random_mean_and_matrix_semidefinite(self,random_means,random_covariances,array = True, num_points = 1): ''' Definition: Parameters ---------- random_means: Matrix Have in each row a two column vector [(low = a,high = b)] Example: means = [[0,1],[2,3],[6,7]] Where each row indicates in which range the uniform random generator can takes values random_covariances: Matrix Is a square matrix, where each row has a column vector, who has inside a vector with two components Example: A matrix 3x3. covariance = [[[a11,b11],[a12,b12],[a13,b13]],[[a21,b21],[a22,b22],[a23,b23]],[[a31,b31],[a32,b32],[a33,b33]]] Where each row indicates in which range the uniform random generator can takes values a = [[[1,2],[-5,5],[-100,6]],[[135,683],[2,285],[-135,13]],[[58,135],[16,35],[5,68478]]] ''' mean = [] for random_mean in random_means: mean.append(random.uniform(low=random_mean[0],high=random_mean[1])) if array == True: covariance = random.rand( random_means.shape[0] , random_means.shape[0])*(random_covariances[1] - random_covariances[0]) + random_covariances[0] covariance = covariance * covariance.transpose() else: covariance = [] for random_covariance in random_covariances: covariance.append([]) for points in random_covariance: covariance[-1].append(random.uniform(low=points[0],high=points[1])) covariance = np.array(covariance) #A*A' is a semedefinite matrix covariance = covariance*covariance.transpose() return [mean,multivariate_normal(mean,covariance,num_points)]
def test_mahalanobis(): """ this is a little test function to make sure i am using scipy special distance mahalanobis properly """ from scipy import random, linalg matrixSize = 10 A = random.rand(matrixSize, matrixSize) B = np.dot(A, A.transpose()) v = np.random.randn(matrixSize) u = np.random.randn(matrixSize) #manual mahalanobis manual_result = np.sqrt((u - v).dot(np.matmul(np.linalg.inv(B), u - v))) #automatic mahalanobis from scipy.spatial.distance import mahalanobis auto_result = mahalanobis(u, v, np.linalg.inv(B)) assert np.isclose( manual_result, auto_result ), f"auto_result: {auto_result}; manual_result: {manual_result}"
def t123(pix): t123_loc = sp.zeros([np * nt, np * nt]) w123 = sp.zeros(np * nt) npairs = 0 npairs_used = 0 for ipix in pix: for d1 in data[ipix]: print("\rcomputing xi: {}%".format( round(counter.value * 100. / ndata, 3)), end="") with lock: counter.value += 1 v1 = v1d(d1.ll) w1 = d1.we c1d_1 = (w1 * w1[:, None]) * c1d( abs(d1.ll - d1.ll[:, None])) * sp.sqrt(v1 * v1[:, None]) r1 = d1.r_comov z1 = 10**d1.ll / lambda_abs - 1 r = random.rand(len(d1.neighs)) w = r > rej npairs += len(d1.neighs) npairs_used += w.sum() for d2 in sp.array(d1.neighs)[w]: ang = d1 ^ d2 same_half_plate = (d1.plate == d2.plate) and\ ( (d1.fid<=500 and d2.fid<=500) or (d1.fid>500 and d2.fid>500) ) v2 = v1d(d2.ll) w2 = d2.we c1d_2 = (w2 * w2[:, None]) * c1d( abs(d2.ll - d2.ll[:, None])) * sp.sqrt(v2 * v2[:, None]) r2 = d2.r_comov z2 = 10**d2.ll / lambda_abs - 1 fill_t123(r1, r2, ang, w1, w2, z1, z2, c1d_1, c1d_2, w123, t123_loc, same_half_plate) setattr(d1, "neighs", None) return w123, t123_loc, npairs, npairs_used
from stats_util import * import numpy as np from scipy import random, linalg p = 10 A = random.rand(p,p) B = np.dot(A,A.transpose()) print 'random positive semi-define matrix for today is', B print sample_invwishart(B,15)
def __call__(self, P): return (random.rand(len(P)) < P.state_(self.state)[:] * P.clock.dt).nonzero()[0]
from scipy import random if __name__ == '__main__': n_samples = 400 n_variables = 5 var_names = [] for n in range(n_variables): var_names.append('var_%d'%n) # Generate random mean vector and covariance matrix mu = random.sample(n_variables) A = random.rand(n_variables, n_variables) R = np.dot(A, A.transpose()) # Generate random samples of correlated variables data = np.random.multivariate_normal(mu, R, size=n_samples) # For continuous data we discretize. bin_size = 3 # Set parameters for the random graph generator max_parents = 2 p_link = 0.7 max_iter = 100 # Hyperparameters for the Genetic Algorithm
from scipy import random, linalg import numpy matrixSize = 500 A = random.rand(matrixSize,matrixSize) B = numpy.dot(A,A.transpose()) for row in B: rowT = [] for cell in row: rowT.append(str(cell)) print ",".join(rowT)
def generate_random_data(size): matrix = random.rand(size, size) matrix = np.dot(matrix, matrix.transpose()) identity_matrix = np.identity(size) * np.random.random_sample() matrix = matrix + identity_matrix return matrix
def timeme(n): p = [(random.rand()*1.8-0.9, random.rand()*1.8-0.9) for i in range(n)] d = Delaunay( p )
def _sample_k(self, jbin): # y(x) = y_j # cdf(x) = ycum_j + y_j*(x - x_j) # x = R/y_j + x_j N = self.N iter=0 c = numpy.zeros(N, dtype='bool')+True if self.mod.multi: sta, end = self.Nstart[jbin], self.Nend[jbin] c[0:sta] = False c[end::] = False ycum = self.ycum[:,c] rtmp = self.r[c] ftmp = self.phihat[c] y = self.y[:,c] x = self.x[:,c] if self.mod.multi: c = numpy.zeros(self.Nj[jbin], dtype='bool')+True while (sum(c) > 0): nc = sum(c) ycum = ycum[:,c] rtmp = rtmp[c] ftmp = ftmp[c] y = y[:,c] x = x[:,c] R = random.rand(nc)*ycum[-1] xtmp = numpy.zeros(nc) P = numpy.zeros(nc) for j in range(self.nx): cr = (R>=ycum[j])&(R<ycum[j+1]) if (sum(cr)>0): xtmp[cr] = (R[cr]-ycum[j,cr])/y[j,cr] + x[j,cr] P[cr] = y[j,cr] R2 = random.rand(nc)*P f = self._pdf_k32(rtmp, ftmp, xtmp, jbin) c = (R2>f) if (iter==0): xsamp = xtmp[~c] r = rtmp[~c] else: xsamp = numpy.r_[xsamp, xtmp[~c]] r = numpy.r_[r, rtmp[~c]] iter+=1 # Assign final velocities and update r because of shuffling self.k = numpy.r_[self.k, xsamp**(2./3)] self.v = numpy.r_[self.v, sqrt(2*xsamp**(2/3)*self.mod.s2)] self.rfinal = numpy.r_[self.rfinal, r]
edges.append( (li, rcand) ) ri = rcand else: edges.append( (ri, lcand) ) li = lcand edges.extend( l ) edges.extend( r ) return edges if __name__ == "__main__": from mpl_toolkits.mplot3d import axes3d from matplotlib.pyplot import * from matplotlib.collections import PolyCollection points = [(random.rand()*1.8-0.9, random.rand()*1.8-0.9) for i in range(100)] ## points = [ ## (0., 1.), ## (1., 1.), ## (0.2, 0.8), ## (0.8, 0.8), ## (0.5, 0.2), ## (0.48, 0.18), ## (0.52, 0.22) ## ] rs = [ 0.2, 0.4, 0.6, 0.8, 1.0 ] tstep = [ pi, pi/2, pi/4, pi/8, pi / 16 ] points = [(r*cos(t), r*sin(t)) for i, r in enumerate(rs)
def ortho_basis(normal, ref_vec=None): """Generates an orthonormal basis in the plane perpendicular to `normal` The orthonormal basis generated spans the plane defined with `normal` as its normal vector. The handedness of `on1` and `on2` in the returned basis is such that: .. math:: \\mathsf{on1} \\times \\mathsf{on2} = {\\mathsf{normal} \\over \\left\\| \\mathsf{normal}\\right\\|} `normal` must be expressible as a one-dimensional |nparray| of length 3. Parameters ---------- normal length-3 |npfloat_| -- The orthonormal basis output will span the plane perpendicular to `normal`. ref_vec length-3 |npfloat_|, optional -- If specified, `on1` will be the normalized projection of `ref_vec` onto the plane perpendicular to `normal`. Default is |None|. Returns ------- on1 length-3 |npfloat_| -- First vector defining the orthonormal basis in the plane normal to `normal` on2 length-3 |npfloat_| -- Second vector defining the orthonormal basis in the plane normal to `normal` Raises ------ ~exceptions.ValueError If `normal` or `ref_vec` is not expressible as a 1-D vector with 3 elements ~opan.error.VectorError (typecode :attr:`~opan.error.VectorError.NONPRL`) If `ref_vec` is specified and it is insufficiently non- parallel with respect to `normal` """ # Imports for library functions import numpy as np from scipy import linalg as spla from scipy import random as sprnd from ..const import PRM from ..error import VectorError # Internal parameters # Magnitude of the perturbation from 'normal' in constructing a random rv RAND_MAG = 0.25 # Test 'normal' for shape and length if not len(normal.shape) == 1: raise ValueError("'normal' is not a vector") ## end if if not normal.shape[0] == 3: raise ValueError("Length of 'normal' is not three") ## end if # Normalize to concise variable 'nv' nv = normal / spla.norm(normal) # Test for specification of ref_vec in the function call if ref_vec is None: # ref_vec not specified. # # Generate reference vector by generation of a random perturbation # vector suitably non-parallel to norm_vec # Generate suitable randomizer, looping as needed rv = nv while parallel_check(nv, rv): rv = np.float64(1.0 - RAND_MAG + 2 * RAND_MAG * sprnd.rand(3)) ## do loop # Calculate rejection of perturbed vector on the normal, then # normalize rv = rej(rv, nv) rv = rv / spla.norm(rv) else: # ref_vec specified, go ahead and use. Start with validity check. if not len(ref_vec.shape) == 1: raise ValueError("ref_vec is not a vector") ## end if if not ref_vec.shape[0] == 3: raise ValueError("ref_vec length is not three") ## end if # Normalize ref_vec to 'rv' rv = ref_vec / spla.norm(ref_vec) # Check for collinearity of nv and rv; raise error if too close if parallel_check(nv, rv): # Essentially equal or opposite vectors, making them too nearly # parallel. raise VectorError(VectorError.NONPRL, "'normal' and 'ref_vec' are too nearly parallel.", "") ## end if # rv is ok to use from here ## end try # on2 is the unit vector parallel to nv x rv on2 = np.cross(nv, rv) on2 = on2 / spla.norm(on2) # on1 is on2 x nv (normalization should not be necessary here, but is # performed just in case) on1 = np.cross(on2, nv) on1 = on1 / spla.norm(on1) # Return the spanning vectors return on1, on2
def uniform_time(tstart, tstop, number): """ Get a set of randomized (integer) event times. """ return random.randint(tstart, tstop, number) + random.rand(number)
def sampler(self, probabilities): result = probabilities > random.rand(self.rbm.hiddenDim) return result.astype('int32')
# LCT of the FP sum m0=N0*p0 sigma20=p0*(1-p0)*N0 # statistic of the true positives p1=0.9 N1=200 # LCT of the TP sum m1=N1*p1 sigma21=p1*(1-p1)*N1 alpha=0.2 Lruns = 3000 F=zeros(Lruns) for ir in range(Lruns): X0=random.rand(N0,1)<p0 X1=random.rand(N1,1)<p1 FP=float(sum(X0)) TP=float(sum(X1)) # FN=N1-TP # recall=TP / (TP+FN) recall = TP/N1 precision = TP/(TP+FP) Fm1=alpha/precision+(1-alpha)/recall F[ir]=1.0/Fm1 meanpz, varpz, pz = asymptoticFalpha(alpha,N1,m0,m1,sigma20,sigma21,x=0.1) print [meanpz, mean(F)] print [varpz**0.5, std(F)]