Daniel@0: function CPD = gaussian_CPD(bnet, self, varargin) Daniel@0: % GAUSSIAN_CPD Make a conditional linear Gaussian distrib. Daniel@0: % Daniel@0: % CPD = gaussian_CPD(bnet, node, ...) will create a CPD with random parameters, Daniel@0: % where node is the number of a node in this equivalence class. Daniel@0: Daniel@0: % To define this CPD precisely, call the continuous (cts) parents (if any) X, Daniel@0: % the discrete parents (if any) Q, and this node Y. Then the distribution on Y is: Daniel@0: % - no parents: Y ~ N(mu, Sigma) Daniel@0: % - cts parents : Y|X=x ~ N(mu + W x, Sigma) Daniel@0: % - discrete parents: Y|Q=i ~ N(mu(i), Sigma(i)) Daniel@0: % - cts and discrete parents: Y|X=x,Q=i ~ N(mu(i) + W(i) x, Sigma(i)) Daniel@0: % Daniel@0: % The list below gives optional arguments [default value in brackets]. Daniel@0: % (Let ns(i) be the size of node i, X = ns(X), Y = ns(Y) and Q = prod(ns(Q)).) Daniel@0: % Parameters will be reshaped to the right size if necessary. Daniel@0: % Daniel@0: % mean - mu(:,i) is the mean given Q=i [ randn(Y,Q) ] Daniel@0: % cov - Sigma(:,:,i) is the covariance given Q=i [ repmat(100*eye(Y,Y), [1 1 Q]) ] Daniel@0: % weights - W(:,:,i) is the regression matrix given Q=i [ randn(Y,X,Q) ] Daniel@0: % cov_type - if 'diag', Sigma(:,:,i) is diagonal [ 'full' ] Daniel@0: % tied_cov - if 1, we constrain Sigma(:,:,i) to be the same for all i [0] Daniel@0: % clamp_mean - if 1, we do not adjust mu(:,i) during learning [0] Daniel@0: % clamp_cov - if 1, we do not adjust Sigma(:,:,i) during learning [0] Daniel@0: % clamp_weights - if 1, we do not adjust W(:,:,i) during learning [0] Daniel@0: % cov_prior_weight - weight given to I prior for estimating Sigma [0.01] Daniel@0: % cov_prior_entropic - if 1, we also use an entropic prior for Sigma [0] Daniel@0: % Daniel@0: % e.g., CPD = gaussian_CPD(bnet, i, 'mean', [0; 0], 'clamp_mean', 1) Daniel@0: Daniel@0: if nargin==0 Daniel@0: % This occurs if we are trying to load an object from a file. Daniel@0: CPD = init_fields; Daniel@0: clamp = 0; Daniel@0: CPD = class(CPD, 'gaussian_CPD', generic_CPD(clamp)); Daniel@0: return; Daniel@0: elseif isa(bnet, 'gaussian_CPD') Daniel@0: % This might occur if we are copying an object. Daniel@0: CPD = bnet; Daniel@0: return; Daniel@0: end Daniel@0: CPD = init_fields; Daniel@0: Daniel@0: CPD = class(CPD, 'gaussian_CPD', generic_CPD(0)); Daniel@0: Daniel@0: args = varargin; Daniel@0: ns = bnet.node_sizes; Daniel@0: ps = parents(bnet.dag, self); Daniel@0: dps = myintersect(ps, bnet.dnodes); Daniel@0: cps = myintersect(ps, bnet.cnodes); Daniel@0: fam_sz = ns([ps self]); Daniel@0: Daniel@0: CPD.self = self; Daniel@0: CPD.sizes = fam_sz; Daniel@0: Daniel@0: % Figure out which (if any) of the parents are discrete, and which cts, and how big they are Daniel@0: % dps = discrete parents, cps = cts parents Daniel@0: CPD.cps = find_equiv_posns(cps, ps); % cts parent index Daniel@0: CPD.dps = find_equiv_posns(dps, ps); Daniel@0: ss = fam_sz(end); Daniel@0: psz = fam_sz(1:end-1); Daniel@0: dpsz = prod(psz(CPD.dps)); Daniel@0: cpsz = sum(psz(CPD.cps)); Daniel@0: Daniel@0: % set default params Daniel@0: CPD.mean = randn(ss, dpsz); Daniel@0: CPD.cov = 100*repmat(eye(ss), [1 1 dpsz]); Daniel@0: CPD.weights = randn(ss, cpsz, dpsz); Daniel@0: CPD.cov_type = 'full'; Daniel@0: CPD.tied_cov = 0; Daniel@0: CPD.clamped_mean = 0; Daniel@0: CPD.clamped_cov = 0; Daniel@0: CPD.clamped_weights = 0; Daniel@0: CPD.cov_prior_weight = 0.01; Daniel@0: CPD.cov_prior_entropic = 0; Daniel@0: nargs = length(args); Daniel@0: if nargs > 0 Daniel@0: CPD = set_fields(CPD, args{:}); Daniel@0: end Daniel@0: Daniel@0: % Make sure the matrices have 1 dimension per discrete parent. Daniel@0: % Bug fix due to Xuejing Sun 3/6/01 Daniel@0: CPD.mean = myreshape(CPD.mean, [ss ns(dps)]); Daniel@0: CPD.cov = myreshape(CPD.cov, [ss ss ns(dps)]); Daniel@0: CPD.weights = myreshape(CPD.weights, [ss cpsz ns(dps)]); Daniel@0: Daniel@0: % Precompute indices into block structured matrices Daniel@0: % to speed up CPD_to_lambda_msg and CPD_to_pi Daniel@0: cpsizes = CPD.sizes(CPD.cps); Daniel@0: CPD.cps_block_ndx = cell(1, length(cps)); Daniel@0: for i=1:length(cps) Daniel@0: CPD.cps_block_ndx{i} = block(i, cpsizes); Daniel@0: end Daniel@0: Daniel@0: %%%%%%%%%%% Daniel@0: % Learning stuff Daniel@0: Daniel@0: % expected sufficient statistics Daniel@0: CPD.Wsum = zeros(dpsz,1); Daniel@0: CPD.WYsum = zeros(ss, dpsz); Daniel@0: CPD.WXsum = zeros(cpsz, dpsz); Daniel@0: CPD.WYYsum = zeros(ss, ss, dpsz); Daniel@0: CPD.WXXsum = zeros(cpsz, cpsz, dpsz); Daniel@0: CPD.WXYsum = zeros(cpsz, ss, dpsz); Daniel@0: Daniel@0: % For BIC Daniel@0: CPD.nsamples = 0; Daniel@0: switch CPD.cov_type Daniel@0: case 'full', Daniel@0: % since symmetric Daniel@0: %ncov_params = ss*(ss-1)/2; Daniel@0: ncov_params = ss*(ss+1)/2; Daniel@0: case 'diag', Daniel@0: ncov_params = ss; Daniel@0: otherwise Daniel@0: error(['unrecognized cov_type ' cov_type]); Daniel@0: end Daniel@0: % params = weights + mean + cov Daniel@0: if CPD.tied_cov Daniel@0: CPD.nparams = ss*cpsz*dpsz + ss*dpsz + ncov_params; Daniel@0: else Daniel@0: CPD.nparams = ss*cpsz*dpsz + ss*dpsz + dpsz*ncov_params; Daniel@0: end Daniel@0: Daniel@0: % for speeding up maximize_params Daniel@0: CPD.useC = exist('rep_mult'); Daniel@0: Daniel@0: clamped = CPD.clamped_mean & CPD.clamped_cov & CPD.clamped_weights; Daniel@0: CPD = set_clamped(CPD, clamped); Daniel@0: Daniel@0: %%%%%%%%%%% Daniel@0: Daniel@0: function CPD = init_fields() Daniel@0: % This ensures we define the fields in the same order Daniel@0: % no matter whether we load an object from a file, Daniel@0: % or create it from scratch. (Matlab requires this.) Daniel@0: Daniel@0: CPD.self = []; Daniel@0: CPD.sizes = []; Daniel@0: CPD.cps = []; Daniel@0: CPD.dps = []; Daniel@0: CPD.mean = []; Daniel@0: CPD.cov = []; Daniel@0: CPD.weights = []; Daniel@0: CPD.clamped_mean = []; Daniel@0: CPD.clamped_cov = []; Daniel@0: CPD.clamped_weights = []; Daniel@0: CPD.cov_type = []; Daniel@0: CPD.tied_cov = []; Daniel@0: CPD.Wsum = []; Daniel@0: CPD.WYsum = []; Daniel@0: CPD.WXsum = []; Daniel@0: CPD.WYYsum = []; Daniel@0: CPD.WXXsum = []; Daniel@0: CPD.WXYsum = []; Daniel@0: CPD.nsamples = []; Daniel@0: CPD.nparams = []; Daniel@0: CPD.cov_prior_weight = []; Daniel@0: CPD.cov_prior_entropic = []; Daniel@0: CPD.useC = []; Daniel@0: CPD.cps_block_ndx = [];