[ca0b55]: src / modules / bugs / distributions / DHyper.cc  Maximize  Restore  History

Download this file

416 lines (357 with data), 9.5 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
* Adapted from R code published in conjunction with:
*
* Liao, J.G. And Rosen, O. (2001) Fast and Stable Algorithms for
* Computing and Sampling from the Noncentral Hypergeometric
* Distribution. The American Statistician 55, 366-369.
*/
#include <config.h>
#include "DHyper.h"
#include <rng/RNG.h>
#include <util/nainf.h>
#include <algorithm>
#include <vector>
#include <stdexcept>
#include <JRmath.h>
using std::vector;
using std::max;
using std::min;
using std::vector;
using std::logic_error;
namespace jags {
namespace bugs {
DHyper::DHyper()
: RScalarDist("dhyper", 4, DIST_SPECIAL, true)
{}
bool DHyper::canBound() const
{
return false;
}
static void
getParameters(int &n1, int &n2, int &m1, double &psi,
vector<double const *> const &parameters)
{
n1 = static_cast<int>(*parameters[0]);
n2 = static_cast<int>(*parameters[1]);
m1 = static_cast<int>(*parameters[2]);
psi = *parameters[3];
}
bool
DHyper::checkParameterDiscrete (vector<bool> const &mask) const
{
// Check that n1, n2, m1 are discrete-valued
for (unsigned int i = 0; i < 3; ++i) {
if (mask[i] == false)
return false;
}
return true;
}
bool DHyper::checkParameterValue (vector<double const *> const &params) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, params);
if (n1 < 0 || n2 < 0)
return false;
else if (m1 < 0 || m1 > n1 + n2)
return false;
else if (psi <= 0)
return false;
else
return true;
}
/*
* Calculates the mode of the hypergeometric distribution
*
* We solve the equation p(x) = p(x-1) for continuous x. Using the
* recurrence relation given in rfunction (see below) this reduces to
* a quadratic equation. We then take the floor of x to find the mode
* of the distribution.
*/
static int modeCompute(int n1, int n2, int m1, double psi)
{
double a = psi - 1;
double b = -((m1 + n1 + 2) * psi + n2 - m1);
double c = psi * (n1 + 1) * (m1 + 1);
double q = b;
if (b > 0) {
q += sqrt(b * b - 4 * a * c);
}
else {
q -= sqrt(b * b - 4 * a * c);
}
q = -q/2;
int mode = static_cast<int>(c/q);
if (mode >= 0 && mode >= m1 - n2 && mode <= n1 && mode <= m1) {
return mode;
}
else {
return static_cast<int>(q/a);
}
}
/*
* Recurrence relation that allows us to calculate hypergeometric
* probabilities without running into combinatoric problems:
*
* p(x) = p(x - 1) * rfunction(n1, n2, m1, psi, x)
*/
static inline double rfunction(int n1, int n2, int m1, double psi, int i)
{
return psi * (n1 - i + 1) * (m1 - i + 1)/(i * (n2 - m1 + i));
}
/**
* Calculates the unnormalized density of x. The mode has reference
* density value 1.
*/
static double density_unnormalized(int x, int n1, int n2, int m1, double psi)
{
int ll = max(0U, m1 - n2);
int uu = min(n1, m1);
if (x < ll || x > uu) return 0;
// Density at mode has reference value 1
int mode = modeCompute(n1, n2, m1, psi);
double r = 1;
if (x > mode) {
for (int i = mode + 1; i <= x; ++i) {
r *= rfunction(n1, n2, m1, psi, i);
}
}
else if (x < mode) {
for (int i = mode - 1; i >= x; --i) {
r /= rfunction(n1, n2, m1, psi, i + 1);
}
}
return r;
}
/*
* Calculates the normalized density of x
*/
static double density_normalized(int x, int n1, int n2, int m1, double psi)
{
int ll = max(0U, m1 - n2);
int uu = min(n1, m1);
if (x < ll || x > uu) return 0;
// Density at mode has reference value 1
int mode = modeCompute(n1, n2, m1, psi);
double psum = 1, px = 1;
// Calculate relative density above the mode
if (mode < uu) {
double r = 1;
for (int i = mode + 1; i <= uu; ++i) {
r *= rfunction(n1, n2, m1, psi, i);
if (i == x) px = r;
psum += r;
}
}
// Calculate relative density below the node
if (mode > ll) {
double r = 1;
for (int i = mode - 1; i >= ll; --i) {
r /= rfunction(n1, n2, m1, psi, i + 1);
if (i == x) px = r;
psum += r;
}
}
// Normalize
return px/psum;
}
/*
* Returns a vector of normalized probabilities for a hypergeometric
* distribution. If the returned vector p is of length N, then p[0]
* represents the probability of the lowest possible value ll = max(0,
* m1 - n2) and p[N - 1] represents the probability of the largest
* possible value uu = min(n1, m1).
*/
static vector<double> density_full(int n1, int n2, int m1, double psi)
{
int ll = max(0U, m1 - n2);
int uu = min(n1, m1);
int N = uu - ll + 1;
vector<double> p(N);
// Density at mode has reference value 1
int mode = modeCompute(n1, n2, m1, psi);
p[mode - ll] = 1;
double sump = 1;
// Calculate relative density above the mode
if (mode < uu) {
double r = 1;
for (int i = mode + 1; i <= uu; ++i) {
r *= rfunction(n1, n2, m1, psi, i);
p[i - ll] = r;
sump += r;
}
}
// Calculate relative density below the node
if (mode > ll) {
double r = 1;
for (int i = mode - 1; i >= ll; --i) {
r /= rfunction(n1, n2, m1, psi, i + 1);
p[i - ll] = r;
sump += r;
}
}
//Normalize
for (int i = 0; i < N; ++i) {
p[i] /= sump;
}
return p;
}
/*
* Sample from a discrete unimodal distribution given a vector of
* normalized probabilities pi[] and an estimate of the mode. U should
* be drawn from a uniform(0,1) distribution. We spend our probability
* allocation U as rapidly as possible, starting at the mode and then
* stepping out to the left and the right.
*
* This function is quite general and could be used for other discrete
* distributions when we have a good guess of the mode. The function
* also works if "mode" is not actually the mode, but is
* correspondingly less efficient.
*/
static int sampleWithMode(int mode, vector<double> const &pi, double U)
{
int N = pi.size();
int left = mode - 1;
int right = mode;
while (left >= 0 || right < N) {
if (right < N && (left < 0 || p[right] > p[left])) {
U -= pi[right];
if (U <= 0) return right;
else ++right;
}
else {
U -= pi[left];
if (U <= 0) return left;
else --left;
}
}
//Only reachable with bad arguments
return mode;
}
double DHyper::d(double z, PDFType type,
vector<double const *> const &parameters,
bool give_log) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, parameters);
int x = static_cast<int>(z);
double den = 0;
if (PDFType == PDF_PRIOR) {
den = density_unnormalized(x, n1, n2, m1, psi);
}
else {
den = density_normalized(x, n1, n2, m1, psi);
}
if (give_log) {
return den == 0 ? JAGS_NEGINF : log(den);
}
else {
return den;
}
}
double DHyper::p(double x, vector<double const *> const &parameters, bool lower,
bool give_log) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, parameters);
int ll = max(0U, m1 - n2);
int uu = min(n1, m1);
double sumpi = 0;
if (x >= ll) {
if (x >= uu) {
sumpi = 1;
}
else {
vector<double> pi = density_full(n1, n2, m1, psi);
for (int i = ll; i <= x; ++i) {
sumpi += pi[i - ll];
}
}
}
if (!lower)
sumpi = max(1 - sumpi, 0.0);
if (give_log) {
return sumpi == 0 ? JAGS_NEGINF : log(sumpi);
}
else {
return sumpi;
}
}
double DHyper::q(double p, vector<double const *> const &parameters, bool lower,
bool log_p) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, parameters);
int ll = max((int) 0, m1 - n2);
int uu = min(n1, m1);
vector<double> pi = density_full(n1, n2, m1, psi);
if (log_p)
p = exp(p);
if (!lower)
p = 1 - p;
double sumpi = 0;
for (int i = ll; i < uu; ++i) {
sumpi += pi[i - ll];
if (sumpi >= p) {
return i;
}
}
return uu;
}
double DHyper::r(vector<double const *> const &parameters, RNG *rng) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, parameters);
int ll = max(0U, m1 - n2);
int mode = modeCompute(n1, n2, m1, psi);
vector<double> pi = density_full(n1, n2, m1, psi);
return sampleWithMode(mode - ll, pi, rng->uniform()) + ll;
}
double DHyper::l(vector<double const *> const &parameters) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, parameters);
return max(0U, m1 - n2);
}
double DHyper::u(vector<double const *> const &parameters) const
{
int n1,n2,m1;
double psi;
getParameters(n1, n2, m1, psi, parameters);
return min(n1, m1);
}
bool DHyper::isSupportFixed(vector<bool> const &fixmask) const
{
return fixmask[0] && fixmask[1] && fixmask[2]; //Margins fixed
}
double DHyper::KL(vector<double const *> const &para,
vector<double const *> const &parb) const
{
int n1a,n2a,m1a;
double psia;
getParameters(n1a, n2a, m1a, psia, para);
int lla = max(0U, m1a - n2a);
int uua = min(n1a, m1a);
int n1b,n2b,m1b;
double psib;
getParameters(n1b, n2b, m1b, psib, para);
int llb = max(0U, m1b - n2b);
int uub = min(n1b, m1b);
if (lla < llb || uua > uub)
return JAGS_POSINF;
vector<double> da = density_full(n1a, n2a, m1a, psia);
vector<double> db = density_full(n1b, n2b, m1b, psib);
double y = 0;
for (int i = lla; i <= uua; ++i) {
double proba = da[i - lla];
double probb = db[i - llb];
y += proba * (log(proba) - log(probb));
}
return y;
}
}}

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

Sign up for the SourceForge newsletter:





No, thanks