wolffd@0: function slice = get_slice_dbn(bnet, state, i, n, j, m, strides, families, ... wolffd@0: CPT) wolffd@0: % slice = get_slice(bnet, state, i, n, j, m, strides, families, cpt) wolffd@0: % wolffd@0: % GET_SLICE get one-dimensional slice of the CPT for node X_i^n wolffd@0: % that corresponds to the different values of X_j^m, where all wolffd@0: % other nodes have values given by state. wolffd@0: % strides is the result of wolffd@0: % calling compute_strides(bnet) wolffd@0: % families is the result of calling compute_families(bnet) wolffd@0: % cpts is the result of calling get_cpts(bnet) wolffd@0: % wolffd@0: % slice is a 1-d array wolffd@0: wolffd@0: wolffd@0: if (n == 1) wolffd@0: wolffd@0: k = bnet.eclass1(i); wolffd@0: c = CPT{k}; wolffd@0: wolffd@0: % Figure out evidence on family wolffd@0: fam = families{i, 1}; wolffd@0: ev = state(fam, 1); wolffd@0: wolffd@0: % Remove evidence on node j wolffd@0: pos = find(fam == j); wolffd@0: ev(pos) = 1; wolffd@0: dim = size(ev, 1); wolffd@0: wolffd@0: % Compute initial index and stride wolffd@0: start_ind = 1+strides(k, 1:dim)*(ev-1); wolffd@0: stride = strides(k, pos); wolffd@0: wolffd@0: % Compute the slice wolffd@0: slice = c(start_ind:stride:start_ind+(bnet.node_sizes(j, 1)-1)*stride); wolffd@0: wolffd@0: else wolffd@0: wolffd@0: k = bnet.eclass2(i); wolffd@0: c = CPT{k}; wolffd@0: wolffd@0: fam = families{i, 2}; wolffd@0: ss = length(bnet.intra); wolffd@0: wolffd@0: % Divide the family into nodes in this time step and nodes in the wolffd@0: % previous time step wolffd@0: this_time_step = fam(find(fam > ss)); wolffd@0: prev_time_step = fam(find(fam <= ss)); wolffd@0: wolffd@0: % Normalize the node numbers wolffd@0: this_time_step = this_time_step - ss; wolffd@0: wolffd@0: % Get the evidence wolffd@0: this_step_ev = state(this_time_step, n); wolffd@0: prev_step_ev = state(prev_time_step, n-1); wolffd@0: wolffd@0: % Remove the evidence for X_j^m wolffd@0: if (m == n) wolffd@0: pos = find(this_time_step == j); wolffd@0: this_step_ev(pos) = 1; wolffd@0: pos = pos + size(prev_time_step, 2); wolffd@0: else wolffd@0: assert (m == n-1); wolffd@0: pos = find(prev_time_step == j); wolffd@0: prev_step_ev(pos) = 1; wolffd@0: end wolffd@0: wolffd@0: % Combine the two time steps wolffd@0: ev = [prev_step_ev; this_step_ev]; wolffd@0: dim = size(ev, 1); wolffd@0: wolffd@0: wolffd@0: % Compute starting index and stride wolffd@0: start_ind = 1 + strides(k, 1:dim)*(ev-1); wolffd@0: stride = strides(k, pos); wolffd@0: wolffd@0: % Compute slice wolffd@0: if (m == 1) wolffd@0: q = 1; wolffd@0: else wolffd@0: q = 2; wolffd@0: end wolffd@0: slice = c(start_ind:stride:start_ind+(bnet.node_sizes(j, q)-1)*stride); wolffd@0: end wolffd@0: wolffd@0: wolffd@0: