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

Download this file

DHyper.cc    366 lines (310 with data), 8.1 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
/*
* 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 and then take
the floor of x.
This reduces to solving a quadratic equation in x, by the
recurrence relation embodied in the function "rfunction", i.e.
p(x) = p(x - 1) * rfunction(n1, n2, m1, psi, x)
*/
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);
}
}
/*
The recurrence relation
p(x) = p(x - 1) * rfunction(n1, n2, m1, psi, x)
allows us to calculate the hypergeometric probabilities
recursively, avoiding combinatoric problems.
*/
double rfunction(int n1, int n2, int m1, double psi, int i)
{
return psi * (n1 - i + 1) * (m1 - i + 1)/(i * (n2 - m1 + i));
}
/**
* 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(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;
// 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;
}
}
// 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;
}
}
//Normalize
double sump = 0;
for (int i = 0; i < N; ++i) {
sump += p[i];
}
for (int i = 0; i < N; ++i) {
p[i] /= sump;
}
return p;
}
/*
* Sample from a unimodal discrete distribution given a vector of normalized
* probabilities pi[] and an estimate of the mode. U should be drawn from a
* uniform(0,1) distribution.
*/
static int sampleWithMode(int mode, vector<double> const &pi, double U)
{
//We try to spend our probability allocation U as quickly as
//possible, starting with the mode.
U -= pi[mode];
if (U <= 0) return mode;
//Then we work our way outwards from the mode, one step at a time.
int lower = mode - 1;
int upper = mode + 1;
while (true) {
if (lower < 0) {
for ( ; upper < pi.size() - 1; ++upper) {
U -= p[upper];
if (U <= 0) break;
}
return upper;
}
else if (upper >= pi.size()) {
for ( ; lower > 0; --lower) {
U -= p[lower];
if (U <= 0) break;
}
return lower;
}
else if (p[upper] > p[lower]) {
U -= p[upper];
if (U <= 0) return upper;
else ++upper;
}
else {
U -= p[lower];
if (U <= 0) return lower;
else --lower;
}
}
return mode; //-Wall
}
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);
int ll = max(0U, m1 - n2);
int uu = min(n1, m1);
double den = 0;
if (x >= ll && x <= uu) {
den = density(n1, n2, m1, psi)[x - ll];
}
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((int) 0, m1 - n2);
int uu = min(n1, m1);
double sumpi = 0;
if (x >= ll) {
if (x >= uu) {
sumpi = 1;
}
else {
vector<double> pi = density(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(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(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(n1a, n2a, m1a, psia);
vector<double> db = density(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;
}
}}