5#ifndef LINE_API_SN_SN_NONMARKOV_TOPH_H
6#define LINE_API_SN_SN_NONMARKOV_TOPH_H
124 std::string m = method;
125 for (
char& c : m) c =
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
126 if (m ==
"none")
return false;
127 if (m ==
"bernstein")
return true;
129 "options.config.nonmkv expects one of 'bernstein', 'none', got '" + method +
"'.");
138 case ProcessType::EXP:
139 case ProcessType::ERLANG:
140 case ProcessType::HYPEREXP:
141 case ProcessType::PH:
142 case ProcessType::APH:
143 case ProcessType::MAP:
144 case ProcessType::DMAP:
145 case ProcessType::MMAP:
148 case ProcessType::MPH:
149 case ProcessType::MMAPT:
150 case ProcessType::MPHT:
151 case ProcessType::BMMAPT:
152 case ProcessType::BMAP:
153 case ProcessType::ME:
154 case ProcessType::RAP:
155 case ProcessType::COXIAN:
156 case ProcessType::COX2:
157 case ProcessType::MMPP2:
158 case ProcessType::IMMEDIATE:
159 case ProcessType::DISABLED:
176 return p == ProcessType::NHPP || p == ProcessType::MAPT || p == ProcessType::PHT ||
177 p == ProcessType::NNPP;
185std::function<double(
double)> nonmkv_density(
const lang::Distrib<T>& d) {
187 std::vector<double> p;
188 for (std::size_t i = 0; i < d.params.size(); ++i)
189 p.push_back(num_traits<T>::to_double(d.params[i]));
191 case ProcessType::GAMMA: {
192 if (p.size() < 2)
break;
193 const double k = p[0], th = p[1];
194 return [k, th](
double x) {
195 if (!(x > 0.0))
return 0.0;
196 return std::exp((k - 1.0) * std::log(x) - x / th - k * std::log(th) -
200 case ProcessType::WEIBULL: {
201 if (p.size() < 2)
break;
202 const double a = p[0], r = p[1];
203 return [a, r](
double x) {
204 if (!(x > 0.0))
return 0.0;
205 return (r / a) * std::pow(x / a, r - 1.0) * std::exp(-std::pow(x / a, r));
208 case ProcessType::LOGNORMAL: {
209 if (p.size() < 2)
break;
210 const double mu = p[0], sg = p[1];
211 return [mu, sg](
double x) {
212 if (!(x > 0.0))
return 0.0;
213 const double z = (std::log(x) - mu) / sg;
214 return std::exp(-0.5 * z * z) / (x * sg * std::sqrt(2.0 * 3.14159265358979323846));
217 case ProcessType::LOGLOGISTIC: {
218 if (p.size() < 2)
break;
219 const double a = p[0], b = p[1];
220 return [a, b](
double x) {
221 if (!(x > 0.0))
return 0.0;
222 const double z = x / a;
223 const double zb = std::pow(z, b);
224 return (b / a) * std::pow(z, b - 1.0) / ((1.0 + zb) * (1.0 + zb));
227 case ProcessType::PARETO: {
228 if (p.size() < 2)
break;
229 const double al = p[0], k = p[1];
230 return [al, k](
double x) {
231 if (!(x >= k) || !(x > 0.0))
return 0.0;
232 return al * std::pow(k, al) / std::pow(x, al + 1.0);
235 case ProcessType::UNIFORM: {
236 if (p.size() < 2)
break;
237 const double a = p[0], b = p[1];
238 return [a, b](
double x) {
return (x >= a && x <= b) ? 1.0 / (b - a) : 0.0; };
243 return std::function<double(double)>();
256std::function<double(
double)> nonmkv_ccdf(
const lang::Distrib<T>& d) {
258 if (d.type != ProcessType::GAMMA && d.type != ProcessType::WEIBULL &&
259 d.type != ProcessType::LOGNORMAL && d.type != ProcessType::PARETO &&
260 d.type != ProcessType::LOGLOGISTIC)
261 return std::function<double(double)>();
262 const lang::Distrib<T> law = d;
263 return [law](
double x) {
264 if (!(x > 0.0))
return 1.0;
265 return 1.0 - num_traits<T>::to_double(
lang::dist_cdf(law, num_traits<T>::from_double(x)));
279mam::Map<T> nonmkv_longtail(
const std::function<
double(
double)>& ccdf,
double mean,
bool& ok) {
284 mam::HyperexpLongtailResult<double> fit;
287 }
catch (
const std::exception&) {
290 const std::size_t n = fit.p.size();
291 if (n == 0)
return out;
292 Matrix<T> D0(n, n, num_traits<T>::from_int(0));
293 Matrix<T> D1(n, n, num_traits<T>::from_int(0));
294 for (std::size_t i = 0; i < n; ++i) {
295 if (!std::isfinite(fit.lambda[i]) || fit.lambda[i] <= 0.0)
return out;
296 D0(i, i) = num_traits<T>::from_double(-fit.lambda[i]);
297 for (std::size_t j = 0; j < n; ++j)
298 D1(i, j) = num_traits<T>::from_double(fit.lambda[i] * fit.p[j]);
313mam::Map<T> nonmkv_concentrated(
double mean,
double scv, std::size_t order,
PhFit phfit) {
317 const double s = scv > 1e-12 ? scv : 1e-12;
320 return mam::map_erlang(num_traits<T>::from_double(mean),
static_cast<unsigned>(order));
325void nonmkv_install(lang::Distrib<T>& d,
const mam::Map<T>& m) {
326 std::vector<Matrix<T>> blocks;
327 blocks.push_back(m.D0);
328 blocks.push_back(m.D1);
333 std::shared_ptr<lang::Distrib<T>> declared(
new lang::Distrib<T>(d));
334 declared->declared.reset();
341 d.declared = declared;
359bool nonmkv_firing_ready(
const qn::TransitionParam<T>& np, std::size_t m) {
360 if (m >= np.firingphases.size() || np.firingphases[m] <= 0)
return false;
361 if (m >= np.firingproc.size())
return false;
362 return np.firingproc[m].D0.rows() ==
static_cast<std::size_t
>(np.firingphases[m]);
377 for (std::size_t ist = 0; ist <
sn.nstations; ++ist)
378 for (std::size_t r = 0; r <
sn.nclasses; ++r) {
379 if (ist <
sn.disabled.size() && r <
sn.disabled[ist].size() &&
sn.disabled[ist][r])
383 if (detail::nonmkv_is_markovian(d.
type) || detail::nonmkv_is_scheduled(d.
type))
385 if (d.
type == ProcessType::DET && preserve_det)
continue;
389 sn.transparam.begin();
390 it !=
sn.transparam.end(); ++it) {
392 for (std::size_t m = 0; m < np.
firingproc.size(); ++m) {
393 if (detail::nonmkv_firing_ready(np, m))
continue;
396 if (detail::nonmkv_is_markovian(d.
type) || detail::nonmkv_is_scheduled(d.
type))
398 if (d.
type == ProcessType::DET && preserve_det)
continue;
415 "sn_nonmarkov_toph evaluates densities and fits moments");
417 if (!opts.enabled)
return;
418 if (opts.order == 0)
throw InputError(
"sn_nonmarkov_toph: the phase budget must be positive");
420 for (std::size_t ist = 0; ist <
sn.nstations; ++ist) {
421 for (std::size_t r = 0; r <
sn.nclasses; ++r) {
422 if (ist <
sn.disabled.size() && r <
sn.disabled[ist].size() &&
sn.disabled[ist][r])
426 const ProcessType p = d.
type;
427 if (detail::nonmkv_is_markovian(p) || detail::nonmkv_is_scheduled(p))
continue;
428 if (p == ProcessType::DET && opts.preserve_det)
continue;
432 if (!std::isfinite(mean) || mean <= 0.0)
continue;
434 if (p == ProcessType::DET) {
435 detail::nonmkv_install(
437 static_cast<unsigned>(opts.order)));
441 if (p == ProcessType::MASSATORIGIN) {
457 const std::function<double(
double)> ccdf = detail::nonmkv_ccdf(d);
460 const mam::Map<T> lt = detail::nonmkv_longtail<T>(ccdf, mean, ok);
462 detail::nonmkv_install(d, lt);
475 if (std::isfinite(scv) && scv >= 1.0) {
480 if (!std::isfinite(scv)) {
483 detail::nonmkv_longtail<T>(detail::nonmkv_ccdf(d), mean, ok);
485 detail::nonmkv_install(d, lt);
491 const std::function<double(
double)> pdf = detail::nonmkv_density(d);
493 detail::nonmkv_install(d,
494 detail::nonmkv_concentrated<T>(mean, scv, opts.order,
498 if (opts.phfit ==
PhFit::Cme && scv >= 0.0 && scv < 1.0) {
499 detail::nonmkv_install(d,
500 detail::nonmkv_concentrated<T>(mean, scv, opts.order,
507 detail::nonmkv_install(d, fit);
512 for (std::size_t ind = 0; ind <
sn.nodes.size(); ++ind) {
513 if (
sn.nodes[ind].nodetype != qn::NodeType::Transition)
continue;
514 typename std::map<std::size_t, qn::TransitionParam<T>>::iterator it =
515 sn.transparam.find(ind + 1);
516 if (it ==
sn.transparam.end())
continue;
518 for (std::size_t m = 0; m < np->
firingproc.size(); ++m) {
522 if (detail::nonmkv_firing_ready(*np, m))
continue;
525 const ProcessType p = d.
type;
526 if (detail::nonmkv_is_markovian(p) || detail::nonmkv_is_scheduled(p))
continue;
527 if (p == ProcessType::DET && opts.preserve_det)
continue;
531 if (p == ProcessType::DET) {
533 const double mu = (std::isfinite(mean) && mean > 0.0) ? mean : 1.0;
535 static_cast<unsigned>(opts.order));
542 const std::function<double(
double)> pdf = detail::nonmkv_density(d);
544 if (!std::isfinite(mean) || mean <= 0.0) {
549 static_cast<unsigned>(opts.order));
555 }
catch (
const std::exception&) {
557 static_cast<unsigned>(opts.order));
561 detail::nonmkv_install(d, fit);
Acyclic phase-type fitters from the first two moments.
A network plus its refreshed NetworkStruct.
Concentrated matrix exponentials, and the two-moment fit built on them.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Fitting a hyperexponential to a long-tail distribution.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Acyclic phase-type approximation of an arbitrary density by Bernstein exponentials.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
bool sn_has_nonmarkov(const qn::NetworkStruct< T > &sn, bool preserve_det=false)
Whether any law in the struct would be replaced, so a caller can skip copying the struct when there i...
bool nonmkv_enabled(const std::string &method)
Resolve options.config.nonmkv to NonmarkovOptions::enabled.
PhFit
Which Markovian surrogate to fit; options.config.phfit.
@ Ph
Bernstein density fit: a genuine phase-type, shape-carrying.
@ Hyperexp
Mixture of exponentials fitted to the ccdf ITSELF across decades of time scale (hyperexp_fit_longtail...
@ Cme
concentrated ME plus exponential tail: two moments, exactly
bool sn_is_phasetype(const std::vector< Matrix< T > > &maps, const std::vector< T > &pie)
Whether a (D0, D1, ...) list is a valid phase-type / MAP representation.
void sn_nonmarkov_toph(qn::NetworkStruct< T > &sn, const NonmarkovOptions &opts=NonmarkovOptions())
Replace every non-Markovian service and firing law by a Markovian surrogate.
T dist_cdf(const Distrib< T > &d, const T &x)
F(x) = P{X <= x}, MATLAB's Distribution.evalCDF.
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
void dist_refresh_moments(Distrib< T > &d)
Fill in the first two moments of a distribution given by its matrices.
Map< T > aph_fit_mean_scv(const T &mean, const T &scv)
Port of APH.fitMeanAndSCV, the entry point the analyzers fit arrivals with.
HyperexpLongtailResult< T > hyperexp_fit_longtail(Ccdf &&ccdf, const T &b=num_traits< T >::from_rational(3, 2), const T &decade=num_traits< T >::from_int(4), const T &c1=num_traits< T >::from_int(0))
The fit with the component count chosen automatically: one per decade between the 0....
Map< T > map_bernstein(const std::function< double(double)> &f, unsigned order=20)
Acyclic phase-type approximation of an arbitrary density by Bernstein exponentials.
Map< T > dist_fit_me(double mean, double scv, std::size_t maxPhases=0)
Two-moment matrix-exponential fit for 0 < scv < 1, a port of dist_fit_me.m.
Map< T > map_erlang(const T &mean, unsigned k)
Erlang-k renewal MAP with the given mean (map_erlang.m).
Map< T > map_scale(const Map< T > &in, const T &new_mean)
Rescale time so that the mean inter-arrival time becomes new_mean.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
Whether a (D0, D1, ...) list is a valid phase-type / MAP representation.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
options.config.nonmkv and friends.
std::size_t order
nonmkvorder, the phase budget
PhFit phfit
which surrogate family
bool preserve_det
Leave Det alone for the exact MAP/D/c branch, options.config.preserveDet.
bool enabled
false is the reference's nonmkv = 'none'
static Distrib map_dist(const Matrix< T > &D0, const Matrix< T > &D1, ProcessType tag)
A MAP given by its two matrices; the moments are those of its stationary phase.
A MAP as the pair of matrices (D0, D1).
std::size_t order() const
The parameters of a Transition node: MATLAB sn.nodeparam{ind} for an SPN transition,...
std::vector< lang::Distrib< T > > firingproc
firing distribution per mode
std::vector< std::size_t > firingphases
phase count per mode, 0 when non-Markovian