[7feccc]: src / lib / function / ArrayLogDensity.cc  Maximize  Restore  History

Download this file

93 lines (73 with data), 2.2 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
#include <config.h>
#include <distribution/ArrayDist.h>
#include <function/ArrayLogDensity.h>
#include <util/dim.h>
using std::vector;
using std::string;
namespace jags {
ArrayLogDensity::ArrayLogDensity(ArrayDist const *dist)
: ArrayFunction(string("logdensity.") + dist->name().substr(1),
dist->npar() + 1),
_dist(dist)
{}
vector<unsigned int> ArrayLogDensity::dim(
vector<vector<unsigned int> > const &dims) const
{
return vector<unsigned int>(1, 1);
}
void ArrayLogDensity::evaluate(
double *value,
vector<double const *> const &args,
vector<vector<unsigned int> > const &dims) const
{
unsigned int npar = _dist->npar();
vector<double const *> dargs(npar);
vector<vector<unsigned int> > ddims(npar);
for (unsigned int i = 0; i < npar; ++i) {
dargs[i] = args[i+1];
ddims[i] = dims[i+1];
}
value[0] = _dist->logDensity(args[0], product(dims[0]), PDF_FULL,
dargs, ddims, 0, 0);
}
bool
ArrayLogDensity::checkParameterDim(vector<vector<unsigned int> > const &dim)
const
{
unsigned int npar = _dist->npar();
vector<vector<unsigned int> > ddim(npar);
for (unsigned int i = 0; i < npar; ++i) {
ddim[i] = dim[i+1];
}
if (!_dist->checkParameterDim(ddim)) return false;
if (dim[0] != _dist->dim(ddim)) return false;
return true;
}
bool
ArrayLogDensity::checkParameterValue(vector<double const *> const &args,
vector<vector<unsigned int> > const &dims)
const
{
//We have to include discreteness check here as there is
//no equivalent of checkParameterDiscrete for Functions.
unsigned int npar = _dist->npar();
vector<bool> mask(npar);
for (unsigned int i = 0; i < npar; ++i) {
double p = *args[i + 1];
mask[i] = (p == static_cast<int>(p));
}
if (!_dist->checkParameterDiscrete(mask)) return false;
if (_dist->isDiscreteValued(mask)) {
if (*args[0] != static_cast<int>(*args[0])) {
return false;
}
}
vector<double const *> dargs(npar);
vector<vector<unsigned int> > ddims(npar);
for (unsigned int i = 0; i < npar; ++i) {
dargs[i] = args[i+1];
ddims[i] = dims[i+1];
}
return _dist->checkParameterValue(dargs, ddims);
}
}