-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTDummyLogLikelihood.H
110 lines (95 loc) · 3.53 KB
/
TDummyLogLikelihood.H
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
#ifndef TDummyLogLikelihood_H_seen
#define TDummyLogLikelihood_H_seen
// A dummy log likelihood for testing. This is an example, but don't
// slavishly copy it (or you will be sorry!).
class TDummyLogLikelihood {
public:
// Determine the number of dimensions. This is where the dimensions are
// defined, and everything else uses it.
std::size_t GetDim() const {return 50;}
// Calculate the log(likelihood). The dummy likelihood is a Gaussian
// (with covariance) centered at zero. The covariance is set in Init()
// (below).
double operator()(const Vector& point) const {
double logLikelihood = 0.0;
for (std::size_t i = 0; i<GetDim(); ++i) {
for (std::size_t j = 0; j<GetDim(); ++j) {
logLikelihood -= 0.5*point[i]*Error(j,i)*point[j];
}
}
return logLikelihood;
}
// Note that this needs to be the grad(log(Likelihood)).
bool operator() (Vector& g, const Vector& p) {
for (int i=0; i<p.size(); ++i) {
g[i] = 0.0;
for (int j=0; j<p.size(); ++j) {
g[i] -= Error(i,j)*p[j];
}
}
return true;
}
void Init() {
Covariance.ResizeTo(GetDim(),GetDim());
Error.ResizeTo(GetDim(),GetDim());
// Set the sigma for each variable.
for (std::size_t i = 0; i<GetDim(); ++i) {
double sigma = 1.0;
// double sigma = 1.0*i + 1.0;
Covariance(i,i) = sigma*sigma;
}
for (std::size_t i = 0; i<GetDim(); ++i) {
for (std::size_t j = i+1; j<GetDim(); ++j) {
double sig1 = std::sqrt(Covariance(i,i));
double sig2 = std::sqrt(Covariance(j,j));
// Now give some correlations to the likelihood. (Uncomment
// the one you want to try).
// Choose a random correlation
#ifdef RANDOM_CORRELATION
#define SET_CORRELATION
Covariance(i,j) = gRandom->Uniform(-0.999,0.999)*sig1*sig2;
#endif
// Choose a correlation based on the variables. Neighbors are
// not correlated, but there is more correlation as the
// variables are further apart.
#define VERY_CORRELATED
#ifdef VERY_CORRELATED
#define SET_CORRELATION
if (i+j==GetDim()-1) {
Covariance(i,j) = 0.900*sig1*sig2*(j - i)/(GetDim()-1.0);
}
#endif
// Choose no correlation
#ifndef SET_CORRELATION
Covariance(i,j) = 0.0;
#endif
Covariance(j,i) = Covariance(i,j);
}
}
// Make sure the covariance is positive definite.
do {
TVectorD eigenValues(GetDim());
Covariance.EigenVectors(eigenValues);
bool positiveDefinite = true;
for (std::size_t i = 0; i<GetDim(); ++i) {
if (eigenValues(i)<0.0) {
positiveDefinite = false;
}
}
if (positiveDefinite) break;
for (std::size_t i = 0; i<GetDim(); ++i) {
for (std::size_t j = i+1; j<GetDim(); ++j) {
Covariance(i,j) = 0.9*Covariance(i,j);
Covariance(j,i) = Covariance(i,j);
}
}
} while (true);
Error = Covariance;
Error.Invert();
}
static TMatrixD Covariance;
static TMatrixD Error;
};
TMatrixD TDummyLogLikelihood::Covariance;
TMatrixD TDummyLogLikelihood::Error;
#endif