-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBadGrad.C
226 lines (197 loc) · 7.61 KB
/
BadGrad.C
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
#include "TSimpleHMC.H"
#include <TMatrixD.h>
#include <TVectorD.h>
#include <sstream>
// A dummy log likelihood for testing. This is a test bed for HMC where the
// gradient of the logLikelihood is being incorrectly calculated. NOTICE: THE
// GRADIENT IS INTENTIONALLY WRONG.
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] -= GradientError(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
Covariance(i,j) = gRandom->Uniform(-0.999,0.999)*sig1*sig2;
#endif
// Choose no correlation
#ifdef NO_CORRELATION
Covariance(i,j) = 0.0;
#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
if (i+j==GetDim()-1) {
Covariance(i,j) = 0.900*sig1*sig2*(j - i)/(GetDim()-1.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();
// Make a gradient based on the covariance. The gradient is
// intentionally WRONG.
GradientCovariance.ResizeTo(GetDim(),GetDim());
GradientError.ResizeTo(GetDim(),GetDim());
for (std::size_t i = 0; i<GetDim(); ++i) {
for (std::size_t j = i; j<GetDim(); ++j) {
double sig1 = std::sqrt(Covariance(i,i));
double sig2 = std::sqrt(Covariance(j,j));
double r = Covariance(i,j);
if (i == j) {
double s = 0.1;
double e = gRandom->Gaus(1.0,s);
while (e < 0.3) e = gRandom->Gaus(1.0,s);
r = r*e;
}
else {
r = r + gRandom->Gaus(0.0,0.3)*sig1*sig2;
}
GradientCovariance(i,j) = r;
GradientCovariance(j,i) = r;
}
}
// Make sure the gradient covariance is positive definite.
do {
TVectorD eigenValues(GetDim());
GradientCovariance.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) {
GradientCovariance(i,j) = 0.9*GradientCovariance(i,j);
GradientCovariance(j,i) = GradientCovariance(i,j);
}
}
} while (true);
GradientError = GradientCovariance;
GradientError.Invert();
Covariance.Print();
GradientCovariance.Print();
}
static TMatrixD Covariance;
static TMatrixD Error;
static TMatrixD GradientCovariance;
static TMatrixD GradientError;
};
TMatrixD TDummyLogLikelihood::Covariance;
TMatrixD TDummyLogLikelihood::Error;
TMatrixD TDummyLogLikelihood::GradientCovariance;
TMatrixD TDummyLogLikelihood::GradientError;
void BadGrad(int maxEvals=-1) {
std::cout << "Simple HMC Loaded" << std::endl;
#ifdef NO_OUTPUT
TFile *outputFile = NULL;
TTree *tree = NULL;
#else
TFile *outputFile = new TFile("badGrad.root","recreate");
TTree *tree = new TTree("BadGrad","Tree of accepted pqoints");
#endif
TSimpleHMC<TDummyLogLikelihood,TDummyLogLikelihood> hmc(tree);
// TSimpleHMC<TDummyLogLikelihood> hmc(tree);
TDummyLogLikelihood& like = hmc.GetLogLikelihood();
// Initialize the likelihood (if you need to). The dummy likelihood
// setups a covariance to make the PDF more interesting.
like.Init();
// The number of dimensions in the point needs to agree with the number of
// dimensions in the likelihood. You can either hard code it, or do like
// I'm doing here and have a likelihood method to return the number of
// dimensions.
Vector p(like.GetDim());
for (std::size_t i=0; i<p.size(); ++i) p[i] = gRandom->Uniform(-1.0,1.0);
hmc.Start(p,false);
// Burnin the chain (don't save the output)
int trials = 100000;
for (int i=0; i<trials; ++i) {
if (i%1000 == 0) {
std::cout << i << " " << hmc.GetPotentialCount()
<< " " << hmc.GetGradientCount()
<< std::endl;
}
hmc.Step(true);
if (maxEvals > 0 && hmc.GetPotentialCount() > maxEvals) break;
}
std::cout << "Finished " << trials
<< " requested trials with calls " << hmc.GetPotentialCount()
<< " + " << hmc.GetGradientCount()
<< " " << 1.0*hmc.GetGradientCount()/hmc.GetPotentialCount()
<< std::endl;
if (tree) tree->Write();
if (outputFile) delete outputFile;
}
#ifdef MAIN_PROGRAM
// This let's the example compile directly. To compile it, use the compile.sh
// script and then run it using ./a.out which will produce a file name
// "badGrad.root"
int main(int argc, char **argv) {
int maxEvaluations = -1;
if (argc > 1) {
std::istringstream input(argv[1]);
input >> maxEvaluations;
}
BadGrad(maxEvaluations);
}
#endif