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

Close

[6f1cc3]: src / modules / glm / samplers / IWLS.cc Maximize Restore History

Download this file

IWLS.cc    215 lines (174 with data), 4.9 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
#include <config.h>
#include "IWLS.h"
#include <graph/LinkNode.h>
#include <graph/StochasticNode.h>
//#include <function/LinkFunction.h>
#include <distribution/Distribution.h>
#include <rng/RNG.h>
#include <cmath>
extern "C" {
#include <cholmod.h>
}
extern cholmod_common *glm_wk;
using std::string;
using std::vector;
using std::exp;
using std::fabs;
using std::log;
static unsigned int nchildren(GraphView const *view)
{
return view->stochasticChildren().size();
}
static double logDet(cholmod_factor *F, bool &status)
{
if (!F->is_ll && !F->is_monotonic) {
status = false;
return 0;
//throw logic_error("Non-monotonic simplicial factor in logDet");
}
int *Fp = static_cast<int*>(F->p);
double *Fx = static_cast<double*>(F->x);
double y = 0;
for (unsigned int r = 0; r < F->n; ++r) {
y += log(Fx[Fp[r]]);
}
return F->is_ll ? 2*y : y;
}
#define MAX_ITER 100
namespace glm {
IWLS::IWLS(GraphView const *view,
vector<GraphView const *> const &sub_views,
unsigned int chain)
: GLMMethod(view, sub_views, chain, true),
_link(nchildren(view)), _family(nchildren(view)),
_init(true), _w(0)
{
vector<StochasticNode const*> const &children =
view->stochasticChildren();
for (unsigned int i = 0; i < children.size(); ++i) {
_link[i] = dynamic_cast<LinkNode const*>(children[i]->parents()[0]);
_family[i] = getFamily(children[i]);
}
}
string IWLS::name() const
{
return "IWLS";
}
double IWLS::getPrecision(unsigned int i) const
{
double w = _w;
if (_family[i] == GLM_BINOMIAL) {
Node const *size = _view->stochasticChildren()[i]->parents()[1];
w *= size->value(_chain)[0];
}
double grad = _link[i]->grad(_chain);
return (w * grad * grad)/ var(i);
}
double IWLS::getValue(unsigned int i) const
{
Node const *child = _view->stochasticChildren()[i];
double y = child->value(_chain)[0];
if (_family[i] == GLM_BINOMIAL) {
double N = child->parents()[1]->value(_chain)[0];
y /= N;
}
double mu = _link[i]->value(_chain)[0];
double eta = _link[i]->eta(_chain);
double grad = _link[i]->grad(_chain);
return eta + (y - mu) / grad;
}
double IWLS::var(unsigned int i) const
{
double mu = _link[i]->value(_chain)[0];
switch(_family[i]) {
case GLM_BERNOULLI: case GLM_BINOMIAL:
return mu * (1 - mu);
break;
case GLM_POISSON:
return mu;
break;
case GLM_NORMAL:
return 1;
break;
case GLM_UNKNOWN:
break;
//throw logic_error("Unknown GLM family in IWLS");
}
return 0; //-Wall
}
double IWLS::logPTransition(vector<double> const &xorig,
vector<double> const &x,
double *b, cholmod_sparse *A, bool &status)
{
unsigned int n = _view->length();
//Difference between new and old values
cholmod_dense *delta = cholmod_allocate_dense(n, 1, n, CHOLMOD_REAL,
glm_wk);
double *dx = static_cast<double*>(delta->x);
for (unsigned int i = 0; i < n; ++i) {
dx[i] = x[i] - xorig[i];
}
int ok = cholmod_factorize(A, _factor, glm_wk);
if (!ok) {
status = false;
return 0;
//throw logic_error("Cholesky decomposition failure in IWLS");
}
//Posterior mean
cholmod_dense *mu = cholmod_solve(CHOLMOD_A, _factor, delta, glm_wk);
double *mux = static_cast<double*>(mu->x);
//Setup pointers to sparse matrix A
int *Ap = static_cast<int*>(A->p);
int *Ai = static_cast<int*>(A->i);
double *Ax = static_cast<double*>(A->x);
double deviance = 0;
for (unsigned int r = 0; r < n; ++r) {
double Adr = 0;
for (int j = Ap[r]; j < Ap[r+1]; ++j) {
Adr += Ax[j] * dx[Ai[j]];
}
deviance += dx[r] * (Adr - 2 * b[r]) + b[r] * mux[r];
}
deviance -= logDet(_factor, status);
cholmod_free_dense(&delta, glm_wk);
cholmod_free_dense(&mu, glm_wk);
if (!status) return 0;
return -deviance/2;
}
bool IWLS::update(RNG *rng)
{
if (_init) {
_w = 0;
for (unsigned int i = 0; i < MAX_ITER; ++i) {
_w += 1.0/MAX_ITER;
if (!updateLM(rng, false)) return false;
}
_init = false;
}
double *b1, *b2;
cholmod_sparse *A1, *A2;
double logp = 0;
vector<double> xold(_view->length());
_view->getValue(xold, _chain);
calCoef(b1, A1);
logp -= _view->logFullConditional(_chain);
if (!updateLM(rng)) return false;
logp += _view->logFullConditional(_chain);
vector<double> xnew(_view->length());
_view->getValue(xnew, _chain);
calCoef(b2, A2);
bool status = true;
logp -= logPTransition(xold, xnew, b1, A1, status);
if (!status) return false;
logp += logPTransition(xnew, xold, b2, A2, status);
if (!status) return false;
cholmod_free_sparse(&A1, glm_wk);
cholmod_free_sparse(&A2, glm_wk);
delete [] b1; delete [] b2;
//Acceptance step
if (rng->uniform() > exp(logp)) {
_view->setValue(xold, _chain);
}
return true;
}
}