wolffd@0: function CPD = gaussian_CPD(varargin) wolffd@0: % GAUSSIAN_CPD Make a conditional linear Gaussian distrib. wolffd@0: % wolffd@0: % To define this CPD precisely, call the continuous (cts) parents (if any) X, wolffd@0: % the discrete parents (if any) Q, and this node Y. Then the distribution on Y is: wolffd@0: % - no parents: Y ~ N(mu, Sigma) wolffd@0: % - cts parents : Y|X=x ~ N(mu + W x, Sigma) wolffd@0: % - discrete parents: Y|Q=i ~ N(mu(i), Sigma(i)) wolffd@0: % - cts and discrete parents: Y|X=x,Q=i ~ N(mu(i) + W(i) x, Sigma(i)) wolffd@0: % wolffd@0: % CPD = gaussian_CPD(bnet, node, ...) will create a CPD with random parameters, wolffd@0: % where node is the number of a node in this equivalence class. wolffd@0: % wolffd@0: % The list below gives optional arguments [default value in brackets]. wolffd@0: % (Let ns(i) be the size of node i, X = ns(X), Y = ns(Y) and Q = prod(ns(Q)).) wolffd@0: % wolffd@0: % mean - mu(:,i) is the mean given Q=i [ randn(Y,Q) ] wolffd@0: % cov - Sigma(:,:,i) is the covariance given Q=i [ repmat(eye(Y,Y), [1 1 Q]) ] wolffd@0: % weights - W(:,:,i) is the regression matrix given Q=i [ randn(Y,X,Q) ] wolffd@0: % cov_type - if 'diag', Sigma(:,:,i) is diagonal [ 'full' ] wolffd@0: % tied_cov - if 1, we constrain Sigma(:,:,i) to be the same for all i [0] wolffd@0: % clamp_mean - if 1, we do not adjust mu(:,i) during learning [0] wolffd@0: % clamp_cov - if 1, we do not adjust Sigma(:,:,i) during learning [0] wolffd@0: % clamp_weights - if 1, we do not adjust W(:,:,i) during learning [0] wolffd@0: % cov_prior_weight - weight given to I prior for estimating Sigma [0.01] wolffd@0: % wolffd@0: % e.g., CPD = gaussian_CPD(bnet, i, 'mean', [0; 0], 'clamp_mean', 'yes') wolffd@0: % wolffd@0: % For backwards compatibility with BNT2, you can also specify the parameters in the following order wolffd@0: % CPD = gaussian_CPD(bnet, self, mu, Sigma, W, cov_type, tied_cov, clamp_mean, clamp_cov, clamp_weight) wolffd@0: % wolffd@0: % Sometimes it is useful to create an "isolated" CPD, without needing to pass in a bnet. wolffd@0: % In this case, you must specify the discrete and cts parents (dps, cps) and the family sizes, followed wolffd@0: % by the optional arguments above: wolffd@0: % CPD = gaussian_CPD('self', i, 'dps', dps, 'cps', cps, 'sz', fam_size, ...) wolffd@0: wolffd@0: wolffd@0: if nargin==0 wolffd@0: % This occurs if we are trying to load an object from a file. wolffd@0: CPD = init_fields; wolffd@0: clamp = 0; wolffd@0: CPD = class(CPD, 'gaussian_CPD', generic_CPD(clamp)); wolffd@0: return; wolffd@0: elseif isa(varargin{1}, 'gaussian_CPD') wolffd@0: % This might occur if we are copying an object. wolffd@0: CPD = varargin{1}; wolffd@0: return; wolffd@0: end wolffd@0: CPD = init_fields; wolffd@0: wolffd@0: CPD = class(CPD, 'gaussian_CPD', generic_CPD(0)); wolffd@0: wolffd@0: wolffd@0: % parse mandatory arguments wolffd@0: if ~isstr(varargin{1}) % pass in bnet wolffd@0: bnet = varargin{1}; wolffd@0: self = varargin{2}; wolffd@0: args = varargin(3:end); wolffd@0: ns = bnet.node_sizes; wolffd@0: ps = parents(bnet.dag, self); wolffd@0: dps = myintersect(ps, bnet.dnodes); wolffd@0: cps = myintersect(ps, bnet.cnodes); wolffd@0: fam_sz = ns([ps self]); wolffd@0: else wolffd@0: disp('parsing new style') wolffd@0: for i=1:2:length(varargin) wolffd@0: switch varargin{i}, wolffd@0: case 'self', self = varargin{i+1}; wolffd@0: case 'dps', dps = varargin{i+1}; wolffd@0: case 'cps', cps = varargin{i+1}; wolffd@0: case 'sz', fam_sz = varargin{i+1}; wolffd@0: end wolffd@0: end wolffd@0: ps = myunion(dps, cps); wolffd@0: args = varargin; wolffd@0: end wolffd@0: wolffd@0: CPD.self = self; wolffd@0: CPD.sizes = fam_sz; wolffd@0: wolffd@0: % Figure out which (if any) of the parents are discrete, and which cts, and how big they are wolffd@0: % dps = discrete parents, cps = cts parents wolffd@0: CPD.cps = find_equiv_posns(cps, ps); % cts parent index wolffd@0: CPD.dps = find_equiv_posns(dps, ps); wolffd@0: ss = fam_sz(end); wolffd@0: psz = fam_sz(1:end-1); wolffd@0: dpsz = prod(psz(CPD.dps)); wolffd@0: cpsz = sum(psz(CPD.cps)); wolffd@0: wolffd@0: % set default params wolffd@0: CPD.mean = randn(ss, dpsz); wolffd@0: CPD.cov = 100*repmat(eye(ss), [1 1 dpsz]); wolffd@0: CPD.weights = randn(ss, cpsz, dpsz); wolffd@0: CPD.cov_type = 'full'; wolffd@0: CPD.tied_cov = 0; wolffd@0: CPD.clamped_mean = 0; wolffd@0: CPD.clamped_cov = 0; wolffd@0: CPD.clamped_weights = 0; wolffd@0: CPD.cov_prior_weight = 0.01; wolffd@0: wolffd@0: nargs = length(args); wolffd@0: if nargs > 0 wolffd@0: if ~isstr(args{1}) wolffd@0: % gaussian_CPD(bnet, self, mu, Sigma, W, cov_type, tied_cov, clamp_mean, clamp_cov, clamp_weights) wolffd@0: if nargs >= 1 & ~isempty(args{1}), CPD.mean = args{1}; end wolffd@0: if nargs >= 2 & ~isempty(args{2}), CPD.cov = args{2}; end wolffd@0: if nargs >= 3 & ~isempty(args{3}), CPD.weights = args{3}; end wolffd@0: if nargs >= 4 & ~isempty(args{4}), CPD.cov_type = args{4}; end wolffd@0: if nargs >= 5 & ~isempty(args{5}) & strcmp(args{5}, 'tied'), CPD.tied_cov = 1; end wolffd@0: if nargs >= 6 & ~isempty(args{6}), CPD.clamped_mean = 1; end wolffd@0: if nargs >= 7 & ~isempty(args{7}), CPD.clamped_cov = 1; end wolffd@0: if nargs >= 8 & ~isempty(args{8}), CPD.clamped_weights = 1; end wolffd@0: else wolffd@0: CPD = set_fields(CPD, args{:}); wolffd@0: end wolffd@0: end wolffd@0: wolffd@0: % Make sure the matrices have 1 dimension per discrete parent. wolffd@0: % Bug fix due to Xuejing Sun 3/6/01 wolffd@0: CPD.mean = myreshape(CPD.mean, [ss ns(dps)]); wolffd@0: CPD.cov = myreshape(CPD.cov, [ss ss ns(dps)]); wolffd@0: CPD.weights = myreshape(CPD.weights, [ss cpsz ns(dps)]); wolffd@0: wolffd@0: CPD.init_cov = CPD.cov; % we reset to this if things go wrong during learning wolffd@0: wolffd@0: % expected sufficient statistics wolffd@0: CPD.Wsum = zeros(dpsz,1); wolffd@0: CPD.WYsum = zeros(ss, dpsz); wolffd@0: CPD.WXsum = zeros(cpsz, dpsz); wolffd@0: CPD.WYYsum = zeros(ss, ss, dpsz); wolffd@0: CPD.WXXsum = zeros(cpsz, cpsz, dpsz); wolffd@0: CPD.WXYsum = zeros(cpsz, ss, dpsz); wolffd@0: wolffd@0: % For BIC wolffd@0: CPD.nsamples = 0; wolffd@0: switch CPD.cov_type wolffd@0: case 'full', wolffd@0: ncov_params = ss*(ss-1)/2; % since symmetric (and positive definite) wolffd@0: case 'diag', wolffd@0: ncov_params = ss; wolffd@0: otherwise wolffd@0: error(['unrecognized cov_type ' cov_type]); wolffd@0: end wolffd@0: % params = weights + mean + cov wolffd@0: if CPD.tied_cov wolffd@0: CPD.nparams = ss*cpsz*dpsz + ss*dpsz + ncov_params; wolffd@0: else wolffd@0: CPD.nparams = ss*cpsz*dpsz + ss*dpsz + dpsz*ncov_params; wolffd@0: end wolffd@0: wolffd@0: wolffd@0: wolffd@0: clamped = CPD.clamped_mean & CPD.clamped_cov & CPD.clamped_weights; wolffd@0: CPD = set_clamped(CPD, clamped); wolffd@0: wolffd@0: %%%%%%%%%%% wolffd@0: wolffd@0: function CPD = init_fields() wolffd@0: % This ensures we define the fields in the same order wolffd@0: % no matter whether we load an object from a file, wolffd@0: % or create it from scratch. (Matlab requires this.) wolffd@0: wolffd@0: CPD.self = []; wolffd@0: CPD.sizes = []; wolffd@0: CPD.cps = []; wolffd@0: CPD.dps = []; wolffd@0: CPD.mean = []; wolffd@0: CPD.cov = []; wolffd@0: CPD.weights = []; wolffd@0: CPD.clamped_mean = []; wolffd@0: CPD.clamped_cov = []; wolffd@0: CPD.clamped_weights = []; wolffd@0: CPD.init_cov = []; wolffd@0: CPD.cov_type = []; wolffd@0: CPD.tied_cov = []; wolffd@0: CPD.Wsum = []; wolffd@0: CPD.WYsum = []; wolffd@0: CPD.WXsum = []; wolffd@0: CPD.WYYsum = []; wolffd@0: CPD.WXXsum = []; wolffd@0: CPD.WXYsum = []; wolffd@0: CPD.nsamples = []; wolffd@0: CPD.nparams = []; wolffd@0: CPD.cov_prior_weight = [];