Loading...
Searching...
No Matches
NoiseGridSearch.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "NoiseGridSearch.h"
6
7#include <cmath>
8#include <limits>
9#include <vector>
10
11namespace gpr {
12
13NoiseGridSearch::NoiseGridSearch(int n_grid, int n_refine, double log_noise_min,
14 double log_noise_max)
15 : n_grid_(n_grid),
16 n_refine_(n_refine),
17 log_noise_min_(log_noise_min),
18 log_noise_max_(log_noise_max)
19{
20}
21
22namespace {
23
24struct GridResult {
25 double best_log_noise = 0.0;
26 double best_value = std::numeric_limits<double>::infinity();
27 int best_idx = 0;
28};
29
30template <typename Fn>
31GridResult searchGrid(const std::vector<double>& log_noise_vals,
32 const Fn& eval_fn)
33{
34 GridResult result;
35 result.best_value = std::numeric_limits<double>::infinity();
36 result.best_idx = 0;
37
38 for (int i = 0; i < static_cast<int>(log_noise_vals.size()); ++i) {
39 double noise_var = std::pow(10.0, log_noise_vals[i]);
40 double val = eval_fn(noise_var);
41 if (std::isfinite(val) && val < result.best_value) {
42 result.best_value = val;
43 result.best_log_noise = log_noise_vals[i];
44 result.best_idx = i;
45 }
46 }
47 return result;
48}
49
50} // namespace
51
53 const FactorizedLikelihood& likelihood) const
54{
55 int total_evals = 0;
56 double lo = log_noise_min_;
57 double hi = log_noise_max_;
58
59 GridResult best;
60 best.best_value = std::numeric_limits<double>::infinity();
61
62 for (int refine = 0; refine <= n_refine_; ++refine) {
63 std::vector<double> grid(n_grid_);
64 for (int i = 0; i < n_grid_; ++i) {
65 grid[i] = lo + (hi - lo) * i / (n_grid_ - 1);
66 }
67
68 auto result = searchGrid(
69 grid, [&](double nv) { return likelihood.evaluateGP(nv); });
70 total_evals += n_grid_;
71
72 if (result.best_value < best.best_value) {
73 best = result;
74 }
75
76 // Zoom into region around best point
77 double margin = (hi - lo) / n_grid_;
78 lo = best.best_log_noise - margin;
79 hi = best.best_log_noise + margin;
80 }
81
82 double best_noise = std::pow(10.0, best.best_log_noise);
83 return {best_noise, best.best_value,
84 likelihood.analyticalPrefactor(best_noise), total_evals};
85}
86
88 const FactorizedLikelihood& likelihood, double a, double b) const
89{
90 int total_evals = 0;
91 double lo = log_noise_min_;
92 double hi = log_noise_max_;
93
94 GridResult best;
95 best.best_value = std::numeric_limits<double>::infinity();
96
97 for (int refine = 0; refine <= n_refine_; ++refine) {
98 std::vector<double> grid(n_grid_);
99 for (int i = 0; i < n_grid_; ++i) {
100 grid[i] = lo + (hi - lo) * i / (n_grid_ - 1);
101 }
102
103 auto result = searchGrid(
104 grid, [&](double nv) { return likelihood.evaluateTP(nv, a, b); });
105 total_evals += n_grid_;
106
107 if (result.best_value < best.best_value) {
108 best = result;
109 }
110
111 double margin = (hi - lo) / n_grid_;
112 lo = best.best_log_noise - margin;
113 hi = best.best_log_noise + margin;
114 }
115
116 double best_noise = std::pow(10.0, best.best_log_noise);
117 return {best_noise, best.best_value,
118 likelihood.tpPrefactor(best_noise, a, b), total_evals};
119}
120
121} // namespace gpr
Exponential grid search for noise hyperparameter optimization.
double evaluateGP(double noise_variance) const
GP negative log marginal likelihood at given noise level.
double evaluateTP(double noise_variance, double a, double b) const
TP negative log marginal likelihood at given noise level.
double analyticalPrefactor(double noise_variance) const
Analytically optimal GP prefactor (signal variance).
double tpPrefactor(double noise_variance, double a, double b) const
TP prefactor from inverse-gamma hyperprior.
NoiseSearchResult searchTP(const FactorizedLikelihood &likelihood, double a, double b) const
Search for optimal noise using TP factorized likelihood.
NoiseSearchResult searchGP(const FactorizedLikelihood &likelihood) const
Search for optimal noise using GP factorized likelihood.
NoiseGridSearch(int n_grid=80, int n_refine=2, double log_noise_min=-10.0, double log_noise_max=2.0)
Configure the grid search.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Result of a noise grid search.