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

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