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/@linear_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/@linear_gaussian_CPD')
6 files changed, 141 insertions, 0 deletions
diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Entries b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Entries new file mode 100644 index 00000000..96e99049 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Entries @@ -0,0 +1,4 @@ +/linear_gaussian_CPD.m/1.1.1.1/Wed May 29 15:59:54 2002// +/log_marg_prob_node.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/@linear_gaussian_CPD/CVS/Repository b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Repository new file mode 100644 index 00000000..ac2255d4 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/CPDs/Old/@linear_gaussian_CPD diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Root b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/linear_gaussian_CPD.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/linear_gaussian_CPD.m new file mode 100644 index 00000000..55076c4b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/linear_gaussian_CPD.m @@ -0,0 +1,87 @@ +function CPD = linear_gaussian_CPD(bnet, self, theta, sigma, theta0, n0, alpha0, beta0) +% LINEAR_GAUSSIAN_CPD Make a linear Gaussian distrib. +% +% CPD = linear_gaussian_CPD(bnet, self, theta, lambda) +% This defines the distribution P(Y|X) = N(y | theta'*x, sigma), +% where y (self) is a scalar, theta is a regression vector, and sigma is the variance. +% Pass in [] to generate a default random value for a parameter. +% +% CPD = linear_gaussian_CPD(bnet, self, [], [], theta0, n0, alpha0, beta0) +% defines a Normal-Gamma prior over the parameters: +% P(theta | lambda) = N(theta | theta0, n0*lambda) +% P(lambda) = Gamma(lambda | alpha0, beta0) +% where lambda = 1/sigma is the precision for y. +% n0 is a precision matrix, beta0 is a scale factor. +% Pass in [] to generate a default value for a hyperparameter. +% theta and sigma will be set to their prior expected values. +% See "Bayesian Theory", Bernardo and Smith (2000), p442. + + +if nargin==0 + % This occurs if we are trying to load an object from a file. + CPD = init_fields; + CPD = class(CPD, 'linear_gaussian_CPD', generic_CPD(0)); + return; +elseif isa(bnet, 'linear_gaussian_CPD') + % This might occur if we are copying an object. + CPD = bnet; + return; +end +CPD = init_fields; + + +ns = bnet.node_sizes; +ps = parents(bnet.dag, self); +d = sum(ns(ps)); +assert(ns(self)==1); + + +if nargin < 5, + prior = []; + if isempty(theta), theta = randn(d, 1); end + if isempty(sigma), sigma = 1; end +else + + %if isempty(theta0), theta0 = zeros(d, 1); end + %if isempty(n0), n0 = 0.1*eye(d); end + %if isempty(alpha0), alpha0 = 0.1; end + %if isempty(beta0), beta0 = 0.1; end + + % use non-informative priors + if isempty(theta0), theta0 = zeros(d, 1); end + if isempty(n0), n0 = 0.001*ones(d); end + if isempty(alpha0), alpha0 = -d/2 + 0.001; end + if isempty(beta0), beta0 = 0.001; end + + prior.theta = theta0; + prior.n = n0; + prior.alpha = alpha0; + prior.beta = beta0; + + % set params to their mean + theta = prior.theta; + %sigma = prior.beta/prior.alpha; % mean of Gamma is E[lambda] = alpha/beta +end + + +CPD.self = self; +CPD.theta = theta; +CPD.sigma = sigma; +CPD.prior = prior; + + +clamped = 0; +CPD = class(CPD, 'linear_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.theta = []; +CPD.sigma = []; +CPD.prior = []; diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/log_marg_prob_node.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/log_marg_prob_node.m new file mode 100644 index 00000000..3d06244f --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/log_marg_prob_node.m @@ -0,0 +1,23 @@ +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 (linear_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 +% We assume there is <= 1 case. + +ncases = length(self_ev); + +if ncases==0 + L = 0; + return; +elseif ncases==1 + y = self_ev{1}; + x = cat(1, pev{:}); % column vector + f = 1-x'*inv(x*x' + CPD.prior.n)*x; + alpha = CPD.prior.alpha; + L = log_student_pdf(y, x'*CPD.prior.theta, f*alpha/CPD.prior.beta, 2*alpha); +else + error('can''t handle batch data'); +end diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/update_params_complete.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/update_params_complete.m new file mode 100644 index 00000000..dbe8d5da --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@linear_gaussian_CPD/update_params_complete.m @@ -0,0 +1,25 @@ +function CPD = update_params_complete(CPD, self_ev, pev) +% UPDATE_PARAMS_COMPLETE Bayesian parameter updating given completely observed data (linear_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 +% +% We update the hyperparams and set the params to the mean of the posterior. + +y = cat(1, self_ev{:}); +X = cell2num(pev)'; +[N k] = size(X); % each row is a case + +n0 = CPD.prior.n; +th0 = CPD.prior.theta; +CPD.prior.theta = inv(n0 + X'*X)*(n0*th0 + X'*y); +thn = CPD.prior.theta; +CPD.prior.beta = CPD.prior.beta + 0.5*(y-X*thn)'*y + 0.5*(th0-thn)'*n0*th0; +CPD.prior.alpha = CPD.prior.alpha + 0.5*N; +CPD.prior.n = CPD.prior.n + X'*X; + + +% set params to their mean +CPD.theta = CPD.prior.theta; +%CPD.sigma = CPD.prior.beta/CPD.prior.alpha; % mean of Gamma is E[lambda] = alpha/beta |
