LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
infer_nhpp_ks.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_INFER_NHPP_KS_H
6#define LINE_API_INFER_NHPP_KS_H
7
8/**
9 * @file
10 * @ingroup api_infer
11 * Kolmogorov-Smirnov tests for a non-homogeneous Poisson arrival process.
12 *
13 * Templated port of matlab/src/api/infer/infer_nhpp_ks.m, cross-checked against
14 * jar/src/main/java/jline/api/infer/InferNhppKs.java.
15 *
16 * THE CONDITIONAL-UNIFORM TRANSFORMATION. Conditional on the number of arrivals
17 * in [T0,T], the arrival times of an NHPP are the order statistics of iid
18 * variables with cdf Lambda(t)/Lambda(T). Mapping the data through that cdf
19 * turns ANY NHPP, whatever its rate, into iid uniforms, so one KS test covers
20 * every rate function.
21 *
22 * WHY THE PLAIN TEST IS WEAK, AND WHAT FIXES IT. The CU KS test has "remarkably
23 * little power" against non-exponential interarrival times: it looks at the
24 * POSITIONS of the points, and those stay nearly uniform for many non-Poisson
25 * processes. Lewis (1965) applies the Durbin (1961) transformation first --
26 * reorder the GAPS ascending, rescale each by how many gaps remain, cumulate --
27 * which turns a difference in the gap DISTRIBUTION into a difference in
28 * position. Measured on 400 replications of an Erlang-4 renewal process, the CU
29 * test rejects at its own size while the Lewis test rejects essentially always.
30 *
31 * TESTING A GIVEN RATE. Tt is the time-transformation test of Nelson and
32 * Leemis (2020, Section 5): the arrivals are mapped to Lambda(T_i) - Lambda(T0),
33 * a rate-1 Poisson process under the null, and their gaps are KS-tested against
34 * the unit exponential. It tests the rate itself, and needs a cumRate obtained
35 * from other data: the same paper shows Poissonness cannot be tested from one
36 * path when the rate is fitted to that path.
37 *
38 * ARITHMETIC. exp and sqrt in the p-value: transcendental only.
39 *
40 * Reference: S.-H. Kim, W. Whitt (2014). Are call center and hospital arrivals
41 * well modeled by nonhomogeneous Poisson processes? M&SOM 16(3), 464-480;
42 * J. Durbin (1961), Biometrika 48, 41-55; P. A. W. Lewis (1965), JRSS B 27;
43 * B. L. Nelson, L. M. Leemis (2020), Proc. Winter Simulation Conference,
44 * 266-276 (the Tt test).
45 */
46
47#include <algorithm>
48#include <cmath>
49#include <cstddef>
50#include <functional>
51#include <string>
52#include <vector>
53
54#include "line/num/number.h"
55#include "line/util/error.h"
56
57namespace line {
58namespace infer {
59
60/** Which test to run on the conditional-uniform data. */
61enum class NhppKsMethod { Cu, Lewis, Tt };
62
63/** Outcome of the KS test. */
64template <class T>
66 T statistic; ///< the KS distance
67 T pvalue; ///< asymptotic p-value
68 std::size_t n = 0; ///< number of arrivals used
69 std::vector<T> uniforms; ///< after the conditional-uniform transformation
70 std::vector<T> transformed; ///< after the Durbin step (Lewis), or 1-exp(-gap) (Tt)
71};
72
73namespace detail {
74
75/** Two-sided KS distance between the sample and the uniform cdf. */
76template <class T>
77T nhpp_ks_stat(std::vector<T> u) {
78 std::sort(u.begin(), u.end());
79 const std::size_t n = u.size();
80 const T nT = num_traits<T>::from_int(static_cast<long>(n));
82 for (std::size_t i = 0; i < n; ++i) {
83 const T up = num_traits<T>::from_int(static_cast<long>(i + 1)) / nT - u[i];
84 const T lo = u[i] - num_traits<T>::from_int(static_cast<long>(i)) / nT;
85 if (up > d) d = up;
86 if (lo > d) d = lo;
87 }
88 return d;
89}
90
91/**
92 * Asymptotic Kolmogorov p-value with the small-sample correction of Stephens:
93 * the effective argument is (sqrt(n)+0.12+0.11/sqrt(n))D, accurate from n = 5.
94 */
95template <class T>
96T nhpp_ks_pvalue(const T& d, std::size_t n) {
97 using std::exp;
98 using std::sqrt;
99 const T one = num_traits<T>::from_int(1);
100 if (n == 0) return one;
101 const T nT = num_traits<T>::from_int(static_cast<long>(n));
102 const T x = (sqrt(nT) + num_traits<T>::from_double(0.12) +
103 num_traits<T>::from_double(0.11) / sqrt(nT)) * d;
104 if (x <= num_traits<T>::from_int(0)) return one;
106 for (int k = 1; k <= 100; ++k) {
107 const T term = exp(-num_traits<T>::from_int(2 * k * k) * x * x);
108 q += (k % 2 == 1) ? term : T(-term);
109 }
110 T p = num_traits<T>::from_int(2) * q;
111 if (p < num_traits<T>::from_int(0)) p = num_traits<T>::from_int(0);
112 if (p > one) p = one;
113 return p;
114}
115
116} // namespace detail
117
118/**
119 * @brief Kolmogorov-Smirnov tests for a non-homogeneous Poisson arrival
120 * process.
121 *
122 * @param times the arrival times, within [T0,T]
123 * @param T the right end of the observation interval
124 * @param cumRate the cumulative rate Lambda(t); a constant rate when empty
125 * @param method Cu for the plain test, Lewis for the Durbin-transformed one,
126 * Tt for the time-transformation test of a given cumRate
127 * @param T0 the left end of the observation interval
128 */
129template <class Tv>
130NhppKsResult<Tv> infer_nhpp_ks(const std::vector<Tv>& times, const Tv& T,
131 const std::function<Tv(const Tv&)>& cumRate =
132 std::function<Tv(const Tv&)>(),
134 const Tv& T0 = num_traits<Tv>::from_int(0)) {
135 static_assert(num_traits<Tv>::has_transcendental, "infer_nhpp_ks needs exp for the p-value");
136 const Tv zero = num_traits<Tv>::from_int(0);
137 const Tv one = num_traits<Tv>::from_int(1);
138 std::vector<Tv> t;
139 for (const Tv& x : times)
140 if (x >= T0 && x <= T) t.push_back(x);
141 std::sort(t.begin(), t.end());
142 const std::size_t n = t.size();
143 if (n < 2) throw InputError("infer_nhpp_ks: at least two arrivals are needed to test");
144 if (method == NhppKsMethod::Tt && !cumRate)
145 throw InputError("infer_nhpp_ks: method 'tt' tests a given rate and needs cumRate");
146
147 std::vector<Tv> u(n);
148 if (!cumRate) {
149 for (std::size_t i = 0; i < n; ++i) u[i] = (t[i] - T0) / (T - T0);
150 } else {
151 const Tv lo = cumRate(T0), hi = cumRate(T);
152 if (hi <= lo)
153 throw InputError("infer_nhpp_ks: the cumulative rate must increase over the interval");
154 for (std::size_t i = 0; i < n; ++i) u[i] = (cumRate(t[i]) - lo) / (hi - lo);
155 }
156 for (Tv& v : u) {
157 if (v < zero) v = zero;
158 if (v > one) v = one;
159 }
160
162 r.n = n;
163 r.uniforms = u;
164 if (method == NhppKsMethod::Cu) {
165 r.transformed = u;
166 } else if (method == NhppKsMethod::Tt) {
167 // Time transformation: under the null Lambda(T_i) - Lambda(T0) are the
168 // epochs of a rate-1 Poisson process, so their gaps are iid Exp(1).
169 using std::exp;
170 const Tv base = cumRate(T0);
171 r.transformed.resize(n);
172 Tv prev = zero;
173 for (std::size_t i = 0; i < n; ++i) {
174 const Tv lam = cumRate(t[i]) - base;
175 const Tv gap = lam - prev;
176 if (gap < zero)
177 throw InputError("infer_nhpp_ks: the cumulative rate must be non-decreasing");
178 r.transformed[i] = one - exp(-gap);
179 prev = lam;
180 }
181 } else {
182 // The Durbin (1961) transformation: gaps, sorted ascending, each
183 // rescaled by how many gaps remain, then cumulated.
184 std::vector<Tv> v = u;
185 std::sort(v.begin(), v.end());
186 std::vector<Tv> gaps(n + 1);
187 gaps[0] = v[0];
188 for (std::size_t i = 1; i < n; ++i) gaps[i] = v[i] - v[i - 1];
189 gaps[n] = one - v[n - 1];
190 std::sort(gaps.begin(), gaps.end());
191 std::vector<Tv> c(n + 1);
192 Tv prev = zero;
193 for (std::size_t i = 0; i <= n; ++i) {
194 c[i] = num_traits<Tv>::from_int(static_cast<long>(n + 1 - i)) * (gaps[i] - prev);
195 prev = gaps[i];
196 }
197 r.transformed.resize(n);
198 Tv acc = zero;
199 for (std::size_t i = 0; i < n; ++i) {
200 acc += c[i];
201 Tv s = acc;
202 if (s < zero) s = zero;
203 if (s > one) s = one;
204 r.transformed[i] = s;
205 }
206 }
207 r.statistic = detail::nhpp_ks_stat(r.transformed);
208 r.pvalue = detail::nhpp_ks_pvalue(r.statistic, n);
209 return r;
210}
211
212} // namespace infer
213} // namespace line
214
215#endif // LINE_API_INFER_NHPP_KS_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
NhppKsResult< Tv > infer_nhpp_ks(const std::vector< Tv > &times, const Tv &T, const std::function< Tv(const Tv &)> &cumRate=std::function< Tv(const Tv &)>(), NhppKsMethod method=NhppKsMethod::Lewis, const Tv &T0=num_traits< Tv >::from_int(0))
Kolmogorov-Smirnov tests for a non-homogeneous Poisson arrival process.
NhppKsMethod
Which test to run on the conditional-uniform data.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
Outcome of the KS test.
T statistic
the KS distance
std::vector< T > uniforms
after the conditional-uniform transformation
std::size_t n
number of arrivals used
T pvalue
asymptotic p-value
std::vector< T > transformed
after the Durbin step (Lewis), or 1-exp(-gap) (Tt)