106 const std::function<std::vector<double>(
double, std::size_t)>& fhat,
109 throw InputError(
"sim_fd_emcfd needs at least 4 differencing pairs, got " + std::to_string(n) +
".");
110 if (!(o.lambda > 0.0 && o.lambda < 1.0))
111 throw InputError(
"sim_fd_emcfd: the allocation ratio lambda must lie in (0,1).");
112 if (!(o.pilot_scale > 0.0) || !std::isfinite(o.pilot_scale))
113 throw InputError(
"sim_fd_emcfd: the pilot scale must be a positive finite number.");
114 if (!(o.min_step >= 0.0) || !(o.max_step > 0.0) || o.min_step > o.max_step)
115 throw InputError(
"sim_fd_emcfd: the step bounds must satisfy 0 <= min_step <= max_step, max_step > 0.");
116 const std::size_t n1 = std::max<std::size_t>(
117 3,
static_cast<std::size_t
>(std::floor(o.lambda *
static_cast<double>(n))));
119 throw InputError(
"sim_fd_emcfd: the E-stage (" + std::to_string(n1) +
120 " pairs) leaves no pair for the M-stage; increase n.");
121 const std::size_t n2 = n - n1;
123 auto checked = [&](
const std::vector<double>& v, std::size_t m) {
125 throw InputError(
"sim_fd_emcfd: the evaluator returned " + std::to_string(v.size()) +
126 " components where " + std::to_string(m) +
" were expected.");
132 std::vector<std::vector<double>> Y(n1);
133 std::vector<double> fsum;
134 for (std::size_t i = 0; i < n1; ++i) {
135 const std::vector<double> fp = fhat(x0 + d[i], i + 1);
136 const std::vector<double> fm = fhat(x0 - d[i], i + 1);
144 for (std::size_t j = 0; j < m; ++j) {
145 Y[i][j] = fp[j] - fm[j];
146 fsum[j] += fp[j] + fm[j];
149 double s2 = 0.0, s4 = 0.0, s6 = 0.0, dmax = 0.0;
150 for (std::size_t i = 0; i < n1; ++i) {
151 const double d2 = d[i] * d[i];
155 dmax = std::max(dmax, d[i]);
157 const double det = s2 * s6 - s4 * s4;
158 if (!(det > 0.0))
throw InputError(
"sim_fd_emcfd: the pilot steps are degenerate; increase pilot_scale.");
161 out.
B.assign(m, 0.0);
163 out.
sigma2.assign(m, 0.0);
164 std::vector<double> fbar(m, 0.0);
165 for (std::size_t j = 0; j < m; ++j) {
166 double s1y = 0.0, s3y = 0.0;
167 for (std::size_t i = 0; i < n1; ++i) {
168 s1y += d[i] * Y[i][j];
169 s3y += d[i] * d[i] * d[i] * Y[i][j];
171 out.
B[j] = (s2 * s3y - s4 * s1y) / (2.0 * det);
172 out.
pilot_grad[j] = (s6 * s1y - s4 * s3y) / (2.0 * det);
174 for (std::size_t i = 0; i < n1; ++i) {
175 const double res = Y[i][j] - 2.0 * d[i] * out.
pilot_grad[j] - 2.0 * d[i] * d[i] * d[i] * out.
B[j];
179 out.
sigma2[j] = ss /
static_cast<double>(n1 - 2) / 2.0;
180 fbar[j] = fsum[j] / (2.0 *
static_cast<double>(n1));
184 if (o.weights.empty()) {
186 for (std::size_t j = 0; j < m; ++j)
187 if (std::fabs(fbar[j]) > 0.0) {
188 out.
weights[j] = 1.0 / (fbar[j] * fbar[j]);
193 if (o.weights.size() != m)
194 throw InputError(
"sim_fd_emcfd: the weights must be " + std::to_string(m) +
" finite non-negative values.");
195 for (std::size_t j = 0; j < m; ++j) {
196 if (!(o.weights[j] >= 0.0) || !std::isfinite(o.weights[j]))
197 throw InputError(
"sim_fd_emcfd: the weights must be " + std::to_string(m) +
198 " finite non-negative values.");
203 double num = 0.0, den = 0.0;
204 for (std::size_t j = 0; j < m; ++j) {
206 den += out.
weights[j] * out.
B[j] * out.
B[j];
209 if (den > 0.0 && num > 0.0)
210 delta = std::pow(num / (4.0 *
static_cast<double>(n2) * den), 1.0 / 6.0);
213 else if (std::isfinite(o.max_step))
217 delta = std::min(std::max(delta, o.min_step), o.max_step);
218 if (!(delta > 0.0)) delta = dmax;
221 std::vector<std::vector<double>> Q(n2, std::vector<double>(m, 0.0));
222 out.
grad.assign(m, 0.0);
223 for (std::size_t k = 0; k < n2; ++k) {
224 const std::size_t idx = n1 + k + 1;
225 const std::vector<double> fp = fhat(x0 + delta, idx);
226 const std::vector<double> fm = fhat(x0 - delta, idx);
229 for (std::size_t j = 0; j < m; ++j) {
230 Q[k][j] = (fp[j] - fm[j]) / (2.0 * delta);
231 out.
grad[j] += Q[k][j];
234 out.
stderr_.assign(m, std::numeric_limits<double>::quiet_NaN());
235 for (std::size_t j = 0; j < m; ++j) {
236 out.
grad[j] /=
static_cast<double>(n2);
239 for (std::size_t k = 0; k < n2; ++k) {
240 const double e = Q[k][j] - out.
grad[j];
243 out.
stderr_[j] = std::sqrt(ss /
static_cast<double>(n2 - 1)) / std::sqrt(
static_cast<double>(n2));