annotate toolboxes/FullBNT-1.0.7/bnt/CPDs/@tabular_CPD/tabular_CPD.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function CPD = tabular_CPD(bnet, self, varargin)
wolffd@0 2 % TABULAR_CPD Make a multinomial conditional prob. distrib. (CPT)
wolffd@0 3 %
wolffd@0 4 % CPD = tabular_CPD(bnet, node) creates a random CPT.
wolffd@0 5 %
wolffd@0 6 % The following arguments can be specified [default in brackets]
wolffd@0 7 %
wolffd@0 8 % CPT - specifies the params ['rnd']
wolffd@0 9 % - T means use table T; it will be reshaped to the size of node's family.
wolffd@0 10 % - 'rnd' creates rnd params (drawn from uniform)
wolffd@0 11 % - 'unif' creates a uniform distribution
wolffd@0 12 % adjustable - 0 means don't adjust the parameters during learning [1]
wolffd@0 13 % prior_type - defines type of prior ['none']
wolffd@0 14 % - 'none' means do ML estimation
wolffd@0 15 % - 'dirichlet' means add pseudo-counts to every cell
wolffd@0 16 % - 'entropic' means use a prior P(theta) propto exp(-H(theta)) (see Brand)
wolffd@0 17 % dirichlet_weight - equivalent sample size (ess) of the dirichlet prior [1]
wolffd@0 18 % dirichlet_type - defines the type of Dirichlet prior ['BDeu']
wolffd@0 19 % - 'unif' means put dirichlet_weight in every cell
wolffd@0 20 % - 'BDeu' means we put 'dirichlet_weight/(r q)' in every cell
wolffd@0 21 % where r = self_sz and q = prod(parent_sz) (see Heckerman)
wolffd@0 22 % trim - 1 means trim redundant params (rows in CPT) when using entropic prior [0]
wolffd@0 23 % entropic_pcases - list of assignments to the parents nodes when we should use
wolffd@0 24 % the entropic prior; all other cases will be estimated using ML [1:psz]
wolffd@0 25 % sparse - 1 means use 1D sparse array to represent CPT [0]
wolffd@0 26 %
wolffd@0 27 % e.g., tabular_CPD(bnet, i, 'CPT', T)
wolffd@0 28 % e.g., tabular_CPD(bnet, i, 'CPT', 'unif', 'dirichlet_weight', 2, 'dirichlet_type', 'unif')
wolffd@0 29 %
wolffd@0 30 % REFERENCES
wolffd@0 31 % M. Brand - "Structure learning in conditional probability models via an entropic prior
wolffd@0 32 % and parameter extinction", Neural Computation 11 (1999): 1155--1182
wolffd@0 33 % M. Brand - "Pattern discovery via entropy minimization" [covers annealing]
wolffd@0 34 % AI & Statistics 1999. Equation numbers refer to this paper, which is available from
wolffd@0 35 % www.merl.com/reports/docs/TR98-21.pdf
wolffd@0 36 % D. Heckerman, D. Geiger and M. Chickering,
wolffd@0 37 % "Learning Bayesian networks: the combination of knowledge and statistical data",
wolffd@0 38 % Microsoft Research Tech Report, 1994
wolffd@0 39
wolffd@0 40
wolffd@0 41 if nargin==0
wolffd@0 42 % This occurs if we are trying to load an object from a file.
wolffd@0 43 CPD = init_fields;
wolffd@0 44 CPD = class(CPD, 'tabular_CPD', discrete_CPD(0, []));
wolffd@0 45 return;
wolffd@0 46 elseif isa(bnet, 'tabular_CPD')
wolffd@0 47 % This might occur if we are copying an object.
wolffd@0 48 CPD = bnet;
wolffd@0 49 return;
wolffd@0 50 end
wolffd@0 51 CPD = init_fields;
wolffd@0 52
wolffd@0 53 ns = bnet.node_sizes;
wolffd@0 54 ps = parents(bnet.dag, self);
wolffd@0 55 fam_sz = ns([ps self]);
wolffd@0 56 psz = prod(ns(ps));
wolffd@0 57 CPD.sizes = fam_sz;
wolffd@0 58 CPD.leftright = 0;
wolffd@0 59 CPD.sparse = 0;
wolffd@0 60
wolffd@0 61 % set defaults
wolffd@0 62 CPD.CPT = mk_stochastic(myrand(fam_sz));
wolffd@0 63 CPD.adjustable = 1;
wolffd@0 64 CPD.prior_type = 'none';
wolffd@0 65 dirichlet_type = 'BDeu';
wolffd@0 66 dirichlet_weight = 1;
wolffd@0 67 CPD.trim = 0;
wolffd@0 68 selfprob = 0.1;
wolffd@0 69 CPD.entropic_pcases = 1:psz;
wolffd@0 70
wolffd@0 71 % extract optional args
wolffd@0 72 args = varargin;
wolffd@0 73 % check for old syntax CPD(bnet, i, CPT) as opposed to CPD(bnet, i, 'CPT', CPT)
wolffd@0 74 if ~isempty(args) & ~isstr(args{1})
wolffd@0 75 CPD.CPT = myreshape(args{1}, fam_sz);
wolffd@0 76 args = [];
wolffd@0 77 end
wolffd@0 78
wolffd@0 79 for i=1:2:length(args)
wolffd@0 80 switch args{i},
wolffd@0 81 case 'CPT',
wolffd@0 82 T = args{i+1};
wolffd@0 83 if ischar(T)
wolffd@0 84 switch T
wolffd@0 85 case 'unif', CPD.CPT = mk_stochastic(myones(fam_sz));
wolffd@0 86 case 'rnd', CPD.CPT = mk_stochastic(myrand(fam_sz));
wolffd@0 87 otherwise, error(['invalid CPT ' T]);
wolffd@0 88 end
wolffd@0 89 else
wolffd@0 90 CPD.CPT = myreshape(T, fam_sz);
wolffd@0 91 end
wolffd@0 92 case 'prior_type', CPD.prior_type = args{i+1};
wolffd@0 93 case 'dirichlet_type', dirichlet_type = args{i+1};
wolffd@0 94 case 'dirichlet_weight', dirichlet_weight = args{i+1};
wolffd@0 95 case 'adjustable', CPD.adjustable = args{i+1};
wolffd@0 96 case 'clamped', CPD.adjustable = ~args{i+1};
wolffd@0 97 case 'trim', CPD.trim = args{i+1};
wolffd@0 98 case 'entropic_pcases', CPD.entropic_pcases = args{i+1};
wolffd@0 99 case 'sparse', CPD.sparse = args{i+1};
wolffd@0 100 otherwise, error(['invalid argument name: ' args{i}]);
wolffd@0 101 end
wolffd@0 102 end
wolffd@0 103
wolffd@0 104 switch CPD.prior_type
wolffd@0 105 case 'dirichlet',
wolffd@0 106 switch dirichlet_type
wolffd@0 107 case 'unif', CPD.dirichlet = dirichlet_weight * myones(fam_sz);
wolffd@0 108 case 'BDeu', CPD.dirichlet = (dirichlet_weight/psz) * mk_stochastic(myones(fam_sz));
wolffd@0 109 otherwise, error(['invalid dirichlet_type ' dirichlet_type])
wolffd@0 110 end
wolffd@0 111 case {'entropic', 'none'}
wolffd@0 112 CPD.dirichlet = [];
wolffd@0 113 otherwise, error(['invalid prior_type ' prior_type])
wolffd@0 114 end
wolffd@0 115
wolffd@0 116
wolffd@0 117
wolffd@0 118 % fields to do with learning
wolffd@0 119 if ~CPD.adjustable
wolffd@0 120 CPD.counts = [];
wolffd@0 121 CPD.nparams = 0;
wolffd@0 122 CPD.nsamples = [];
wolffd@0 123 else
wolffd@0 124 %CPD.counts = zeros(size(CPD.CPT));
wolffd@0 125 CPD.counts = zeros(prod(size(CPD.CPT)), 1);
wolffd@0 126 psz = fam_sz(1:end-1);
wolffd@0 127 ss = fam_sz(end);
wolffd@0 128 if CPD.leftright
wolffd@0 129 % For each of the Qps contexts, we specify Q elements on the diagoanl
wolffd@0 130 CPD.nparams = Qps * Q;
wolffd@0 131 else
wolffd@0 132 % sum-to-1 constraint reduces the effective arity of the node by 1
wolffd@0 133 CPD.nparams = prod([psz ss-1]);
wolffd@0 134 end
wolffd@0 135 CPD.nsamples = 0;
wolffd@0 136 end
wolffd@0 137
wolffd@0 138 CPD.trimmed_trans = [];
wolffd@0 139 fam_sz = CPD.sizes;
wolffd@0 140
wolffd@0 141 %psz = prod(fam_sz(1:end-1));
wolffd@0 142 %ssz = fam_sz(end);
wolffd@0 143 %CPD.trimmed_trans = zeros(psz, ssz); % must declare before reading
wolffd@0 144
wolffd@0 145 %sparse CPT
wolffd@0 146 if CPD.sparse
wolffd@0 147 CPD.CPT = sparse(CPD.CPT(:));
wolffd@0 148 end
wolffd@0 149
wolffd@0 150 CPD = class(CPD, 'tabular_CPD', discrete_CPD(~CPD.adjustable, fam_sz));
wolffd@0 151
wolffd@0 152
wolffd@0 153 %%%%%%%%%%%
wolffd@0 154
wolffd@0 155 function CPD = init_fields()
wolffd@0 156 % This ensures we define the fields in the same order
wolffd@0 157 % no matter whether we load an object from a file,
wolffd@0 158 % or create it from scratch. (Matlab requires this.)
wolffd@0 159
wolffd@0 160 CPD.CPT = [];
wolffd@0 161 CPD.sizes = [];
wolffd@0 162 CPD.prior_type = [];
wolffd@0 163 CPD.dirichlet = [];
wolffd@0 164 CPD.adjustable = [];
wolffd@0 165 CPD.counts = [];
wolffd@0 166 CPD.nparams = [];
wolffd@0 167 CPD.nsamples = [];
wolffd@0 168 CPD.trim = [];
wolffd@0 169 CPD.trimmed_trans = [];
wolffd@0 170 CPD.leftright = [];
wolffd@0 171 CPD.entropic_pcases = [];
wolffd@0 172 CPD.sparse = [];
wolffd@0 173