Daniel@0: function CPD = tabular_CPD(bnet, self, varargin) Daniel@0: % TABULAR_CPD Make a multinomial conditional prob. distrib. (CPT) Daniel@0: % Daniel@0: % CPD = tabular_CPD(bnet, node) creates a random CPT. Daniel@0: % Daniel@0: % The following arguments can be specified [default in brackets] Daniel@0: % Daniel@0: % CPT - specifies the params ['rnd'] Daniel@0: % - T means use table T; it will be reshaped to the size of node's family. Daniel@0: % - 'rnd' creates rnd params (drawn from uniform) Daniel@0: % - 'unif' creates a uniform distribution Daniel@0: % adjustable - 0 means don't adjust the parameters during learning [1] Daniel@0: % prior_type - defines type of prior ['none'] Daniel@0: % - 'none' means do ML estimation Daniel@0: % - 'dirichlet' means add pseudo-counts to every cell Daniel@0: % - 'entropic' means use a prior P(theta) propto exp(-H(theta)) (see Brand) Daniel@0: % dirichlet_weight - equivalent sample size (ess) of the dirichlet prior [1] Daniel@0: % dirichlet_type - defines the type of Dirichlet prior ['BDeu'] Daniel@0: % - 'unif' means put dirichlet_weight in every cell Daniel@0: % - 'BDeu' means we put 'dirichlet_weight/(r q)' in every cell Daniel@0: % where r = self_sz and q = prod(parent_sz) (see Heckerman) Daniel@0: % trim - 1 means trim redundant params (rows in CPT) when using entropic prior [0] Daniel@0: % entropic_pcases - list of assignments to the parents nodes when we should use Daniel@0: % the entropic prior; all other cases will be estimated using ML [1:psz] Daniel@0: % sparse - 1 means use 1D sparse array to represent CPT [0] Daniel@0: % Daniel@0: % e.g., tabular_CPD(bnet, i, 'CPT', T) Daniel@0: % e.g., tabular_CPD(bnet, i, 'CPT', 'unif', 'dirichlet_weight', 2, 'dirichlet_type', 'unif') Daniel@0: % Daniel@0: % REFERENCES Daniel@0: % M. Brand - "Structure learning in conditional probability models via an entropic prior Daniel@0: % and parameter extinction", Neural Computation 11 (1999): 1155--1182 Daniel@0: % M. Brand - "Pattern discovery via entropy minimization" [covers annealing] Daniel@0: % AI & Statistics 1999. Equation numbers refer to this paper, which is available from Daniel@0: % www.merl.com/reports/docs/TR98-21.pdf Daniel@0: % D. Heckerman, D. Geiger and M. Chickering, Daniel@0: % "Learning Bayesian networks: the combination of knowledge and statistical data", Daniel@0: % Microsoft Research Tech Report, 1994 Daniel@0: 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: CPD = class(CPD, 'tabular_CPD', discrete_CPD(0, [])); Daniel@0: return; Daniel@0: elseif isa(bnet, 'tabular_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: ns = bnet.node_sizes; Daniel@0: ps = parents(bnet.dag, self); Daniel@0: fam_sz = ns([ps self]); Daniel@0: psz = prod(ns(ps)); Daniel@0: CPD.sizes = fam_sz; Daniel@0: CPD.leftright = 0; Daniel@0: CPD.sparse = 0; Daniel@0: Daniel@0: % set defaults Daniel@0: CPD.CPT = mk_stochastic(myrand(fam_sz)); Daniel@0: CPD.adjustable = 1; Daniel@0: CPD.prior_type = 'none'; Daniel@0: dirichlet_type = 'BDeu'; Daniel@0: dirichlet_weight = 1; Daniel@0: CPD.trim = 0; Daniel@0: selfprob = 0.1; Daniel@0: CPD.entropic_pcases = 1:psz; Daniel@0: Daniel@0: % extract optional args Daniel@0: args = varargin; Daniel@0: % check for old syntax CPD(bnet, i, CPT) as opposed to CPD(bnet, i, 'CPT', CPT) Daniel@0: if ~isempty(args) & ~isstr(args{1}) Daniel@0: CPD.CPT = myreshape(args{1}, fam_sz); Daniel@0: args = []; Daniel@0: end Daniel@0: Daniel@0: for i=1:2:length(args) Daniel@0: switch args{i}, Daniel@0: case 'CPT', Daniel@0: T = args{i+1}; Daniel@0: if ischar(T) Daniel@0: switch T Daniel@0: case 'unif', CPD.CPT = mk_stochastic(myones(fam_sz)); Daniel@0: case 'rnd', CPD.CPT = mk_stochastic(myrand(fam_sz)); Daniel@0: otherwise, error(['invalid CPT ' T]); Daniel@0: end Daniel@0: else Daniel@0: CPD.CPT = myreshape(T, fam_sz); Daniel@0: end Daniel@0: case 'prior_type', CPD.prior_type = args{i+1}; Daniel@0: case 'dirichlet_type', dirichlet_type = args{i+1}; Daniel@0: case 'dirichlet_weight', dirichlet_weight = args{i+1}; Daniel@0: case 'adjustable', CPD.adjustable = args{i+1}; Daniel@0: case 'clamped', CPD.adjustable = ~args{i+1}; Daniel@0: case 'trim', CPD.trim = args{i+1}; Daniel@0: case 'entropic_pcases', CPD.entropic_pcases = args{i+1}; Daniel@0: case 'sparse', CPD.sparse = args{i+1}; Daniel@0: otherwise, error(['invalid argument name: ' args{i}]); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: switch CPD.prior_type Daniel@0: case 'dirichlet', Daniel@0: switch dirichlet_type Daniel@0: case 'unif', CPD.dirichlet = dirichlet_weight * myones(fam_sz); Daniel@0: case 'BDeu', CPD.dirichlet = (dirichlet_weight/psz) * mk_stochastic(myones(fam_sz)); Daniel@0: otherwise, error(['invalid dirichlet_type ' dirichlet_type]) Daniel@0: end Daniel@0: case {'entropic', 'none'} Daniel@0: CPD.dirichlet = []; Daniel@0: otherwise, error(['invalid prior_type ' prior_type]) Daniel@0: end Daniel@0: Daniel@0: Daniel@0: Daniel@0: % fields to do with learning Daniel@0: if ~CPD.adjustable Daniel@0: CPD.counts = []; Daniel@0: CPD.nparams = 0; Daniel@0: CPD.nsamples = []; Daniel@0: else Daniel@0: %CPD.counts = zeros(size(CPD.CPT)); Daniel@0: CPD.counts = zeros(prod(size(CPD.CPT)), 1); Daniel@0: psz = fam_sz(1:end-1); Daniel@0: ss = fam_sz(end); Daniel@0: if CPD.leftright Daniel@0: % For each of the Qps contexts, we specify Q elements on the diagoanl Daniel@0: CPD.nparams = Qps * Q; Daniel@0: else Daniel@0: % sum-to-1 constraint reduces the effective arity of the node by 1 Daniel@0: CPD.nparams = prod([psz ss-1]); Daniel@0: end Daniel@0: CPD.nsamples = 0; Daniel@0: end Daniel@0: Daniel@0: CPD.trimmed_trans = []; Daniel@0: fam_sz = CPD.sizes; Daniel@0: Daniel@0: %psz = prod(fam_sz(1:end-1)); Daniel@0: %ssz = fam_sz(end); Daniel@0: %CPD.trimmed_trans = zeros(psz, ssz); % must declare before reading Daniel@0: Daniel@0: %sparse CPT Daniel@0: if CPD.sparse Daniel@0: CPD.CPT = sparse(CPD.CPT(:)); Daniel@0: end Daniel@0: Daniel@0: CPD = class(CPD, 'tabular_CPD', discrete_CPD(~CPD.adjustable, fam_sz)); Daniel@0: 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.CPT = []; Daniel@0: CPD.sizes = []; Daniel@0: CPD.prior_type = []; Daniel@0: CPD.dirichlet = []; Daniel@0: CPD.adjustable = []; Daniel@0: CPD.counts = []; Daniel@0: CPD.nparams = []; Daniel@0: CPD.nsamples = []; Daniel@0: CPD.trim = []; Daniel@0: CPD.trimmed_trans = []; Daniel@0: CPD.leftright = []; Daniel@0: CPD.entropic_pcases = []; Daniel@0: CPD.sparse = []; Daniel@0: