diff options
| author | ziejd2 | 2018-03-14 23:23:33 -0500 |
|---|---|---|
| committer | GitHub | 2018-03-14 23:23:33 -0500 |
| commit | 1ff6baa44e22b91eefb48aea6f3befa078c0489b (patch) | |
| tree | e0fd79d2e32fd2aedda2eadaed0f19af3514c520 /sourcecodes/bnt-master/BNT/CPDs/Old | |
| parent | 6882395afdadf4e982b25b5215071a0932730950 (diff) | |
| parent | c80226899f5cdd9f11c163817d59445213f5bef0 (diff) | |
| download | BNW-1ff6baa44e22b91eefb48aea6f3befa078c0489b.tar.gz | |
Merge pull request #1 from ziejd2/octave_php_separate
Octave php separate
Diffstat (limited to 'sourcecodes/bnt-master/BNT/CPDs/Old')
21 files changed, 332 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 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)); + diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CPD_to_upot.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CPD_to_upot.m new file mode 100644 index 00000000..3ce87d0b --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CPD_to_upot.m @@ -0,0 +1,6 @@ +function pot = CPD_to_upot(CPD, domain) +% CPD_TO_UPOT Convert a CPD to a utility potential +% pot = CPD_to_upot(CPD, domain) + +sz = CPD.size; % mysize(CPD.CPT); +pot = upot(domain, sz, CPD.CPT, 0*myones(sz)); diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Entries b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Entries new file mode 100644 index 00000000..02628802 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Entries @@ -0,0 +1,3 @@ +/CPD_to_upot.m/1.1.1.1/Wed May 29 15:59:54 2002// +/tabular_chance_node.m/1.1.1.1/Wed May 29 15:59:54 2002// +D diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Repository b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Repository new file mode 100644 index 00000000..3a232db2 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/CPDs/Old/@tabular_chance_node diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Root b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/tabular_chance_node.m b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/tabular_chance_node.m new file mode 100644 index 00000000..3476536c --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/@tabular_chance_node/tabular_chance_node.m @@ -0,0 +1,39 @@ +function CPD = tabular_chance_node(sz, CPT) +% TABULAR_CHANCE_NODE Like tabular_CPD, but simplified +% CPD = tabular_chance_node(sz, CPT) +% +% sz(1:end-1) is the sizes of the parents, sz(end) is the size of this node +% By default, CPT is a random stochastic matrix. + +if nargin==0 + % This occurs if we are trying to load an object from a file. + CPD = init_fields; + CPD = class(CPD, 'tabular_chance_node'); + return; +elseif isa(sz, 'tabular_chance_node') + % This might occur if we are copying an object. + CPD = sz; + return; +end +CPD = init_fields; + +if nargin < 2, + CPT = mk_stochastic(myones(sz)); +else + CPT = myreshape(CPT, sz); +end + +CPD.CPT = CPT; +CPD.size = sz; + +CPD = class(CPD, 'tabular_chance_node'); + +%%%%%%%%%%% + +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.CPT = []; +CPD.size = []; diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Entries b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Entries new file mode 100644 index 00000000..17848105 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Entries @@ -0,0 +1 @@ +D diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Entries.Log b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Entries.Log new file mode 100644 index 00000000..ed4a9516 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Entries.Log @@ -0,0 +1,3 @@ +A D/@linear_gaussian_CPD//// +A D/@root_gaussian_CPD//// +A D/@tabular_chance_node//// diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Repository b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Repository new file mode 100644 index 00000000..cf1b510a --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Repository @@ -0,0 +1 @@ +FullBNT/BNT/CPDs/Old diff --git a/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Root b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Root new file mode 100644 index 00000000..f3bd14a6 --- /dev/null +++ b/sourcecodes/bnt-master/BNT/CPDs/Old/CVS/Root @@ -0,0 +1 @@ +:ext:nsaunier@bnt.cvs.sourceforge.net:/cvsroot/bnt |
