Exemple #1
0
 def _rvs_helper(self):
   num_samples = 10000
   xs = gauss(0, 1).rvs((num_samples, 3))
   xs = divide(xs, reshape(norm(xs, 1), (num_samples, 1)))
   pvalues = self.pdf(xs, normalize=False)
   fmax = self.pdf_max(normalize=False)
   return xs[uniform(0, fmax).rvs(num_samples) < pvalues]
 def _rvs_helper(self):
     num_samples = 10000
     xs = gauss(0, 1).rvs((num_samples, 3))
     xs = divide(xs, reshape(norm(xs, 1), (num_samples, 1)))
     pvalues = self.pdf(xs, normalize=False)
     fmax = self.pdf_max(normalize=False)
     return xs[uniform(0, fmax).rvs(num_samples) < pvalues]
Exemple #3
0
def GMM(data, k, dim):
	# Initializing pi, mu and Sigma
	pi = [1.0/k]*k
	mu = []
	sigma = []
	N = len(data)
	for x in xrange(k):
		mu.append(ease * (np.random.rand(dim) - 0.5))
		cov = 4 * (np.random.rand(dim, dim) - 0.5)
		cov = np.dot(cov.T, cov)
		sigma.append(cov)

	while True:
		# Estimating Expectation
		gamma = np.empty(shape=[0, N])
		for x in xrange(k):
			# print sigma[x]
			Normal= gauss(mean=mu[x], cov=sigma[x])
			N_n = pi[x] * Normal.pdf(data)
			gamma = np.append(gamma, N_n.reshape(1, N), axis=0)
		print "Shape of the gamma:", np.shape(gamma)
		for x in xrange(np.shape(gamma)[1]):
			# print gamma[:,x],
			gamma[:,x] = gamma[:,x] / np.sum(gamma[:,x])
			# print gamma[:,x]

		# Maximizing Expectation
		mu = np.dot(gamma, data)
		for x in xrange(k):
			N_k = np.sum(mu[x])
			mu[x] = mu[x] / N_k
			pi[x] = N_k / N

			cov = data - [mu[x]]*N
			for i in xrange(len(cov)):
				dummy = np.multiply(np.outer(cov[i], cov[i]), gamma[x][i])
				if not is_pos_def(dummy):
					print "BETA: One of the included matrices is not positive definite!!!"
			# cov = np.array([np.multiply(np.outer(cov[i], cov[i]), gamma[x][i]) for i in xrange(len(cov))])
			cov = np.sum([np.multiply(np.outer(cov[i], cov[i]), gamma[x][i]) for i in xrange(len(cov))], axis=0)
			# print cov, np.shape(cov)
			if not is_pos_def(cov):
				print "BETA: This is not positive definite!!!"
			sigma[x] = cov
		print "Shape of sigma", np.shape(sigma)
Exemple #4
0
# Create Original Data
time = np.linspace(0, 1, 101)
M = time.size
N = 30
lam = 0.001
center = np.array([.35, .5, .65])
center2 = np.array([4, 3.7, 4])
sd1 = .05
gam_sd = 8
num_comp = 5
f_orig = np.zeros((M, N * center.size))
omega = 2 * np.pi
cnt = 0
for ii in range(0, center.size):
    tmp = gauss(loc=center[ii], scale=.075)
    for jj in range(0, N):
        f_orig[:, cnt] = normal(center2[ii], sd1) * tmp.pdf(time)
        cnt += 1

q_orig = fs.f_to_srsf(f_orig, time)
y_orig = np.ones(q_orig.shape[1], dtype=int)
y_orig[N:2*N] = 2
y_orig[2*N:3*N] = 3

f = np.zeros((M, f_orig.shape[1]))
q = np.zeros((M, f_orig.shape[1]))
cnt = 0
gam_orig = fs.rgam(M, gam_sd, 3*N)
for ii in range(0, center.size):
    for ii in range(0, N):