def propose(values, log_likelihood): trials = 1 curr_values = np.copy(values) for i in range(trials): # propose weights log_weights = np.log(values[:components]) new_log_weights = multivariate_normal.rvs(mean=log_weights, cov=np.eye(components)*WEIGHT_VAR) # convert from softmax new_weights = np.exp(new_log_weights) # propose means means = values[components:(2*components)] new_means = multivariate_normal.rvs(mean=means, cov=np.eye(components)*MEAN_VAR) # propose variances variances = values[(2*components):(3*components)] new_vars = multivariate_normal.rvs(mean=variances, cov=np.eye(components)*VAR_VAR) new_value = np.append(new_weights, np.append(new_means, new_vars)) try: prob_new = log_likelihood(new_value) prob_old = log_likelihood(values) except: continue if prob_new == -np.inf or prob_old == -np.inf: continue if np.all(means >= 0) and np.all(variances >= 0) and \ (prob_new > prob_old or random.random() < np.exp(prob_new - prob_old)): curr_values = np.copy(new_value) return curr_values
def gen_synthetic_classifications(f_prior_mean=None, nx=100, ny=100): # f_prior_mean should contain the means for all the grid squares # Generate some data ls = [10, 10] sigma = 0.1 N = 2000 #C = 100 # number of labels for training s = 1 # inverse precision scale for the latent function. # Some random feature values xvals = np.random.choice(nx, N, replace=True)[:, np.newaxis] yvals = np.random.choice(ny, N, replace=True)[:, np.newaxis] # remove repeated coordinates for coord in range(N): while np.sum((xvals==xvals[coord]) & (yvals==yvals[coord])) > 1: xvals[coord] = np.random.choice(nx, 1) yvals[coord] = np.random.choice(ny, 1) K = matern_3_2_from_raw_vals(np.concatenate((xvals, yvals), axis=1), ls) if f_prior_mean is None: f = mvn.rvs(cov=K/s) # zero mean else: f = mvn.rvs(mean=f_prior_mean[xvals, yvals].flatten(), cov=K/s) # zero mean # generate the noisy function values for the pairs fnoisy = norm.rvs(scale=sigma, size=N) + f # generate the discrete labels from the noisy function labels = sigmoid(fnoisy) return N, nx, ny, labels, xvals, yvals, f, K
def HtWtDataGenerator(nSubj, rndsd=None): # Specify parameters of multivariate normal (MVN) distributions. # Men: HtMmu = 69.18 HtMsd = 2.87 lnWtMmu = 5.14 lnWtMsd = 0.17 Mrho = 0.42 Mmean = np.array([HtMmu , lnWtMmu]) Msigma = np.array([[HtMsd**2, Mrho * HtMsd * lnWtMsd], [Mrho * HtMsd * lnWtMsd, lnWtMsd**2]]) # Women cluster 1: HtFmu1 = 63.11 HtFsd1 = 2.76 lnWtFmu1 = 5.06 lnWtFsd1 = 0.24 Frho1 = 0.41 prop1 = 0.46 Fmean1 = np.array([HtFmu1, lnWtFmu1]) Fsigma1 = np.array([[HtFsd1**2, Frho1 * HtFsd1 * lnWtFsd1], [Frho1 * HtFsd1 * lnWtFsd1, lnWtFsd1**2]]) # Women cluster 2: HtFmu2 = 64.36 HtFsd2 = 2.49 lnWtFmu2 = 4.86 lnWtFsd2 = 0.14 Frho2 = 0.44 prop2 = 1 - prop1 Fmean2 = np.array([HtFmu2, lnWtFmu2]) Fsigma2 = np.array([[HtFsd2**2 , Frho2 * HtFsd2 * lnWtFsd2], [Frho2 * HtFsd2 * lnWtFsd2 , lnWtFsd2**2]]) # Randomly generate data values from those MVN distributions. if rndsd is not None: np.random.seed(rndsd) datamatrix = np.zeros((nSubj, 3)) # arbitrary coding values maleval = 1 femaleval = 0 for i in range(0, nSubj): # Flip coin to decide sex sex = np.random.choice([maleval, femaleval], replace=True, p=(.5,.5), size=1) if sex == maleval: datum = multivariate_normal.rvs(mean=Mmean, cov=Msigma) if sex == femaleval: Fclust = np.random.choice([1, 2], replace=True, p=(prop1, prop2), size=1) if Fclust == 1: datum = multivariate_normal.rvs(mean=Fmean1, cov=Fsigma1) if Fclust == 2: datum = multivariate_normal.rvs(mean=Fmean2, cov=Fsigma2) datamatrix[i] = np.concatenate([sex, np.round([datum[0], np.exp(datum[1])], 1)]) return datamatrix
def new_cluster(self, X, a, B, c, m): """ x is a np.matrix s is a float a is a float c is a float B is a np.matrix m is a np.matrix """ d = float(X.shape[0]) s = float(X.shape[1]) x_mean = np.mean(X, axis=1) m_p = c/(s+c) * m + 1/(s+c) * np.sum(X, axis=1) c_p = s + c a_p = a + s sum_B = np.matlib.zeros((d,d)) for i in np.arange(s): sum_B = sum_B + (X - x_mean) * (X - x_mean).T B_p = B + sum_B + s/(a*s + 1) * (x_mean - m) * (x_mean - m).T sigma_p = wishart.rvs(df=a_p, scale=inv(B_p)) mean_p = multivariate_normal.rvs(mean=m_p, cov=inv(c_p * sigma_p)) #transpose omitted return mean_p, sigma_p
def randomBatch(batch_size, sequenceLength): cov = np.identity(sequenceLength) for i in range(0, sequenceLength): for j in range(0, sequenceLength): if abs(i - j) < 2: cov[i][j] = 0.05 if i % 10 == j % 10: cov[i][j] = 1.0 y = [] for i in range(0, batch_size): meanDelta = np.random.randint(-5, 5) mean = [0.0] for j in range(1, sequenceLength): mean += [mean[-1] + 0.01 * meanDelta] y_new = multivariate_normal.rvs(mean = mean, cov = cov) plt.plot(y_new) plt.show() y += [y_new] return np.asarray(y)
def rvs(self, size=1): """ """ c_weights = self.weights.cumsum() # Cumulative weights c_weights = np.hstack([0, c_weights]) r_weights = np.random.rand(size) # Randomly sampled weights r_weights = np.sort(r_weights) if self.ndims > 1: rvs = np.zeros((size, self.ndims)) else: rvs = np.zeros(size) prev_max = 0 for i, c_weight in enumerate(c_weights): if i == c_weights.size - 1: break size_i = r_weights[r_weights > c_weight].size size_i = size_i - r_weights[r_weights > c_weights[i + 1]].size range_ = np.arange(size_i) + prev_max range_ = range_.astype(int) prev_max = range_[-1] mean = self.means[i] covariance = self.covariances[i] rvs[range_] = multivariate_normal.rvs(mean, covariance, size_i) return rvs
def fit_nce(self, X, k=1, mu_noise=None, L_noise=None, mu0=None, L0=None, c0=None, method='minimize', maxnumlinesearch=None, maxnumfuneval=None, verbose=False): _class = self.__class__ D, Td = X.shape self._init_params(D, mu_noise, L_noise, mu0, L0, c0) noise = self._params_noise Y = mvn.rvs(noise.mu, noise.L, k * Td).T maxnumlinesearch = maxnumlinesearch or DEFAULT_MAXNUMLINESEARCH obj = lambda u: _class.J(X, Y, noise.mu, noise.L, *vec_to_params(u)) grad = lambda u: params_to_vec( *_class.dJ(X, Y, noise.mu, noise.L, *vec_to_params(u))) t0 = params_to_vec(*self._params_nce) if method == 'minimize': t_star = minimize(t0, obj, grad, maxnumlinesearch=maxnumlinesearch, maxnumfuneval=maxnumfuneval, verbose=verbose)[0] else: t_star = sp_minimize(obj, t0, method='BFGS', jac=grad, options={'disp': verbose, 'maxiter': maxnumlinesearch}).x self._params_nce = GaussParams(*vec_to_params(t_star)) return (self._params_nce, Y)
def test_rvs_shape(): # Check that rvs parses the mean and covariance correctly, and returns # an array of the right shape N = 300 d = 4 sample = multivariate_normal.rvs(mean=np.zeros(d), cov=1, size=N) assert_equal(sample.shape, (N, d)) sample = multivariate_normal.rvs(mean=None, cov=np.array([[2, .1], [.1, 1]]), size=N) assert_equal(sample.shape, (N, 2)) u = multivariate_normal(mean=0, cov=1) sample = u.rvs(N) assert_equal(sample.shape, (N, ))
def __call__(self, state_post_prev): state_post_prev = np.array(state_post_prev) n_particles, n_states = state_post_prev.shape # assume that noise are generated by multivariate gaussian noise = multivariate_normal.rvs(cov=self.noise_cov, size=n_particles) noise = noise.reshape(n_particles, n_states) state_pre = np.dot(self.transition_matrix, state_post_prev.T).T + noise return state_pre
def SampleGP(mean,Cholesky): ''' Transform sample from MVN to BetaMatrix format Care the interface of cholesky decomposition ''' n=mean.size sample=multivariate_normal.rvs(np.zeros(n),np.identity(n)) GP=mean+np.dot(Cholesky,sample) return GP
def test_check_grad(self): D = 2 S = c_[[2., .2], [.2, 2.]] X = mvn.rvs(randn(2), S, 100).T mu_noise, P_noise = r_[-1., 1.], .5 * c_[[1., .1], [.1, 1.]] L_noise = cholesky(P_noise) Y = mvn.rvs(mu_noise, inv(P_noise), 200).T obj = lambda u: NceGauss.J( X, Y, mu_noise, L_noise, *vec_to_params(u)) grad = lambda u: params_to_vec( *NceGauss.dJ(X, Y, mu_noise, L_noise, *vec_to_params(u))) grad_diff = lambda u: check_grad(obj, grad, u) for i in xrange(100): u = r_[0,0,2,0,2,1] + randn(6) / 10 self.assertLess(grad_diff(u), 1e-5)
def sample_state(self, num_time_instants): """Sample a sequence of states from the prior""" # Get system matices F = self.transition_matrix() Q = self.transition_covariance() # Initialise state sequence state = np.zeros((num_time_instants,self.ds)) # Sample first value from prior state[0] = mvn.rvs( mean=self.initial_state_prior.mn, cov=self.initial_state_prior.vr ) # Loop through time, sampling each state for kk in range(1,num_time_instants): state[kk] = mvn.rvs( mean=np.dot(F,state[kk-1]), cov=Q ) return state
def simple_model_sample_prior(): vals = np.zeros(3 * components) vals[:components] = \ np.exp(multivariate_normal.rvs(cov=COV_ALPHA*np.eye(components))) for i in range(components, 2*components): vals[i] = random.random() * (END - START) + START vals[(2*components):(3*components)] = \ invgamma.rvs(A, LOC, SCALE, size=components) return vals
def sample_gp(t, cov_func): ''' Draws samples from a gaussian process with covariance given by cov_func. cov_func should be a function of 2 variables e.g. cov_func(tx, ty). For the x,y coordinates of the matrix. If the underlying covariance function requires more than 2 arguments, then they should be passed via a lambda function. ''' tx, ty = np.meshgrid(t, t) cov = cov_func(tx, ty) return norm.rvs(cov = np.array(cov))
def mc_spearmanr(x, y, cov, N=1000): mu = np.asarray(zip(x, y)) rs = list() for i in range(N): rvs = list() for m, s in zip(mu, cov): rv = multivariate_normal.rvs(mean=m, cov=s) rvs.append(rv) rvs = np.asarray(rvs) rs.append(spearmanr(*rvs.T)) rs = np.asarray(rs)[:, 0] return rs.mean(), rs.std()
def build_toy_dataset(N, V): """A simulator mimicking the data set from 2015-2016 NBA season with 308 NBA players and ~150,000 shots.""" L = np.tril(np.random.normal(2.5, 0.1, size=[V, V])) K = np.matmul(L, L.T) x = np.zeros([N, V]) for n in range(N): f_n = multivariate_normal.rvs(cov=K, size=1) for v in range(V): x[n, v] = poisson.rvs(mu=np.exp(f_n[v]), size=1) return x
def multivariate_t(nu, mu, sigma): """ Density of the multivariate t distribution with nu degress of freedom. Keyword parameters: nu -- degress of fredom mu -- location sigma -- scale """ d = len(mu) Y = multivariate_normal.rvs(mean=np.zeros(d), cov=sigma) U = chi2.rvs(nu) return mu + Y*np.sqrt(nu/U)
def InitialGP(DistanceMatrix,parameter=np.array((1,1))): ''' #The cholesky decomposition for corvarianceMatrix #Sigma=L%*%L.T cholesky decomposition #we want get sample~MVN(0,Sigma) #so get sample~MVN(0,Identity(n))%*%L ''' num_people=DistanceMatrix.shape[0] cov=CovarianceMatrix(DistanceMatrix,parameter) n=cov.shape[0] cho=np.linalg.cholesky(cov) sample=multivariate_normal.rvs(np.zeros(n),np.identity(n)) sample=sample.dot(cho) return sample
def test_sanity_fit(self): mu, P = r_[0., 0.], c_[[2., .2], [.2, 2.]] L = cholesky(P) Td, k = 100, 2 X = mvn.rvs(mu, inv(P), Td).T theta = GaussParams(zeros(2), eye(2), 1.) theta_star, Y = self.model.fit_nce( X, k, mu_noise=randn(2), L_noise=(rand() + 1) * eye(2), mu0=mu, L0=L, maxnumlinesearch=2000, verbose=False) noise = self.model.params_noise self.assertLess(NceGauss.J(X, Y, noise.mu, noise.L, *theta_star), NceGauss.J(X, Y, noise.mu, noise.L, *theta)) self.assertLess(np.sum(params_to_vec( *NceGauss.dJ(X, Y, noise.mu, noise.L, *theta_star)) ** 2), 1e-6)
def test_large_sample(): # Generate large sample and compare sample mean and sample covariance # with mean and covariance matrix. np.random.seed(2846) n = 3 mean = np.random.randn(n) M = np.random.randn(n, n) cov = np.dot(M, M.T) size = 5000 sample = multivariate_normal.rvs(mean, cov, size) assert_allclose(numpy.cov(sample.T), cov, rtol=1e-1) assert_allclose(sample.mean(0), mean, rtol=1e-1)
def sample_observ(self, state): """Sample a sequence of observations from the prior""" # Get system matrices H = self.observation_matrix() R = self.observation_covariance() # Initialise observation sequence num_time_instants = len(state) observ = np.zeros((num_time_instants,self.do)) # Loop through time, sampling each observation for kk in range(num_time_instants): observ[kk] = mvn.rvs( mean=np.dot(H,state[kk]), cov=R ) return observ
def IntegralApprox(y,lam,r,beta,w,G=1,size=100): """estimates the integral in Eq.17 of Rasmussen (2000)""" temp = np.zeros(len(y)) inv_betaw = inv(beta*w) inv_r = inv(r) i = 0 bad = 0 while i<size: mu = mv_norm.rvs(mean=lam,cov=inv_r,size=1) s = drawWishart(float(beta),inv_betaw) try: temp += mv_norm.pdf(y,mean=np.squeeze(mu),cov=G*inv(s)) except: bad += 1 pass i += 1 return temp/float(size)
def backward_simulation(self, flt): """Use backward simulation to sample from the state joint posterior""" # Get system matrices F = self.transition_matrix() Q = self.transition_covariance() + 1E-10*np.identity(self.ds) # Initialise sampled sequence num_time_instants = flt.num_time_instants x = np.zeros((num_time_instants, self.ds)) # Loop through time instatnts, sampling each state for kk in reversed(range(num_time_instants)): if kk < num_time_instants-1: samp_dens,_ = kal.correct(flt.get_instant(kk), x[kk+1], F, Q) else: samp_dens = flt.get_instant(kk) x[kk] = mvn.rvs(mean=samp_dens.mn, cov=samp_dens.vr) return x
def sample_GP(num_functions,n_pts,mean,cov): ''' From updated mean and covariance, sample the num_functions from the prescribed GP INPUTS: num_functions: the number of functions you want to sample from the GP n_pts : the number of points to sample for each function mean : the updated mean of the GP cov : the updated covariance of the GP OUTPUTS: sampled_func : ndarray where the columns are the sampled functions ------------------------------------------------------------------------ Notes: Written by TWK to remove multiple instances of the same/similar loop to do this ''' sampled_func = np.zeros((n_pts,num_functions)) for ii in xrange(num_functions): # Sample the function from a multivariate normal, prescribed by mean and cov sampled_func[:,ii] = multivariate_normal.rvs(mean=mean,cov=cov) return sampled_func
def fit(self, X, y): # If polynomial transformation if self.poly_degree: X = polynomial_features(X, degree=self.poly_degree) n_samples, n_features = np.shape(X) X_X = X.T.dot(X) # Least squares approximate of beta beta_hat = np.linalg.pinv(X_X).dot(X.T).dot(y) # The posterior parameters can be determined analytically since we assume # conjugate priors for the likelihoods. # Normal prior / likelihood => Normal posterior mu_n = np.linalg.pinv(X_X + self.omega0).dot(X_X.dot(beta_hat)+self.omega0.dot(self.mu0)) omega_n = X_X + self.omega0 # Scaled inverse chi-squared prior / likelihood => Scaled inverse chi-squared posterior nu_n = self.nu0 + n_samples sigma_sq_n = (1.0/nu_n)*(self.nu0*self.sigma_sq0 + \ (y.T.dot(y) + self.mu0.T.dot(self.omega0).dot(self.mu0) - mu_n.T.dot(omega_n.dot(mu_n)))) # Simulate parameter values for n_draws beta_draws = np.empty((self.n_draws, n_features)) for i in range(self.n_draws): sigma_sq = self._draw_scaled_inv_chi_sq(n=1, df=nu_n, scale=sigma_sq_n) beta = multivariate_normal.rvs(size=1, mean=mu_n[:,0], cov=sigma_sq*np.linalg.pinv(omega_n)) # Save parameter draws beta_draws[i, :] = beta # Select the mean of the simulated variables as the ones used to make predictions self.w = np.mean(beta_draws, axis=0) # Lower and upper boundary of the credible interval l_eti = 50 - self.cred_int/2 u_eti = 50 + self.cred_int/2 self.eti = np.array([[np.percentile(beta_draws[:,i], q=l_eti), np.percentile(beta_draws[:,i], q=u_eti)] \ for i in range(n_features)])
def GPGenerate(self, predict_range = ((0, 1), (0, 1)), num_samples = (20, 20), seed = 142857): """ Generates a draw from the gaussian process @param predict_range - map range for each dimension @param num_samples - number of samples for each dimension @return dict mapping locations to values """ assert(len(predict_range) == len(num_samples)) # Number of dimensions of the multivariate gaussian is equal to the number of grid points ndims = len(num_samples) grid_res = [float(predict_range[x][1]-predict_range[x][0])/float(num_samples[x]) for x in xrange(ndims)] npoints = reduce(lambda a, b: a * b, num_samples) # Mean function is assumed to be zero u = np.zeros(npoints) # List of points grid1dim = [slice(predict_range[x][0],predict_range[x][1],grid_res[x]) for x in xrange(ndims)] grids = np.mgrid[grid1dim] points = grids.reshape(ndims, -1).T #print points #raw_input() assert points.shape[0] == npoints # construct covariance matrix cov_mat = self.CovarianceMesh(points, points) # Draw vector np.random.seed(seed=seed) drawn_vector = multivariate_normal.rvs(mean=u, cov=cov_mat) assert drawn_vector.shape[0] == npoints return MapValueDict(points, drawn_vector)
def myMcMcSampler_mwg(lnlike, xinit, Vinit, draws, burn, damper, aopt, *args): nb = np.shape(xinit)[1] xold = xinit lam = 2.38**2/nb*np.ones((1,nb)) old = lnlike(xold, *args) val = [] Accept = np.zeros((1,nb)) alpha = np.zeros((1,nb)) xs = [] mu = np.array(xold).flatten() for i in tqdm(range(0,draws), leave=True): accept = np.zeros((1,nb)) for j in range(0,nb): xpro = np.copy(xold) xpro[0,j] = xold[0,j]+multivariate_normal.rvs(0)*np.sqrt(Vinit[j,j])*lam[0,j] xpro = np.reshape(np.matrix(xpro),(1,-1)) pro = lnlike(xpro, *args) if np.isnan(xpro[0,j]): alpha[0,j] = 0 elif pro > old: alpha[0,j] = 1 else: alpha[0,j] = np.exp(pro - old) if np.random.uniform(0,1) < alpha[0,j]: old = pro xold = xpro accept[0,j] = 1 lam = lam*np.exp(1/(i+1)**damper*(alpha-aopt)) xs.append(xold) val.append(old) Accept = np.vstack((Accept,accept)) return xs, val, Accept
def fit(self, X, y): X_X = X.T.dot(X) # Least squares approximate of beta beta_hat = np.linalg.pinv(X_X).dot(X.T).dot(y) # The posterior parameters can be determined analytically since we assume conjugate priors for the likelihoods # Normal prior / likelihood => Normal posterior mu_n = np.linalg.pinv(X_X + self.omega0).dot(X_X.dot(beta_hat) + self.omega0.dot(self.mu0)) omega_n = X_X + self.omega0 # Scaled inverse chi-squared prior / likelihood => Scaled inverse chi-squared posterior nu_n = self.nu0 + np.shape(X)[0] sigma_sq_n = (1.0 / nu_n) * (self.nu0 * self.sigma_sq0 + (y.T.dot(y) + self.mu0.T.dot(self.omega0).dot(self.mu0) - mu_n.T.dot(omega_n.dot(mu_n)))) # Simulate parameter values for n_iter beta_draws = np.empty((self.n_iterations, np.shape(X)[1])) for i in range(self.n_iterations): # Allows for simulation from the scaled inverse chi squared distribution. X = chi2.rvs(size=1, df=nu_n) sigma_sq = nu_n * sigma_sq_n / X beta = multivariate_normal.rvs(size=1, mean=mu_n[:, 0], cov=sigma_sq * np.linalg.pinv(omega_n)) beta_draws[i, :] = beta self.w = np.mean(beta_draws, axis=0)
H_Q = ('iw', df, df * np.diag( (transX_object_1sd**2, transY_object_1sd**2, rotAngle_object_1sd**2))) H_x = ('mvnL', np.eye(dxA), .01 * np.eye(dxA)) H_S = ('iw', df, df * np.diag( (transX_parts_1sd**2, transY_parts_1sd**2, rotAngle_parts_1sd**2))) # be careful with initial part configuration prior H_theta = ('mvnL', np.eye(dxA), np.diag((10**2, 10**2, 1**2))) H_E = ('iw', df, df * np.diag((5, 1))) # true parameters zero = np.zeros(dxA) Q = np.diag(np.diag(iw.rvs(*H_Q[1:]))) x = np.zeros((T, ) + dxGm) x[0] = m.expm(m.alg(mvn.rvs(m.algi(m.logm(H_x[1])), H_x[2]))) for t in range(1, T): xVec = mvn.rvs(zero, Q) x[t] = x[t - 1].dot(m.expm(m.alg(xVec))) # part E = np.stack([np.diag(np.diag(iw.rvs(*H_E[1:]))) for k in range(K)]) S = np.stack([iw.rvs(*H_S[1:]) for k in range(K)]) theta = np.zeros((T, K) + dxGm) for k in range(K): theta[0, k] = m.expm(m.alg(mvn.rvs(m.algi(m.logm(H_theta[1])), H_theta[2]))) for t in range(1, T):
[0.5, 0, 2, 0, 0, 1], [1, 0, 0, 3, 0, 0], [0, 0.5, 0, 0, 1, 0], [0.5, 0, 1, 0, 0, 2]]) # Reference measure REF = tensap.RandomVector( [tensap.UniformRandomVariable(-x, x) for x in 5 * np.diag(S)]) # Density, defined with respect to the reference measure def U(x): return multivariate_normal.pdf(x, np.zeros(ORDER), S) * \ np.prod(2*5*np.diag(S)) # %% Training and test samples NUM_TRAIN = 1e5 # Training sample size X_TRAIN = multivariate_normal.rvs(np.zeros(ORDER), S, int(NUM_TRAIN)) NUM_TEST = 1e4 # Test sample size X_TEST = multivariate_normal.rvs(np.zeros(ORDER), S, int(NUM_TEST)) NUM_REF = 1e4 # Size of the sample used to compute the L2 error XI = REF.random(NUM_REF) # %% Approximation basis DEGREE = 20 # Orthonormal bases in each dimension, with respect to the reference measure BASES = [ tensap.PolynomialFunctionalBasis(x.orthonormal_polynomials(), range(DEGREE + 1)) for x in REF.random_variables ]
def test_ABC(k1, k2, k3): #Generate a model model = MM_SSA(k1, k2, k3, max_time=10) #Set threshold epsilon = 1200 #Generate a random trajectory - considered real trajectory model.SSA() T = model.get_trajectory() #Get number of samples n_samples = len(model.time) #Initialize index i = 0 #Initialize first prior mutlivariate Gaussian distribution (mean, covariance) # theta_guess = np.random.uniform(0, 1, (3,3)) theta_guess = np.array([[k1 - 0.1, k2 - 0.002, k3 - 0.03], [k1 - 0.04, k2 - 0.05, k3], [k1 - 0.7, k2 - 0.01, k3 + 0.2]]) #theta_guess = np.array([k1-0.01, k2-0.02, k3-0.1]).reshape(1,3) # distribution = { 'mean': np.mean(theta_guess, axis=0), 'cov': np.cov(theta_guess, rowvar=True) } #Create the theta_list theta = theta_guess print('Starting example') print( multivariate_normal.rvs(size=1, mean=distribution['mean'], cov=distribution['cov']).reshape(1, 3)) while (i < 400): #Generate a random set (k1,k2,k3) by prior distribution which is GOOD trails_500 = 0 trails_1000 = 0 while (True): #print('Entered Loop') theta_guess = multivariate_normal.rvs( size=1, mean=distribution['mean'], cov=distribution['cov']).reshape(1, 3) if (trails_500 > 100): theta_guess = distribution['mean'].reshape(1, 3) #print('Blocked here 1') model_guess = MM_SSA(*theta_guess[0], max_time=n_samples + 1) #print('Blocked here 2') model_guess.SSA() #print('Blocked here 3') T_star = model_guess.get_trajectory() #print('Blocked here 4') if (epsilon_test(np.array(T), np.array(T_star), epsilon)): theta = np.append(theta, theta_guess, axis=0) epsilon = (manhattan_distance(np.array(T), np.array(T_star)) + 12 * epsilon) / 13 break if (trails_500 > 3999): trails_500 = 0 epsilon = ( 2 * manhattan_distance(np.array(T), np.array(T_star)) + epsilon) / 3 elif (trails_1000 > 4000): return distribution trails_500, trails_1000 = trails_500 + 1, trails_1000 + 1 #print(f'{trails} trails') print(f'Accepted with epsilon = {epsilon}') #Create the new posterior distribution #Prior Distribution prior_distribution = distribution #Likelihood D try: mean = np.mean(theta, axis=0) cov = np.cov(theta, rowvar=0) likelihood_distribution = {'mean': mean, 'cov': cov} except: likelihood_distribution = {'mean': mean, 'cov': cov} finally: #Update new posterior distribution distribution = { 'mean': Prod_Multivar_Normal(prior_distribution, likelihood_distribution)[0], 'cov': Prod_Multivar_Normal(prior_distribution, likelihood_distribution)[1] } print(f'i= {i}') #Update index i = i + 1 return distribution
def sample(self, num_sample): dw_sample = normal.rvs( size=[num_sample, self.num_time_interval]) * self.sqrth return dw_sample[:, np.newaxis, :]
def plot_vi_logistics(number_points_per_class, interactive=False, interval_plot=1, number_iterations=1000): from distribution_prediction.blackbox_vi.blackbox_vi_logistics import variational_inference_logistics def _plot(fig, ax1, ax2, mean, sigma, array_samples_theta, interactive=False): colorbar = None colorbar_2 = None plt.gca() # plt.cla() # plt.clf() fig.clear() fig.add_axes(ax1) fig.add_axes(ax2) plt.cla() xlim = (-5., 5.) ylim = (-5., 5.) xlist = np.linspace(*xlim, 100) ylist = np.linspace(*ylim, 100) X_, Y_ = np.meshgrid(xlist, ylist) Z = np.dstack((X_, Y_)) Z = Z.reshape(-1, 2) predictions = onp.mean(probability_class_1(Z, array_samples_theta), axis=1) predictions = predictions.reshape(100, 100) # print("finished") ax1.clear() if np.size(predictions): CS = ax1.contourf(X_, Y_, predictions, cmap="cividis") ax1.scatter(X_1[:, 0], X_1[:, 1]) ax1.scatter(X_2[:, 0], X_2[:, 1]) ax1.set_xlim(*xlim) ax1.set_ylim(*ylim) ax1.set_title("Predicted probability of belonging to C_1") ax3 = fig.add_axes(Bbox([[0.43, 0.11], [0.453, 0.88]])) if np.size(predictions): colorbar = fig.colorbar( CS, cax=ax3, ) ax1.set_position(Bbox([[0.125, 0.11], [0.39, 0.88]])) x_prior = np.linspace(-3, 3, 100) y_prior = np.linspace(-3, 3, 100) X_prior, Y_prior = np.meshgrid(x_prior, y_prior) Z = np.dstack((X_prior, Y_prior)) Z = Z.reshape(-1, 2) prior_values = multivariate_normal.pdf(Z, np.zeros(2), np.identity(2)) prior_values = prior_values.reshape(100, 100) std_x = onp.sqrt(sigma[0, 0]) std_y = onp.sqrt(sigma[1, 1]) x_posterior = np.linspace(mean[0] - 3 * std_x, mean[0] + 3 * std_x, 100) y_posterior = np.linspace(mean[1] - 3 * std_y, mean[1] + 3 * std_y, 100) X_post, Y_post = np.meshgrid(x_posterior, y_posterior) Z_post = np.dstack((X_post, Y_post)).reshape(-1, 2) posterior_values = multivariate_normal.pdf(Z_post, mean, sigma) posterior_values = posterior_values.reshape(100, 100) ax2.contour(X_post, Y_post, posterior_values) ax2.contour(X_, Y_, prior_values, cmap="inferno") ax2.set_title("Two contour plots respectively showing\n" "The prior and the approximated posterior distributions") plt.pause(0.001) if interactive: if np.size(predictions): colorbar.remove() return True mean_1 = onp.array([-2, 2]) mean_2 = onp.array([2, -2]) X_1 = onp.random.randn(number_points_per_class, 2) + mean_1 X_2 = onp.random.randn(number_points_per_class, 2) + mean_2 X = onp.vstack((X_1, X_2)) y_1 = onp.ones(shape=(X_1.shape[0], 1)) y_2 = onp.zeros(shape=(X_2.shape[0], 1)) y = onp.vstack((y_1, y_2)) fig, (ax1, ax2) = plt.subplots(1, 2) ####################### mean, sigma = None, None count = 0 for mean, sigma, *_ in variational_inference_logistics( X, y, 50, sigma_prior=1., number_iterations=number_iterations): mean = mean.flatten() array_samples_theta = multivariate_normal.rvs(mean, sigma, 1000) if interactive and count % interval_plot == 0: _plot(fig, ax1, ax2, mean, sigma, array_samples_theta, interactive=interactive) count += 1 else: array_samples_theta = multivariate_normal.rvs(mean, sigma, 1000) _plot(fig, ax1, ax2, mean, sigma, array_samples_theta, interactive=False) ######################## xlim = (-4., 4.) ylim = (-4., 4.) xlist = onp.linspace(*xlim, 100) ylist = onp.linspace(*ylim, 100) X_, Y_ = onp.meshgrid(xlist, ylist) Z = onp.dstack((X_, Y_)) Z = Z.reshape(-1, 2) predictions = onp.mean(probability_class_1(Z, array_samples_theta), axis=1) predictions = predictions.reshape(100, 100) print("finished") fig, ax = plt.subplots(1, 1) CS = plt.contourf(X_, Y_, predictions, levels=20) colorbar = plt.colorbar(CS) plt.scatter(X_1[:, 0], X_1[:, 1]) plt.scatter(X_2[:, 0], X_2[:, 1]) plt.xlim(*xlim) plt.ylim(*ylim) plt.show()
def sample(self, n_samples=2000, observed_states=None, random_state=None): """Generate random samples from the self. Parameters ---------- n : int Number of samples to generate. observed_states : array If provided, states are not sampled. random_state: RandomState or an int seed A random number generator instance. If None is given, the object's random_state is used Returns ------- samples : array_like, length (``n_samples``, ``n_features``) List of samples states : array_like, shape (``n_samples``) List of hidden states (accounting for tied states by giving them the same index) """ if random_state is None: random_state = self.random_state random_state = check_random_state(random_state) samples = np.zeros((n_samples, self.n_features)) states = np.zeros(n_samples) if observed_states is None: startprob_pdf = np.exp(np.copy(self._log_startprob)) startdist = stats.rv_discrete(name='custm', values=(np.arange(startprob_pdf.shape[0]), startprob_pdf), seed=random_state) states[0] = startdist.rvs(size=1)[0] transmat_pdf = np.exp(np.copy(self._log_transmat)) transmat_cdf = np.cumsum(transmat_pdf, 1) nrand = random_state.rand(n_samples) for idx in range(1,n_samples): newstate = (transmat_cdf[int(states[idx-1])] > nrand[idx-1]).argmax() states[idx] = newstate else: states = observed_states mu = np.copy(self._mu_) precision = np.copy(self._precision_) for idx in range(n_samples): mean_ = mu[states[idx]] covar_ = np.linalg.inv(precision[states[idx]]) samples[idx] = multivariate_normal.rvs(mean=mean_, cov=covar_, random_state=random_state) states = self._process_sequence(states) return samples, states
empiricalCorrelations = ['r', 'r_S', 'r_Q'] variateCorrelations = [[[[0 for l in range(len(empiricalCorrelations))] for k in range(len(measuares))] for j in range(len(sizes))] for i in range(len(correlations_1))] mixedVariateCorrelations = [[[0 for l in range(len(empiricalCorrelations))] for k in range(len(measuares))] for j in range(len(sizes))] for i in range(len(correlations_1)): covariance = Covariance(variances_1, [[1, correlations_1[i]], [correlations_1[i], 1]]) for j in range(len(sizes)): P, S, Q = [], [], [] for m in range(count): rvs = multivariate_normal.rvs(means, covariance, sizes[i]) x, y = rvs[:, 0], rvs[:, 1] p, tmp = pearsonr(x, y) s, tmp = spearmanr(x, y) P.append(p) S.append(s) Q.append(QuadrantCorrelation(x, y)) empCors = [P, S, Q] for k in range(len(measuares)): for l in range(len(empCors)): variateCorrelations[i][j][k][l] = measuares[k][1](empCors[l]) mixedCovariances = [Covariance(variances_1, [[1, correlations_2[0]], [correlations_2[0], 1]]), Covariance(variances_2, [[1, correlations_2[1]], [correlations_2[1], 1]])] for j in range(len(sizes)): P, S, Q = [], [], []
# why 2 * 2? # we need 2 components for the mean, # and 2 components for the standard deviation! # ignore bias terms for simplicity. def forward(x, W1, W2): hidden = np.tanh(x.dot(W1)) output = hidden.dot(W2) # no activation! mean = output[:2] stddev = softplus(output[2:]) return mean, stddev # make a random input x = np.random.randn(4) # get the parameters of the Gaussian mean, stddev = forward(x, W1, W2) print("mean:", mean) print("stddev:", stddev) # draw samples samples = mvn.rvs(mean=mean, cov=stddev**2, size=10000) # plot the samples plt.scatter(samples[:, 0], samples[:, 1], alpha=0.5) plt.show()
for k, (mean, covar, color) in enumerate(zip(Mu_p, Sig_p, color_iter)): v, w = linalg.eigh(covar) u = w[0] / linalg.norm(w[0]) angle = np.arctan(u[1] / u[0]) angle = 180 * angle / np.pi # convert to degrees ell = mpl.patches.Ellipse([mean[1], mean[0]], v[0], v[1], 180 + angle, color=color) ell.set_clip_box(splot.bbox) ell.set_alpha(0.5) #splot.add_artist(ell) #ガウス分布から大量にサンプリングしてプロットする場合 for i in range(int(5000 * 2 * pi_p[k])): #)):# X = multivariate_normal.rvs(mean=mean, cov=covar) plt.scatter(X[1], X[0], s=5, marker='.', color=color, alpha=0.2) #データをクラスごとに色分けしてプロットする場合 for i in range(len(p_temp)): plt.scatter(p_temp[i][1], p_temp[i][0], marker='x', c=colors[zp[i]]) """ # Number of samples per component n_samples = 500 # Generate random sample, two components np.random.seed(0) C = np.array([[0., -0.1], [1.7, .4]]) X = np.r_[np.dot(np.random.randn(n_samples, 2), C), .7 * np.random.randn(n_samples, 2) + np.array([-6, 3])]
row_sums = tmpprod.sum(axis=1) new_matrix = tmpprod / row_sums _inputs = new_matrix if _inputs.shape[0] > 2: inputs = Variable(torch.from_numpy( _inputs[:-2]).float()).unsqueeze( 2) # use all orders excpet the last one for each user targets = Variable( torch.from_numpy(_inputs[1:-1]).float().unsqueeze( 2)) # use shifted input as output daysS = np.asarray([dictOrderIDDayS[x] for x in tmp])[:-1] user_g = torch.from_numpy( multivariate_normal.rvs( mean=mu[iter], cov=np.diag(np.exp(np.asarray(logvar[iter]))))).float() rhot = Variable(torch.from_numpy(pow((t0 + daysS), -kappa)), requires_grad=False).float() # Use teacher forcing 50% of the time force = random.random() < 0.5 outputs, hidden, mutmp, logvartmp, recon_batch = model( inputs, user_g, rhot, None, force) mu[iter] = mutmp.detach().numpy() logvar[iter] = logvartmp.detach().numpy()[0] optimizer.zero_grad() loss = lossFun_vae(recon_batch, targets, mutmp, logvartmp, inputsize, outputs) loss.backward()
from scipy.stats import rv_discrete import numpy as np import matplotlib.pyplot as plt K = 3 N = 1000 mean = [np.array([0, 0]), np.array([10, 3]), np.array([5, 9])] cov = [ np.array([[1, 0], [0, 2]]), np.array([[5, 1.5], [1, 2]]), np.array([[1, 2], [2, 7]]) ] vk = np.arange(K) pk = (0.3, 0.4, 0.3) sk = np.zeros((N, K)) cat = rv_discrete(name='cat', values=(vk, pk)) xs = [] ys = [] for i in range(N): sk = cat.rvs() (x, y) = mult.rvs(mean=mean[sk], cov=cov[sk]) xs = xs + [x] ys = ys + [y] plt.scatter(xs, ys) plt.show()
def bivar(graphtype=2, mubass=[4, 3], sigmabass=[[1, 0.75], [0.75, 1]], musalmon=[2, 2], sigmasalmon=[[1, 0], [0, 1]], interactive=False): # Compute probability of (x,y) for a given mu and sigma def getprob(x, y, mu, sigma): vec = np.matrix([x, y]) mua = np.matrix(mu) E = 2.0 * np.pi * np.sqrt(np.linalg.det(sigma)) P = (1/E) * np.exp(-1 * ((vec-mua) * np.linalg.inv(sigma) * (vec.T-mua.T) / 2.0)) return float(P) # initialise default parameters Nbass = 100 Nsalmon = 100 # Generate distribution surface data bass = multivariate_normal.rvs(mubass,sigmabass,Nbass); salmon = multivariate_normal.rvs(musalmon,sigmasalmon,Nsalmon); x = np.arange(0, 6.1, 0.1) y = np.arange(0, 6.1, 0.1) Pbass = np.zeros((len(y), len(x))) Psalmon = np.zeros((len(y), len(x))) for i in range(len(x)): for j in range(len(y)): Pbass[j,i] = getprob(x[i], y[j], mubass, sigmabass) Psalmon[j,i] = getprob(x[i], y[j], musalmon, sigmasalmon) Pb = (1 / (2 * np.pi * np.sqrt(np.linalg.det(sigmabass )))) * np.exp(-1/2.0) Ps = (1 / (2 * np.pi * np.sqrt(np.linalg.det(sigmasalmon)))) * np.exp(-1/2.0) LR = np.divide(Pbass, Psalmon) # Do the plotting if interactive: plt.ion() if graphtype == 2: # 2D scatter plots and standard deviation contours fig2D = plt.figure() ax = fig2D.add_subplot( 111 ) ax.grid(True) ax.scatter(bass[:,0], bass[:,1], color='k',) ax.scatter(salmon[:,0], salmon[:,1], color='r') ax.contour(x, y, Pbass, [Pb], colors='k') ax.contour(x, y, Psalmon, [Ps], colors='r') ax.contour(x, y, LR, [1], colors='g') elif graphtype == 32: # 2D projection of 3D surfaces + standard deviation contours fig = plt.figure() ax = fig.add_subplot(111) vmin=np.min(np.nanmin(Pbass), np.nanmin(Psalmon)) vmax=np.max(np.nanmax(Pbass), np.nanmax(Psalmon)) Pbs = np.maximum(Pbass, Psalmon) ax.contourf(x, y, Pbs, vmin=vmin, vmax=vmax) # ax.contourf(x, y, Psalmon, vmin=vmin, vmax=vmax, alpha=.6) # ax.contourf(x, y, Pbass, vmin=vmin, vmax=vmax, alpha=.4) ax.contour(x, y, Pbass, [Pb], colors='k', linewidths=3) ax.contour(x, y, Psalmon, [Ps], colors='r', linewidths=3) ax.contour(x, y, LR, [1], colors='g', linewidths=3) elif graphtype == 3: # 3D surfaces and standard deviation contours fig = plt.figure() ax = fig.add_subplot(111, projection='3d') Pbs = np.maximum(Pbass, Psalmon) vmin=np.nanmin(Pbs) vmax=np.nanmax(Pbs) xl, yl= np.meshgrid(x, y) ax.plot_surface(xl, yl, Pbs,\ vmin=vmin, vmax=vmax,\ rstride=1, cstride=1, cmap=plt.cm.jet, alpha=0.3, linewidth=0, antialiased=False) ax.contour(xl, yl, Pbass, [Pb], colors='k', linewidths=5) ax.contour(xl, yl, Psalmon, [Ps], colors='r', linewidths=5) ax.contour(xl, yl, LR, [1], offset=0, colors='g', linewidths=5) ax.set_zlim(0, 1.5*vmax) ax.view_init(elev=90, azim=-90) else: print("Unknown plotting option: %d" % graphtype) if interactive: plt.draw() else: plt.show()
g = 9.81 ### grav. zrychlení [m/s^2] dt = 1 # čas. krok v sekundách A = np.array([[1, dt], [0, 1]]) # matice A B = np.array([-.5 * dt, -dt]) # matice B u = g # pro forma, g=u var_wht = 10 ### variance šumu na výšce w_ht var_wvt = 10 ### variance šumu na rychlosti w_vt var_y = 90000 ### variance šumu měření v_t x = np.zeros((2, ndat)) x[:, 0] = [h0, v0] y = np.zeros(ndat) for t in range(1, ndat): x[:, t] = A.dot(x[:, t - 1]) + B.dot(u) x[:, t] += mvn.rvs(cov=np.diag([var_wht, var_wvt])) y[t] = x[0, t] + norm.rvs(scale=np.sqrt(var_y)) #--------- Q = np.diag([var_wht, var_wvt]) # Toto už jsme v simulaci také udělali R = var_y H = np.array([1, 0]) filtr = KF(A=A, B=B, H=H, R=R, Q=Q) # Instance Kalmanova filtru for yt in y: filtr.predict(u) filtr.update(yt) filtr.log() # input('asdf') log_x = np.array(filtr.log_x).T #----------
def draw_MVNormal(mean=0, cov=1, size=1): """ returns multivariate normally distributed samples """ return mv_norm.rvs(mean=mean, cov=cov, size=size)
def test1(): """クラスタリング法1のアルゴリズム""" # データの生成 data_count = 500 data = np.zeros([5, data_count // 5, 2]) for cc in range(data.shape[0]): centers = [[0.0, 0.0], [0.0, 1.0], [0.5, 0.5], [1.0, 0.0], [1.0, 1.0]][cc] data[cc] = np.random.randn(data.shape[1], 2) + np.array(centers) * 10 - 5 data = data.reshape([-1, 2]) n = len(data) # (12.33), (12.34) のハイパーパラメータ(少し改ざん) alpha = 3 # 書籍は1 beta = 1 / 3 nu = 3 # 書籍は15 S = np.array([[0.1, 0], [0, 0.1]]) S_inv = inv(S) # 初期設定 s = np.zeros([n], dtype=int) c = 1 n_i = np.array([n], dtype=int) mu0 = data.mean(axis=0) Lambda_all = inv(np.cov(data.T)) thetas = [(mu0, Lambda_all)] p_max = -np.inf best_s = s best_thetas = thetas # (12.35) の不変部分 v12_35 = beta / ((1 + beta) * np.pi) * gamma( (nu + 1) / 2) / (det(S)**(nu / 2) * gamma((nu + 1 - 2) / 2)) for iteration in range(100): # 所属クラスタの更新 for k in range(n): Sb = inv(S_inv + beta / (1 + beta) * np.outer(data[k] - mu0, data[k] - mu0)) probs = [(n_i[i] - (1 if s[k] == i else 0)) * p_x_theta(data[k], thetas[i]) for i in range(c)] probs.append(alpha * v12_35 * det(Sb)**((nu + 1) / 2)) probs = np.array(probs, dtype=float) probs /= probs.sum() new_c = np.random.choice(c + 1, p=probs) old_c = s[k] s[k] = new_c if new_c == c: n_i = np.concatenate((n_i, [1])) thetas.append((data[k], Lambda_all)) c += 1 else: n_i[new_c] += 1 n_i[old_c] -= 1 if n_i[old_c] == 0: n_i = np.delete(n_i, old_c) del thetas[old_c] c -= 1 s[np.where(s >= old_c)] -= 1 # 各クラスタのパラメータの更新 x_bar = np.zeros([c, 2]) mu_c = np.zeros([c, 2]) for k in range(n): x_bar[s[k]] += data[k] for i in range(c): mu_c[i] = (x_bar[i] + beta * mu0) / (n_i[i] + beta) x_bar[i] /= n_i[i] Sq_inv = np.zeros((c, ) + S.shape) + S_inv for i in range(c): Sq_inv[i] += n_i[i] * beta / (n_i[i] + beta) * np.outer( x_bar[i] - mu0, x_bar[i] - mu0) for k in range(n): Sq_inv[s[k]] += np.outer(data[k] - x_bar[s[k]], data[k] - x_bar[s[k]]) for i in range(c): nu_c = nu + n_i[i] Lambda_i = wishart.rvs(nu_c, inv(Sq_inv[i])) Lambda_c = (n_i[i] + beta) * Lambda_i mu_i = multivariate_normal.rvs(mu_c[i], inv(Lambda_c)) thetas[i] = (mu_i, Lambda_i) # 事後確率最大化(対数を取っています) v = log_p_s(alpha, n_i, c, n) v += sum(math.log(g0(theta, mu0, beta, nu, S)) for theta in thetas) v += sum(math.log(p_x_theta(data[k], thetas[s[k]])) for k in range(n)) if v > p_max: p_max = v best_s = copy.copy(s) best_thetas = copy.copy(thetas) print("p_max", p_max) for i in range(c): print("\t", thetas[i][0]) # Matplotlib で表示 plt.plot(data[:, 0], data[:, 1], "ro") for i in range(len(best_thetas)): plot_cov_ellipse(best_thetas[i][1], best_thetas[i][0]) plt.show()
#Plot data plt.title('GMM') for i, color in colors: cluster = clusters[i - 1] plt.scatter([c[0] for c in cluster], [c[1] for c in cluster], color=color) #Plot centroids plt.scatter([c[0] for c in centroids], [c[1] for c in centroids], color='black') plt.show() # Non-convex clusters c1 = multivariate_normal.rvs(mean=[0, 2], cov=0.1, size=100) c1 = np.vstack((c1, multivariate_normal.rvs(mean=[0, 1], cov=0.1, size=100))) c1 = np.vstack((c1, multivariate_normal.rvs(mean=[0, 0], cov=0.1, size=100))) c1 = np.vstack((c1, multivariate_normal.rvs(mean=[1, 0], cov=0.1, size=100))) c1 = np.vstack((c1, multivariate_normal.rvs(mean=[2, 0], cov=0.1, size=100))) c1 = np.vstack((c1, multivariate_normal.rvs(mean=[3, 0], cov=0.1, size=100))) c2 = multivariate_normal.rvs(mean=[1.5, 2], cov=0.1, size=100) c2 = np.vstack((c2, multivariate_normal.rvs(mean=[2.5, 2], cov=0.1, size=100))) c2 = np.vstack((c2, multivariate_normal.rvs(mean=[3.5, 2], cov=0.1, size=100))) X = np.vstack((c1, c2)) g = mixture.GMM(n_components=2, covariance_type='full') g.fit(X)
C : array, shape = [n_samples] Predicted class label per sample. """ metric = SIM_METRICS[self.metric] # compute fuzzy similarity between samples and Decision templates dist = metric(X, self.DT_) return self.classes_.take(np.argmax(dist, axis=1), axis=0) if __name__ == "__main__": from sklearn.datasets import load_svmlight_file #X, y = load_svmlight_file("meta_level.svm") true_mean = [2, 4] true_cov = [[10, 5], [5, 10]] X = multivariate_normal.rvs(mean=true_mean, cov=true_cov, size=250, random_state=42) y = np.ones(X.shape[0]) N, D = X.shape vig = VIG() vig.fit(X, y) pred = vig.predict(X) print(pred) print(multivariate_normal.pdf(X, mean=true_mean, cov=true_cov))
truth.append( GroundTruthState(np.array([[xy[0]], [xy[1]]]), timestamp=start_time + timedelta(seconds=n))) #Plot the result ax.plot([state.state_vector[0, 0] for state in truth], [state.state_vector[1, 0] for state in truth], linestyle="--") from scipy.stats import multivariate_normal from stonesoup.types.detection import Detection measurements = [] for state in truth: x, y = multivariate_normal.rvs(state.state_vector.ravel(), cov=np.diag([0.75, 0.75])) measurements.append( Detection(np.array([[x], [y]]), timestamp=state.timestamp)) # Plot the result ax.scatter([state.state_vector[0, 0] for state in measurements], [state.state_vector[1, 0] for state in measurements], color='b') fig # Create Models and Kalman Filter from stonesoup.models.transition.linear import CombinedLinearGaussianTransitionModel, ConstantVelocity transition_model = CombinedLinearGaussianTransitionModel( (ConstantVelocity(0.05), ConstantVelocity(0.05))) transition_model.matrix(time_interval=timedelta(seconds=1)) transition_model.covar(time_interval=timedelta(seconds=1))
# u3 = np.array([16, 1]).T # cov1 = cov2 = cov3 = np.array([[5, 3],[3, 4]]) ## Q12c print("\nLetra c") u1 = np.array([1, 1]).T u2 = np.array([10, 5]).T u3 = np.array([11, 1]).T cov1 = cov2 = cov3 = np.array([[7, 4], [4, 5]]) # Create random data x1 = np.empty([N1, 2]) x2 = np.empty([N1, 2]) x3 = np.empty([N1, 2]) for i in range(N1): x1[i, :] = multivariate_normal.rvs(mean=u1, cov=cov1) x2[i, :] = multivariate_normal.rvs(mean=u2, cov=cov2) x3[i, :] = multivariate_normal.rvs(mean=u3, cov=cov3) x = np.reshape([x1, x2, x3], [N, 2]) y = np.reshape([np.zeros([N1, 1]), np.ones([N1, 1]), np.ones([N1, 1]) * 2], [ N, ]).astype(int) plt.scatter(x[:, 0], x[:, 1]) plt.show() ## Euclidean classifier # Class 1: y = 0
# %% # The next tutorial will go into much more detail on sampling methods. For the moment we'll just # assert that we're generating 2000 points from the state prediction above. # # We need these imports and parameters: from scipy.stats import multivariate_normal from stonesoup.types.particle import Particle from stonesoup.types.numeric import Probability # Similar to a float type from stonesoup.types.state import ParticleState number_particles = 2000 # Sample from the Gaussian prediction distribution samples = multivariate_normal.rvs(prediction.state_vector.ravel(), prediction.covar, size=number_particles) particles = [ Particle(sample.reshape(-1, 1), weight=Probability(1 / number_particles)) for sample in samples ] # Create prior particle state. pred_samples = ParticleState(particles, timestamp=start_time) from stonesoup.resampler.particle import SystematicResampler resampler = SystematicResampler() from stonesoup.updater.particle import ParticleUpdater pupdater = ParticleUpdater(measurement_model, resampler) predict_meas_samples = pupdater.predict_measurement(pred_samples)
def x_MC_samples(self): if self.method == 'Monte_Carlo': return multivariate_normal.rvs(self.x, self.cov_matrix, self.MC_sample_size) else: return print('x_MC_samples defined only for Monte_Carlo method')
# Initialise a prior # ^^^^^^^^^^^^^^^^^^ # To start we create a prior estimate. This is a set of :class:`~.Particle` and we sample from # Gaussian distribution (using the same parameters we had in the previous examples). from scipy.stats import multivariate_normal from stonesoup.types.particle import Particle from stonesoup.types.numeric import Probability # Similar to a float type from stonesoup.types.state import ParticleState number_particles = 200 # Sample from the prior Gaussian distribution samples = multivariate_normal.rvs(np.array([0, 1, 0, 1]), np.diag([1.5, 0.5, 1.5, 0.5]), size=number_particles) particles = [ Particle(sample.reshape(-1, 1), weight=Probability(1 / number_particles)) for sample in samples ] # Create prior particle state. prior = ParticleState(particles, timestamp=start_time) # %% # Run the tracker # ^^^^^^^^^^^^^^^ # We now run the predict and update steps, propagating the collection of particles and re-sampling # when told to (at every step).
x, y = np.meshgrid(np.linspace(-5, 5, 100), np.linspace(-5, 5, 100)) z = np.dstack((x, y)) # hyper parameter of prior distribution hp_m = np.array([0.0, 0.0]) hp_beta = 2.0 hp_W = np.array([[0.5, 1.0], [0.5, 1.5]]) hp_v = 2.0 # parameter of posterior gaussian distribution Sigma = np.array([[1.0, 0.5], [0.5, 2.0]]) mu = np.array([1.0, 1.0]) for j, N in enumerate([3, 10, 50]): # observed data X = multivariate_normal.rvs(mu, Sigma, N, random_state=1234) # update parameter sum_x = np.matrix(np.sum(X, axis=0)) new_beta = N + hp_beta new_m = (1 / new_beta) * (sum_x + hp_beta * hp_m) hp_m_ = np.asmatrix(hp_m).reshape(2, 1) new_m_ = np.asmatrix(new_m).reshape(2, 1) X_ = np.matrix(X).reshape(2, N) new_W_inv = (X_ * X_.T) + (hp_beta * hp_m_ * hp_m_.T) - ( new_beta * new_m_ * new_m_.T) + np.linalg.inv(hp_W) new_v = N + hp_v # posterior distribution new_Lambda = wishart.rvs(df=new_v, scale=np.linalg.inv(new_W_inv), size=N) new_mu = multivariate_normal.pdf(z, np.squeeze(np.asarray(new_m)), np.linalg.inv(new_beta * new_Lambda))
def draw(self, mode_size=1000): return np.vstack([ multivariate_normal.rvs(mean, self.cov, size=mode_size) for mean in self.means ])
def HtWtDataGenerator(nSubj, rndsd=None): # Specify parameters of multivariate normal (MVN) distributions. # Men: HtMmu = 69.18 HtMsd = 2.87 lnWtMmu = 5.14 lnWtMsd = 0.17 Mrho = 0.42 Mmean = np.array([HtMmu, lnWtMmu]) Msigma = np.array([[HtMsd**2, Mrho * HtMsd * lnWtMsd], [Mrho * HtMsd * lnWtMsd, lnWtMsd**2]]) # Women cluster 1: HtFmu1 = 63.11 HtFsd1 = 2.76 lnWtFmu1 = 5.06 lnWtFsd1 = 0.24 Frho1 = 0.41 prop1 = 0.46 Fmean1 = np.array([HtFmu1, lnWtFmu1]) Fsigma1 = np.array([[HtFsd1**2, Frho1 * HtFsd1 * lnWtFsd1], [Frho1 * HtFsd1 * lnWtFsd1, lnWtFsd1**2]]) # Women cluster 2: HtFmu2 = 64.36 HtFsd2 = 2.49 lnWtFmu2 = 4.86 lnWtFsd2 = 0.14 Frho2 = 0.44 prop2 = 1 - prop1 Fmean2 = np.array([HtFmu2, lnWtFmu2]) Fsigma2 = np.array([[HtFsd2**2, Frho2 * HtFsd2 * lnWtFsd2], [Frho2 * HtFsd2 * lnWtFsd2, lnWtFsd2**2]]) # Randomly generate data values from those MVN distributions. if rndsd is not None: np.random.seed(rndsd) datamatrix = np.zeros((nSubj, 3)) # arbitrary coding values maleval = 1 femaleval = 0 for i in range(0, nSubj): # Flip coin to decide sex sex = np.random.choice([maleval, femaleval], replace=True, p=(.5, .5), size=1) if sex == maleval: datum = multivariate_normal.rvs(mean=Mmean, cov=Msigma) if sex == femaleval: Fclust = np.random.choice([1, 2], replace=True, p=(prop1, prop2), size=1) if Fclust == 1: datum = multivariate_normal.rvs(mean=Fmean1, cov=Fsigma1) if Fclust == 2: datum = multivariate_normal.rvs(mean=Fmean2, cov=Fsigma2) datamatrix[i] = np.concatenate( [sex, np.round([datum[0], np.exp(datum[1])], 1)]) return datamatrix
def genSamp(th_t,model): # proposal algorithm, given the current theta_t, propose the next theta sig = model['sig_th']/1.5 # std deviation for the proposal distr th = mvn.rvs(th_t,np.power(sig,2)) return th
def sample_given_y(self, y): g = self.gaussians[y] return clamp_sample( mvn.rvs(mean=g['m'], cov=g['c']) )
def SCEP_MH_COV(x_obs, gamma, mu_vector, rhos, param_list): Cov_matrix, matrix_diag, cond_coeff, cond_means_coeff, cond_vars = param_list p = len(x_obs) Cov_matrix_off = np.matrix.copy(Cov_matrix) for i in range(p): Cov_matrix_off[i, i] = Cov_matrix[i, i] - matrix_diag[i] rej = 0 tildexs = [] cond_mean = mu_vector + np.reshape( cond_coeff @ np.reshape(x_obs - mu_vector, [p, 1]), p) cond_cov = Cov_matrix - cond_coeff @ Cov_matrix_off ws = multivariate_normal.rvs(cond_mean, cond_cov) def q_prop_pdf_log(num_j, vec_j, prop_j): num_j = num_j + 1 if num_j != (len(vec_j) - p + 1): return ("error") temp_mean = ( mu_vector[num_j - 1] + cond_means_coeff[num_j - 1] @ np.reshape( vec_j - np.concatenate([mu_vector, mu_vector[0:(num_j - 1)]]), [len(vec_j), 1]))[0, 0] return (-(prop_j - temp_mean)**2 / (2 * cond_vars[num_j - 1]) - 0.5 * math.log(2 * math.pi * cond_vars[num_j - 1])) parallel_chains = np.reshape(x_obs.tolist() * p, [p, p]) for j in range(p): parallel_chains[j, j] = ws[j] marg_density_log = p_marginal_log(rhos, p, x_obs) parallel_marg_density_log = [0] * p for j in range(p): if j == 0: parallel_marg_density_log[ j] = marg_density_log + p_marginal_trans_log( rhos, p, 1, ws[0], 0) + p_marginal_trans_log( rhos, p, 2, x_obs[1], ws[0]) - p_marginal_trans_log( rhos, p, 1, x_obs[0], 0) - p_marginal_trans_log( rhos, p, 2, x_obs[1], x_obs[0]) if j == p - 1: parallel_marg_density_log[ j] = marg_density_log + p_marginal_trans_log( rhos, p, p, ws[p - 1], x_obs[p - 2]) - p_marginal_trans_log( rhos, p, p, x_obs[p - 1], x_obs[p - 2]) if j > 0 and j < p - 1: parallel_marg_density_log[ j] = marg_density_log + p_marginal_trans_log( rhos, p, j + 1, ws[j], x_obs[j - 1]) + p_marginal_trans_log( rhos, p, j + 2, x_obs[j + 1], ws[j]) - p_marginal_trans_log( rhos, p, j + 1, x_obs[j], x_obs[j - 1]) - p_marginal_trans_log( rhos, p, j + 2, x_obs[j + 1], x_obs[j]) cond_density_log = 0 parallel_cond_density_log = [0] * p for j in range(p): true_vec = np.concatenate([x_obs, ws[0:j]]) alter_vec = np.concatenate([x_obs, ws[0:j]]) alter_vec[j] = ws[j] acc_ratio_log = q_prop_pdf_log( j, alter_vec, x_obs[j] ) + parallel_marg_density_log[j] + parallel_cond_density_log[j] - ( marg_density_log + cond_density_log + q_prop_pdf_log(j, true_vec, ws[j])) if math.log(np.random.uniform()) <= acc_ratio_log + math.log(gamma): tildexs.append(ws[j]) cond_density_log = cond_density_log + q_prop_pdf_log( j, true_vec, ws[j]) + log(gamma) + min(0, acc_ratio_log) if j + 2 <= p: true_vec_j = np.concatenate( [parallel_chains[j + 1, :], ws[0:j]]) alter_vec_j = np.concatenate( [parallel_chains[j + 1, :], ws[0:j]]) alter_vec_j[j] = ws[j] j_acc_ratio_log = q_prop_pdf_log( j, alter_vec_j, x_obs[j] ) + parallel_cond_density_log[ j] + parallel_marg_density_log[j] + p_marginal_trans_log( rhos, p, j + 2, ws[j + 1], ws[j]) - p_marginal_trans_log( rhos, p, j + 2, x_obs[j + 1], ws[j]) if j + 3 <= p: j_acc_ratio_log = j_acc_ratio_log + p_marginal_trans_log( rhos, p, j + 3, x_obs[j + 2], ws[j + 1]) - p_marginal_trans_log( rhos, p, j + 3, x_obs[j + 2], x_obs[j + 1]) j_acc_ratio_log = j_acc_ratio_log - ( parallel_cond_density_log[j + 1] + parallel_marg_density_log[j + 1] + q_prop_pdf_log(j, true_vec_j, ws[j])) parallel_cond_density_log[ j + 1] = parallel_cond_density_log[j + 1] + q_prop_pdf_log( j, true_vec_j, ws[j]) + log(gamma) + min( 0, j_acc_ratio_log) if j + 3 <= p: for ii in range(j + 2, p): parallel_cond_density_log[ii] = cond_density_log else: rej = rej + 1 tildexs.append(x_obs[j]) cond_density_log = cond_density_log + q_prop_pdf_log( j, true_vec, ws[j]) + log(1 - gamma * min(1, math.exp(acc_ratio_log))) if j + 2 <= p: true_vec_j = np.concatenate( [parallel_chains[j + 1, :], ws[0:j]]) alter_vec_j = np.concatenate( [parallel_chains[j + 1, :], ws[0:j]]) alter_vec_j[j] = ws[j] j_acc_ratio_log = q_prop_pdf_log( j, alter_vec_j, x_obs[j] ) + parallel_cond_density_log[ j] + parallel_marg_density_log[j] + p_marginal_trans_log( rhos, p, j + 2, ws[j + 1], ws[j]) - p_marginal_trans_log( rhos, p, j + 2, x_obs[j + 1], ws[j]) if j + 3 <= p: j_acc_ratio_log = j_acc_ratio_log + p_marginal_trans_log( rhos, p, j + 3, x_obs[j + 2], ws[j + 1]) - p_marginal_trans_log( rhos, p, j + 3, x_obs[j + 2], x_obs[j + 1]) j_acc_ratio_log = j_acc_ratio_log - ( parallel_cond_density_log[j + 1] + parallel_marg_density_log[j + 1] + q_prop_pdf_log(j, true_vec_j, ws[j])) parallel_cond_density_log[ j + 1] = parallel_cond_density_log[j + 1] + q_prop_pdf_log( j, true_vec_j, ws[j]) + log(1 - gamma * min(1, math.exp(j_acc_ratio_log))) if j + 3 <= p: for ii in range(j + 2, p): parallel_cond_density_log[ii] = cond_density_log #tildexs.append(rej) return (tildexs)
def rvs(cls, mean: OptNumeric = None, cov: Numeric = 1, df: float = None, size: Numeric = 1, type_='shifted', random_state: Optional[int] = None): """ Draw random samples from a multivariate student T distribution Parameters ---------- mean: array like, optional Mean of the distribution (default zero) cov: array like, optional Covariance matrix of the distribution (default 1.0) df: float, optional Degrees of freedom for the distribution (default 4.6692) size: int, optional Number of samples to draw (default 1) type_: {'kshirsagar', 'shifted'}, optional Type of non-central multivariate t distribution. 'Kshirsagar' is the non-central t-distribution needed for calculating the power of multiple contrast tests under a normality assumption. 'Shifted' is a location shifted version of the central t-distribution. This non-central multivariate t-distribution appears for example as the Bayesian posterior distribution for the regression coefficients in a linear regression. In the central case both types coincide. Defaults ('shifted') random_state: int, optional Sets the seed for drawing the random variates Returns ------- rvs: ndarray or scalar Random variates of size (`size`, `N`), where `N` is the dimension of the random variable. """ if not (isinstance(size, int) or np.asarray(size).dtype == 'int'): raise ValueError('size argument must be integer or iterable of integers') if df is None: raise ValueError("Degrees of freedom 'df' must be a scalar float and greater than 0") elif df <= 0: raise ValueError("Degrees of freedom 'df' must be greater than 0") elif df > cls._T_LIMIT: return np.asarray(mvn.rvs(mean, cov, size, random_state), dtype=float) if random_state is not None: np.random.seed(random_state) d = np.sqrt(np.random.chisquare(df, size) / df) if isinstance(size, abc.Iterable): d = d.reshape(*size, -1) size_is_iterable = True else: if size > 1: d = d.reshape(size, -1) size_is_iterable = False # size and dim used to reshape generated tensor correctly before dividing by chi-square rvs dim = 1 if not isinstance(cov, abc.Sized) else len(cov) if type_.casefold() == 'kshirsagar': r = mvn.rvs(mean, cov, size, random_state) if size_is_iterable: r = r.reshape(*size, dim) r /= d elif type_.casefold() == 'shifted': r = mvn.rvs(np.zeros(len(cov)), cov, size, random_state) if size_is_iterable: r = r.reshape(*size, dim) r /= d if mean is not None: r += mean # location shifting else: raise ValueError(f"Unknown centrality type {type_}. Use one of 'Kshirsagar', 'shifted'") return float(r) if r.size == 1 else np.asarray(r)
def get_gen_data(path_to_gen_file, X_good, n_samples, n_components=15, sigma=0.01, recalculate=False): """ Params ------ path_to_gen_file : str Full path to file where gen data stored(or will store) X_good : np.array Train data which have "0" label value. n_samples : int Number of generated samples. n_components : int The number of mixture components. sigma : float Standard deviation of the normal distribution which adds to data for fit GMM. recalculate : bool If it is False, that will be loaded old values from gen_file. Else these values will be calculated and saved to path_to_gen_file. Return ------ X_gen : np.array, shape=(n_samples, X_good.shape[1]) Generated data. w_gen : np.array, shape=(n_samples, ) Weights of samples. """ if os.path.isfile(path_to_gen_file) and not recalculate: # load gen_file = np.load(path_to_gen_file) X_gen = gen_file['X_gen'] w_gen = gen_file['w_gen'] return X_gen, w_gen # calculate GMM gm = GaussianMixture(n_components=n_components, n_init=4, covariance_type="full", verbose=0) gm.fit(X_good + np.random.normal(0, sigma, X_good.shape)) print("BIC: ", gm.bic(X_good)) # generate data X_gen = np.array( multivariate_normal.rvs(mean=gm.means_[0], cov=gm.covariances_[0], size=ceil(gm.weights_[0] * n_samples))) for d in range(1, gm.n_components): X_gen = np.vstack( (X_gen, multivariate_normal.rvs(mean=gm.means_[d], cov=gm.covariances_[d], size=ceil(gm.weights_[d] * n_samples)))) X_gen = np.random.permutation(X_gen)[:n_samples] # weights ~ 1/max_proba probas = np.empty((gm.n_components, X_gen.shape[0])) for d in range(gm.n_components): probas[d] = multivariate_normal.pdf(X_gen, mean=gm.means_[d], cov=gm.covariances_[d], allow_singular=True) maxprob = np.max(probas, axis=0) w_gen = 1. / (maxprob + 1e-2) w_gen = w_gen * n_samples / np.sum(w_gen) # save np.savez(path_to_gen_file, X_gen=X_gen, w_gen=w_gen) return X_gen, w_gen
def generate_sample(self, frozen_params=None): if frozen_params is None: frozen_params = self.params() mu, Sigma = frozen_params return np.atleast_1d(multivariate_normal.rvs(mean=mu, cov=Sigma))
def sample_given_y(self, c): g = self.gaussians[c] return mvn.rvs(mean=g["mean"], cov=g["cov"])