Computer oevelser

(Difference between revisions)
 Revision as of 16:46, 14 March 2007 (view source)Kimstp (Talk | contribs)← Older edit Revision as of 18:06, 14 March 2007 (view source)Hauberg (Talk | contribs) Newer edit → Line 1: Line 1: '''Opgave: EM algoritmen og Gaussiske mikstur modeller (GMM)''' '''Opgave: EM algoritmen og Gaussiske mikstur modeller (GMM)''' - Foelgende matlab scripts genererer et data saet: + Foelgende matlab scripts genererer et datasaet: - gmmdataset.m: + ''gmmdataset.m:'' - % Makes a 2 dimensional data set for which you can fit a 3 component + % Makes a 2 dimensional data set for which you can fit a 3 component - % Gaussian Mixture Model (GMM) to. + % Gaussian Mixture Model (GMM) to. - clear all; + clear all; + + + prior = [0.2, 0.5, 0.3]; + + m = [0.1, 0.4; + 0.6, 0.6; + 1.0, 0.7]'; + + R45 = [cos(pi/4) -sin(pi/4); + sin(pi/4) cos(pi/4)] + + S = cell(1,3); + S{1} = R45' * diag([0.01, 0.25]); + S{2} = R45 * diag([0.005, 0.25]); + S{3} = R45' * diag([0.01, 0.25]); + + N = 500; + X = zeros(2,N); + for i=1:N + % Sample component k from prior + k = randpmf([1,2,3], prior, 1); + + % Sample from component k Gaussian + X(:, i) = gausssamples(1, m(:, k), S{k}); + + end + + + figure(1) + plot(m(1,:), m(2,:), 'o') + hold on; + + plot(X(1,:),X(2,:), '.') + hold off - prior = [0.2, 0.5, 0.3]; + ''gausssampler.m:'' - m = [0.1, 0.4; + function X=gausssamples(N, m, S) - 0.6, 0.6; + % X=gausssamples(N, m, S) - 1.0, 0.7]'; + % + % Take N from a multivariate Gaussian distribution with mean m and + % covariance matrix S. + + D=length(m); + [V,E]=eig(S); + X = sqrt(E) * V * randn(D,N) + repmat(m, 1, N); - R45 = [cos(pi/4) -sin(pi/4); - sin(pi/4) cos(pi/4)] - S = cell(1,3); + ''randpmf.m:'' - S{1} = R45' * diag([0.01, 0.25]); + function r = randpmf(x, p, dim) - S{2} = R45 * diag([0.005, 0.25]); + %  function r = randpmf(x, p, dim) - S{3} = R45' * diag([0.01, 0.25]); + %  x  - Labels to sample from - + %  p  - Distribution of labels - N = 500; + %  dim - Dimension of the output matrix - X = zeros(2,N); + %  r  - A matrix with random samples of p(x) - for i=1:N + % - % Sample component k from prior + % Sample elements from the vector x following the discrete probability - k = randpmf([1,2,3], prior, 1); + % mass function p(x). - + % - % Sample from component k Gaussian + %  By Kim S. Pedersen, ITU, 2004 - X(:, i) = gausssamples(1, m(:, k), S{k}); + - + [n,m]=size(p); - end + - + - + - figure(1) + - plot(m(1,:), m(2,:), 'o') + - hold on; + - + - plot(X(1,:),X(2,:), '.') + - hold off + - + - + - gausssampler.m: + - + - function X=gausssamples(N, m, S) + - % X=gausssamples(N, m, S) + - % + - % Take N from a multivariate Gaussian distribution with mean m and + - % covariance matrix S. + - + - D=length(m); + - [V,E]=eig(S); + - X = sqrt(E) * V * randn(D,N) + repmat(m, 1, N); + - + - + - randpmf.m: + - function r = randpmf(x, p, dim) + - %  function r = randpmf(x, p, dim) + - %  x  - Labels to sample from + - %  p  - Distribution of labels + - %  dim - Dimension of the output matrix + - %  r  - A matrix with random samples of p(x) + - % + - % Sample elements from the vector x following the discrete probability + - % mass function p(x). + - % + - %  By Kim S. Pedersen, ITU, 2004 + + if n==0 & m==0 + error('p must be a vector'); + end - [n,m]=size(p); + if n>1 && m>1 - + error('p must be a vector'); - if n==0 & m==0 + end - error('p must be a vector'); + - end + - if n>1 && m>1 + % Force p to be a 1xN vector - error('p must be a vector'); + if n>1 - end + p=p'; - + end - % Force p to be a 1xN vector + - if n>1 + - p=p'; + - end + - cp = cumsum(p); + cp = cumsum(p); - r = rand(dim); + r = rand(dim); - for i=1:prod(dim) + for i=1:prod(dim) - idx = find(r(i) >= [0 cp(1:end-1)]); + idx = find(r(i) >= [0 cp(1:end-1)]); - % r < cp(2:end) + % r < cp(2:end) - % idx2 = find(r < cp(2:end))+1 + % idx2 = find(r < cp(2:end))+1 - + - r(i) = x(idx(end)); + - end + + r(i) = x(idx(end)); + end + Modeller dette data saet med en 3 komponent Gaussisk mikstur model. Implementer EM algoritmen for GMM og anvend den til at estimere parametrene i modellen. Modeller dette data saet med en 3 komponent Gaussisk mikstur model. Implementer EM algoritmen for GMM og anvend den til at estimere parametrene i modellen.

Revision as of 18:06, 14 March 2007

Opgave: EM algoritmen og Gaussiske mikstur modeller (GMM)

Foelgende matlab scripts genererer et datasaet:

gmmdataset.m:

```% Makes a 2 dimensional data set for which you can fit a 3 component
% Gaussian Mixture Model (GMM) to.
clear all;

prior = [0.2, 0.5, 0.3];

m = [0.1, 0.4;
0.6, 0.6;
1.0, 0.7]';

R45 = [cos(pi/4) -sin(pi/4);
sin(pi/4) cos(pi/4)]

S = cell(1,3);
S{1} = R45' * diag([0.01, 0.25]);
S{2} = R45 * diag([0.005, 0.25]);
S{3} = R45' * diag([0.01, 0.25]);

N = 500;
X = zeros(2,N);
for i=1:N
% Sample component k from prior
k = randpmf([1,2,3], prior, 1);

% Sample from component k Gaussian
X(:, i) = gausssamples(1, m(:, k), S{k});

end

figure(1)
plot(m(1,:), m(2,:), 'o')
hold on;

plot(X(1,:),X(2,:), '.')
hold off
```

gausssampler.m:

```function X=gausssamples(N, m, S)
% X=gausssamples(N, m, S)
%
% Take N from a multivariate Gaussian distribution with mean m and
% covariance matrix S.

D=length(m);
[V,E]=eig(S);
X = sqrt(E) * V * randn(D,N) + repmat(m, 1, N);
```

randpmf.m:

```function r = randpmf(x, p, dim)
%  function r = randpmf(x, p, dim)
%   x   - Labels to sample from
%   p   - Distribution of labels
%   dim - Dimension of the output matrix
%   r   - A matrix with random samples of p(x)
%
% Sample elements from the vector x following the discrete probability
% mass function p(x).
%
%  By Kim S. Pedersen, ITU, 2004

[n,m]=size(p);
```
```if n==0 & m==0
error('p must be a vector');
end

if n>1 && m>1
error('p must be a vector');
end

% Force p to be a 1xN vector
if n>1
p=p';
end

cp = cumsum(p);
r = rand(dim);

for i=1:prod(dim)
idx = find(r(i) >= [0 cp(1:end-1)]);
% r < cp(2:end)
% idx2 = find(r < cp(2:end))+1

r(i) = x(idx(end));
end

```

Modeller dette data saet med en 3 komponent Gaussisk mikstur model. Implementer EM algoritmen for GMM og anvend den til at estimere parametrene i modellen.