[e23e50]: src / modules / bugs / samplers / ConjugateMNormal.cc  Maximize  Restore  History

Download this file

320 lines (263 with data), 8.3 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <config.h>
#include <rng/RNG.h>
#include <graph/AggNode.h>
#include <graph/MixtureNode.h>
#include <graph/NodeError.h>
#include <graph/LogicalNode.h>
#include <graph/StochasticNode.h>
#include <sampler/Linear.h>
#include <sampler/SingletonGraphView.h>
#include <module/ModuleError.h>
#include "lapack.h"
#include <set>
#include <vector>
#include <cmath>
#include <string>
#include "ConjugateMNormal.h"
#include "DMNorm.h"
#include <JRmath.h>
using std::string;
using std::vector;
using std::set;
using std::sqrt;
using std::string;
namespace jags {
namespace bugs {
static void calBeta(double *betas, SingletonGraphView const *gv,
unsigned int chain)
{
StochasticNode *snode = gv->node();
double const *xold = snode->value(chain);
unsigned int nrow = snode->length();
double *xnew = new double[nrow];
for (unsigned int i = 0; i < nrow; ++i) {
xnew[i] = xold[i];
}
vector<StochasticNode *> const &stoch_children =
gv->stochasticChildren();
unsigned long nchildren = stoch_children.size();
double *beta_j = betas;
for (unsigned int j = 0; j < nchildren; ++j) {
StochasticNode const *snode = stoch_children[j];
double const *mu = snode->parents()[0]->value(chain);
unsigned int nrow_child = snode->length();
for (unsigned int k = 0; k < nrow_child; ++k) {
for (unsigned int i = 0; i < nrow; ++i) {
beta_j[nrow * k + i] = -mu[k];
}
}
beta_j += nrow_child * nrow;
}
for (unsigned int i = 0; i < nrow; ++i) {
xnew[i] += 1;
gv->setValue(xnew, nrow, chain);
beta_j = betas;
for (unsigned int j = 0; j < nchildren; ++j) {
StochasticNode const *snode = stoch_children[j];
double const *mu = snode->parents()[0]->value(chain);
unsigned int nrow_child = snode->length();
for (unsigned int k = 0; k < nrow_child; ++k) {
beta_j[nrow * k + i] += mu[k];
}
beta_j += nrow_child * nrow;
}
xnew[i] -= 1;
}
gv->setValue(xnew, nrow, chain);
delete [] xnew;
}
static unsigned int sumChildrenLength(SingletonGraphView const *gv)
{
vector<StochasticNode *> const &children =
gv->stochasticChildren();
unsigned int N = 0;
for (unsigned int i = 0; i < children.size(); ++i) {
N += children[i]->length();
}
return N;
}
ConjugateMNormal::ConjugateMNormal(SingletonGraphView const *gv)
: ConjugateMethod(gv), _betas(0),
_length_betas(sumChildrenLength(gv) * gv->length())
{
if(!gv->deterministicChildren().empty() && checkLinear(gv, true))
{
_betas = new double[_length_betas];
calBeta(_betas, gv, 0);
}
}
ConjugateMNormal::~ConjugateMNormal()
{
delete [] _betas;
}
bool ConjugateMNormal::canSample(StochasticNode *snode, Graph const &graph)
{
if (getDist(snode) != MNORM)
return false;
if (isBounded(snode))
return false;
SingletonGraphView gv(snode, graph);
vector<StochasticNode *> const &schild = gv.stochasticChildren();
// Check stochastic children
for (unsigned int i = 0; i < schild.size(); ++i) {
if (getDist(schild[i]) != MNORM && getDist(schild[i]) != NORM) {
return false; //Not normal or multivariate normal
}
if (isBounded(schild[i])) {
return false;
}
if (gv.isDependent(schild[i]->parents()[1])) {
return false; //Precision depends on snode
}
}
// Check linearity of deterministic descendants
if (!checkLinear(&gv, false))
return false;
return true; //We made it!
}
void ConjugateMNormal::update(unsigned int chain, RNG *rng) const
{
vector<StochasticNode *> const &stoch_children =
_gv->stochasticChildren();
unsigned int nchildren = stoch_children.size();
StochasticNode *snode = _gv->node();
double const *xold = snode->value(chain);
double const *priormean = snode->parents()[0]->value(chain);
double const *priorprec = snode->parents()[1]->value(chain);
int nrow = snode->length();
/*
The log of the full conditional density takes the form
-1/2(t(x) %*% A %*% x - 2 * b %*% x)
For computational convenience, we reset the origin to xold,
the current value of the node.
*/
int N = nrow * nrow;
double *b = new double[nrow];
double *A = new double[N];
for (int i = 0; i < nrow; ++i) {
b[i] = 0;
for (int i2 = 0; i2 < nrow; ++i2) {
b[i] += priorprec[i * nrow + i2] * (priormean[i2] - xold[i2]);
}
}
for (int i = 0; i < N; ++i) {
A[i] = priorprec[i];
}
/* FORTRAN routines are all call-by-reference, so we need to create
* these constants */
double zero = 0;
double d1 = 1;
int i1 = 1;
if (_gv->deterministicChildren().empty()) {
// This can only happen if the stochastic children are all
// multivariate normal with the same number of rows and
// columns. We know alpha = 0, beta = I.
double *delta = new double[nrow];
for (unsigned int j = 0; j < nchildren; ++j) {
double const *Y = stoch_children[j]->value(chain);
double const *tau = stoch_children[j]->parents()[1]->value(chain);
double alpha = 1;
F77_DAXPY (&N, &alpha, tau, &i1, A, &i1);
for (int i = 0; i < nrow; ++i) {
delta[i] = Y[i] - xold[i];
}
F77_DGEMV ("N", &nrow, &nrow, &alpha, tau, &nrow, delta, &i1,
&d1, b, &i1);
}
delete [] delta;
}
else {
bool temp_beta = (_betas == 0);
double *betas = 0;
if (temp_beta) {
betas = new double[_length_betas];
calBeta(betas, _gv, chain);
}
else {
betas = _betas;
}
//Calculate largest possible size of working matrix C
unsigned int max_nrow_child = 0;
for (unsigned int j = 0; j < nchildren; ++j) {
unsigned int nrow_j = stoch_children[j]->length();
if (nrow_j > max_nrow_child) max_nrow_child = nrow_j;
}
double *C = new double[nrow * max_nrow_child];
double *delta = new double[max_nrow_child];
/* Now add the contribution of each term to A, b
b += N_j * beta_j %*% tau_j (Y_j - mu_j)
A += N_j * beta_j %*% tau_j %*% t(beta_j)
where
- N_j is the frequency weight of child j
- beta_j is a matrix of linear coefficients
- tau_j is the variance-covariance matrix of child j
- mu_j is the mean of child j
- Y_j is the value of child j
We make use of BLAS routines for efficiency.
*/
double const *beta_j = betas;
for (unsigned int j = 0; j < nchildren; ++j) {
StochasticNode const *schild = stoch_children[j];
double const *Y = schild->value(chain);
double const *mu = schild->parents()[0]->value(chain);
double const *tau = schild->parents()[1]->value(chain);
int nrow_child = schild->length();
if (nrow_child == 1) {
double alpha = tau[0];
F77_DSYR("L", &nrow, &alpha, beta_j, &i1, A, &nrow);
alpha *= (Y[0] - mu[0]);
F77_DAXPY(&nrow, &alpha, beta_j, &i1, b, &i1);
}
else {
double alpha = 1;
F77_DSYMM("R", "L", &nrow, &nrow_child, &alpha, tau,
&nrow_child, beta_j, &nrow, &zero, C, &nrow);
for (int i = 0; i < nrow_child; ++i) {
delta[i] = Y[i] - mu[i];
}
F77_DGEMV("N", &nrow, &nrow_child, &d1, C, &nrow,
delta, &i1, &d1, b, &i1);
F77_DGEMM("N","T", &nrow, &nrow, &nrow_child,
&d1, C, &nrow, beta_j, &nrow, &d1, A, &nrow);
}
beta_j += nrow_child * nrow;
}
delete [] C;
delete [] delta;
if (temp_beta) {
delete [] betas;
}
}
/*
Solve the equation A %*% x = b to get the posterior mean.
We have to take a copy of A as it is overwritten during
the call to DPOSV. The result is stored in b
*/
double * Acopy = new double[N];
for (int i = 0; i < N; ++i) {
Acopy[i] = A[i];
}
int one = 1;
int info;
F77_DPOSV ("L", &nrow, &one, Acopy, &nrow, b, &nrow, &info);
if (info != 0) {
delete [] Acopy;
delete [] A;
delete [] b;
throwNodeError(snode,
"unable to solve linear equations in ConjugateMNormal");
}
//Shift origin back to original scale
for (int i = 0; i < nrow; ++i) {
b[i] += xold[i];
}
double *xnew = new double[nrow];
//NB. This uses the lower triangle of A
DMNorm::randomsample(xnew, b, A, true, nrow, rng);
_gv->setValue(xnew, nrow, chain);
delete [] A;
delete [] Acopy;
delete [] b;
delete [] xnew;
}
}}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks