Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[01a998]: src / modules / glm / samplers / HolmesHeldB.cc Maximize Restore History

Download this file

HolmesHeldB.cc    217 lines (186 with data), 6.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
#include <config.h>
#include "HolmesHeldB.h"
#include "KS.h"
#include <graph/StochasticNode.h>
#include <graph/LinkNode.h>
#include <sampler/GraphView.h>
#include <rng/TruncatedNormal.h>
#include <rng/RNG.h>
#include <algorithm>
using std::vector;
using std::string;
using std::copy;
/* Modified version of cs_updown:
- Sigma can be an arbitrary value instead of just (-1,+1)
- We can take the rth column of C (counting from 0) for the up/downdate
instead of column 0
*/
static int jags_updown (cs *L, double sigma, const cs *C, int r,
const int *parent)
{
int n, p, f, j, *Lp, *Li, *Cp, *Ci ;
double *Lx, *Cx, alpha, beta = 1, delta, gamma, w1, w2, *w, beta2 = 1 ;
int sign = (sigma > 0) ? 1 : -1;
double sigmart = sqrt(fabs(sigma));
if (!CS_CSC (L) || !CS_CSC (C) || !parent) return (0) ; // check inputs
Lp = L->p ; Li = L->i ; Lx = L->x ; n = L->n ;
Cp = C->p ; Ci = C->i ; Cx = C->x ;
if ((p = Cp [r]) >= Cp [r+1]) return (1) ; // return if C empty
w = (double*) cs_malloc (n, sizeof (double)) ; // get workspace
if (!w) return (0) ; // out of memory
f = Ci [p] ;
for ( ; p < Cp [r+1] ; p++) f = CS_MIN (f, Ci [p]) ; //f = min (find (C))
for (j = f ; j != -1 ; j = parent [j]) w [j] = 0 ; // clear workspace w
for (p = Cp [r] ; p < Cp [r+1] ; p++) w [Ci [p]] = sigmart * Cx [p] ;
for (j = f ; j != -1 ; j = parent [j]) // walk path f up to root
{
p = Lp [j] ;
alpha = w [j] / Lx [p] ; // alpha = w(j) / L(j,j)
beta2 = beta*beta + sign*alpha*alpha ;
if (beta2 <= 0) break ; // not positive definite
beta2 = sqrt (beta2) ;
delta = (sign > 0) ? (beta / beta2) : (beta2 / beta) ;
gamma = sign * alpha / (beta2 * beta) ;
Lx [p] = delta * Lx [p] + ((sign > 0) ? (gamma * w [j]) : 0) ;
beta = beta2 ;
for (p++ ; p < Lp [j+1] ; p++)
{
w1 = w [Li [p]] ;
w [Li [p]] = w2 = w1 - alpha * Lx [p] ;
Lx [p] = delta * Lx [p] + gamma * ((sign > 0) ? w1 : w2) ;
}
}
cs_free (w) ;
return (beta2 > 0) ;
}
//Left truncated logit
static double llogit(double left, RNG *rng)
{
double qleft = 1/(1 + exp(-left));
double x = qleft + (1 - qleft) * rng->uniform();
return log(x) - log(1 - x);
}
//Right truncated logit
static double rlogit(double right, RNG *rng)
{
double qright = 1/(1 + exp(-right));
double x = qright * rng->uniform();
return log(x) - log(1 - x);
}
namespace glm {
HolmesHeldB::HolmesHeldB(GraphView const *view,
vector<GraphView const *> const &sub_views,
unsigned int chain)
: BinaryGLM(view, sub_views, chain),
_z1(view->stochasticChildren().size(), 0)
{
}
void HolmesHeldB::updateAuxiliary(double *b, csn *N, RNG *rng)
{
/*
In the parent GLMMethod class, the posterior precision is
represented by the matrix "A"; the posterior mean "mu"
solves A %*% mu = b.
*/
vector<StochasticNode const *> const &schildren =
_view->stochasticChildren();
unsigned int nrow = schildren.size();
unsigned int ncol = _view->length();
//Transpose and permute the design matrix
cs *t_x = cs_transpose(_x, 1);
cs *Pt_x = cs_permute(t_x, _symbol->pinv, 0, 1);
cs_spfree(t_x);
//Workspace
double *ur = new double[ncol];
int *xi = new int[2*ncol]; //Stack
double *vr = new double[ncol];
int *pp = Pt_x->p;
int *pi = Pt_x->i;
double *px = Pt_x->x;
for (unsigned int r = 0; r < nrow; ++r) {
if (_outcome[r] == BGLM_LOGIT || _outcome[r] == BGLM_PROBIT)
{
double mu_r = getMean(r);
//Downdate contribution from observation r
/*
if(!jags_updown(N->L, -_tau[r], Pt_x, r, _symbol->parent)) {
throw runtime_error("Downdate error in HolmesHeldB");
}
*/
for (unsigned int j = pp[r]; j < pp[r+1]; ++j) {
b[pi[j]] -= _tau[r] * (_z[r] - mu_r) * px[j];
}
//Calculate mean and precision of z[r] conditional
//on z[s] for s != r
double v2 = 0;
int top = cs_spsolve(N->L, Pt_x, r, xi, ur, 0, 1);
for (unsigned int j = top; j < ncol; ++j) {
v2 += ur[xi[j]] * ur[xi[j]];
}
copy (b, b + ncol, vr);
cs_lsolve(N->L, vr); //Too slow!
double zr_mean = mu_r;
for (unsigned int j = top; j < ncol; ++j) {
zr_mean += vr[xi[j]] * ur[xi[j]];
}
double yr = schildren[r]->value(_chain)[0];
double zr_old = _z[r];
if (_outcome[r] == BGLM_LOGIT) {
_z1[r] -= zr_mean;
double z2 = _z[r] - _z1[r];
if (yr == 1) {
z2 = lnormal(-_z1[r], rng, zr_mean, sqrt(v2));
_z1[r] = llogit(-z2, rng);
_tau[r] = 1/sample_lambda(fabs(_z1[r]), rng);
z2 = lnormal(-_z1[r], rng, zr_mean, sqrt(v2));
}
else if (yr == 0) {
z2 = rnormal(-_z1[r], rng, zr_mean, sqrt(v2));
_z1[r] = rlogit(-z2, rng);
_tau[r] = 1/sample_lambda(fabs(_z1[r]), rng);
z2 = rnormal(-_z1[r], rng, zr_mean, sqrt(v2));
}
else {
return;
//throw logic_error("Invalid child value in HolmesHeldB");
}
_z[r] = _z1[r] + z2;
_z1[r] += zr_mean;
}
else {
if (yr == 1) {
_z[r] = lnormal(0, rng, zr_mean, sqrt(1 + v2));
}
else if (yr == 0) {
_z[r] = rnormal(0, rng, zr_mean, sqrt(1 + v2));
}
else {
return;
//throw logic_error("Invalid child value in HolmesHeldB");
}
}
//Update contribution from observation r
if(!jags_updown(N->L, _tau[r], Pt_x, r, _symbol->parent)) {
return;
//throw runtime_error("Update error in HolmesHeldB");
}
for (unsigned int j = pp[r]; j < pp[r+1]; ++j) {
b[pi[j]] += _tau[r] * (_z[r] - mu_r) * px[j];
}
}
}
//Free workspace
delete [] ur;
delete [] vr;
delete [] xi;
cs_spfree(Pt_x);
}
bool HolmesHeldB::update(RNG *rng)
{
return updateLM(rng, true, false);
}
string HolmesHeldB::name() const
{
return "Holmes-HeldB";
}
}