# Computer oevelser

(Difference between revisions)
 Revision as of 16:40, 14 March 2007 (view source)Kimstp (Talk | contribs)← Older edit Latest revision as of 09:14, 15 March 2007 (view source)Kimstp (Talk | contribs) (6 intermediate revisions not shown) Line 1: Line 1: '''Opgave: EM algoritmen og Gaussiske mikstur modeller (GMM)''' '''Opgave: EM algoritmen og Gaussiske mikstur modeller (GMM)''' - F?lgende matlab scripts genererer et data s?t: + Anvend nedenstå matlab scripts som genererer et datasæt. Modeller dette datasæt med en 3 komponent Gaussisk mikstur model. Implementer EM algoritmen for GMM og anvend den til at estimere parametrene i modellen. - Modeller dette data s?t med en 3 komponent Gaussisk mikstur model. Implementer EM algoritmen for GMM og anvend den til at estimere parametrene i modellen. + + ''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 + + + ''gausssamples.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

## Latest revision as of 09:14, 15 March 2007

Opgave: EM algoritmen og Gaussiske mikstur modeller (GMM)

Anvend nedenstå matlab scripts som genererer et datasæt. Modeller dette datasæt med en 3 komponent Gaussisk mikstur model. Implementer EM algoritmen for GMM og anvend den til at estimere parametrene i modellen.

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
```

gausssamples.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
```