5#ifndef LINE_API_INFER_NHPP_FIT_H
6#define LINE_API_INFER_NHPP_FIT_H
89std::vector<T> nhpp_fit_knots(
const std::vector<T>& times,
const T& horizon,
const char* caller) {
92 throw InputError(std::string(caller) +
": the horizon T must be a positive finite scalar");
94 throw InputError(std::string(caller) +
": at least one arrival is needed to estimate the rate");
95 std::vector<T> t = times;
96 std::sort(t.begin(), t.end());
97 if (!(t.front() > zero) || t.back() > horizon)
98 throw InputError(std::string(caller) +
": the arrival times must lie in (0,T]");
100 k.reserve(t.size() + 2);
102 k.insert(k.end(), t.begin(), t.end());
103 k.push_back(horizon);
104 for (std::size_t i = 1; i < k.size(); ++i)
105 if (!(k[i] > k[i - 1]))
107 ": the arrival times must be distinct and strictly below T: a tie "
108 "gives the piecewise-linear estimator a jump that no finite rate "
114template <
class T,
class It>
115std::size_t nhpp_fit_upper(It first, It last,
const T& x) {
116 return static_cast<std::size_t
>(std::upper_bound(first, last, x) - first);
133 const std::vector<T>& tq = std::vector<T>(),
134 double alpha = 0.05) {
137 const std::vector<T> k = detail::nhpp_fit_knots(times, horizon,
"infer_nhpp_leemis");
138 const std::size_t n = k.size() - 2;
139 if (!(alpha > 0.0 && alpha < 1.0))
140 throw InputError(
"infer_nhpp_leemis: alpha must lie in (0,1)");
150 for (std::size_t i = 0; i <= n; ++i)
153 r.
rates.resize(n + 1);
154 for (std::size_t i = 0; i <= n; ++i) r.
rates[i] = nT / (n1 * (k[i + 1] - k[i]));
160 for (
const T& x : r.
tq)
161 if (x < zero || x > horizon)
162 throw InputError(
"infer_nhpp_leemis: the query points must lie in [0,T]");
165 const std::size_t m = r.
tq.size();
170 for (std::size_t j = 0; j < m; ++j) {
171 const T& q = r.
tq[j];
173 std::size_t idx = detail::nhpp_fit_upper(k.begin(), k.end(), q);
174 idx = idx == 0 ? 0 : idx - 1;
175 if (idx > n) idx = n;
177 const std::size_t s = detail::nhpp_fit_upper(k.begin() + 1, k.end() - 1, q);
179 const T half = z * sqrt(sT);
181 const T lo = sT - half;
182 r.
lower[j] = lo > zero ? lo : zero;
183 r.
upper[j] = sT + half;
198 double alpha = 0.05) {
203 if (!(alpha > 0.0 && alpha < 1.0))
204 throw InputError(
"infer_nhpp_powerlaw: alpha must lie in (0,1)");
208 throw InputError(
"infer_nhpp_powerlaw: the horizon T must be a positive finite scalar");
210 throw InputError(
"infer_nhpp_powerlaw: at least one arrival is needed to fit the power law");
211 const std::size_t n = times.size();
214 for (
const T& x : times) {
215 if (!(x > zero) || x > horizon)
216 throw InputError(
"infer_nhpp_powerlaw: the arrival times must lie in (0,T]");
219 const T denom = nT * log(horizon) - sumLog;
221 throw InputError(
"infer_nhpp_powerlaw: every arrival is at T, so the power-law shape is unbounded");
222 const T b = nT / denom;
223 const T a = pow(nT, one / b) / horizon;
224 const T aT = a * horizon;
225 const T L = pow(aT, b);
226 const T lnaT = log(aT);
234 r.
loglik = nT * b * log(a) + nT * log(b) + (b - one) * sumLog - L;
236 const T Tb = pow(horizon, b);
237 const T iaa = nT * b / (a * a) + b * (b - one) * pow(a, b - two) * Tb;
238 const T iab = -nT / a + pow(a, b - one) * Tb * (one + b * lnaT);
239 const T ibb = nT / (b * b) + L * lnaT * lnaT;
240 const T det = iaa * ibb - iab * iab;
245 r.
cov[0][0] = ibb / det;
246 r.
cov[0][1] = -iab / det;
247 r.
cov[1][0] = -iab / det;
248 r.
cov[1][1] = iaa / det;
249 r.
se[0] = sqrt(r.
cov[0][0]);
250 r.
se[1] = sqrt(r.
cov[1][1]);
The exception types the port throws.
NhppPowerLawResult< T > infer_nhpp_powerlaw(const std::vector< T > ×, const T &horizon, double alpha=0.05)
Maximum likelihood power-law NHPP fit, Lambda(t) = (a t)^b, from one sample path, with observed-infor...
NhppLeemisResult< T > infer_nhpp_leemis(const std::vector< T > ×, const T &horizon, const std::vector< T > &tq=std::vector< T >(), double alpha=0.05)
Nonparametric integrated-rate estimate of an NHPP from one sample path: the step-function estimator w...
double sim_norminv(double p)
Standard normal quantile function.
Conservation laws of a layered queueing network, enumerated from its structure.
Number-type abstraction for the templated API port.
Normal and Student t quantiles used by the output-analysis routines.
Outcome of infer_nhpp_leemis.
std::vector< T > tq
query points
std::vector< T > knots
breakpoints [0, times, T], length N+2
T horizon
the observation horizon
std::vector< T > rates
piecewise-constant rate on each gap, length N+1
std::size_t n
number of arrivals N
std::vector< T > knotValues
piecewise-linear estimate at the knots
std::vector< T > cumRate
piecewise-linear estimate at tq
std::vector< T > upper
upper pointwise bound at tq
std::vector< T > step
step-function estimate at tq
std::vector< T > lower
lower pointwise bound at tq, floored at zero
Outcome of infer_nhpp_powerlaw.
T shape
the power-law shape b
T se[2]
standard errors of (a, b)
T scaleCI[2]
Wald interval for a.
T horizon
the observation horizon
T info[2][2]
observed information, order (a, b)
T shapeCI[2]
Wald interval for b.
T scale
the power-law scale a
T loglik
maximised log-likelihood
std::size_t n
number of arrivals N