[669123]: src / lib / distribution / RScalarDist.cc Maximize Restore History

Download this file

RScalarDist.cc    187 lines (153 with data), 4.6 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
#include <config.h>
#include <distribution/RScalarDist.h>
#include <rng/RNG.h>
#include <util/nainf.h>
#include <util/dim.h>
#include <cmath>
#include <algorithm>
using std::string;
using std::vector;
using std::log;
using std::min;
using std::max;
namespace jags {
double RScalarDist::calPlower(double lower,
vector<double const*> const &parameters) const
{
//P(X < lower)
if (_discrete) lower -= 1;
return p(lower, parameters, true, false);
}
double RScalarDist::calPupper(double upper,
vector<double const*> const &parameters) const
{
//P(X <= upper)
return p(upper, parameters, true, false);
}
RScalarDist::RScalarDist(string const &name, unsigned int npar,
Support support, bool discrete)
: ScalarDist(name, npar, support), _support(support), _discrete(discrete),
_npar(npar)
{
}
double
RScalarDist::typicalValue(vector<double const *> const &parameters,
double const *lower, double const *upper) const
{
double llimit = l(parameters), ulimit = u(parameters);
double plower = 0, pupper = 1;
if (lower) {
llimit = max(llimit, *lower);
plower = calPlower(llimit, parameters);
}
if (upper) {
ulimit = min(ulimit, *upper);
pupper = calPupper(ulimit, parameters);
}
double pmed = (plower + pupper)/2;
double med = q(pmed, parameters, true, false);
//Calculate the log densities
double dllimit = d(llimit, PDF_FULL, parameters, true);
double dulimit = d(ulimit, PDF_FULL, parameters, true);
double dmed = d(med, PDF_FULL, parameters, true);
//Pick the median if it has the highest density, otherwise pick
//a point near to (but not on) the boundary
if (dmed >= dllimit && dmed >= dulimit) {
return med;
}
else if (dulimit > dllimit) {
return q(0.1 * plower + 0.9 * pupper, parameters, true, false);
}
else {
return q(0.9 * plower + 0.1 * pupper, parameters, true, false);
}
}
double
RScalarDist::logDensity(double x, PDFType type,
vector<double const *> const &parameters,
double const *lower, double const *upper) const
{
if (lower && x < *lower)
return JAGS_NEGINF;
if (upper && x > *upper)
return JAGS_NEGINF;
if (upper && lower && *upper < *lower)
return JAGS_NEGINF;
double loglik = d(x, type, parameters, true);
if (type != PDF_PRIOR && (lower || upper)) {
//Normalize truncated distributions
double ll = l(parameters);
if (lower && *lower < ll) ll = *lower;
if (_discrete) ll -= 1; //Adjustment for discrete valued distributions
/* In theory, we just have to subtract log[P(lower <= X <=
upper)] from the log likelihood. But we need to work around
numerical problems. */
bool have_lower = lower && p(ll, parameters, true, false) > 0;
bool have_upper = upper && p(*upper, parameters, false, false) > 0;
if (have_lower && have_upper) {
if (p(ll, parameters, false, false) < 0.5) {
//Use upper tail
loglik -= log(p(ll, parameters, false, false) -
p(*upper, parameters, false, false));
}
else {
//Use lower tail
loglik -= log(p(*upper, parameters, true, false) -
p(ll, parameters, true, false));
}
}
else if (have_lower) {
loglik -= p(ll, parameters, false, true);
}
else if (have_upper) {
loglik -= p(*upper, parameters, true, true);
}
}
return loglik;
}
double
RScalarDist::randomSample(vector<double const *> const &parameters,
double const *lower, double const *upper,
RNG *rng) const
{
if (!lower && !upper) {
return r(parameters, rng);
}
double plower = lower ? calPlower(*lower, parameters) : 0;
double pupper = upper ? calPupper(*upper, parameters) : 1;
if (pupper - plower > 0.25) {
//Rejection sampling if expected number of samples is 4 or less
while (true) {
double y = r(parameters, rng);
if (lower && y < *lower) continue;
if (upper && y > *upper) continue;
return y;
}
}
//Inversion
//FIXME: We probably need to take care of tail behaviour here
double u = plower + rng->uniform() * (pupper - plower);
return q(u, parameters, true, false);
}
bool RScalarDist::canBound() const
{
return true;
}
bool RScalarDist::isDiscreteValued(vector<bool> const &mask) const
{
return _discrete;
}
bool RScalarDist::discrete() const
{
return _discrete;
}
unsigned int RScalarDist::npar() const
{
return _npar;
}
double xlog0(double x, bool give_log) {
if (x < 0) return JAGS_POSINF;
else if (x > 0) return give_log ? JAGS_NEGINF : 0;
else return give_log ? 0 : 1;
}
}