function [mvg, weights] = gbn_to_mvg(bnet, num_nodes)
% Compute the Multivariate Gaussian which is equivalent to the Gaussian belief net.
% [mvg, weights] = gbn_to_mvg(bnet, num_nodes)
%
% See "Gaussian Influence Diagrams", R. Shachter and C. R. Kenley, 1989,
% Management Science, 35(5):527--550.

if nargin<2, num_nodes = length(bnet.adj_mat); end

[weights, D, mu] = extract_params_from_gbn(bnet, num_nodes);
Sigma = weights_to_covar(weights, D);

mvg.mu = mu;
mvg.Sigma = Sigma;
mvg.logp = log(normal_coef(Sigma));


%%%%%%%

function Sigma = weights_to_covar(B,D)
%
% Given a weight matrix B and a block diagonal matrix D of variances
% compute the equivalent covariance.

[n n2] = size(B);
U = inv(eye(n,n) - B')';
Sigma = U' * D * U;






