5#ifndef LINE_SOLVERS_CTMC_SOLVER_CTMC_CHAIN_H
6#define LINE_SOLVERS_CTMC_SOLVER_CTMC_CHAIN_H
53using lang::GlobalConstants;
67namespace chain_detail {
77bool chain_isvalid(
const std::vector<T>& pi, std::size_t n) {
78 if (pi.size() != n)
return false;
80 for (std::size_t i = 0; i < n; ++i) {
82 if (!std::isfinite(v))
return false;
92 if (chain.state_space.rows() > 0)
return chain.state_space;
109 const std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
112 const std::size_t n = chain.
mat.rows();
113 if (n == 0 || chain.
mat.cols() != n)
114 throw InputError(
"solver_ctmc_chain: the chain matrix is empty or not square");
129 out.
state_space = chain_detail::chain_space(chain, n);
130 out.
runtime = std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
151 const std::vector<T>& pi0in,
const T& t0in,
153 const std::size_t n = chain.
mat.rows();
154 if (n == 0 || chain.
mat.cols() != n)
155 throw InputError(
"solver_ctmc_chain_transient: the chain matrix is empty or not square");
157 std::vector<T> pi0 = pi0in;
160 }
else if (pi0.size() != n) {
161 throw InputError(
"solver_ctmc_chain_transient: the initial distribution has the wrong length");
164 for (std::size_t i = 0; i < n; ++i) total += num_traits<T>::to_double(pi0[i]);
166 throw InputError(
"solver_ctmc_chain_transient: the initial distribution must sum to one");
170 if (!std::isfinite(d1))
172 "solver_ctmc_chain_transient: a finite timespan is required, e.g. --timespan 0,T");
175 if (!std::isfinite(d0)) d0 = 0.0;
181 const long long k0 =
static_cast<long long>(std::ceil(d0));
182 const long long k1 =
static_cast<long long>(std::floor(d1));
184 throw InputError(
"solver_ctmc_chain_transient: the timespan contains no integer step of "
186 const std::size_t steps =
static_cast<std::size_t
>(k1 - k0 + 1);
191 std::vector<T> pik = pi0;
193 std::vector<T> next(n);
194 for (
long long k = 0; k < k0; ++k) {
195 for (std::size_t j = 0; j < n; ++j) {
197 for (std::size_t i = 0; i < n; ++i) acc += pik[i] * P(i, j);
205 for (std::size_t s = 0; s < steps; ++s) {
207 for (std::size_t j = 0; j < n; ++j) out.
pi_t(s, j) = pik[j];
208 for (std::size_t j = 0; j < n; ++j) {
210 for (std::size_t i = 0; i < n; ++i) acc += pik[i] * P(i, j);
Steady-state distribution of a continuous-time Markov chain.
Limiting distribution of a CTMC whose generator may be reducible.
Transient distribution of a CTMC over a time interval, by integrating the forward equations d pi/dt =...
Normalize a non-negative matrix into a stochastic transition matrix.
Equilibrium distribution of a discrete-time Markov chain, and stochastic complementation.
Limiting distribution of a discrete-time Markov chain whose transition matrix may be reducible.
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
The MarkovProcess / MarkovChain object surface.
Dense matrix and non-owning view.
CtmcChainTransientSolution< T > solver_ctmc_chain_transient(const MarkovChainModel< T > &chain, const std::vector< T > &pi0in, const T &t0in, const T &t1)
Transient distribution of a user-supplied Markov chain over [t0,t1].
CtmcChainSolution< T > solver_ctmc_chain(const MarkovChainModel< T > &chain)
Steady-state analysis of a user-supplied Markov chain.
lang::processes::MarkovChainModel< T > MarkovChainModel
A user-supplied chain: a MarkovProcess when discrete is false, else a MarkovChain.
ReducibleResult< T > dtmc_solve_reducible(const Matrix< T > &P, const std::vector< T > &pin, double zeroColTol=1e-12)
Limiting distribution of a discrete-time Markov chain whose transition matrix may be reducible.
std::vector< T > ctmc_solve(const Matrix< T > &Qin)
Steady-state distribution of a continuous-time Markov chain.
ReducibleResult< T > ctmc_solve_reducible(const Matrix< T > &Q, const std::vector< T > &pi0, double zeroColTol=1e-12)
Limiting distribution of a CTMC whose generator may be reducible.
std::vector< T > dtmc_solve(const Matrix< T > &P)
Stationary distribution of a stochastic matrix P.
TransientResult< T > ctmc_transient(const Matrix< T > &Q, const std::vector< T > &pi0, const T &t0, const T &t1, double rtol=1e-3, double atol=1e-6)
Transient distribution of a CTMC over a time interval, by integrating the forward equations d pi/dt =...
Number-type abstraction for the templated API port.
Matrix< T > state_space
the chain's space, or the state indices when it carries none
std::vector< T > pi
stationary distribution, length n
Matrix< T > infgen
Q for a CTMC, the uniformized P-I for a DTMC.
Matrix< T > pi_t
one row per time point
std::vector< T > t
the time points; integer STEPS for a DTMC
static constexpr double FineTol
A user-supplied chain: a MarkovProcess when discrete is false, else a MarkovChain.
Matrix< T > mat
generator Q (CTMC) or transition matrix P (DTMC)
bool discrete
true for a MarkovChain
std::vector< T > t
accepted time points, the first being t0
Matrix< T > pi
one row per time point