diff options
| author | ziejd2 | 2017-09-28 15:04:40 -0500 |
|---|---|---|
| committer | ziejd2 | 2017-09-28 15:04:40 -0500 |
| commit | 8070dc963753142bb86c4ed698d91fd623ed28e7 (patch) | |
| tree | d0f6dd8fc46a49b819aa55c1a90faa14d8448883 /sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine | |
| parent | 7cc31810d53176e805532b2789955f4eedbce6bb (diff) | |
| download | BNW-8070dc963753142bb86c4ed698d91fd623ed28e7.tar.gz | |
BNW using Octave instead of Matlab.
This version of BNW should perform the same as the original version. The only difference is that it uses Octave instead of Matlab when running BayesNet Toolbox during parameter learning. I am calling this BNW_1.02. It can be accessed at: compbio.uthsc.edu/BNW_1.02
Diffstat (limited to 'sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine')
10 files changed, 134 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Entries new file mode 100644 index 00000000..16ace516 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Entries @@ -0,0 +1,4 @@ +/enter_evidence.m/1.1.1.1/Wed May 29 15:59:56 2002// +/gaussian_inf_engine.m/1.1.1.1/Fri May 14 01:13:26 2004// +/marginal_nodes.m/1.1.1.1/Wed May 29 15:59:56 2002// +D/private//// diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Repository new file mode 100644 index 00000000..26418ea5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/static/@gaussian_inf_engine diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/enter_evidence.m b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/enter_evidence.m new file mode 100644 index 00000000..c509a725 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/enter_evidence.m @@ -0,0 +1,46 @@ +function [engine, loglik] = enter_evidence(engine, evidence, varargin) +% ENTER_EVIDENCE Add the specified evidence to the network (gaussian_inf_engine) +% [engine, loglik] = enter_evidence(engine, evidence, ...) +% +% evidence{i} = [] if if X(i) is hidden, and otherwise contains its observed value (scalar or column vector) + +bnet = bnet_from_engine(engine); +ns = bnet.node_sizes; +O = find(~isemptycell(evidence)); +H = find(isemptycell(evidence)); +vals = cat(1, evidence{O}); + +% Compute Pr(H|o) +[Hmu, HSigma, loglik] = condition_gaussian(engine.mu, engine.Sigma, H, O, vals(:), ns); + +engine.Hmu = Hmu; +engine.HSigma = HSigma; +engine.hnodes = H; + +%%%%%%%% + +function [mu2, Sigma2, loglik] = condition_gaussian(mu, Sigma, X, Y, y, ns) +% CONDITION_GAUSSIAN Compute Pr(X|Y=y) where X and Y are jointly Gaussian. +% [mu2, Sigma2, ll] = condition_gaussian(mu, Sigma, X, Y, y, ns) + +if isempty(y) + mu2 = mu; + Sigma2 = Sigma; + loglik = 0; + return; +end + +use_log = 1; + +if length(Y)==length(mu) % instantiating every variable + mu2 = y; + Sigma2 = zeros(length(y)); + loglik = gaussian_prob(y, mu, Sigma, use_log); + return; +end + +[muX, muY, SXX, SXY, SYX, SYY] = partition_matrix_vec(mu, Sigma, X, Y, ns); +K = SXY*inv(SYY); +mu2 = muX + K*(y-muY); +Sigma2 = SXX - K*SYX; +loglik = gaussian_prob(y, muY, SYY, use_log); diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/gaussian_inf_engine.m b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/gaussian_inf_engine.m new file mode 100644 index 00000000..3e34c166 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/gaussian_inf_engine.m @@ -0,0 +1,25 @@ +function engine = gaussian_inf_engine(bnet) +% GAUSSIAN_INF_ENGINE Computes the joint multivariate Gaussian corresponding to the bnet +% engine = gaussian_inf_engine(bnet) +% +% For details on how to compute the joint Gaussian from the bnet, see +% - "Gaussian Influence Diagrams", R. Shachter and C. R. Kenley, Management Science, 35(5):527--550, 1989. +% Once we have the Gaussian, we can apply the standard formulas for conditioning and marginalization. + +assert(isequal(bnet.cnodes, 1:length(bnet.dag))); + +[W, D, mu] = extract_params_from_gbn(bnet); +U = inv(eye(size(W)) - W')'; +Sigma = U' * D * U; + +engine.mu = mu; +engine.Sigma = Sigma; +%engine.logp = log(normal_coef(Sigma)); + +% This is where we will store the results between enter_evidence and marginal_nodes +engine.Hmu = []; +engine.HSigma = []; +engine.hnodes = []; + +engine = class(engine, 'gaussian_inf_engine', inf_engine(bnet)); + diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/marginal_nodes.m b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/marginal_nodes.m new file mode 100644 index 00000000..f3142cd5 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/marginal_nodes.m @@ -0,0 +1,15 @@ +function marginal = marginal_nodes(engine, query) +% MARGINAL_NODES Compute the marginal on the specified query nodes (gaussian) +% marginal = marginal_nodes(engine, query) + +% Compute sum_{Hsum} Pr(Hkeep, Hsum | o) +H = engine.hnodes; +bnet = bnet_from_engine(engine); +ns = bnet.node_sizes; +Hkeep = myintersect(H, query); +Hsum = mysetdiff(H, Hkeep); + +[marginal.mu, marginal.Sigma] = marginalize_gaussian(engine.Hmu, engine.HSigma, Hkeep, Hsum, ns); +marginal.domain = query; +marginal.T = 1; + diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Entries b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Entries new file mode 100644 index 00000000..de387328 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Entries @@ -0,0 +1,2 @@ +/extract_params_from_gbn.m/1.1.1.1/Wed May 29 15:59:56 2002// +D diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Repository b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Repository new file mode 100644 index 00000000..15f3d8c4 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/inference/static/@gaussian_inf_engine/private diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Root b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/extract_params_from_gbn.m b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/extract_params_from_gbn.m new file mode 100644 index 00000000..86345830 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/inference/static/@gaussian_inf_engine/private/extract_params_from_gbn.m @@ -0,0 +1,38 @@ +function [B,D,mu] = extract_params_from_gbn(bnet) +% Extract all the local parameters of each Gaussian node, and collect them into global matrices. +% [B,D,mu] = extract_params_from_gbn(bnet) +% +% B(i,j) is a block matrix that contains the transposed weight matrix from node i to node j. +% D(i,i) is a block matrix that contains the noise covariance matrix for node i. +% mu(i) is a block vector that contains the shifted noise mean for node i. + +% In Shachter's model, the mean of each node in the global gaussian is +% the same as the node's local unconditional mean. +% In Alag's model (which we use), the global mean gets shifted. + + +num_nodes = length(bnet.dag); +bs = bnet.node_sizes(:); % bs = block sizes +N = sum(bs); % num scalar nodes + +B = zeros(N,N); +D = zeros(N,N); +mu = zeros(N,1); + +for i=1:num_nodes % in topological order + ps = parents(bnet.dag, i); + e = bnet.equiv_class(i); + %[m, Sigma, weights] = extract_params_from_CPD(bnet.CPD{e}); + s = struct(bnet.CPD{e}); % violate privacy of object + m = s.mean; Sigma = s.cov; weights = s.weights; + if length(ps) == 0 + mu(block(i,bs)) = m; + else + mu(block(i,bs)) = m + weights * mu(block(ps,bs)); + end + B(block(ps,bs), block(i,bs)) = weights'; + D(block(i,bs), block(i,bs)) = Sigma; +end + + + |
