LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
ctmc_stmonotone.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_MC_CTMC_STMONOTONE_H
6#define LINE_API_MC_CTMC_STMONOTONE_H
7
8/**
9 * @file
10 * @ingroup api_mc
11 * Stochastically monotone upper bound of a Markov chain.
12 *
13 * Templated port of jar/src/main/java/jline/api/mc/Ctmc_stmonotone.java, which
14 * has no MATLAB twin. Given a row-stochastic P the algorithm of Abu-Kamel and
15 * Stewart builds the smallest st-monotone Q that dominates P in the strong
16 * stochastic order, by filling the tail sums from the last column backwards:
17 * Q(0,l..n-1) = P(0,l..n-1),
18 * Q(i,l..n-1) = max( Q(i-1,l..n-1), P(i,l..n-1) ),
19 * and recovering Q(i,l) as that tail sum minus the tail already assigned. Every
20 * row tail of Q then dominates the corresponding row tail of P and is
21 * nondecreasing in i, which is exactly st-monotonicity.
22 *
23 * The CTMC entry uniformizes Q at max|Q| (the rate the reference uses), repairs
24 * the rounding through dtmc_makestochastic, bounds the embedded chain and maps
25 * the bound back to a generator with ctmc_makeinfgen. Uniformization is a
26 * bijection between the two, so the bound is preserved.
27 *
28 * ARITHMETIC: field plus comparisons. Exact under Rational.
29 */
30
31#include <cstddef>
32
36#include "line/num/number.h"
37#include "line/util/error.h"
38#include "line/util/matrix.h"
39
40namespace line {
41namespace mc {
42
43/**
44 * @param P (n x n) row-stochastic transition matrix
45 * @return the st-monotone upper bound of P, itself row-stochastic
46 */
47template <class T>
49 const std::size_t n = P.rows();
50 if (n == 0) throw InputError("dtmc_stmonotone: the matrix is empty");
51 if (P.cols() != n) throw InputError("dtmc_stmonotone: the matrix is not square");
52 const T zero = num_traits<T>::from_int(0);
53 Matrix<T> Q(n, n, zero);
54
55 Q(0, n - 1) = P(0, n - 1);
56 for (std::size_t i = 1; i < n; ++i)
57 Q(i, n - 1) = Q(i - 1, n - 1) > P(i, n - 1) ? Q(i - 1, n - 1) : P(i, n - 1);
58
59 for (std::size_t lp = n - 1; lp-- > 0;) {
60 Q(0, lp) = P(0, lp);
61 for (std::size_t i = 1; i < n; ++i) {
62 T tailPrev = zero, tailP = zero, tailRight = zero;
63 for (std::size_t k = lp; k < n; ++k) {
64 tailPrev += Q(i - 1, k);
65 tailP += P(i, k);
66 if (k > lp) tailRight += Q(i, k);
67 }
68 Q(i, lp) = T((tailPrev > tailP ? tailPrev : tailP) - tailRight);
69 }
70 }
71 return Q;
72}
73
74/**
75 * @brief Stochastically monotone upper bound of a Markov chain.
76 *
77 * @param Q (n x n) generator
78 * @return the generator of the st-monotone upper bound of Q
79 */
80template <class T>
82 const T m = ctmc_maxabs(Q);
83 // An all-zero generator has no rate to uniformize at; any positive q gives P = Id.
84 const T q = (m == num_traits<T>::from_int(0)) ? num_traits<T>::from_int(1) : m;
86}
87
88} // namespace mc
89} // namespace line
90
91#endif // LINE_API_MC_CTMC_STMONOTONE_H
InputError(const std::string &what)
Definition error.h:39
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
Uniformization (randomization) of a CTMC: the embedded DTMC P = I + Q/q.
Steady-state distribution of a continuous-time Markov chain.
Normalize a non-negative matrix into a stochastic transition matrix.
The exception types the port throws.
Dense matrix and non-owning view.
Matrix< T > dtmc_stmonotone(const Matrix< T > &P)
Matrix< T > ctmc_makeinfgen(const Matrix< T > &Q)
Set the diagonal so that every row sums to zero (ctmc_makeinfgen).
Definition ctmc_solve.h:58
T ctmc_maxabs(const Matrix< T > &Q)
Largest magnitude of any entry of Q; equals max_i |q_ii| for a generator.
RandomizationResult< T > ctmc_randomization(const Matrix< T > &Q, const T &q)
Uniformization (randomization) of a CTMC: the embedded DTMC P = I + Q/q.
Matrix< T > ctmc_stmonotone(const Matrix< T > &Q)
Stochastically monotone upper bound of a Markov chain.
Number-type abstraction for the templated API port.