[go: up one dir, main page]

Menu

[de244b]: / src / analyse.c  Maximize  Restore  History

Download this file

287 lines (253 with data), 8.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
 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
APEMoST - Automated Parameter Estimation and Model Selection Toolkit
Copyright (C) 2009 Johannes Buchner
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <omp.h>
#include "mcmc.h"
#include "parallel_tempering_config.h"
#include "debug.h"
#include "define_defaults.h"
#include "gsl_helper.h"
#include "parallel_tempering_run.h"
#include "histogram.h"
#include "utils.h"
/*
* calculate data probability
*/
void analyse_data_probability() {
unsigned int i;
unsigned int j;
unsigned long n = 0;
double v;
double w;
double sums[100];
double previous_beta;
double data_logprob;
char buf[100];
FILE * f;
unsigned int n_beta = N_BETA;
mcmc ** chains = setup_chains();
read_calibration_file(chains, n_beta);
assert(n_beta < 100);
for (i = 0; i < n_beta; i++) {
sprintf(buf, "prob-chain%d.dump", i);
dump_s("summing up probability file", buf);
printf("reading probabilities of chain %d\r", i);
fflush(stdout);
f = fopen(buf, "r");
if (f == NULL) {
fprintf(stderr,
"calculating data probability failed: file %s not found\n",
buf);
return;
}
n = 0;
sums[i] = 0;
while (!feof(f)) {
if (fscanf(f, "%le\t%le", &w, &v) == 2) {
/*
* note: rounding errors could occur here
* since the values are of the same magnitude, they hopefully won't
*/
sums[i] += v;
n++;
}
}
if (n == 0) {
fprintf(stderr, "calculating data probability failed: "
"no data points found in %s\n", buf);
return;
}
sums[i] = sums[i] / get_beta(chains[i]) / n;
}
data_logprob = 0;
previous_beta = 0;
/* calculate the integral by an estimate */
for (j = n_beta - 1;; j--) {
assert((get_beta(chains[j]) > previous_beta));
data_logprob += sums[j] * (get_beta(chains[j]) - previous_beta);
if (j == 0)
break;
previous_beta = get_beta(chains[j]);
}
printf("Model probability ln(p(D|M, I)): [about 10^%.0f] %.5f"
"\n"
"\nTable to compare support against other models (Jeffrey):\n"
" other model ln(p(D|M,I)) | supporting evidence for this model\n"
" --------------------------------- \n"
" > %04.1f \tnegative (supports other model)\n"
" %04.1f .. %04.1f \tBarely worth mentioning\n"
" %04.1f .. %04.1f \tSubstantial\n"
" %04.1f .. %04.1f \tStrong\n"
" %04.1f .. %04.1f \tVery strong\n"
" < %04.1f \tDecisive\n", data_logprob / gsl_sf_log(10),
data_logprob, data_logprob, data_logprob,
data_logprob - gsl_sf_log(3), data_logprob - gsl_sf_log(3),
data_logprob - gsl_sf_log(10), data_logprob - gsl_sf_log(10),
data_logprob - gsl_sf_log(30), data_logprob - gsl_sf_log(30),
data_logprob - gsl_sf_log(100), data_logprob - gsl_sf_log(100)
);
printf("\nbe careful.\n");
}
double calc_mcmc_error(const double mean, const char * filename,
unsigned long batchsize) {
double v;
unsigned long n = 0;
int nbatches = 0;
FILE * f = openfile(filename);
double batchsum = 0;
double batchmean;
double batcherror;
double errorsum = 0;
while (!feof(f)) {
if (fscanf(f, "%lf", &v) == 1) {
n++;
batchsum += v;
if (n % batchsize == batchsize - 1) {
batchmean = batchsum / batchsize;
/*printf("batchmean: %f (batchsize = %lu, batchnr %d)\n",
batchmean, batchsize, nbatches);*/
batcherror = /*batchsize * */pow(batchmean - mean, 2);
errorsum += batcherror;
batchsum = 0;
nbatches++;
}
}
}
return sqrt(errorsum / nbatches);
}
#ifndef NBINS
#define NBINS 200
#endif
#ifdef __NEVER_SET_FOR_DOCUMENTATION_ONLY
/**
* If not set, the marginal distribution will be calculated for the whole
* parameter range. Pros: faster, comparable. Cons: Not as detailed.
*
* If set, the maximum and minimum values found are used for
* the histogram. Pros: more detailed in the area of interest
*/
#define HISTOGRAMS_MINMAX
#endif
void calc_marginal_distribution(mcmc ** chains, unsigned int n_beta,
unsigned int param, int find_minmax) {
const unsigned int nbins = NBINS;
char ** filenames;
unsigned int filecount = n_beta;
unsigned int i;
gsl_vector * min;
gsl_vector * max;
gsl_histogram * h;
FILE * outfile;
double iter;
double mean;
double sigma;
double mcmcerror;
char outfilename[100];
const char * paramname = get_params_descr(chains[0])[param];
#ifdef HISTOGRAMS_ALLCHAINS
filecount = n_beta;
#else
filecount = 1;
#endif
filenames = (char **) calloc(filecount + 1, sizeof(char*));
for (i = 0; i < filecount; i++) {
filenames[i] = (char *) malloc(100 * sizeof(char));
sprintf(filenames[i], "%s-chain-%d.prob.dump", paramname, i);
}
sprintf(outfilename, "%s.histogram", paramname);
min = gsl_vector_alloc(1);
max = gsl_vector_alloc(1);
gsl_vector_set(min, 0, get_params_min_for(chains[0], param));
gsl_vector_set(max, 0, get_params_max_for(chains[0], param));
if (find_minmax != 0) {
for (i = 0; i < filecount; i++) {
printf("minmax search : chain %3d parameter %s \r", i, paramname);
fflush(stdout);
if (1 != get_column_count(filenames[i])) {
fprintf(
stderr,
"number of columns different in file %s: %i vs %i in %s\n",
filenames[i], 1, get_column_count(filenames[i]),
filenames[0]);
exit(1);
}
if (i == 0)
find_min_max(filenames[0], min, max);
else
update_min_max(filenames[i], min, max);
}
dump_v("minima", min);
dump_v("maxima", max);
}
h = create_hist(nbins, gsl_vector_get(min, 0), gsl_vector_get(max, 0));
debug("filling histogram... ");
for (i = 0; i < filecount; i++) {
printf("reading values: chain %3d parameter %s \r", i, paramname);
fflush(stdout);
dump_s("with file", filenames[i]);
append_to_hists(&h, 1, filenames[i]);
}
iter = gsl_histogram_sum(h);
gsl_histogram_scale(h, (gsl_vector_get(max, 0) - gsl_vector_get(min, 0))
/ nbins / iter);
outfile = fopen(outfilename, "w");
debug("writing histogram... ");
assert(outfile != NULL);
gsl_histogram_fprintf(outfile, h, DUMP_FORMAT, DUMP_FORMAT);
dump_s("histogram file done", outfilename);
fclose(outfile);
mean = gsl_histogram_mean(h);
sigma = gsl_histogram_sigma(h);
for (i = 0; i < filecount; i++) {
mcmcerror = calc_mcmc_error(mean, filenames[i], sqrt(iter));
printf("mcmc error estimate of %s: %f %s\n", paramname, mcmcerror,
(mcmcerror > sigma * 0.01 ? "** high!" : " (ok)"));
free(filenames[i]);
}
printf("Note: Include a error estimate in your publication!\n");
free(filenames);
gsl_histogram_free(h);
}
#ifndef GNUPLOT_STYLE
#define GNUPLOT_STYLE "with histeps"
#endif
void analyse_marginal_distributions() {
unsigned int i;
int find_minmax = 0;
unsigned int n_beta = N_BETA;
mcmc ** chains = setup_chains();
FILE * plotplate;
read_calibration_file(chains, n_beta);
#ifdef HISTOGRAMS_MINMAX
find_minmax = 1;
#endif
for (i = 0; i < get_n_par(chains[0]); i++) {
calc_marginal_distribution(chains, n_beta, i, find_minmax);
}
plotplate = fopen("marginal_distributions.gnuplot", "w");
assert(plotplate != NULL);
fprintf(plotplate, "# set terminal png size %d,%d; set output "
"\"marginal_distributions.png\"\n", 600, 300 * get_n_par(chains[0]));
fprintf(plotplate, "set multiplot\n");
fprintf(plotplate, "set size 1,%f\n", 1. / get_n_par(chains[0]));
for (i = 0; i < get_n_par(chains[0]); i++) {
fprintf(plotplate, "set origin 0,%f\n", (get_n_par(chains[0]) - i - 1)
* 1. / get_n_par(chains[0]));
fprintf(plotplate, "plot \"%s.histogram\" u 1:3 title \"%s\" "
GNUPLOT_STYLE
"\n", get_params_descr(chains[0])[i], get_params_descr(chains[0])[i]);
}
fprintf(plotplate, "unset multiplot\n");
fclose(plotplate);
}