comparison toolboxes/FullBNT-1.0.7/KPMstats/mixgauss_prob_test.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function test_eval_pdf_cond_mixgauss()
2
3 %Q = 10; M = 100; d = 20; T = 500;
4 Q = 2; M = 3; d = 4; T = 5;
5
6 mu = rand(d,Q,M);
7 data = randn(d,T);
8 %mixmat = mk_stochastic(rand(Q,M));
9 mixmat = mk_stochastic(ones(Q,M));
10
11 % tied scalar
12 Sigma = 0.01;
13
14 mu = rand(d,M,Q);
15 weights = mixmat';
16 N = M*ones(1,Q);
17 tic; [B, B2, D] = parzen(data, mu, Sigma, N, weights); toc
18 tic; [BC, B2C, DC] = parzenC(data, mu, Sigma, N); toc
19 approxeq(B,BC)
20 B2C = reshape(B2C,[M Q T]);
21 approxeq(B2,B2C)
22 DC = reshape(DC,[M Q T]);
23 approxeq(D,DC)
24
25
26 return
27
28 tic; [B, B2] = eval_pdf_cond_mixgauss(data, mu, Sigma, mixmat); toc
29 tic; C = eval_pdf_cond_parzen(data, mu, Sigma); toc
30 approxeq(B,C)
31
32 return;
33
34
35 mu = reshape(mu, [d Q*M]);
36
37 data = mk_unit_norm(data);
38 mu = mk_unit_norm(mu);
39 tic; D = 2 -2*(data'*mu); toc % avoid an expensive repmat
40 tic; D2 = sqdist(data, mu); toc
41 approxeq(D,D2)
42
43
44 % D(t,m) = sq dist between data(:,t) and mu(:,m)
45 mu = reshape(mu, [d Q*M]);
46 D = dist2(data', mu');
47 %denom = (2*pi)^(d/2)*sqrt(abs(det(C)));
48 denom = (2*pi*Sigma)^(d/2); % sqrt(det(2*pi*Sigma))
49 numer = exp(-0.5/Sigma * D');
50 B2 = numer / denom;
51 B2 = reshape(B2, [Q M T]);
52
53 tic; B = squeeze(sum(B2 .* repmat(mixmat, [1 1 T]), 2)); toc
54
55 tic
56 A = zeros(Q,T);
57 for q=1:Q
58 A(q,:) = mixmat(q,:) * squeeze(B2(q,:,:)); % sum over m
59 end
60 toc
61 assert(approxeq(A,B))
62
63 tic
64 A = zeros(Q,T);
65 for t=1:T
66 A(:,t) = sum(mixmat .* B2(:,:,t), 2); % sum over m
67 end
68 toc
69 assert(approxeq(A,B))
70
71
72
73
74 mu = reshape(mu, [d Q M]);
75 B3 = zeros(Q,M,T);
76 for j=1:Q
77 for k=1:M
78 B3(j,k,:) = gaussian_prob(data, mu(:,j,k), Sigma*eye(d));
79 end
80 end
81 assert(approxeq(B2, B3))
82
83 logB4 = -(d/2)*log(2*pi*Sigma) - (1/(2*Sigma))*D; % det(sigma*I) = sigma^d
84 B4 = reshape(exp(logB4), [Q M T]);
85 assert(approxeq(B4, B3))
86
87
88
89
90 % tied cov matrix
91
92 Sigma = rand_psd(d,d);
93 mu = reshape(mu, [d Q*M]);
94 D = sqdist(data, mu, inv(Sigma))';
95 denom = sqrt(det(2*pi*Sigma));
96 numer = exp(-0.5 * D);
97 B2 = numer / denom;
98 B2 = reshape(B2, [Q M T]);
99
100 mu = reshape(mu, [d Q M]);
101 B3 = zeros(Q,M,T);
102 for j=1:Q
103 for k=1:M
104 B3(j,k,:) = gaussian_prob(data, mu(:,j,k), Sigma);
105 end
106 end
107 assert(approxeq(B2, B3))
108
109 logB4 = -(d/2)*log(2*pi) - 0.5*logdet(Sigma) - 0.5*D;
110 B4 = reshape(exp(logB4), [Q M T]);
111 assert(approxeq(B4, B3))