Daniel@0:
Daniel@0:Daniel@0:
Daniel@0: Note that "temporal Bayesian network" would be a better name than Daniel@0: "dynamic Bayesian network", since Daniel@0: it is assumed that the model structure does not change, but Daniel@0: the term DBN has become entrenched. Daniel@0: We also normally assume that the parameters do not Daniel@0: change, i.e., the model is time-invariant. Daniel@0: However, we can always add extra Daniel@0: hidden nodes to represent the current "regime", thereby creating Daniel@0: mixtures of models to capture periodic non-stationarities. Daniel@0:
Daniel@0: There are some cases where the size of the state space can change over Daniel@0: time, e.g., tracking a variable, but unknown, number of objects. Daniel@0: In this case, we need to change the model structure over time. Daniel@0: BNT does not support this. Daniel@0: Daniel@0: Daniel@0: Daniel@0:
Daniel@0:
Daniel@0:
Daniel@0: We have "unrolled" the model for three "time slices" -- the structure and parameters are Daniel@0: assumed to repeat as the model is unrolled further. Daniel@0: Hence to specify a DBN, we need to Daniel@0: define the intra-slice topology (within a slice), Daniel@0: the inter-slice topology (between two slices), Daniel@0: as well as the parameters for the first two slices. Daniel@0: (Such a two-slice temporal Bayes net is often called a 2TBN.) Daniel@0:
Daniel@0: We can specify the topology as follows. Daniel@0:
Daniel@0: intra = zeros(2); Daniel@0: intra(1,2) = 1; % node 1 in slice t connects to node 2 in slice t Daniel@0: Daniel@0: inter = zeros(2); Daniel@0: inter(1,1) = 1; % node 1 in slice t-1 connects to node 1 in slice t Daniel@0:Daniel@0: We can specify the parameters as follows, Daniel@0: where for simplicity we assume the observed node is discrete. Daniel@0:
Daniel@0: Q = 2; % num hidden states Daniel@0: O = 2; % num observable symbols Daniel@0: Daniel@0: ns = [Q O]; Daniel@0: dnodes = 1:2; Daniel@0: bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes); Daniel@0: for i=1:4 Daniel@0: bnet.CPD{i} = tabular_CPD(bnet, i); Daniel@0: end Daniel@0:Daniel@0:
Daniel@0: We assume the distributions P(X(t) | X(t-1)) and Daniel@0: P(Y(t) | X(t)) are independent of t for t > 1. Daniel@0: Hence the CPD for nodes 5, 7, ... is the same as for node 3, so we say they Daniel@0: are in the same equivalence class, with node 3 being the "representative" Daniel@0: for this class. In other words, we have tied the parameters for nodes Daniel@0: 3, 5, 7, ... Daniel@0: Similarly, nodes 4, 6, 8, ... are tied. Daniel@0: Note, however, that (the parameters for) nodes 1 and 2 are not tied to Daniel@0: subsequent slices. Daniel@0:
Daniel@0: Above we assumed the observation model P(Y(t) | X(t)) is independent of t for t>1, but Daniel@0: it is conventional to assume this is true for all t. Daniel@0: So we would like to put nodes 2, 4, 6, ... all in the same class. Daniel@0: We can do this by explicitely defining the equivalence classes, as Daniel@0: follows (see here for more details on Daniel@0: parameter tying). Daniel@0:
Daniel@0: We define eclass1(i) to be the equivalence class that node i in slice Daniel@0: 1 belongs to. Daniel@0: Similarly, we define eclass2(i) to be the equivalence class that node i in slice Daniel@0: 2, 3, ..., belongs to. Daniel@0: For an HMM, we have Daniel@0:
Daniel@0: eclass1 = [1 2]; Daniel@0: eclass2 = [3 2]; Daniel@0: eclass = [eclass1 eclass2]; Daniel@0:Daniel@0: This ties the observation model across slices, Daniel@0: since e.g., eclass(4) = eclass(2) = 2. Daniel@0:
Daniel@0: By default, Daniel@0: eclass1 = 1:ss, and eclass2 = (1:ss)+ss, where ss = slice size = the Daniel@0: number of nodes per slice. Daniel@0: Daniel@0: But by using the above tieing pattern, Daniel@0: we now only have 3 CPDs to specify, instead of 4: Daniel@0:
Daniel@0: bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2); Daniel@0: prior0 = normalise(rand(Q,1)); Daniel@0: transmat0 = mk_stochastic(rand(Q,Q)); Daniel@0: obsmat0 = mk_stochastic(rand(Q,O)); Daniel@0: bnet.CPD{1} = tabular_CPD(bnet, 1, prior0); Daniel@0: bnet.CPD{2} = tabular_CPD(bnet, 2, obsmat0); Daniel@0: bnet.CPD{3} = tabular_CPD(bnet, 3, transmat0); Daniel@0:Daniel@0: We discuss how to do inference and learning on this model Daniel@0: below. Daniel@0: (See also Daniel@0: my HMM toolbox, which is included with BNT.) Daniel@0: Daniel@0:
Daniel@0: Some common variants on HMMs are shown below. Daniel@0: BNT can handle all of these. Daniel@0:
Daniel@0:
![]() | ![]() ![]() |
![]() | ![]() ![]() |
Daniel@0: x(t+1) = A*x(t) + w(t), w ~ N(0, Q), x(0) ~ N(init_x, init_V) Daniel@0: y(t) = C*x(t) + v(t), v ~ N(0, R) Daniel@0:Daniel@0: Some simple variants are shown below. Daniel@0:
Daniel@0:
![]() | ![]() | ![]() | ![]() |
Daniel@0: Daniel@0: We can create a regular LDS in BNT as follows. Daniel@0:
Daniel@0: Daniel@0: intra = zeros(2); Daniel@0: intra(1,2) = 1; Daniel@0: inter = zeros(2); Daniel@0: inter(1,1) = 1; Daniel@0: n = 2; Daniel@0: Daniel@0: X = 2; % size of hidden state Daniel@0: Y = 2; % size of observable state Daniel@0: Daniel@0: ns = [X Y]; Daniel@0: dnodes = []; Daniel@0: onodes = [2]; Daniel@0: eclass1 = [1 2]; Daniel@0: eclass2 = [3 2]; Daniel@0: bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2); Daniel@0: Daniel@0: x0 = rand(X,1); Daniel@0: V0 = eye(X); % must be positive semi definite! Daniel@0: C0 = rand(Y,X); Daniel@0: R0 = eye(Y); Daniel@0: A0 = rand(X,X); Daniel@0: Q0 = eye(X); Daniel@0: Daniel@0: bnet.CPD{1} = gaussian_CPD(bnet, 1, 'mean', x0, 'cov', V0, 'cov_prior_weight', 0); Daniel@0: bnet.CPD{2} = gaussian_CPD(bnet, 2, 'mean', zeros(Y,1), 'cov', R0, 'weights', C0, ... Daniel@0: 'clamp_mean', 1, 'cov_prior_weight', 0); Daniel@0: bnet.CPD{3} = gaussian_CPD(bnet, 3, 'mean', zeros(X,1), 'cov', Q0, 'weights', A0, ... Daniel@0: 'clamp_mean', 1, 'cov_prior_weight', 0); Daniel@0:Daniel@0: We discuss how to do inference and learning on this model Daniel@0: below. Daniel@0: (See also Daniel@0: my Kalman filter toolbox, which is included with BNT.) Daniel@0:
Daniel@0: Daniel@0: Daniel@0:
Daniel@0:
Daniel@0:
Daniel@0: We can make this using the function Daniel@0:
Daniel@0: Q = 2; % binary hidden nodes Daniel@0: discrete_obs = 0; % cts observed nodes Daniel@0: Y = 1; % scalar observed nodes Daniel@0: bnet = mk_chmm(N, Q, Y, discrete_obs); Daniel@0:Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0:
Daniel@0:
Daniel@0: ss = 12; % slice size Daniel@0: intra = zeros(ss); Daniel@0: intra(1,9) = 1; Daniel@0: intra(3,10) = 1; Daniel@0: intra(4,11) = 1; Daniel@0: intra(8,12) = 1; Daniel@0: Daniel@0: inter = zeros(ss); Daniel@0: inter(1, [1 3]) = 1; % node 1 in slice 1 connects to nodes 1 and 3 in slice 2 Daniel@0: inter(2, [2 3 7]) = 1; Daniel@0: inter(3, [3 4 5]) = 1; Daniel@0: inter(4, [3 4 6]) = 1; Daniel@0: inter(5, [3 5 6]) = 1; Daniel@0: inter(6, [4 5 6]) = 1; Daniel@0: inter(7, [7 8]) = 1; Daniel@0: inter(8, [6 7 8]) = 1; Daniel@0: Daniel@0: onodes = 9:12; % observed Daniel@0: dnodes = 1:ss; % discrete Daniel@0: ns = 2*ones(1,ss); % binary nodes Daniel@0: eclass1 = 1:12; Daniel@0: eclass2 = [13:20 9:12]; Daniel@0: eclass = [eclass1 eclass2]; Daniel@0: bnet = mk_dbn(intra, inter, ns, 'discrete', dnodes, 'eclass1', eclass1, 'eclass2', eclass2); Daniel@0: for e=1:max(eclass) Daniel@0: bnet.CPD{e} = tabular_CPD(bnet, e); Daniel@0: end Daniel@0:Daniel@0: We have tied the observation parameters across all slices. Daniel@0: Click here for a more complex example Daniel@0: of parameter tieing. Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0:
Daniel@0:
Daniel@0: Since this topology is so complicated, Daniel@0: it is useful to be able to refer to the nodes by name, instead of Daniel@0: number. Daniel@0:
Daniel@0: names = {'LeftClr', 'RightClr', 'LatAct', ... 'Bclr', 'BYdotDiff'}; Daniel@0: ss = length(names); Daniel@0:Daniel@0: We can specify the intra-slice topology using a cell array as follows, Daniel@0: where each row specifies a connection between two named nodes: Daniel@0:
Daniel@0: intrac = {... Daniel@0: 'LeftClr', 'LeftClrSens'; Daniel@0: 'RightClr', 'RightClrSens'; Daniel@0: ... Daniel@0: 'BYdotDiff', 'BcloseFast'}; Daniel@0:Daniel@0: Finally, we can convert this cell array to an adjacency matrix using Daniel@0: the following function: Daniel@0:
Daniel@0: [intra, names] = mk_adj_mat(intrac, names, 1); Daniel@0:Daniel@0: This function also permutes the names so that they are in topological Daniel@0: order. Daniel@0: Given this ordering of the names, we can make the inter-slice Daniel@0: connectivity matrix as follows: Daniel@0:
Daniel@0: interc = {... Daniel@0: 'LeftClr', 'LeftClr'; Daniel@0: 'LeftClr', 'LatAct'; Daniel@0: ... Daniel@0: 'FBStatus', 'LatAct'}; Daniel@0: Daniel@0: inter = mk_adj_mat(interc, names, 0); Daniel@0:Daniel@0: Daniel@0: To refer to a node, we must know its number, which can be computed as Daniel@0: in the following example: Daniel@0:
Daniel@0: obs = {'LeftClrSens', 'RightClrSens', 'TurnSignalSens', 'XdotSens', 'YdotSens', 'FYdotDiffSens', ... Daniel@0: 'FclrSens', 'BXdotSens', 'BclrSens', 'BYdotDiffSens'}; Daniel@0: for i=1:length(obs) Daniel@0: onodes(i) = stringmatch(obs{i}, names); Daniel@0: end Daniel@0: onodes = sort(onodes); Daniel@0:Daniel@0: (We sort the onodes since most BNT routines assume that set-valued Daniel@0: arguments are in sorted order.) Daniel@0: We can now make the DBN: Daniel@0:
Daniel@0: dnodes = 1:ss; Daniel@0: ns = 2*ones(1,ss); % binary nodes Daniel@0: bnet = mk_dbn(intra, inter, ns, 'iscrete', dnodes); Daniel@0:Daniel@0: To specify the parameters, we must know the order of the parents. Daniel@0: See the function BNT/general/mk_named_CPT for a way to do this in the Daniel@0: case of tabular nodes. For simplicity, we just generate random Daniel@0: parameters: Daniel@0:
Daniel@0: for i=1:2*ss Daniel@0: bnet.CPD{i} = tabular_CPD(bnet, i); Daniel@0: end Daniel@0:Daniel@0: A complete version of this example is available in BNT/examples/dynamic/bat1.m. Daniel@0: Daniel@0: Daniel@0: Daniel@0: Daniel@0:
Daniel@0:
Daniel@0:
Daniel@0:
Daniel@0:
Daniel@0: BNT can currently only handle offline smoothing. Daniel@0: (The HMM engine handles filtering and, to a limited extent, prediction.) Daniel@0: The usage is similar to static Daniel@0: inference engines, except now the evidence is a 2D cell array of Daniel@0: size ss*T, where ss is the number of nodes per slice (ss = slice sizee) and T is the Daniel@0: number of slices. Daniel@0: Also, 'marginal_nodes' takes two arguments, the nodes and the time-slice. Daniel@0: For example, to compute P(X(i,t) | y(:,1:T)), we proceed as follows Daniel@0: (where onodes are the indices of the observedd nodes in each slice, Daniel@0: which correspond to y): Daniel@0:
Daniel@0: ev = sample_dbn(bnet, T); Daniel@0: evidence = cell(ss,T); Daniel@0: evidence(onodes,:) = ev(onodes, :); % all cells besides onodes are empty Daniel@0: [engine, ll] = enter_evidence(engine, evidence); Daniel@0: marg = marginal_nodes(engine, i, t); Daniel@0:Daniel@0: Daniel@0: Daniel@0:
Daniel@0: A DBN can be converted to an HMM if all the hidden nodes are discrete. Daniel@0: In this case, you can use Daniel@0: hmm_inf_engine. This is faster than jtree for small models Daniel@0: because the constant factors of the algorithm are lower, but can be Daniel@0: exponentially slower for models with many variables Daniel@0: (e.g., > 6 binary hidden nodes). Daniel@0: Daniel@0:
Daniel@0: The use of both Daniel@0: jtree_dbn_inf_engine Daniel@0: and Daniel@0: hmm_inf_engine Daniel@0: is deprecated. Daniel@0: A better approach is to construct a smoother engine out of lower-level Daniel@0: engines, which implement forward/backward operators. Daniel@0: You can create these engines as follows. Daniel@0:
Daniel@0: engine = smoother_engine(hmm_2TBN_inf_engine(bnet)); Daniel@0: or Daniel@0: engine = smoother_engine(jtree_2TBN_inf_engine(bnet)); Daniel@0:Daniel@0: You then call them in the usual way: Daniel@0:
Daniel@0: engine = enter_evidence(engine, evidence); Daniel@0: m = marginal_nodes(engine, nodes, t); Daniel@0:Daniel@0: Note: you must declare the observed nodes in the bnet before using Daniel@0: hmm_2TBN_inf_engine. Daniel@0: Daniel@0: Daniel@0:
Daniel@0: Unfortunately, when all the hiddden nodes are discrete, Daniel@0: exact inference takes O(2^n) time, where n is the number of hidden Daniel@0: nodes per slice, Daniel@0: even if the model is sparse. Daniel@0: The basic reason for this is that two nodes become correlated, even if Daniel@0: there is no direct connection between them in the 2TBN, Daniel@0: by virtue of sharing common ancestors in the past. Daniel@0: Hence we need to use approximations. Daniel@0:
Daniel@0: A popular approximate inference algorithm for discrete DBNs, known as BK, is described in Daniel@0:
Daniel@0: engine = bk_inf_engine(bnet, { [1 2], [3 4 5 6], [7 8] }); Daniel@0:Daniel@0: This engine can now be used just like the jtree engine. Daniel@0: Two special cases of the BK algorithm are supported: 'ff' (fully Daniel@0: factored) means each node has its own cluster, and 'exact' means there Daniel@0: is 1 cluster that contains the whole slice. These can be created as Daniel@0: follows: Daniel@0:
Daniel@0: engine = bk_inf_engine(bnet, 'ff'); Daniel@0: engine = bk_inf_engine(bnet, 'exact'); Daniel@0:Daniel@0: For pedagogical purposes, an implementation of BK-FF that uses an HMM Daniel@0: instead of junction tree is available at Daniel@0: bk_ff_hmm_inf_engine. Daniel@0: Daniel@0: Daniel@0: Daniel@0:
Daniel@0: For nonlinear systems with Gaussian noise, the unscented Kalman filter (UKF), Daniel@0: due to Julier and Uhlmann, is far superior to the well-known extended Kalman Daniel@0: filter (EKF), both in theory and practice. Daniel@0: Daniel@0: The key idea of the UKF is that it is easier to estimate a Gaussian distribution Daniel@0: from a set of points than to approximate an arbitrary non-linear Daniel@0: function. Daniel@0: We start with points that are plus/minus sigma away from the mean along Daniel@0: each dimension, and then pipe them through the nonlinearity, and Daniel@0: then fit a Gaussian to the transformed points. Daniel@0: (No need to compute Jacobians, unlike the EKF!) Daniel@0: Daniel@0:
Daniel@0: For systems with non-Gaussian noise, I recommend Daniel@0: Particle Daniel@0: filtering (PF), which is a popular sequential Monte Carlo technique. Daniel@0: Daniel@0:
Daniel@0: The EKF can be used as a proposal distribution for a PF. Daniel@0: This method is better than either one alone. Daniel@0: See The Unscented Particle Filter, Daniel@0: by R van der Merwe, A Doucet, JFG de Freitas and E Wan, May 2000. Daniel@0: Matlab Daniel@0: software for the UPF is also available. Daniel@0:
Daniel@0: Note: none of this software is part of BNT. Daniel@0: Daniel@0: Daniel@0: Daniel@0:
Daniel@0: ncases = 2; Daniel@0: cases = cell(1, ncases); Daniel@0: for i=1:ncases Daniel@0: ev = sample_dbn(bnet, T); Daniel@0: cases{i} = cell(ss,T); Daniel@0: cases{i}(onodes,:) = ev(onodes, :); Daniel@0: end Daniel@0: [bnet2, LLtrace] = learn_params_dbn_em(engine, cases, 'max_iter', 10); Daniel@0:Daniel@0: If the observed node is vector-valued and stored in an OxT array, you Daniel@0: need to assign each vector to a single cell, as in the following Daniel@0: example. Daniel@0:
Daniel@0: data = [xpos(:)'; ypos(:)']; Daniel@0: ncases = 1; Daniel@0: cases = cell(1, ncases); Daniel@0: onodes = bnet.observed; Daniel@0: for i=1:ncases Daniel@0: cases{i} = cell(ss,T); Daniel@0: cases{i}(onodes,:) = num2cell(data(:,1:T), 1); Daniel@0: end Daniel@0:Daniel@0:
Daniel@0: For a complete code listing of how to do EM in a simple DBN, click Daniel@0: here. Daniel@0: Daniel@0:
Daniel@0: inter = learn_struct_dbn_reveal(cases, ns, max_fan_in, penalty) Daniel@0:Daniel@0: A full example is given in BNT/examples/dynamic/reveal1.m. Daniel@0: Setting the penalty term to 0 gives the maximum likelihood model; this Daniel@0: is equivalent to maximizing the mutual information between parents and Daniel@0: child (in the bioinformatics community, this is known as the REVEAL Daniel@0: algorithm). A non-zero penalty invokes the BIC criterion, which Daniel@0: lessens the chance of overfitting. Daniel@0:
Daniel@0: Daniel@0: Dirk Husmeier has extended MCMC model selection to DBNs. Daniel@0: Daniel@0: