5#ifndef LINE_API_SIM_SIM_STS_QUANTILE_AREAS_H
6#define LINE_API_SIM_SIM_STS_QUANTILE_AREAS_H
88 explicit RankTree(std::size_t m) : f_(m + 1, 0), m_(m) {
90 while (step_ * 2 <= m) step_ *= 2;
93 void add(std::size_t rank) {
94 for (std::size_t i = rank; i <= m_; i += i & (~i + 1)) ++f_[i];
101 std::size_t select(std::size_t L)
const {
102 std::size_t pos = 0, rem = L;
103 for (std::size_t step = step_; step > 0; step >>= 1) {
104 const std::size_t cand = pos + step;
105 if (cand <= m_ && f_[cand] < rem) {
114 std::vector<std::size_t> f_;
132 double p,
double weight = std::sqrt(12.0)) {
134 "sim_sts_quantile_areas: the areas carry the irrational normalizing weight "
135 "sqrt(12)/(m sqrt(m)), so exact arithmetic is refused");
136 if (b < 1)
throw InputError(
"sim_sts_quantile_areas: the batch count b must be positive");
137 if (m < 1)
throw InputError(
"sim_sts_quantile_areas: the batch size m must be positive");
138 if (!(p > 0.0) || !(p < 1.0))
139 throw InputError(
"sim_sts_quantile_areas: p must be a real scalar in (0,1)");
141 throw InputError(
"sim_sts_quantile_areas: weight must be a nonzero real scalar");
143 const std::size_t n = b * m;
145 throw InputError(
"sim_sts_quantile_areas: Y must hold exactly b*m observations");
146 for (std::size_t i = 0; i < n; ++i)
147 if (!detail::num_isfinite(Y[i]))
148 throw InputError(
"sim_sts_quantile_areas: the sample path must be finite");
159 const T scale = T(wgt / T(md * detail::num_sqrt(md)));
160 const std::size_t bqeIdx =
static_cast<std::size_t
>(
161 std::ceil(
static_cast<double>(m) * p)) - 1;
163 std::vector<std::size_t> ord(m), rnk(m);
164 for (std::size_t j = 0; j < b; ++j) {
165 const T* col = &Y[j * m];
167 std::iota(ord.begin(), ord.end(),
static_cast<std::size_t
>(0));
169 std::stable_sort(ord.begin(), ord.end(),
170 [col](std::size_t a, std::size_t c) { return col[a] < col[c]; });
171 std::vector<T> sorted(m);
172 for (std::size_t r = 0; r < m; ++r) {
173 sorted[r] = col[ord[r]];
177 st.
bqe[j] = sorted[bqeIdx];
179 detail::RankTree tree(m);
181 for (std::size_t k = 1; k <= m; ++k) {
182 tree.add(rnk[k - 1]);
183 const std::size_t L =
184 static_cast<std::size_t
>(std::ceil(p *
static_cast<double>(k)));
185 const T qk = sorted[tree.select(L)];
188 st.
areas[j] = T(scale * acc);
191 std::vector<T> all(Y);
192 std::sort(all.begin(), all.end());
193 st.
quantile = all[
static_cast<std::size_t
>(std::ceil(
static_cast<double>(n) * p)) - 1];
196 for (std::size_t j = 0; j < b; ++j) sumSq += T(st.
areas[j] * st.
areas[j]);
201 for (std::size_t j = 0; j < b; ++j) {
210 st.
Np = detail::num_nan<T>();
211 st.
Vp = detail::num_nan<T>();
The exception types the port throws.
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.
Conservation laws of a layered queueing network, enumerated from its structure.
Number-type abstraction for the templated API port.
Shared arithmetic helpers for the templated simulation output-analysis port.
Batched-quantile statistics of one sample path.
std::size_t n
Number of observations used, b*m.
std::size_t b
Batch count.
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).