about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/BNT/examples/dynamic/HHMM/hhmm_jtree_clqs.m
blob: 4192af113d9b7d0499a491ea0538502638818670 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
% Find out how big the cliques are in an HHMM as a function of depth
% (This is how we get the complexity bound of O(D K^{1.5D}).)

if 0
Qsize = [];
Fsize = [];
Nclqs = [];
end

ds = 1:15;

for d = ds
  allQ = 1;
  [intra, inter, Qnodes, Fnodes, Onode] = mk_hhmm_topo(d, allQ);
  
  N = length(intra);
  ns = 2*ones(1,N);
  
  bnet = mk_dbn(intra, inter, ns);
  for i=1:N
    bnet.CPD{i} = tabular_CPD(bnet, i);
  end
  
  if 0
    T = 5;
    dag = unroll_dbn_topology(intra, inter, T);
    engine = jtree_unrolled_dbn_inf_engine(bnet, T, 'constrained', 1);
    S = struct(engine);
    S1 = struct(S.sub_engine);
  end
  
  engine = jtree_dbn_inf_engine(bnet);
  S = struct(engine);
  J = S.jtree_struct;
  
  ss = 2*d+1;
  Qnodes2 = Qnodes + ss;
  QQnodes = [Qnodes Qnodes2];
  
  % find out how many Q nodes in each clique, and how many F nodes
  C = length(J.cliques);
  Nclqs(d) = 0;
  for c=1:C
    Qsize(c,d) = length(myintersect(J.cliques{c}, QQnodes));
    Fsize(c,d) = length(myintersect(J.cliques{c}, Fnodes));
    if length(J.cliques{c}) > 1 % exclude observed leaves
      Nclqs(d) = Nclqs(d) + 1;
    end
  end
  %pred_max_Qsize(d) = ceil(d+(d+1)/2);
  pred_max_Qsize(d) = ceil(1.5*d);
  
  fprintf('d=%d\n', d);
  %fprintf('D=%d, max F = %d. max Q = %d, pred max Q = %d\n', ...
	%  D, max(Fsize), max(Qsize), ceil(D+(D+1)/2));
	     
  %histc(Qsize,1:max(Qsize)) % how many of each size?
end % next d


Q = 2;
pred_mass = ds.*(Q.^ds) + Q.^(ceil(1.5 * ds))
pred_mass2 = Q.^(ceil(1.5 * ds))

for d=ds
  mass(d) = 0;
  for c=1:C
    mass(d) = mass(d) + Q^Qsize(c,d);
  end
end
    

if 0
%plot(ds, max(Qsize), 'o-',  ds, pred_max_Qsize, '*--');
%plot(ds, max(Qsize), 'o-',  ds, 1.5*ds, '*--');
%plot(ds, mass, 'o-',  ds, pred_mass, '*--');
D = 15;
%plot(ds(1:D), mass(1:D), 'bo-',  ds(1:D), pred_mass(1:D), 'g*--', ds(1:D), pred_mass2(1:D), 'k+-.');
plot(ds(1:D), log(mass(1:D)), 'bo-',  ds(1:D), log(pred_mass(1:D)), 'g*--', ds(1:D), log(pred_mass2(1:D)), 'k+-.');

grid on
xlabel('depth of hierarchy')
title('max num Q nodes in any clique vs. depth')
legend('actual', 'predicted')

%previewfig(gcf, 'width', 3, 'height', 1.5, 'color', 'bw');
%exportfig(gcf, '/home/cs/murphyk/WP/ConferencePapers/HHMM/clqsize2.eps', ...
%          'width', 3, 'height', 1.5, 'color', 'bw');   

end


if 0
for d=ds
  effnumclqs(d) = length(find(Qsize(:,d)>0));
end
ds = 1:10;
Qs = 2:10;
maxC = size(Qsize, 1);
cost = [];
cost_bound = [];
for qi=1:length(Qs)
  Q = Qs(qi);
  for d=ds
    cost(d,qi) = 0;
    for c=1:maxC
      if length(Qsize(c,d) > 0) % this clique contains Q nodes
	cost(d,qi) = cost(d,qi) + Q^Qsize(c,d)*2^Fsize(c,d);
      end
    end
    %cost_bound(d,qi) = effnumclqs(d) * 8 * Q^(max(Qsize(:,d)));
    cost_bound(d,qi) = (effnumclqs(d)*8) + Q^(max(Qsize(:,d)));
  end
end

qi=2; plot(ds, cost(:,qi), 'o-',  ds, cost_bound(:,qi), '*--');
end


if 0
% convert numbers in cliques into names
for d=1:D
  Fdecode(Fnodes(d)) = d;
end
for c=8:15
  clqs = J.cliques{c};
  fprintf('clique %d: ', c);
  for k=clqs
    if myismember(k, Qnodes)
      fprintf('Q%d ', k)
    elseif myismember(k, Fnodes)
      fprintf('F%d ', Fdecode(k))
    elseif isequal(k, Onode)
      fprintf('O ')
    elseif myismember(k, Qnodes2)
      fprintf('Q%d* ', k-ss)
    else
      error(['unrecognized node ' k])
    end
  end
  fprintf('\n');
end
end