-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_kmeans.m
executable file
·47 lines (46 loc) · 1.19 KB
/
init_kmeans.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function [mp,lambdap,ap,bp,Counter,sprior] = init_kmeans(v,K,lambdai)
data = [];
data_un = [];
%%%%% Standardize data %%%%%%%%%%%%%%
for n = 1:length(v)
x = v{n};
data_un = [data_un x];
for t = 1:size(v{n},2)
x(:,t) = x(:,t)/norm(x(:,t));
end
data = [data x];
end
%[states] = kmeans(data',K,'Replicates',5,'Distance','cosine','emptyaction','drop');
[states] = kmeans(data',K,'Replicates',5,'Distance','sqEuclidean','emptyaction','drop');
S = length(unique(states)); % # of states
sprior = zeros(1,S);
for s = 1:S
ix = states == s;
sprior(s) = sum(ix);
end
[M,T] = size(data);
%estimate mean & lambdap for each state
mp = zeros(M,S);
lambdap = zeros(1,S);
for s = 1:S
ix = find(states == s);
lambdap(s) = lambdai + length(ix)/T;
y = data_un(:,ix);
mp(:,s) = mean(y,2);
end
% Estimate Covariance for each state
ap = zeros(S,1); bp = zeros(M,M,S);
for s = 1:S
ix = find(states == s);
ap(s) = length(ix);
y = data_un(:,ix)';
bp(:,:,s) = cov(y) + eps.*eye(M);
end
%Estimate Transition Probability Matrix
Counter = zeros(S);
for t1 = 1:T-1
t2 = t1+1;
m = states(t2); n = states(t1);
Counter(m,n) = Counter(m,n) + 1;
end
Counter = Counter';