annotate toolboxes/FullBNT-1.0.7/bnt/learning/learn_struct_dbn_reveal.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function inter = learn_struct_dbn_reveal(seqs, ns, max_fan_in, penalty)
Daniel@0 2 % LEARN_STRUCT_DBN_REVEAL Learn inter-slice adjacency matrix given fully observable discrete time series
Daniel@0 3 % inter = learn_struct_dbn_reveal(seqs, node_sizes, max_fan_in, penalty)
Daniel@0 4 %
Daniel@0 5 % seqs{l}{i,t} = value of node i in slice t of time-series l.
Daniel@0 6 % If you have a single time series in an N*T array D, use
Daniel@0 7 % seqs = { num2cell(D) }.
Daniel@0 8 % If you have L time series, each of length T, in an N*T*L array D, use
Daniel@0 9 % seqs= cell(1,L); for l=1:L, seqs{l} = num2cell(D(:,:,l)); end
Daniel@0 10 % or, in vectorized form,
Daniel@0 11 % seqs = squeeze(num2cell(num2cell(D),[1 2]));
Daniel@0 12 % Currently the data is assumed to be discrete (1,2,...)
Daniel@0 13 %
Daniel@0 14 % node_sizes(i) is the number of possible values for node i
Daniel@0 15 % max_fan_in is the largest number of parents we allow per node (default: N)
Daniel@0 16 % penalty is weight given to the complexity penalty (default: 0.5)
Daniel@0 17 % A penalty of 0.5 gives the BIC score.
Daniel@0 18 % A penalty of 0 gives the ML score.
Daniel@0 19 % Maximizing likelihood is equivalent to maximizing mutual information between parents and child.
Daniel@0 20 %
Daniel@0 21 % inter(i,j) = 1 iff node in slice t connects to node j in slice t+1
Daniel@0 22 %
Daniel@0 23 % The parent set for each node in slice 2 is computed by evaluating all subsets of nodes in slice 1,
Daniel@0 24 % and picking the largest scoring one. This takes O(n^k) time per node, where n is the num. nodes
Daniel@0 25 % per slice, and k <= n is the max fan in.
Daniel@0 26 % Since all the nodes are observed, we do not need to use an inference engine.
Daniel@0 27 % And since we are only learning the inter-slice matrix, we do not need to check for cycles.
Daniel@0 28 %
Daniel@0 29 % This algorithm is described in
Daniel@0 30 % - "REVEAL: A general reverse engineering algorithm for inference of genetic network
Daniel@0 31 % architectures", Liang et al. PSB 1998
Daniel@0 32 % - "Extended dependency analysis of large systems",
Daniel@0 33 % Roger Conant, Intl. J. General Systems, 1988, vol 14, pp 97-141
Daniel@0 34 % - "Learning the structure of DBNs", Friedman, Murphy and Russell, UAI 1998.
Daniel@0 35
Daniel@0 36 n = length(ns);
Daniel@0 37
Daniel@0 38 if nargin < 3, max_fan_in = n; end
Daniel@0 39 if nargin < 4, penalty = 0.5; end
Daniel@0 40
Daniel@0 41 inter = zeros(n,n);
Daniel@0 42
Daniel@0 43 if ~iscell(seqs)
Daniel@0 44 data{1} = seqs;
Daniel@0 45 end
Daniel@0 46
Daniel@0 47 nseq = length(seqs);
Daniel@0 48 nslices = 0;
Daniel@0 49 data = cell(1, nseq);
Daniel@0 50 for l=1:nseq
Daniel@0 51 nslices = nslices + size(seqs{l}, 2);
Daniel@0 52 data{l} = cell2num(seqs{l})'; % each row is a case
Daniel@0 53 end
Daniel@0 54 ndata = nslices - nseq; % subtract off the initial slice of each sequence
Daniel@0 55
Daniel@0 56 % We concatenate the sequences as in the following example.
Daniel@0 57 % Let there be 2 sequences of lengths 4 and 5, with n nodes per slice,
Daniel@0 58 % and let i be the target node.
Daniel@0 59 % Then we construct following matrix D
Daniel@0 60 %
Daniel@0 61 % s{1}{1,1} ... s{1}{1,3} s{2}{1,1} ... s{2}{1,4}
Daniel@0 62 % ....
Daniel@0 63 % s{1}{n,1} ... s{1}{n,3} s{2}{n,1} ... s{2}{n,4}
Daniel@0 64 % s{1}{i,2} ... s{1}{i,4} s{2}{i,2} ... s{2}{i,5}
Daniel@0 65 %
Daniel@0 66 % D(1:n, i) is the i'th input and D(n+1, i) is the i'th output.
Daniel@0 67 %
Daniel@0 68 % We concatenate each sequence separately to avoid treating the transition
Daniel@0 69 % from the end of one sequence to the beginning of another as a "normal" transition.
Daniel@0 70
Daniel@0 71
Daniel@0 72 for i=1:n
Daniel@0 73 D = [];
Daniel@0 74 for l=1:nseq
Daniel@0 75 T = size(seqs{l}, 2);
Daniel@0 76 A = cell2num(seqs{l}(:, 1:T-1));
Daniel@0 77 B = cell2num(seqs{l}(i, 2:T));
Daniel@0 78 C = [A;B];
Daniel@0 79 D = [D C];
Daniel@0 80 end
Daniel@0 81 SS = subsets(1:n, max_fan_in, 1); % skip the empty set
Daniel@0 82 nSS = length(SS);
Daniel@0 83 bic_score = zeros(1, nSS);
Daniel@0 84 ll_score = zeros(1, nSS);
Daniel@0 85 target = n+1;
Daniel@0 86 ns2 = [ns ns(i)];
Daniel@0 87 for h=1:nSS
Daniel@0 88 ps = SS{h};
Daniel@0 89 dom = [ps target];
Daniel@0 90 counts = compute_counts(D(dom, :), ns2(dom));
Daniel@0 91 CPT = mk_stochastic(counts);
Daniel@0 92 [bic_score(h), ll_score(h)] = bic_score_family(counts, CPT, ndata);
Daniel@0 93 end
Daniel@0 94 if penalty == 0
Daniel@0 95 h = argmax(ll_score);
Daniel@0 96 else
Daniel@0 97 h = argmax(bic_score);
Daniel@0 98 end
Daniel@0 99 ps = SS{h};
Daniel@0 100 inter(ps, i) = 1;
Daniel@0 101 end