Draw K samples from N(theta_map, hessian^{-1}), populate out.theta_samples / alpha_samples / weights / k_hat / reliable. Returns true on success (every log_target evaluation finite, proposal Cholesky succeeded). Returns false if the proposal covariance cannot be Cholesky-factored even after jittering the Hessian by options.hessian_jitter_max.
89{
93 out.n_target_evals = 0;
94
95 const Eigen::Index p = theta_map.size();
96 if (p == 0 || hessian.rows() != p || hessian.cols() != p) return false;
97
98
99
100
101
102
103
104
105 Eigen::MatrixXd H = hessian;
106 Eigen::LLT<Eigen::MatrixXd> llt_H(H);
107 if (llt_H.info() != Eigen::Success) {
108
109 double jitter = 1.0e-9;
110 bool ok = false;
111 while (jitter <= opts_.hessian_jitter_max) {
112 H = hessian;
113 H.diagonal().array() += jitter;
114 llt_H.compute(H);
115 if (llt_H.info() == Eigen::Success) {
116 ok = true;
117 break;
118 }
119 jitter *= 10.0;
120 }
121 if (!ok) return false;
122 }
123
124
125
126
127 const Eigen::MatrixXd U_H = llt_H.matrixU();
128 const Eigen::MatrixXd I =
129 Eigen::MatrixXd::Identity(static_cast<int>(p), static_cast<int>(p));
130 const Eigen::MatrixXd L =
131 U_H.transpose().template triangularView<Eigen::Lower>().solve(I);
132
133 out.theta_map = theta_map;
134 out.proposal_cov_chol = L;
135
136
137
138
139
140
141
142
143
144
145
146
147 int K = std::max(1, opts_.n_samples);
148 std::vector<Eigen::VectorXd> sample_positions;
149 if (opts_.deterministic_axial_grid) {
150 K = 1 + 2 * static_cast<int>(p);
151 sample_positions.reserve(K);
152 sample_positions.push_back(theta_map);
153 for (Eigen::Index j = 0; j < p; ++j) {
154 Eigen::VectorXd e_j = Eigen::VectorXd::Zero(p);
155 e_j(j) = 1.0;
156 const Eigen::VectorXd step = L * e_j;
157 sample_positions.push_back(theta_map + step);
158 sample_positions.push_back(theta_map - step);
159 }
160 } else {
161 sample_positions.reserve(K);
162 std::normal_distribution<double> normal_init(0.0, 1.0);
163 for (int k = 0; k < K; ++k) {
164 Eigen::VectorXd
z(p);
165 for (Eigen::Index j = 0; j < p; ++j)
166 z(j) = normal_init(rng_);
167 sample_positions.push_back(theta_map + L *
z);
168 }
169 }
170
171 out.theta_samples.clear();
172 out.theta_samples.reserve(K);
173 out.alpha_samples.clear();
174 out.alpha_samples.reserve(K);
175 out.log_weights_raw.clear();
176 out.log_weights_raw.reserve(K);
177
178
179
180
181 for (int k = 0; k < K; ++k) {
182 Eigen::VectorXd theta_k = sample_positions[k];
183
184 const double log_p_target = log_target(theta_k);
185 if (!std::isfinite(log_p_target)) {
186
187
188
189 out.theta_samples.push_back(std::move(theta_k));
190 out.alpha_samples.emplace_back();
191 out.log_weights_raw.push_back(
192 std::numeric_limits<double>::quiet_NaN());
193 continue;
194 }
195 ++
out.n_target_evals;
196
197
198
199
200
201 const Eigen::VectorXd delta = theta_k - theta_map;
203 const double log_w = log_p_target - log_q;
204
205
206 Eigen::VectorXd alpha;
207 if (alpha_fn) alpha = alpha_fn(theta_k);
208
209 out.theta_samples.push_back(std::move(theta_k));
210 out.alpha_samples.push_back(std::move(alpha));
211 out.log_weights_raw.push_back(log_w);
212 }
213
214
215
216 std::vector<double> log_w_finite;
217 log_w_finite.reserve(
out.log_weights_raw.size());
218 std::vector<int> finite_idx;
219 finite_idx.reserve(
out.log_weights_raw.size());
220 for (std::size_t i = 0; i <
out.log_weights_raw.size(); ++i) {
221 if (std::isfinite(
out.log_weights_raw[i])) {
222 log_w_finite.push_back(
out.log_weights_raw[i]);
223 finite_idx.push_back(static_cast<int>(i));
224 }
225 }
226 out.weights.assign(
out.log_weights_raw.size(), 0.0);
227 if (log_w_finite.empty()) return false;
228
230 for (std::size_t j = 0; j < finite_idx.size(); ++j) {
231 out.weights[
static_cast<std::size_t
>(finite_idx[j])] = w_finite[j];
232 }
233
234
235
236
237
238 std::vector<double> w_sorted = w_finite;
239 std::sort(w_sorted.begin(), w_sorted.end(), std::greater<double>());
240 const int K_finite = static_cast<int>(w_sorted.size());
241 const int M_pareto = std::min(std::max(K_finite / 5, 4), K_finite - 1);
243 out.reliable = (
out.k_hat <= opts_.k_hat_threshold);
244
246 return true;
247}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double estimateParetoKHat(const std::vector< double > &weights_descending, int M_pareto)
double logGaussianDensity(const Eigen::VectorXd &delta, const Eigen::MatrixXd &cov_chol)
std::vector< double > selfNormalisedWeights(const std::vector< double > &log_w)