Search for optimal (lengthscales, noise) using GP likelihood.
41{
42 if (
y.size() == 0 || !
y.allFinite() || n_pairtypes <= 0) {
43 throw std::invalid_argument(
44 "ARD search needs finite targets and positive dimensions");
45 }
46 std::exception_ptr failure;
47 ARDSearchResult best;
48 best.best_likelihood = std::numeric_limits<double>::infinity();
49 best.total_evaluations = 0;
50
51
52 std::vector<double> ls_grid(n_ls_per_dim_);
53 for (int i = 0; i < n_ls_per_dim_; ++i) {
54 ls_grid[i] = std::pow(10.0, log_ls_min_ + (log_ls_max_ - log_ls_min_) *
55 i / (n_ls_per_dim_ - 1));
56 }
57
58
59 std::vector<double> noise_grid(n_noise_);
60 for (int i = 0; i < n_noise_; ++i) {
61 noise_grid[i] =
62 std::pow(10.0, log_noise_min_ + (log_noise_max_ - log_noise_min_) *
63 i / (n_noise_ - 1));
64 }
65
66
67
68 if (n_pairtypes == 1) {
69 const int n_total = n_ls_per_dim_ * n_noise_;
70 int best_idx = n_total;
71#ifdef USE_OPENMP
72#pragma omp parallel
73#endif
74 {
75 ARDSearchResult local_best;
76 local_best.best_likelihood =
77 std::numeric_limits<double>::infinity();
78 int local_best_idx = n_total;
79 int local_evals = 0;
80
81#ifdef USE_OPENMP
82#pragma omp for nowait schedule(static)
83#endif
84 for (int idx = 0; idx < n_total; ++idx) {
85 int i_ls = idx / n_noise_;
86 int i_noise = idx % n_noise_;
87 double ls = ls_grid[i_ls];
88 double noise = noise_grid[i_noise];
89
90 try {
92 if (K.rows() !=
y.size() || K.cols() !=
y.size() ||
93 !K.allFinite()) {
94 throw std::invalid_argument(
95 "kernel matrix must match the finite targets");
96 }
97 Eigen::SelfAdjointEigenSolver<EigenMatrix> solver(K);
98
99 FactorizedLikelihood fl;
100 fl.setEigendecomposition(solver.eigenvalues(),
101 solver.eigenvectors(),
y);
102 double ll = fl.evaluateGP(noise);
103 local_evals++;
104
105 if (std::isfinite(ll) &&
106 (ll < local_best.best_likelihood ||
107 (ll == local_best.best_likelihood &&
108 idx < local_best_idx))) {
109 local_best.best_likelihood = ll;
110 local_best_idx = idx;
111 local_best.best_lengthscales = {ls};
112 local_best.best_noise = noise;
113 local_best.best_prefactor =
114 fl.analyticalPrefactor(noise);
115 }
116 } catch (...) {
117#ifdef USE_OPENMP
118#pragma omp critical(gpr_ard_search_error)
119#endif
120 {
121 if (!failure) failure = std::current_exception();
122 }
123 }
124 }
125
126#ifdef USE_OPENMP
127#pragma omp critical
128#endif
129 {
130 if (local_best_idx < n_total &&
131 (local_best.best_likelihood < best.best_likelihood ||
132 (local_best.best_likelihood == best.best_likelihood &&
133 local_best_idx < best_idx))) {
134 best.best_likelihood = local_best.best_likelihood;
135 best_idx = local_best_idx;
136 best.best_lengthscales = local_best.best_lengthscales;
137 best.best_noise = local_best.best_noise;
138 best.best_prefactor = local_best.best_prefactor;
139 }
140 best.total_evaluations += local_evals;
141 }
142 }
143 } else if (n_pairtypes == 2) {
144 const int n_total = n_ls_per_dim_ * n_ls_per_dim_ * n_noise_;
145 int best_idx = n_total;
146#ifdef USE_OPENMP
147#pragma omp parallel
148#endif
149 {
150 ARDSearchResult local_best;
151 local_best.best_likelihood =
152 std::numeric_limits<double>::infinity();
153 int local_best_idx = n_total;
154 int local_evals = 0;
155
156#ifdef USE_OPENMP
157#pragma omp for nowait schedule(static)
158#endif
159 for (int idx = 0; idx < n_total; ++idx) {
160 int i_ls1 = idx / (n_ls_per_dim_ * n_noise_);
161 int rem = idx % (n_ls_per_dim_ * n_noise_);
162 int i_ls2 = rem / n_noise_;
163 int i_noise = rem % n_noise_;
164 double ls1 = ls_grid[i_ls1];
165 double ls2 = ls_grid[i_ls2];
166 double noise = noise_grid[i_noise];
167
168 try {
170 if (K.rows() !=
y.size() || K.cols() !=
y.size() ||
171 !K.allFinite()) {
172 throw std::invalid_argument(
173 "kernel matrix must match the finite targets");
174 }
175 Eigen::SelfAdjointEigenSolver<EigenMatrix> solver(K);
176
177 FactorizedLikelihood fl;
178 fl.setEigendecomposition(solver.eigenvalues(),
179 solver.eigenvectors(),
y);
180 double ll = fl.evaluateGP(noise);
181 local_evals++;
182
183 if (std::isfinite(ll) &&
184 (ll < local_best.best_likelihood ||
185 (ll == local_best.best_likelihood &&
186 idx < local_best_idx))) {
187 local_best.best_likelihood = ll;
188 local_best_idx = idx;
189 local_best.best_lengthscales = {ls1, ls2};
190 local_best.best_noise = noise;
191 local_best.best_prefactor =
192 fl.analyticalPrefactor(noise);
193 }
194 } catch (...) {
195#ifdef USE_OPENMP
196#pragma omp critical(gpr_ard_search_error)
197#endif
198 {
199 if (!failure) failure = std::current_exception();
200 }
201 }
202 }
203
204#ifdef USE_OPENMP
205#pragma omp critical
206#endif
207 {
208 if (local_best_idx < n_total &&
209 (local_best.best_likelihood < best.best_likelihood ||
210 (local_best.best_likelihood == best.best_likelihood &&
211 local_best_idx < best_idx))) {
212 best.best_likelihood = local_best.best_likelihood;
213 best_idx = local_best_idx;
214 best.best_lengthscales = local_best.best_lengthscales;
215 best.best_noise = local_best.best_noise;
216 best.best_prefactor = local_best.best_prefactor;
217 }
218 best.total_evaluations += local_evals;
219 }
220 }
221 } else {
222
223
224 const int n_total = n_ls_per_dim_ * n_noise_;
225 int best_idx = n_total;
226#ifdef USE_OPENMP
227#pragma omp parallel
228#endif
229 {
230 ARDSearchResult local_best;
231 local_best.best_likelihood =
232 std::numeric_limits<double>::infinity();
233 int local_best_idx = n_total;
234 int local_evals = 0;
235
236#ifdef USE_OPENMP
237#pragma omp for nowait schedule(static)
238#endif
239 for (int idx = 0; idx < n_total; ++idx) {
240 int i_ls = idx / n_noise_;
241 int i_noise = idx % n_noise_;
242 double ls = ls_grid[i_ls];
243 double noise = noise_grid[i_noise];
244 std::vector<double> ls_vec(n_pairtypes, ls);
245
246 try {
248 if (K.rows() !=
y.size() || K.cols() !=
y.size() ||
249 !K.allFinite()) {
250 throw std::invalid_argument(
251 "kernel matrix must match the finite targets");
252 }
253 Eigen::SelfAdjointEigenSolver<EigenMatrix> solver(K);
254
255 FactorizedLikelihood fl;
256 fl.setEigendecomposition(solver.eigenvalues(),
257 solver.eigenvectors(),
y);
258 double ll = fl.evaluateGP(noise);
259 local_evals++;
260
261 if (std::isfinite(ll) &&
262 (ll < local_best.best_likelihood ||
263 (ll == local_best.best_likelihood &&
264 idx < local_best_idx))) {
265 local_best.best_likelihood = ll;
266 local_best_idx = idx;
267 local_best.best_lengthscales = ls_vec;
268 local_best.best_noise = noise;
269 local_best.best_prefactor =
270 fl.analyticalPrefactor(noise);
271 }
272 } catch (...) {
273#ifdef USE_OPENMP
274#pragma omp critical(gpr_ard_search_error)
275#endif
276 {
277 if (!failure) failure = std::current_exception();
278 }
279 }
280 }
281
282#ifdef USE_OPENMP
283#pragma omp critical
284#endif
285 {
286 if (local_best_idx < n_total &&
287 (local_best.best_likelihood < best.best_likelihood ||
288 (local_best.best_likelihood == best.best_likelihood &&
289 local_best_idx < best_idx))) {
290 best.best_likelihood = local_best.best_likelihood;
291 best_idx = local_best_idx;
292 best.best_lengthscales = local_best.best_lengthscales;
293 best.best_noise = local_best.best_noise;
294 best.best_prefactor = local_best.best_prefactor;
295 }
296 best.total_evaluations += local_evals;
297 }
298 }
299 }
300
301 if (failure) std::rethrow_exception(failure);
302 return best;
303}
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix