5#ifndef LINE_API_SIM_SIM_FQUEST_H
6#define LINE_API_SIM_SIM_FQUEST_H
108inline long warmup_next_m(
long N,
long b,
long m,
bool& atMax) {
109 const long full = N / b;
110 const long next =
static_cast<long>(std::llround(
static_cast<double>(m) * std::sqrt(2.0)));
111 if (N < b * next && next != full)
return full;
133 "sim_fquest: the interval is a t quantile times a square root, so exact "
134 "arithmetic is refused");
137 if (!(p > 0.0) || !(p < 1.0))
138 throw InputError(
"sim_fquest: p must be a real scalar in (0,1)");
139 if (!(alpha > 0.0) || !(alpha < 1.0))
140 throw InputError(
"sim_fquest: alpha must be a real scalar in (0,1)");
141 const long sLast =
opt.s.back();
143 throw InputError(
"sim_fquest: the stage tests need at least 3 batches, so min(s) >= 3");
145 const long N =
static_cast<long>(Y.size());
146 for (std::size_t i = 0; i < Y.size(); ++i)
147 if (!detail::num_isfinite(Y[i]))
148 throw InputError(
"sim_fquest: the sample path must be finite");
150 throw InputError(
"sim_fquest: the sample path is too short for the stage tests");
157 if (N < b * m) m = N / b;
159 throw InputError(
"sim_fquest: the sample path is too short for the initial batch count b0");
162 bool atMax =
false, passed =
false;
164 const std::vector<T> head(Y.begin(), Y.begin() +
static_cast<std::ptrdiff_t
>(b * m));
166 head,
static_cast<std::size_t
>(b),
static_cast<std::size_t
>(m), p,
opt.weight);
168 opt.beta * std::exp(-
opt.eta * std::pow(
static_cast<double>(ell - 1),
opt.theta));
175 m = detail::warmup_next_m(N, b, m, atMax);
179 res.
warnings.push_back(
"the warmup randomness test could not be passed at the largest "
180 "admissible batch size, the sample path is too short");
183 const long truncated = m > 0 ? m : 0;
184 const std::vector<T> Yt(Y.begin() +
static_cast<std::ptrdiff_t
>(truncated), Y.end());
185 const long Nstar =
static_cast<long>(Yt.size());
191 bool ok =
true, haveStats =
false;
193 for (
int stage = 1; stage <= 4; ++stage) {
199 const std::vector<T> kept(Yt.end() -
static_cast<std::ptrdiff_t
>(b * m), Yt.end());
201 static_cast<std::size_t
>(m), p,
opt.weight);
203 const std::vector<T>& sample = stage <= 2 ? stats.
areas : stats.
bqe;
208 if (v >=
opt.s.size()) {
218 if (!haveStats || m < 1)
219 throw InputError(
"sim_fquest: the sample path is too short to form min(s) batches");
221 res.
b =
static_cast<std::size_t
>(b);
222 res.
m =
static_cast<std::size_t
>(m);
224 res.
truncated =
static_cast<std::size_t
>(truncated);
233 sim_tinv(1.0 - alpha / 2.0,
static_cast<double>(2 * b - 1)));
234 const T half = T(t * detail::num_sqrt(T(stats.
Vp / nst)));
240 res.
warnings.push_back(
"a randomness or normality test failed at b = " +
241 std::to_string(
opt.s.back()) +
242 ", the delivered interval is heuristic");
251 res.
lower = detail::num_nan<T>();
252 res.
upper = detail::num_nan<T>();
The exception types the port throws.
QuestResult< T > sim_fquest(const std::vector< T > &Y, double p, double alpha=0.05, const QuestOptions &options=QuestOptions())
Fixed-sample-size confidence interval for a steady-state quantile.
StsQuantileStats< T > sim_sts_quantile_areas(const std::vector< T > &Y, std::size_t b, std::size_t m, double p, double weight=std::sqrt(12.0))
Standardized time series areas of the batched quantile process.
VonNeumannResult< T > sim_vonneumann(const std::vector< T > &x, double alpha=0.05)
Von Neumann ratio test for randomness of a sequence.
double sim_tinv(double p, double nu)
Quantile function of Student's t distribution.
QuestOptions sim_quest_options(const QuestOptions &options=QuestOptions())
Validates an option set and returns it.
ShapiroWilkResult< T > sim_shapirowilk(const std::vector< T > &x, double alpha=0.05)
Shapiro-Wilk test for univariate normality.
QuestInterval< T > sim_quest_heuristic_ci(const std::vector< T > &bqe, const T ¢re, const T &Ap, const T &Np, std::size_t nstar, double alpha, bool useAutocorr)
Fallback interval used when a QUEST stage test fails.
Number-type abstraction for the templated API port.
Normal and Student t quantiles used by the output-analysis routines.
Fallback interval used when a QUEST stage test fails.
Options of the QUEST procedures, with the published FQUEST defaults.
Shapiro-Wilk test for univariate normality.
Standardized time series areas of the batched quantile process.
Shared arithmetic helpers for the templated simulation output-analysis port.
Von Neumann ratio test for randomness of a sequence.
A confidence interval, asymmetric about the point estimate in general.
Procedure constants shared by sim_fquest and sim_firquest.
Point estimate and interval delivered by the QUEST procedures.
T halfwidth
(upper-lower)/2, attained only on average when asymmetric
T estimate
Full-sample empirical p-quantile of the truncated path.
std::size_t truncated
Observations deleted from the front of each path.
T upper
Upper confidence limit, NaN if refused.
T Vp
Combined variance-parameter estimator.
T Ap
STS area variance-parameter estimator.
std::size_t b
Final batch count, per replication in sim_firquest.
std::size_t R
Replications, 1 for sim_fquest.
std::size_t n
Observations the interval rests on.
bool heuristic
True when a stage test failed.
T lower
Lower confidence limit, NaN if refused.
std::size_t m
Final batch size.
std::vector< std::string > warnings
Diagnostics, empty on a clean run.
T Np
NBQ variance-parameter estimator.
Batched-quantile statistics of one sample path.
std::size_t n
Number of observations used, b*m.
T Ap
Batched STS area estimator A_p(w;b,m).
std::vector< T > areas
b signed STS areas A_p(w;j,m)
T Np
NBQ variance-parameter estimator N_p(b,m), NaN at b = 1.
std::vector< T > bqe
b batched quantile estimators yhat_p(j,m)
T Vp
Combined variance-parameter estimator, NaN at b = 1.
T quantile
Full-sample empirical p-quantile ytilde_p(n).