wolffd@0: function inter = learn_struct_dbn_reveal(seqs, ns, max_fan_in, penalty) wolffd@0: % LEARN_STRUCT_DBN_REVEAL Learn inter-slice adjacency matrix given fully observable discrete time series wolffd@0: % inter = learn_struct_dbn_reveal(seqs, node_sizes, max_fan_in, penalty) wolffd@0: % wolffd@0: % seqs{l}{i,t} = value of node i in slice t of time-series l. wolffd@0: % If you have a single time series in an N*T array D, use wolffd@0: % seqs = { num2cell(D) }. wolffd@0: % If you have L time series, each of length T, in an N*T*L array D, use wolffd@0: % seqs= cell(1,L); for l=1:L, seqs{l} = num2cell(D(:,:,l)); end wolffd@0: % or, in vectorized form, wolffd@0: % seqs = squeeze(num2cell(num2cell(D),[1 2])); wolffd@0: % Currently the data is assumed to be discrete (1,2,...) wolffd@0: % wolffd@0: % node_sizes(i) is the number of possible values for node i wolffd@0: % max_fan_in is the largest number of parents we allow per node (default: N) wolffd@0: % penalty is weight given to the complexity penalty (default: 0.5) wolffd@0: % A penalty of 0.5 gives the BIC score. wolffd@0: % A penalty of 0 gives the ML score. wolffd@0: % Maximizing likelihood is equivalent to maximizing mutual information between parents and child. wolffd@0: % wolffd@0: % inter(i,j) = 1 iff node in slice t connects to node j in slice t+1 wolffd@0: % wolffd@0: % The parent set for each node in slice 2 is computed by evaluating all subsets of nodes in slice 1, wolffd@0: % and picking the largest scoring one. This takes O(n^k) time per node, where n is the num. nodes wolffd@0: % per slice, and k <= n is the max fan in. wolffd@0: % Since all the nodes are observed, we do not need to use an inference engine. wolffd@0: % And since we are only learning the inter-slice matrix, we do not need to check for cycles. wolffd@0: % wolffd@0: % This algorithm is described in wolffd@0: % - "REVEAL: A general reverse engineering algorithm for inference of genetic network wolffd@0: % architectures", Liang et al. PSB 1998 wolffd@0: % - "Extended dependency analysis of large systems", wolffd@0: % Roger Conant, Intl. J. General Systems, 1988, vol 14, pp 97-141 wolffd@0: % - "Learning the structure of DBNs", Friedman, Murphy and Russell, UAI 1998. wolffd@0: wolffd@0: n = length(ns); wolffd@0: wolffd@0: if nargin < 3, max_fan_in = n; end wolffd@0: if nargin < 4, penalty = 0.5; end wolffd@0: wolffd@0: inter = zeros(n,n); wolffd@0: wolffd@0: if ~iscell(seqs) wolffd@0: data{1} = seqs; wolffd@0: end wolffd@0: wolffd@0: nseq = length(seqs); wolffd@0: nslices = 0; wolffd@0: data = cell(1, nseq); wolffd@0: for l=1:nseq wolffd@0: nslices = nslices + size(seqs{l}, 2); wolffd@0: data{l} = cell2num(seqs{l})'; % each row is a case wolffd@0: end wolffd@0: ndata = nslices - nseq; % subtract off the initial slice of each sequence wolffd@0: wolffd@0: % We concatenate the sequences as in the following example. wolffd@0: % Let there be 2 sequences of lengths 4 and 5, with n nodes per slice, wolffd@0: % and let i be the target node. wolffd@0: % Then we construct following matrix D wolffd@0: % wolffd@0: % s{1}{1,1} ... s{1}{1,3} s{2}{1,1} ... s{2}{1,4} wolffd@0: % .... wolffd@0: % s{1}{n,1} ... s{1}{n,3} s{2}{n,1} ... s{2}{n,4} wolffd@0: % s{1}{i,2} ... s{1}{i,4} s{2}{i,2} ... s{2}{i,5} wolffd@0: % wolffd@0: % D(1:n, i) is the i'th input and D(n+1, i) is the i'th output. wolffd@0: % wolffd@0: % We concatenate each sequence separately to avoid treating the transition wolffd@0: % from the end of one sequence to the beginning of another as a "normal" transition. wolffd@0: wolffd@0: wolffd@0: for i=1:n wolffd@0: D = []; wolffd@0: for l=1:nseq wolffd@0: T = size(seqs{l}, 2); wolffd@0: A = cell2num(seqs{l}(:, 1:T-1)); wolffd@0: B = cell2num(seqs{l}(i, 2:T)); wolffd@0: C = [A;B]; wolffd@0: D = [D C]; wolffd@0: end wolffd@0: SS = subsets(1:n, max_fan_in, 1); % skip the empty set wolffd@0: nSS = length(SS); wolffd@0: bic_score = zeros(1, nSS); wolffd@0: ll_score = zeros(1, nSS); wolffd@0: target = n+1; wolffd@0: ns2 = [ns ns(i)]; wolffd@0: for h=1:nSS wolffd@0: ps = SS{h}; wolffd@0: dom = [ps target]; wolffd@0: counts = compute_counts(D(dom, :), ns2(dom)); wolffd@0: CPT = mk_stochastic(counts); wolffd@0: [bic_score(h), ll_score(h)] = bic_score_family(counts, CPT, ndata); wolffd@0: end wolffd@0: if penalty == 0 wolffd@0: h = argmax(ll_score); wolffd@0: else wolffd@0: h = argmax(bic_score); wolffd@0: end wolffd@0: ps = SS{h}; wolffd@0: inter(ps, i) = 1; wolffd@0: end