-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.cpp
155 lines (129 loc) · 3.62 KB
/
sample.cpp
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
#include <iostream>
#include "hit_and_run.h"
#include "logconcave_hmc.h"
#include "probabilistic_programming.h"
#include "probability_distributions.h"
double uniform_distribution_neg_log_prob(const double *current_state) {
return 0;
}
double *uniform_distribution_gradient(const double *current_state) {
double *result = new double[2];
result[0] = 0;
result[1] = 0;
return result;
}
double *create_A() {
unsigned int num_rows = 6;
unsigned int num_cols = 2;
double *A = new double[num_rows * num_cols];
A[0] = -1;
A[1] = 0; // 0 <= x
A[2] = 1;
A[3] = 0; // x <= 5
A[4] = 0;
A[5] = -1; // 0 <= y
A[6] = 0;
A[7] = 1; // y <= 5
A[8] = 1;
A[9] = -1; // x y <= 0.01
A[10] = -1;
A[11] = 1; // -x + y <= 0.01
return A;
}
double *create_b() {
unsigned int num_rows = 6;
double *b = new double[num_rows];
b[0] = 0;
b[1] = 10;
b[2] = 0;
b[3] = 10;
b[4] = 0.001;
b[5] = 0.001;
return b;
}
void hmc_for_testing() {
unsigned int num_rows = 6;
unsigned int num_cols = 2;
double *A = create_A();
double *b = create_b();
double L = 4;
double m = 4;
unsigned int num_samples = 200;
unsigned int walk_length = 150;
double step_size = 1;
double *starting_point = new double[num_cols];
starting_point[0] = 0.9;
starting_point[1] = 0.9;
double *array_samples = hmc_function_pointer_interface(
num_rows, num_cols, A, b, L, m, num_samples, walk_length, step_size,
starting_point, uniform_distribution_neg_log_prob,
uniform_distribution_gradient);
unsigned int num_burns = num_samples / 2;
unsigned int num_samples_after_burns = num_samples - num_burns;
// Print the samples stored in array_samples
std::cout << "Result of reflective HMC" << std::endl;
for (auto i = 0; i != num_samples_after_burns; i++) {
std::cout << "Sample " << i << ": ";
for (auto j = 0; j != num_cols; j++) {
std::cout << array_samples[i * num_cols + j] << " ";
}
std::cout << "\n";
}
// Clean up the memory
delete[] A;
delete[] b;
delete[] starting_point;
}
void gaussian_rdhr_for_testing() {
unsigned int num_rows = 6;
unsigned int num_cols = 2;
double *A = create_A();
double *b = create_b();
double variance = 36;
unsigned int num_samples = 200;
unsigned int walk_length = 150;
double *array_samples = gaussian_rdhr(num_rows, num_cols, A, b, variance,
num_samples, walk_length);
// Print the samples stored in array_samples
std::cout << "Result of Gaussian RDHR" << std::endl;
for (auto i = 0; i != num_samples; i++) {
std::cout << "Sample " << i << ": ";
for (auto j = 0; j != num_cols; j++) {
std::cout << array_samples[i * num_cols + j] << " ";
}
std::cout << "\n";
}
// Clean up the memory
delete[] A;
delete[] b;
}
void uniform_rdhr_for_testing() {
unsigned int num_rows = 6;
unsigned int num_cols = 2;
double *A = create_A();
double *b = create_b();
unsigned int num_samples = 200;
unsigned int walk_length = 150;
double *array_samples =
uniform_rdhr(num_rows, num_cols, A, b, num_samples, walk_length);
// Print the samples stored in array_samples
std::cout << "Result of uniform RDHR" << std::endl;
for (auto i = 0; i != num_samples; i++) {
std::cout << "Sample " << i << ": ";
for (auto j = 0; j != num_cols; j++) {
std::cout << array_samples[i * num_cols + j] << " ";
}
std::cout << "\n";
}
// Clean up the memory
delete[] A;
delete[] b;
}
int main() {
// hmc_for_testing();
// gaussian_rdhr_for_testing();
// uniform_rdhr_for_testing();
// test_gumbel();
test_automatic_differentiation();
return 0;
}