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/CPDs/Old/@root_gaussian_CPD | |
| 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/CPDs/Old/@root_gaussian_CPD')
6 files changed, 135 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Entries b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Entries new file mode 100644 index 00000000..5335ec72 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Entries @@ -0,0 +1,4 @@ +/log_marg_prob_node.m/1.1.1.1/Wed May 29 15:59:54 2002// +/root_gaussian_CPD.m/1.1.1.1/Wed May 29 15:59:54 2002// +/update_params_complete.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Repository b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Repository new file mode 100644 index 00000000..ff9bf8d4 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/CPDs/Old/@root_gaussian_CPD diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Root b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/log_marg_prob_node.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/log_marg_prob_node.m new file mode 100644 index 00000000..4d4e21fc --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/log_marg_prob_node.m @@ -0,0 +1,26 @@ +function L = log_marg_prob_node(CPD, self_ev, pev) +% LOG_MARG_PROB_NODE Compute prod_m log P(x(i,m)| x(pi_i,m)) for node i (root_gaussian) +% L = log_marg_prob_node(CPD, self_ev, pev) +% +% This differs from log_prob_node because we integrate out the parameters. +% self_ev{m} is the evidence on this node in case m. +% pev{i,m} is the evidence on the i'th parent in case m (ignored). + +ncases = length(self_ev); + +if ncases==0 + L = 0; + return; +elseif ncases==1 + x = cat(1, self_ev{:}); + k = length(x); + n0 = CPD.prior.n; + mu = CPD.prior.mu; + alpha = CPD.prior.alpha; + beta = CPD.prior.beta; + gamma = 2*alpha - k + 1; + % Bernardo and Smith p441 + L = log_student_pdf(x, mu, n0/(n0+1)*0.5*gamma*inv(beta), gamma); +else + error('can''t handle batch data'); +end diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/root_gaussian_CPD.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/root_gaussian_CPD.m new file mode 100644 index 00000000..bd4ffd9e --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/root_gaussian_CPD.m @@ -0,0 +1,74 @@ +function CPD = root_gaussian_CPD(bnet, self, mu, Sigma, mu0, n0, alpha0, beta0) +% ROOT_GAUSSIAN_CPD Make an unconditional Gaussian distrib. +% +% CPD = root_gaussian_CPD(bnet, self, mu, Sigma) +% This defines the distribution Y ~ N(mu, Sigma), +% Pass in [] to generate a default random value for a parameter. +% +% CPD = root_gaussian_CPD(bnet, self, [], [], mu0, n0, alpha0, beta0) +% defines a Normal-Wishart prior over the parameters: +% P(mu | lambda) = N(mu | mu0, n0*lambda) +% P(lambda) = Wishart(lambda | alpha0, beta0) +% where lambda = inv(Sigma) is the precision matrix of mu. +% n0 is a scale factor, beta0 is a precision matrix. +% Pass in [] to generate a default value for a hyperparameter. +% mu and Sigma will be set to their prior expected values. +% See "Bayesian Theory", Bernardo and Smith (2000), p441. + + +if nargin==0 + % This occurs if we are trying to load an object from a file. + CPD = init_fields; + CPD = class(CPD, 'root_gaussian_CPD', generic_CPD(0)); + return; +elseif isa(bnet, 'root_gaussian_CPD') + % This might occur if we are copying an object. + CPD = bnet; + return; +end +CPD = init_fields; + + +ns = bnet.node_sizes; +d = ns(self); + +if nargin < 5, + prior = []; + if isempty(mu), mu = randn(d, 1); end + if isempty(Sigma), Sigma = eye(d); end +else + if isempty(mu0), mu0 = zeros(d, 1); end + if isempty(n0), n0 = 0.1; end + if isempty(alpha0), alpha0 = (d-1)/2 + 1; end % Wishart requires 2 alpha > d-1 + if isempty(beta0), beta0 = eye(d); end + + prior.mu = mu0; + prior.n = n0; + prior.alpha = alpha0; + prior.beta = beta0; + + % set params to their mean + mu = prior.mu; + Sigma = prior.beta/prior.alpha; % mean of Wishart is E[lambda] = alpha*inv(beta) +end + +CPD.self = self; +CPD.mu = mu; +CPD.Sigma = Sigma; +CPD.prior = prior; + +clamped = 0; +CPD = class(CPD, 'root_gaussian_CPD', generic_CPD(clamped)); + + +%%%%%%%%%%% + +function CPD = init_fields() +% This ensures we define the fields in the same order +% no matter whether we load an object from a file, +% or create it from scratch. (Matlab requires this.) + +CPD.self = []; +CPD.mu = []; +CPD.Sigma = []; +CPD.prior = []; diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/update_params_complete.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/update_params_complete.m new file mode 100644 index 00000000..7ce58944 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@root_gaussian_CPD/update_params_complete.m @@ -0,0 +1,29 @@ +function CPD = update_params_complete(CPD, self_ev, pev) +% UPDATE_PARAMS_COMPLETE Bayesian parameter updating given completely observed data (root_gaussian) +% CPD = update_params_complete(CPD, self_ev, pev) +% +% self_ev{m} is the evidence on this node in case m. +% pev{i,m} is the evidence on the i'th parent in case m (ignored) +% +% We update the hyperparams and set the params to the mean of the posterior. + +X = cell2num(self_ev); +[k N] = size(X); % each column is a case + +one = ones(N,1); +xbar = X*one / N; % = mean(X')' +S = X*(eye(N) - one*one'/N)*X'; + +n0 = CPD.prior.n; +nn = 1/(n0 + N); +mu0 = CPD.prior.mu; +CPD.prior.mu = nn*(n0*mu0 + N*xbar); +CPD.prior.alpha = CPD.prior.alpha + 0.5*N; +CPD.prior.beta = CPD.prior.beta + 0.5*S + 0.5*nn*N*n0*(mu0-xbar)*(mu0-xbar)'; +CPD.prior.n = CPD.prior.n + N; + +% set params to their mean +CPD.mu = CPD.prior.mu; +% E[Cov] = E inv(n lambda) = 1/(n (alpha-(k+1)/2)) beta +CPD.Sigma = CPD.prior.beta /(CPD.prior.n * (CPD.prior.alpha - (k+1)/2)); + |
