Skip to content

Commit

Permalink
refine mix rnd: done
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Jan 13, 2016
1 parent 4afc694 commit e56db34
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
26 changes: 13 additions & 13 deletions chapter09/demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@
% subplot(2,1,1);plot(x); axis([x_range,y_range]); title('Original Signal');
% subplot(2,1,2);plot(m); axis([x_range,y_range]); title('Recovery Signal');
%% classification
clear; close all
k = 2;
d = 2;
n = 1000;
[X,t] = kmeansRnd(d,k,n);
[x1,x2] = meshgrid(linspace(min(X(1,:)),max(X(1,:)),n), linspace(min(X(2,:)),max(X(2,:)),n));

[model, llh] = rvmBinEm(X,t-1);
plot(llh);
y = rvmBinPred(model,X)+1;
figure;
binPlot(model,X,y);
% clear; close all
% k = 2;
% d = 2;
% n = 1000;
% [X,t] = kmeansRnd(d,k,n);
% [x1,x2] = meshgrid(linspace(min(X(1,:)),max(X(1,:)),n), linspace(min(X(2,:)),max(X(2,:)),n));
%
% [model, llh] = rvmBinEm(X,t-1);
% plot(llh);
% y = rvmBinPred(model,X)+1;
% figure;
% binPlot(model,X,y);
%% demo: kmeans
% close all; clear;
% d = 2;
Expand Down Expand Up @@ -98,4 +98,4 @@
% figure;
% plotClass(X,z);
% figure;
% plot(llh);
% plot(llh);
4 changes: 2 additions & 2 deletions chapter09/kmeansRnd.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
% Sampling from a Gaussian mixture distribution with common variances (kmeans model).
% Written by Michael Chen ([email protected]).
alpha = 1;
beta = 6*nthroot(k,d);
beta = k;

X = randn(d,n);
w = dirichletRnd(alpha,ones(1,k)/k);
z = discreteRnd(w,n);
E = full(sparse(z,1:n,1,k,n,n));
center = rand(d,k)*beta;
center = randn(d,k)*beta;
X = X+center*E;
4 changes: 2 additions & 2 deletions chapter09/mixGaussRnd.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
W0 = eye(d); % hyperparameter of inverse Wishart prior of covariances
v0 = d+1; % hyperparameter of inverse Wishart prior of covariances
mu0 = zeros(d,1); % hyperparameter of Guassian prior of means
beta0 = 1/(nthroot(k,d))^2; % hyperparameter of Guassian prior of means
beta0 = k; % hyperparameter of Guassian prior of means

w = dirichletRnd(alpha0,ones(1,k)/k);
z = discreteRnd(w,n);
Expand All @@ -16,7 +16,7 @@
for i = 1:k
idx = z==i;
Sigma(:,:,i) = iwishrnd(W0,v0); % invpd(wishrnd(W0,v0));
mu(:,i) = gaussRnd(mu0,Sigma(:,:,i)/beta0);
mu(:,i) = gaussRnd(mu0,beta0*Sigma(:,:,i));
X(:,idx) = gaussRnd(mu(:,i),Sigma(:,:,i),sum(idx));
end
model.mu = mu;
Expand Down

0 comments on commit e56db34

Please sign in to comment.