Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/bnt/inference/static/@gibbs_sampling_inf_engine/gibbs_sampling_inf_engine.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function engine = gibbs_sampling_inf_engine(bnet, varargin) | |
2 % GIBBS_SAMPLING_INF_ENGINE | |
3 % | |
4 % engine = gibbs_sampling_inf_engine(bnet, ...) | |
5 % | |
6 % Optional parameters [default in brackets] | |
7 % 'burnin' - How long before you start using the samples [100]. | |
8 % 'gap' - how often you use the samples in the estimate [1]. | |
9 % 'T' - number of samples [1000] | |
10 % i.e, number of node flips (so, for | |
11 % example if there are 10 nodes in the bnet, and T is 1000, each | |
12 % node will get flipped 100 times (assuming a deterministic schedule)) | |
13 % The total running time is proportional to burnin + T*gap. | |
14 % | |
15 % 'order' - if the sampling schedule is deterministic, use this | |
16 % parameter to specify the order in which nodes are sampled. | |
17 % Order is allowed to include multiple copies of nodes, which is | |
18 % useful if you want to, say, focus sampling on particular nodes. | |
19 % Default is to use a deterministic schedule that goes through the | |
20 % nodes in order. | |
21 % | |
22 % 'sampling_dist' - when using a stochastic sampling method, at | |
23 % each step the node to sample is chosen according to this | |
24 % distribution (may be unnormalized) | |
25 % | |
26 % The sampling_dist and order parameters shouldn't both be used, | |
27 % and this will cause an assert. | |
28 % | |
29 % | |
30 % Written by "Bhaskara Marthi" <bhaskara@cs.berkeley.edu> Feb 02. | |
31 | |
32 | |
33 engine.burnin = 100; | |
34 engine.gap = 1; | |
35 engine.T = 1000; | |
36 use_default_order = 1; | |
37 engine.deterministic = 1; | |
38 engine.order = {}; | |
39 engine.sampling_dist = {}; | |
40 | |
41 if nargin >= 2 | |
42 args = varargin; | |
43 nargs = length(args); | |
44 for i = 1:2:nargs | |
45 switch args{i} | |
46 case 'burnin' | |
47 engine.burnin = args{i+1}; | |
48 case 'gap' | |
49 engine.gap = args{i+1}; | |
50 case 'T' | |
51 engine.T = args{i+1}; | |
52 case 'order' | |
53 assert (use_default_order); | |
54 use_default_order = 0; | |
55 engine.order = args{i+1}; | |
56 case 'sampling_dist' | |
57 assert (use_default_order); | |
58 use_default_order = 0; | |
59 engine.deterministic = 0; | |
60 engine.sampling_dist = args{i+1}; | |
61 otherwise | |
62 error(['unrecognized parameter to gibbs_sampling_inf_engine']); | |
63 end | |
64 end | |
65 end | |
66 | |
67 engine.slice_size = size(bnet.dag, 2); | |
68 if (use_default_order) | |
69 engine.order = 1:engine.slice_size; | |
70 end | |
71 engine.hnodes = []; | |
72 engine.onodes = []; | |
73 engine.evidence = []; | |
74 engine.state = []; | |
75 engine.marginal_counts = {}; | |
76 | |
77 % Precompute the strides for each CPT | |
78 engine.strides = compute_strides(bnet); | |
79 | |
80 % Precompute graphical information | |
81 engine.families = compute_families(bnet); | |
82 engine.children = compute_children(bnet); | |
83 | |
84 % For convenience, store the CPTs as tables rather than objects | |
85 engine.CPT = get_cpts(bnet); | |
86 | |
87 engine = class(engine, 'gibbs_sampling_inf_engine', inf_engine(bnet)); | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 |