[4634c7]: src / modules / glm / samplers / GLMFactory.cc Maximize Restore History

Download this file

GLMFactory.cc    287 lines (241 with data), 7.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include <config.h>
#include "GLMFactory.h"
#include "GLMSampler.h"
#include <graph/GraphMarks.h>
#include <graph/Graph.h>
#include <graph/StochasticNode.h>
#include <graph/DeterministicNode.h>
#include <graph/LinkNode.h>
#include <distribution/Distribution.h>
#include <sampler/Linear.h>
#include <sampler/GraphView.h>
#include <sampler/SingletonGraphView.h>
#include <set>
#include <map>
#include <algorithm>
using std::set;
using std::vector;
using std::map;
using std::stable_sort;
using std::string;
namespace jags {
/*
Aggregates candidate Nodes into a joint linear model.
*/
static bool aggregateLinear(SingletonGraphView const *candidate,
set<StochasticNode const *> &stochastic_children,
Graph const &graph)
{
// Check that there is some overlap in stochastic children between
// candidate node and current set.
vector<StochasticNode *> const &candidate_children =
candidate->stochasticChildren();
bool overlap = false;
for (unsigned int i = 0; i < candidate_children.size(); ++i) {
if (stochastic_children.count(candidate_children[i]) > 0) {
stochastic_children.insert(candidate_children.begin(),
candidate_children.end());
return true;
}
}
return overlap;
}
struct less_view {
/*
Comparison operator for views which sorts them in
reverse order of the number of stochastic children
*/
bool operator()(GraphView const *x, GraphView const *y) const {
return (x->stochasticChildren().size() >
y->stochasticChildren().size());
};
};
namespace glm {
GLMFactory::GLMFactory(string const &name)
: _name(name)
{}
bool GLMFactory::checkDescendants(SingletonGraphView const *view) const
{
// Check stochastic children
vector<StochasticNode *> const &stoch_nodes =
view->stochasticChildren();
for (unsigned int i = 0; i < stoch_nodes.size(); ++i) {
if (isBounded(stoch_nodes[i])) {
return false; //Truncated outcome variable
}
if (!checkOutcome(stoch_nodes[i])) {
return false; //Invalid outcome or link
}
if (fixedOutcome() && !isObserved(stoch_nodes[i])) {
return false; //Unobserved outcome not allowed by sampler
}
//Check that other parameters do not depend on snode
vector<Node const *> const &param = stoch_nodes[i]->parents();
for (unsigned int j = 1; j < param.size(); ++j) {
if (view->isDependent(param[j])) {
return false;
}
}
}
// Check linearity of deterministic descendants
if (!checkLinear(view, fixedDesign(), true))
return false;
return true;
}
SingletonGraphView *
GLMFactory::makeView(StochasticNode *snode, Graph const &graph) const
{
/*
Returns a newly allocated GraphView if node can be sampled,
otherwise zero pointer.
*/
string dname = snode->distribution()->name();
if (dname != "dnorm" && dname != "dmnorm")
return 0; //Must have normal prior
if (!canSample(snode))
return 0;
SingletonGraphView *view = new SingletonGraphView(snode, graph);
if (!checkDescendants(view)) {
delete view;
return 0;
}
else {
return view;
}
}
GLMFactory::~GLMFactory()
{}
Sampler *
GLMFactory::makeSampler(set<StochasticNode*> const &nodes,
Graph const &graph) const
{
/*
Find candidate nodes that could be in a linear model.
Keep track of the number of stochastic children
*/
vector<SingletonGraphView*> candidates;
for (set<StochasticNode*>::const_iterator p = nodes.begin();
p != nodes.end(); ++p)
{
SingletonGraphView *up = makeView(*p, graph);
if (up) {
candidates.push_back(up);
}
}
if (candidates.empty())
return 0;
//Sort candidates in order of decreasing number of stochastic children
stable_sort(candidates.begin(), candidates.end(), less_view());
//Now try to aggregate nodes into a joint linear model
unsigned int Nc = candidates.size();
vector<bool> keep(Nc, false);
vector<bool> resolved(Nc, false);
GraphView *view = 0;
for (unsigned int i = 0; i < Nc; ++i) {
keep[i] = true;
resolved[i] = true;
set<StochasticNode const *> stoch_children;
stoch_children.insert(candidates[i]->stochasticChildren().begin(),
candidates[i]->stochasticChildren().end());
//Find a joint linear model.
bool loop = false;
do {
loop = false;
for (unsigned int j = i+1; j < candidates.size(); ++j) {
if (!resolved[j]) {
keep[j] = aggregateLinear(candidates[j],
stoch_children,
graph);
if (keep[j]) {
loop = true;
resolved[j] = true;
}
}
}
} while (loop);
// Remove candidate nodes that are stochastic children of
// another candidate node. All GLMMethod algorithms rely
// on the stochastic parents being fixed within any update.
set<StochasticNode const*> all_children;
for (unsigned int j = 0; j < candidates.size(); ++j) {
if (keep[j]) {
vector<StochasticNode *> const &children_j =
candidates[j]->stochasticChildren();
all_children.insert(children_j.begin(), children_j.end());
}
}
for (unsigned int j = 0; j < candidates.size(); ++j) {
if (keep[j]) {
if (all_children.count(candidates[j]->nodes()[0])) {
keep[j] = false;
}
}
}
vector<StochasticNode*> sample_nodes;
for (unsigned int j = 0; j < candidates.size(); ++j) {
if (keep[j]) {
sample_nodes.push_back(candidates[j]->nodes()[0]);
}
}
if (sample_nodes.size() > 1) {
view = new GraphView(sample_nodes, graph);
if (checkLinear(view, fixedDesign(), true)) {
break;
}
else {
delete view; view = 0;
}
}
for (unsigned int j = i; j < candidates.size(); ++j) {
keep[j] = false;
}
}
vector<SingletonGraphView*> sub_views;
for (unsigned int i = 0; i < Nc; ++i) {
if (keep[i]) {
sub_views.push_back(candidates[i]);
}
else {
delete candidates[i];
}
}
if (!sub_views.empty()) {
unsigned int Nch = nchain(view);
vector<SampleMethod*> methods(Nch, 0);
vector<SingletonGraphView const*> const_sub_views(sub_views.size());
//FIXME: std::copy
for (unsigned int i = 0; i < sub_views.size(); ++i) {
const_sub_views[i] = sub_views[i];
}
for (unsigned int ch = 0; ch < Nch; ++ch) {
methods[ch] = newMethod(view, const_sub_views, ch);
}
return new GLMSampler(view, sub_views, methods);
}
else {
return 0;
}
}
string GLMFactory::name() const
{
return _name;
}
vector<Sampler*>
GLMFactory::makeSamplers(set<StochasticNode*> const &nodes,
Graph const &graph) const
{
Sampler *s = makeSampler(nodes, graph);
if (s)
return vector<Sampler*>(1, s);
else
return vector<Sampler*>();
}
bool GLMFactory::fixedDesign() const
{
return false;
}
bool GLMFactory::fixedOutcome() const
{
return false;
}
}}