[0bd8b6]: src / modules / bugs / distributions / DMNorm.cc  Maximize  Restore  History

Download this file

168 lines (140 with data), 4.0 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
#include <config.h>
#include <util/dim.h>
#include <util/nainf.h>
#include "DMNorm.h"
#include <lapack.h>
#include <matrix.h>
#include <cmath>
#include <vector>
#include <JRmath.h>
using std::vector;
namespace jags {
namespace bugs {
DMNorm::DMNorm()
: ArrayDist("dmnorm", 2)
{}
double DMNorm::logDensity(double const *x, unsigned int m, PDFType type,
vector<double const *> const &parameters,
vector<vector<unsigned int> > const &dims,
double const *lower, double const *upper) const
{
double const * mu = parameters[0];
double const * T = parameters[1];
double loglik = 0;
vector<double> delta(m);
for (unsigned int i = 0; i < m; ++i) {
delta[i] = x[i] - mu[i];
loglik -= (delta[i] * T[i + i * m] * delta[i])/2;
for (unsigned int j = 0; j < i; ++j) {
loglik -= (delta[i] * T[i + j * m] * delta[j]);
}
}
switch(type) {
case PDF_PRIOR:
break;
case PDF_LIKELIHOOD:
loglik += logdet(T, m)/2;
break;
case PDF_FULL:
loglik += logdet(T, m)/2 - m * M_LN_SQRT_2PI;
break;
}
return loglik;
}
void DMNorm::randomSample(double *x, unsigned int m,
vector<double const *> const &parameters,
vector<vector<unsigned int> > const &dims,
double const *lower, double const *upper,
RNG *rng) const
{
double const * mu = parameters[0];
double const * T = parameters[1];
randomsample(x, mu, T, true, m, rng);
}
void DMNorm::randomsample(double *x, double const *mu, double const *T,
bool prec, int nrow, RNG *rng)
{
//FIXME: do something with rng
int N = nrow*nrow;
double * Tcopy = new double[N];
for (int i = 0; i < N; ++i) {
Tcopy[i] = T[i];
}
double * w = new double[nrow];
int info = 0;
double worktest;
int lwork = -1;
// Workspace query
F77_DSYEV ("V", "L", &nrow, Tcopy, &nrow, w, &worktest, &lwork, &info);
// Now get eigenvalues/vectors with optimal work space
lwork = static_cast<int>(worktest + DBL_EPSILON);
double * work = new double[lwork];
F77_DSYEV ("V", "L", &nrow, Tcopy, &nrow, w, work, &lwork, &info);
delete [] work;
/* Generate independent random normal variates, scaled by
the eigen values. We reuse the array w. */
if (prec) {
for (int i = 0; i < nrow; ++i) {
w[i] = rnorm(0, 1/sqrt(w[i]), rng);
}
}
else {
for (int i = 0; i < nrow; ++i) {
w[i] = rnorm(0, sqrt(w[i]), rng);
}
}
/* Now transform them to dependant variates
(On exit from DSYEV, Tcopy contains the eigenvectors)
*/
for (int i = 0; i < nrow; ++i) {
x[i] = mu ? mu[i] : 0;
for (int j = 0; j < nrow; ++j) {
x[i] += Tcopy[i + j * nrow] * w[j];
}
}
delete [] w;
delete [] Tcopy;
}
bool DMNorm::checkParameterDim(vector<vector<unsigned int> > const &dims) const
{
//Allow scalar mean and precision.
if (isScalar(dims[0]) && isScalar(dims[1])) return true;
//Vector mean and matrix precision
if (!isVector(dims[0])) return false;
if (!isSquareMatrix(dims[1])) return false;
if (dims[0][0] != dims[1][0]) return false;
return true;
}
vector<unsigned int> DMNorm::dim(vector<vector<unsigned int> > const &dims) const
{
return dims[0];
}
bool
DMNorm::checkParameterValue(vector<double const *> const &parameters,
vector<vector<unsigned int> > const &dims) const
{
return check_symmetry(parameters[1], dims[0][0]);
}
void DMNorm::support(double *lower, double *upper, unsigned int length,
vector<double const *> const &parameters,
vector<vector<unsigned int> > const &dims) const
{
for (unsigned int i = 0; i < length; ++i) {
lower[i] = JAGS_NEGINF;
upper[i] = JAGS_POSINF;
}
}
void DMNorm::typicalValue(double *x, unsigned int m,
vector<double const *> const &parameters,
vector<vector<unsigned int> > const &dims,
double const *lower, double const *upper) const
{
for (unsigned int i = 0; i < m; ++i) {
x[i] = parameters[0][i];
}
}
bool DMNorm::isSupportFixed(vector<bool> const &fixmask) const
{
return true;
}
}}

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

Sign up for the SourceForge newsletter:





No, thanks