LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
ctmc_cosimla.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_COSIMLA_H
6#define LINE_API_MC_CTMC_COSIMLA_H
7
8/**
9 * @file
10 * @ingroup api_mc
11 * COSIMLA (COmbined SIMulation and Linear Algebra) estimate of a stationary
12 * expectation of a Markov chain with a large or infinite state space, with a
13 * general finite regeneration set (Glynn and Zheng, WSC 2023, extending Zheng,
14 * Infanger and Glynn, WSC 2022).
15 *
16 * Port of matlab/src/api/mc/private/cosimla_core.m behind ctmc_cosimla.m and
17 * dtmc_cosimla.m. With K a subset of the finite center A, linear algebra on A
18 * gives the reward and the transitions of the chain watched on K up to its
19 * first exit from A, and simulated excursions from every boundary state (the
20 * states outside A one step away from A) back to K supply the rest. A CTMC is
21 * analysed through its embedded jump chain with reward r/q and holding weight
22 * 1/q, q the total outgoing rate.
23 *
24 * The simulation draws from MT19937 through genrand_res53, the stream of
25 * MATLAB's RandStream('mt19937ar'): with the same seed and transition function
26 * the sample paths, and so the results, coincide with MATLAB, JAR and Python.
27 * The center is solved with dense LU, so it suits centers of up to a few
28 * thousand states.
29 */
30
31#include <chrono>
32#include <cmath>
33#include <cstddef>
34#include <functional>
35#include <map>
36#include <random>
37#include <sstream>
38#include <string>
39#include <vector>
40
44#include "line/util/error.h"
45#include "line/util/lu.h"
46#include "line/util/matrix.h"
47
48namespace line {
49namespace mc {
50
51/** Successor states and their weights (probabilities for a DTMC, rates for a CTMC). */
53 std::vector<std::vector<double>> states;
54 std::vector<double> weights;
55};
56
57/** One-step transition function; it must accept any state the chain can reach. */
58using CosimlaTransition = std::function<CosimlaSuccessors(const std::vector<double>&)>;
59
60/** Reward function returning one or more rewards per state. */
61using CosimlaReward = std::function<std::vector<double>(const std::vector<double>&)>;
62
63/** Options, with the MATLAB defaults. */
65 std::size_t nsamples = 1000; ///< paths per boundary state (at least 2)
66 unsigned seed = 23000u; ///< seed of the MT19937 stream
67 double maxsteps = 1e7; ///< cap on one excursion; reaching it is an error
68 double conflevel = 0.95; ///< confidence level of the interval
69};
70
71/** Estimate, confidence interval and diagnostics. */
73 std::vector<double> alpha; ///< estimate of pi*r, one entry per reward
74 std::vector<std::vector<double>> ci; ///< R rows of [lower, upper]
75 std::vector<double> halfwidth;
76 std::vector<double> stderr_; ///< standard error (trailing _ avoids the stdio macro)
77 double conflevel = 0.0;
78 std::vector<double> piK; ///< stationary law of the chain watched on K
79 Matrix<double> PK; ///< estimated transition matrix on K
80 std::vector<std::vector<double>> D; ///< boundary states, in discovery order
81 std::size_t nA = 0, nK = 0, nD = 0, nsamples = 0;
82 std::size_t steps = 0; ///< simulated transitions in total
83 std::vector<double> alphaTrunc; ///< plain truncation on the same center
84 double latime = 0.0, simtime = 0.0;
85};
86
87namespace detail {
88
89inline std::string cosimla_str(const std::vector<double>& x) {
90 std::ostringstream os;
91 os << '[';
92 for (std::size_t i = 0; i < x.size(); ++i) os << (i ? " " : "") << x[i];
93 os << ']';
94 return os.str();
95}
96
97inline std::vector<double> cosimla_key(const std::vector<double>& x) {
98 std::vector<double> k(x);
99 for (double& v : k) v += 0.0; // folds -0 onto 0
100 return k;
101}
102
103struct CosimlaStep {
104 std::vector<std::vector<double>> Y;
105 std::vector<double> p, w, cum, r;
106 double h = 1.0;
107};
108
109inline std::vector<double> cosimla_reward(const CosimlaReward& reward, const std::vector<double>& x,
110 const std::string& caller) {
111 std::vector<double> rx = reward(x);
112 bool ok = !rx.empty();
113 for (double v : rx) ok = ok && std::isfinite(v);
114 if (!ok)
115 throw InputError(ErrorKind{"Argument"},
116 caller + ": the reward function returned an empty or non-finite value at state " +
117 cosimla_str(x) + ".");
118 return rx;
119}
120
121inline CosimlaStep cosimla_step(const CosimlaTransition& trans, const std::vector<double>& x, bool isctmc,
122 const std::string& caller) {
123 const CosimlaSuccessors s = trans(x);
124 if (s.states.size() != s.weights.size())
125 throw InputError(ErrorKind{"Argument"}, caller + ": the transition function must return one successor row of width " +
126 std::to_string(x.size()) + " per weight.");
127 const std::vector<double> kx = cosimla_key(x);
128 CosimlaStep st;
129 double q = 0.0;
130 for (std::size_t j = 0; j < s.weights.size(); ++j) {
131 if (s.states[j].size() != x.size())
132 throw InputError(ErrorKind{"Argument"}, caller + ": the transition function must return one successor row of width " +
133 std::to_string(x.size()) + " per weight.");
134 const double w = s.weights[j];
135 if (!(w >= 0.0) || !std::isfinite(w))
136 throw InputError(ErrorKind{"Argument"}, caller + ": the transition function returned a negative or non-finite weight at state " +
137 cosimla_str(x) + ".");
138 if (w > 0.0 && !(isctmc && cosimla_key(s.states[j]) == kx)) {
139 st.Y.push_back(s.states[j]);
140 st.w.push_back(w);
141 q += w;
142 }
143 }
144 const std::size_t m = st.w.size();
145 st.p.resize(m);
146 if (isctmc) {
147 if (!(q > 0.0))
148 throw InputError(ErrorKind{"Argument"}, caller + ": state " + cosimla_str(x) +
149 " is absorbing (zero total outgoing rate): the chain is not irreducible.");
150 for (std::size_t j = 0; j < m; ++j) st.p[j] = st.w[j] / q;
151 st.h = 1.0 / q;
152 } else {
153 if (std::fabs(q - 1.0) > 1e-8) {
154 std::ostringstream os;
155 os << caller << ": the transition probabilities out of state " << cosimla_str(x) << " sum to " << q << ", not 1.";
156 throw InputError(ErrorKind{"Argument"}, os.str());
157 }
158 st.p = st.w;
159 st.h = 1.0;
160 }
161 st.cum.resize(m);
162 double acc = 0.0;
163 for (std::size_t j = 0; j < m; ++j) {
164 acc += st.p[j];
165 st.cum[j] = acc;
166 }
167 return st;
168}
169
170inline CosimlaResult cosimla_core(const std::vector<std::vector<double>>& A, const std::vector<std::size_t>& K,
171 const CosimlaTransition& trans, const CosimlaReward& reward,
172 const CosimlaOptions& options, bool isctmc, const std::string& caller) {
173 using clock = std::chrono::steady_clock;
174 const std::size_t n = options.nsamples;
175 if (n < 2)
176 throw InputError(ErrorKind{"Option"}, caller + ": options.nsamples must be an integer >= 2 (it sets the variance estimate of every boundary state).");
177 if (!(options.conflevel > 0.0 && options.conflevel < 1.0))
178 throw InputError(ErrorKind{"Option"}, caller + ": options.conflevel must lie in (0,1).");
179 if (!(options.maxsteps >= 1.0))
180 throw InputError(ErrorKind{"Option"}, caller + ": options.maxsteps must be a positive number.");
181 if (A.empty())
182 throw InputError(ErrorKind{"Argument"}, caller + ": the center set A must be non-empty, one state per row.");
183 const std::size_t nA = A.size(), dim = A[0].size();
184 if (K.empty())
185 throw InputError(ErrorKind{"Argument"}, caller + ": the regeneration set K must be a non-empty set of row indices of A.");
186 std::vector<char> isK(nA, 0);
187 for (std::size_t k : K) {
188 if (k >= nA)
189 throw InputError(ErrorKind{"Argument"}, caller + ": the regeneration set K must be a non-empty set of row indices of A.");
190 isK[k] = 1;
191 }
192 std::vector<std::size_t> kList, apList;
193 std::vector<std::size_t> posK(nA, 0);
194 for (std::size_t i = 0; i < nA; ++i) {
195 if (isK[i]) {
196 posK[i] = kList.size();
197 kList.push_back(i);
198 } else {
199 apList.push_back(i);
200 }
201 }
202 const std::size_t nK = kList.size(), nAp = apList.size();
203
204 std::map<std::vector<double>, std::size_t> Amap;
205 for (std::size_t i = 0; i < nA; ++i) {
206 if (A[i].size() != dim)
207 throw InputError(ErrorKind{"Argument"}, caller + ": every state of the center set A must have the same width.");
208 if (!Amap.emplace(cosimla_key(A[i]), i).second)
209 throw InputError(ErrorKind{"Argument"}, caller + ": the center set A contains the state " + cosimla_str(A[i]) + " twice.");
210 }
211
212 // one-step transitions out of every state of A, boundary set D
213 const auto tla = clock::now();
214 std::map<std::vector<double>, std::size_t> Dmap;
215 std::vector<std::vector<double>> Dlist;
216 std::size_t R = 0;
217 std::vector<std::vector<double>> rA(nA), rrA(nA);
218 std::vector<double> hA(nA);
219 struct Arc { std::size_t i, j; double p, w; };
220 std::vector<Arc> aa, ad;
221 for (std::size_t i = 0; i < nA; ++i) {
222 const CosimlaStep st = cosimla_step(trans, A[i], isctmc, caller);
223 const std::vector<double> rx = cosimla_reward(reward, A[i], caller);
224 if (i == 0) R = rx.size();
225 else if (rx.size() != R)
226 throw InputError(ErrorKind{"Argument"}, caller + ": the reward function must return the same number of rewards in every state.");
227 rA[i] = rx;
228 rrA[i].resize(R);
229 for (std::size_t k = 0; k < R; ++k) rrA[i][k] = rx[k] * st.h;
230 hA[i] = st.h;
231 for (std::size_t j = 0; j < st.Y.size(); ++j) {
232 const std::vector<double> key = cosimla_key(st.Y[j]);
233 auto ia = Amap.find(key);
234 if (ia != Amap.end()) {
235 aa.push_back({i, ia->second, st.p[j], st.w[j]});
236 } else {
237 auto d = Dmap.find(key);
238 std::size_t di;
239 if (d == Dmap.end()) {
240 di = Dlist.size();
241 Dmap.emplace(key, di);
242 Dlist.push_back(st.Y[j]);
243 } else {
244 di = d->second;
245 }
246 ad.push_back({i, di, st.p[j], st.w[j]});
247 }
248 }
249 }
250 const std::size_t nD = Dlist.size();
251 Matrix<double> PAA(nA, nA, 0.0), WAA(nA, nA, 0.0), PAD(nA, nD ? nD : 1, 0.0);
252 for (const Arc& a : aa) {
253 PAA(a.i, a.j) += a.p;
254 WAA(a.i, a.j) += a.w;
255 }
256 for (const Arc& a : ad) PAD(a.i, a.j) += a.p;
257
258 // linear algebra on the center (Section 3 of the paper)
259 Matrix<double> ar(nK, R, 0.0), Atil(nK, nK, 0.0), F(nK, nD ? nD : 1, 0.0);
260 std::vector<double> ae(nK, 0.0);
261 for (std::size_t a = 0; a < nK; ++a) {
262 const std::size_t i = kList[a];
263 for (std::size_t k = 0; k < R; ++k) ar(a, k) = rrA[i][k];
264 ae[a] = hA[i];
265 for (std::size_t b = 0; b < nK; ++b) Atil(a, b) = PAA(i, kList[b]);
266 for (std::size_t d = 0; d < nD; ++d) F(a, d) = PAD(i, d);
267 }
268 if (nAp > 0) {
269 const std::size_t ncol = R + 1 + nK + nD;
270 Matrix<double> M(nAp, nAp, 0.0);
271 for (std::size_t a = 0; a < nAp; ++a) {
272 M(a, a) = 1.0;
273 for (std::size_t b = 0; b < nAp; ++b) M(a, b) -= PAA(apList[a], apList[b]);
274 }
275 const std::vector<std::size_t> piv = lu_factor(M);
276 Matrix<double> X(nAp, ncol, 0.0);
277 std::vector<double> col(nAp);
278 for (std::size_t c = 0; c < ncol; ++c) {
279 for (std::size_t a = 0; a < nAp; ++a) {
280 const std::size_t i = apList[a];
281 if (c < R) col[a] = rrA[i][c];
282 else if (c == R) col[a] = hA[i];
283 else if (c < R + 1 + nK) col[a] = PAA(i, kList[c - R - 1]);
284 else col[a] = PAD(i, c - R - 1 - nK);
285 }
286 lu_solve(M, piv, col);
287 for (std::size_t a = 0; a < nAp; ++a) X(a, c) = col[a];
288 }
289 for (std::size_t a = 0; a < nK; ++a) {
290 const std::size_t i = kList[a];
291 for (std::size_t c = 0; c < nAp; ++c) {
292 const double p12 = PAA(i, apList[c]);
293 if (p12 == 0.0) continue;
294 for (std::size_t k = 0; k < R; ++k) ar(a, k) += p12 * X(c, k);
295 ae[a] += p12 * X(c, R);
296 for (std::size_t b = 0; b < nK; ++b) Atil(a, b) += p12 * X(c, R + 1 + b);
297 for (std::size_t d = 0; d < nD; ++d) F(a, d) += p12 * X(c, R + 1 + nK + d);
298 }
299 }
300 }
301 const double latime = std::chrono::duration<double>(clock::now() - tla).count();
302
303 // simulation of the excursions from D back to K
304 const auto tsim = clock::now();
305 std::mt19937 mt(options.seed);
306 const auto res53 = [&mt]() -> double {
307 const double a = static_cast<double>(mt() >> 5);
308 const double b = static_cast<double>(mt() >> 6);
309 return (a * 67108864.0 + b) / 9007199254740992.0;
310 };
311 Matrix<double> wr(nD ? nD : 1, R, 0.0), Hn(nD ? nD : 1, nK, 0.0);
312 std::vector<double> we(nD, 0.0);
313 std::vector<double> pathR(nD * n * R), pathE(nD * n);
314 std::vector<std::size_t> pathHit(nD * n);
315 std::map<std::vector<double>, CosimlaStep> cache;
316 std::size_t totsteps = 0;
317 for (std::size_t d = 0; d < nD; ++d) {
318 for (std::size_t s = 0; s < n; ++s) {
319 std::vector<double> key = cosimla_key(Dlist[d]);
320 std::vector<double> Rsum(R, 0.0);
321 double Esum = 0.0;
322 double steps = 0.0;
323 std::size_t ia = 0;
324 while (true) {
325 auto it = cache.find(key);
326 if (it == cache.end()) {
327 CosimlaStep c = cosimla_step(trans, key, isctmc, caller);
328 const std::vector<double> rc = cosimla_reward(reward, key, caller);
329 if (rc.size() != R)
330 throw InputError(ErrorKind{"Argument"}, caller + ": the reward function must return the same number of rewards in every state.");
331 c.r.resize(R);
332 for (std::size_t k = 0; k < R; ++k) c.r[k] = rc[k] * c.h;
333 it = cache.emplace(key, std::move(c)).first;
334 }
335 const CosimlaStep& c = it->second;
336 for (std::size_t k = 0; k < R; ++k) Rsum[k] += c.r[k];
337 Esum += c.h;
338 const double target = res53() * c.cum.back();
339 std::size_t j = c.cum.size() - 1;
340 for (std::size_t t = 0; t < c.cum.size(); ++t)
341 if (c.cum[t] > target) {
342 j = t;
343 break;
344 }
345 key = cosimla_key(c.Y[j]);
346 steps += 1.0;
347 auto hit = Amap.find(key);
348 if (hit != Amap.end() && isK[hit->second]) {
349 ia = hit->second;
350 break;
351 }
352 if (steps >= options.maxsteps) {
353 std::ostringstream os;
354 os << caller << ": an excursion from boundary state " << cosimla_str(Dlist[d])
355 << " did not return to the regeneration set within options.maxsteps = " << options.maxsteps
356 << " steps; the chain may not be positive recurrent, or K should be enlarged.";
357 throw NumericError(ErrorKind{"NonConvergence"}, os.str());
358 }
359 }
360 totsteps += static_cast<std::size_t>(steps);
361 for (std::size_t k = 0; k < R; ++k) pathR[(d * n + s) * R + k] = Rsum[k];
362 pathE[d * n + s] = Esum;
363 pathHit[d * n + s] = posK[ia];
364 }
365 for (std::size_t s = 0; s < n; ++s) {
366 for (std::size_t k = 0; k < R; ++k) wr(d, k) += pathR[(d * n + s) * R + k];
367 we[d] += pathE[d * n + s];
368 Hn(d, pathHit[d * n + s]) += 1.0;
369 }
370 for (std::size_t k = 0; k < R; ++k) wr(d, k) /= static_cast<double>(n);
371 we[d] /= static_cast<double>(n);
372 for (std::size_t b = 0; b < nK; ++b) Hn(d, b) /= static_cast<double>(n);
373 }
374 const double simtime = std::chrono::duration<double>(clock::now() - tsim).count();
375
376 // estimator (1)
377 Matrix<double> PK(nK, nK, 0.0);
378 for (std::size_t a = 0; a < nK; ++a)
379 for (std::size_t b = 0; b < nK; ++b) {
380 double v = Atil(a, b);
381 for (std::size_t d = 0; d < nD; ++d) v += F(a, d) * Hn(d, b);
382 PK(a, b) = v;
383 }
384 std::vector<double> piK = dtmc_solve(PK);
385 double tot = 0.0;
386 for (double v : piK) tot += v;
387 for (double& v : piK) v /= tot;
388 std::vector<double> num(R, 0.0);
389 double den = 0.0;
390 for (std::size_t a = 0; a < nK; ++a) {
391 double e = ae[a];
392 for (std::size_t d = 0; d < nD; ++d) e += F(a, d) * we[d];
393 den += piK[a] * e;
394 for (std::size_t k = 0; k < R; ++k) {
395 double v = ar(a, k);
396 for (std::size_t d = 0; d < nD; ++d) v += F(a, d) * wr(d, k);
397 num[k] += piK[a] * v;
398 }
399 }
400 std::vector<double> alpha(R);
401 for (std::size_t k = 0; k < R; ++k) alpha[k] = num[k] / den;
402
403 // delta-method confidence interval (linearization after (1))
404 const double z = sim::sim_norminv((1.0 + options.conflevel) / 2.0);
405 std::vector<double> se(R, 0.0);
406 if (nD > 0) {
407 Matrix<double> Gin(nK, nK, 0.0);
408 for (std::size_t a = 0; a < nK; ++a)
409 for (std::size_t b = 0; b < nK; ++b) Gin(a, b) = (a == b ? 1.0 : 0.0) - PK(a, b) + piK[b];
410 const std::vector<std::size_t> gpiv = lu_factor(Gin);
411 std::vector<double> f(nD, 0.0);
412 for (std::size_t d = 0; d < nD; ++d)
413 for (std::size_t a = 0; a < nK; ++a) f[d] += piK[a] * F(a, d);
414 for (std::size_t k = 0; k < R; ++k) {
415 std::vector<double> cv(nK, 0.0);
416 for (std::size_t a = 0; a < nK; ++a) {
417 double v = ar(a, k) - alpha[k] * ae[a];
418 for (std::size_t d = 0; d < nD; ++d) v += F(a, d) * (wr(d, k) - alpha[k] * we[d]);
419 cv[a] = v;
420 }
421 lu_solve(Gin, gpiv, cv);
422 double var = 0.0;
423 std::vector<double> Z(n);
424 for (std::size_t d = 0; d < nD; ++d) {
425 double mean = 0.0;
426 for (std::size_t s = 0; s < n; ++s) {
427 Z[s] = pathR[(d * n + s) * R + k] - alpha[k] * pathE[d * n + s] + cv[pathHit[d * n + s]];
428 mean += Z[s];
429 }
430 mean /= static_cast<double>(n);
431 double ss = 0.0;
432 for (std::size_t s = 0; s < n; ++s) ss += (Z[s] - mean) * (Z[s] - mean);
433 var += f[d] * f[d] * (ss / static_cast<double>(n - 1)) / static_cast<double>(n);
434 }
435 se[k] = std::sqrt(var) / den;
436 }
437 }
438
439 // plain truncation at the same center, for comparison
440 std::vector<double> piT;
441 if (isctmc) {
442 Matrix<double> QA(nA, nA, 0.0);
443 for (std::size_t i = 0; i < nA; ++i) {
444 double out = 0.0;
445 for (std::size_t j = 0; j < nA; ++j) {
446 QA(i, j) = WAA(i, j);
447 out += WAA(i, j);
448 }
449 QA(i, i) -= out;
450 }
451 piT = ctmc_solve(QA);
452 } else {
453 Matrix<double> PT(nA, nA, 0.0);
454 for (std::size_t i = 0; i < nA; ++i) {
455 double lost = 0.0;
456 for (std::size_t d = 0; d < nD; ++d) lost += PAD(i, d);
457 for (std::size_t j = 0; j < nA; ++j) PT(i, j) = PAA(i, j);
458 PT(i, i) += lost;
459 }
460 piT = dtmc_solve(PT);
461 }
462
463 CosimlaResult res;
464 res.alpha = alpha;
465 res.stderr_ = se;
466 res.halfwidth.resize(R);
467 res.ci.assign(R, std::vector<double>(2));
468 for (std::size_t k = 0; k < R; ++k) {
469 res.halfwidth[k] = z * se[k];
470 res.ci[k][0] = alpha[k] - res.halfwidth[k];
471 res.ci[k][1] = alpha[k] + res.halfwidth[k];
472 }
473 res.alphaTrunc.assign(R, 0.0);
474 for (std::size_t i = 0; i < nA; ++i)
475 for (std::size_t k = 0; k < R; ++k) res.alphaTrunc[k] += piT[i] * rA[i][k];
476 res.conflevel = options.conflevel;
477 res.piK = piK;
478 res.PK = PK;
479 res.D = Dlist;
480 res.nA = nA;
481 res.nK = nK;
482 res.nD = nD;
483 res.nsamples = n;
484 res.steps = totsteps;
485 res.latime = latime;
486 res.simtime = simtime;
487 return res;
488}
489
490} // namespace detail
491
492/**
493 * COSIMLA estimate of a stationary expectation of a DTMC.
494 *
495 * @param A center set, one state per row
496 * @param K 0-based row indices of A forming the regeneration set
497 * @param trans successors of a state and their probabilities (summing to 1)
498 * @param reward one or more rewards per state
499 * @param options nsamples, seed, maxsteps, conflevel
500 * @return estimate, delta-method confidence interval and diagnostics; the
501 * field alphaTrunc is the estimate under plain truncation to A
502 * @throws InputError (LINE:input:Argument / LINE:input:Option) on bad input,
503 * NumericError (LINE:numerical:NonConvergence) when an excursion
504 * exceeds options.maxsteps
505 */
506inline CosimlaResult dtmc_cosimla(const std::vector<std::vector<double>>& A, const std::vector<std::size_t>& K,
507 const CosimlaTransition& trans, const CosimlaReward& reward,
508 const CosimlaOptions& options = CosimlaOptions()) {
509 return detail::cosimla_core(A, K, trans, reward, options, false, "dtmc_cosimla");
510}
511
512/**
513 * COSIMLA estimate of a stationary expectation of a CTMC, through its
514 * embedded jump chain; trans returns transition rates (a successor equal to
515 * the state itself is ignored). alphaTrunc drops the arcs leaving A, as
516 * SolverCTMC does under a cutoff. Otherwise as dtmc_cosimla.
517 */
518inline CosimlaResult ctmc_cosimla(const std::vector<std::vector<double>>& A, const std::vector<std::size_t>& K,
519 const CosimlaTransition& trans, const CosimlaReward& reward,
520 const CosimlaOptions& options = CosimlaOptions()) {
521 return detail::cosimla_core(A, K, trans, reward, options, true, "ctmc_cosimla");
522}
523
524} // namespace mc
525} // namespace line
526
527#endif // LINE_API_MC_CTMC_COSIMLA_H
InputError(const std::string &what)
Definition error.h:105
NumericError(const std::string &what)
Definition error.h:112
Steady-state distribution of a continuous-time Markov chain.
Equilibrium distribution of a discrete-time Markov chain, and stochastic complementation.
The exception types the port throws.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
double z(double v)
Definition lqn_focus.h:261
double tot(const std::vector< double > &pops)
double var(const std::vector< double > &x)
Sample variance with the n - 1 denominator.
std::vector< T > ctmc_solve(const Matrix< T > &Qin, const std::string &linsolver=std::string())
Steady-state distribution of a continuous-time Markov chain.
Definition ctmc_solve.h:133
CosimlaResult dtmc_cosimla(const std::vector< std::vector< double > > &A, const std::vector< std::size_t > &K, const CosimlaTransition &trans, const CosimlaReward &reward, const CosimlaOptions &options=CosimlaOptions())
COSIMLA estimate of a stationary expectation of a DTMC.
CosimlaResult ctmc_cosimla(const std::vector< std::vector< double > > &A, const std::vector< std::size_t > &K, const CosimlaTransition &trans, const CosimlaReward &reward, const CosimlaOptions &options=CosimlaOptions())
COSIMLA estimate of a stationary expectation of a CTMC, through its embedded jump chain; trans return...
std::function< std::vector< double >(const std::vector< double > &)> CosimlaReward
Reward function returning one or more rewards per state.
std::vector< T > dtmc_solve(const Matrix< T > &P)
Stationary distribution of a stochastic matrix P.
Definition dtmc_solve.h:106
std::function< CosimlaSuccessors(const std::vector< double > &)> CosimlaTransition
One-step transition function; it must accept any state the chain can reach.
double sim_norminv(double p)
Standard normal quantile function.
Definition sim_dist.h:62
Matrix< double > trans(const Matrix< double > &A)
Definition mg1.h:932
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
void lu_solve(const Matrix< T > &LU, const std::vector< std::size_t > &piv, std::vector< T > &b)
Solve LUx = Pb in place on b, using the factors from lu_factor.
Definition lu.h:94
std::vector< std::size_t > lu_factor(Matrix< T > &A)
In-place LU of A (n x n).
Definition lu.h:48
Normal and Student t quantiles used by the output-analysis routines.
Options, with the MATLAB defaults.
double conflevel
confidence level of the interval
std::size_t nsamples
paths per boundary state (at least 2)
double maxsteps
cap on one excursion; reaching it is an error
unsigned seed
seed of the MT19937 stream
Estimate, confidence interval and diagnostics.
std::vector< double > piK
stationary law of the chain watched on K
std::vector< double > alpha
estimate of pi*r, one entry per reward
std::vector< double > alphaTrunc
plain truncation on the same center
std::vector< std::vector< double > > ci
R rows of [lower, upper].
Matrix< double > PK
estimated transition matrix on K
std::vector< double > stderr_
standard error (trailing _ avoids the stdio macro)
std::vector< double > halfwidth
std::size_t steps
simulated transitions in total
std::vector< std::vector< double > > D
boundary states, in discovery order
Successor states and their weights (probabilities for a DTMC, rates for a CTMC).
std::vector< std::vector< double > > states
std::vector< double > weights