forked from AbsInt/CompCert
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRuntime.c
208 lines (160 loc) · 4.19 KB
/
Runtime.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
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <getopt.h>
#include <time.h>
#include "stanlib.h"
#include "prelude.h"
static int debug = 0;
double model(struct Data* d, struct Params *__p__);
int main(int argc, char* argv[]) {
static struct option long_options[] = {
{"debug", no_argument, &debug, 1},
{"num_samples", required_argument, 0, 'n'},
{"thin", required_argument, 0, 't'},
{"init", required_argument, 0, 'i'},
{"data", required_argument, 0, 'd'},
{"seed", required_argument, 0, 's'},
{NULL, 0, NULL, 0},
};
int n = 0;
int thin = 1;
char *dataname = NULL;
char *paramsname = NULL;
unsigned int seed = time(NULL);
while (1) {
int option_index = 0;
int c = getopt_long(argc, argv, "n:t:i:d:s:",
long_options, &option_index);
// No more arguments; break
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'n':
n = atoi(optarg);
printf("num_samples = %s (%d)\n", optarg, n);
break;
case 't':
thin = atoi(optarg);
printf("thin = %s (%d)\n", optarg, thin);
break;
case 'd':
dataname = (char *) malloc(strlen(optarg) + 1 * sizeof(char));
strcpy(dataname, optarg);
printf("dataname = %s (%s)\n", optarg, dataname);
break;
case 'i':
paramsname = (char *) malloc(strlen(optarg) + 1 * sizeof(char));
strcpy(paramsname, optarg);
printf("paramsname = %s (%s)\n", optarg, paramsname);
break;
case 's':
seed = (unsigned int) atoi(optarg);
printf("seed = %s (%u)\n", optarg, seed);
}
}
if (dataname == NULL) {
printf("No data file specified\n");
exit(1);
}
if (paramsname == NULL) {
printf("No params file specified\n");
exit(1);
}
srand(seed);
/*
if (argc < 4 || argc > 5) {
printf("Three arguments required: number of iterations, data file, params file\n");
printf("One argument optional, debug");
exit(1);
}
*/
/*
if (argc == 5) {
debug = true;
}
*/
FILE *output = fopen("output.csv","w");
fprintf(output,"lp__,accept_stat__,stepsize__,int_time__,energy__,");
print_params_names(output);
fprintf(output,"\n");
struct Data* observations = alloc_data();
read_data(observations,dataname,"r");
if (debug) {
print_data(observations,stdout);
}
struct Params* state = alloc_params();
read_params(state,paramsname,"r");
if (debug) {
print_params(state,false,stdout);
printf("\n");
}
struct Params* candidate = alloc_params();
struct Params* statistics = alloc_params();
copy_params(statistics,state);
int counter = 0;
for (int i = 0; i < n; ++i) {
if (debug) {
printf("\n\n\nIteration: %i\n\n\n", i);
print_params(state,false,stdout);
printf("\n");
}
double lp_parameters = model(observations,state);
if (debug) {
printf("P = %lf\n",exp(lp_parameters));
}
if (debug) {
printf("\nproposal:\n");
}
propose(state,candidate);
if (debug) {
print_params(candidate,false,stdout);
printf("\n");
}
double lp_candidate = model(observations,candidate);
if (debug) {
printf("P = %lf\n",exp(lp_candidate));
}
double u = (double) rand() / RAND_MAX;
double lu = log(u);
if(debug) {
printf("u = %f\n", u);
printf("lu = %f\n", lu);
printf("lp_candidate - lp_parameters = %f\n", lp_candidate - lp_parameters);
}
if (lu <= lp_candidate - lp_parameters) {
copy_params(state,candidate);
counter += 1;
if (debug) {
printf("\n-> Accepted\n");
print_params(state,false,stdout);
printf("\n");
}
} else {
if (debug) {
printf("\n-> Rejected\n");
}
}
if (debug) {
printf("i mod thin == %d\n", i % thin);
}
copy_params(statistics,state);
if (i % thin == 0) {
if (debug) {
printf("keeping sample\n");
}
fprintf(output,"%lf,%lf,%lf,%lf,%lf,",0.0,0.0,0.0,0.0,0.0);
print_params(statistics,true,output);
fprintf(output,"\n");
}
}
printf("\nExecution success\n");
printf("Acceptance ratio: %lf\n", (float)counter / (float)n);
fclose(output);
return 0;
}