LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
distributions.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_LANG_DISTRIBUTIONS_H
6#define LINE_LANG_DISTRIBUTIONS_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * @ingroup line_public
12 * The distributions a user names, spelled as their Python twins.
13 *
14 * queue1.set_service(closed, Exp(1.0));
15 * delay.set_service(open, Erlang(3.0, 2));
16 * delay.set_service(open, HyperExp(0.5, 3.0, 10.0));
17 *
18 * against Python's `Exp(1.0)`, `Erlang(3.0, 2)`, `HyperExp(0.5, 3.0, 10.0)`.
19 *
20 * EACH IS A THIN NAME OVER THE FACTORY THAT ALREADY EXISTS. Every constructor
21 * assigns the `Distrib<double>` that `lang_types.h` builds, so the numeric
22 * behaviour is the factory's and nothing new is computed here; deriving from
23 * `Distrib<double>` is what lets one slice cleanly into `set_service`, which
24 * takes the base by const reference.
25 *
26 * The fitters keep Python's static-method spelling
27 * (`HyperExp::fit_mean_and_scv`), forwarding to `line/lang/dist_fitters.h`.
28 *
29 * `double`-only, as Python is. Reach for `lang::Distrib<T>`'s own factories
30 * directly for the multiprecision paths.
31 */
32
33#include <cmath>
34#include <cstddef>
35#include <fstream>
36#include <string>
37#include <vector>
38
41#include "line/util/error.h"
42
43namespace line {
44
46
47/** Every named law below is a `Distrib<double>` under another name. */
48#define LINE_DIST_CTOR(Cls) \
49 struct Cls : lang::Distrib<double>
50
51/** `Exp(rate)`: the exponential law of the given RATE, as Python's `Exp`. */
53 explicit Exp(double rate) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::exp_rate(rate); }
54 /** `Exp.fitMean(mean)`. */
55 static Exp fit_mean(double m) { return Exp(1.0 / m); }
56 /** `Exp.fitRate(rate)`. */
57 static Exp fit_rate(double r) { return Exp(r); }
58};
59
60/** `Erlang(phase_rate, nphases)`. */
62 Erlang(double phase_rate, std::size_t nphases) {
63 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::erlang(phase_rate, nphases);
64 }
65 /** `Erlang.fitMeanAndSCV(mean, scv)`. */
66 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
68 }
69 /** `Erlang.fitMeanAndOrder(mean, k)`. */
70 static lang::Distrib<double> fit_mean_and_order(double m, std::size_t k) {
72 }
73};
74
75/** `HyperExp(p, lambda1, lambda2)`. */
76LINE_DIST_CTOR(HyperExp) {
77 HyperExp(double p, double lambda1, double lambda2) {
78 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::hyperexp(p, lambda1, lambda2);
79 }
80 HyperExp(const std::vector<double>& p, const std::vector<double>& lambda) {
81 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::hyperexp_n(p, lambda);
82 }
83 /** `HyperExp.fitMeanAndSCV(mean, scv)`. */
84 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
86 }
87 /** `HyperExp.fitMeanAndSCVBalanced(mean, scv)`. */
88 static lang::Distrib<double> fit_mean_and_scv_balanced(double m, double scv) {
90 }
91};
92
93/** `Coxian(mu, phi)`. */
95 Coxian(const std::vector<double>& mu, const std::vector<double>& phi) {
96 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::coxian(mu, phi);
97 }
98 /** `Coxian.fitMeanAndSCV(mean, scv)`. */
99 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
101 }
102 /** `Coxian.fitCentral(mean, var, skew)`: the SECOND argument is the variance, not the SCV. */
103 static lang::Distrib<double> fit_central(double m, double var, double skew) {
104 return lang::coxian_fit_central<double>(m, var, skew);
105 }
106};
107
108/** `Cox2(mu1, mu2, phi1)`. */
110 Cox2(double mu1, double mu2, double phi1) {
111 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::cox2(mu1, mu2, phi1);
112 }
113 /** `Cox2.fitMeanAndSCV(mean, scv)`: the Coxian two-moment fit, two phases for SCV >= 1/2. */
114 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
116 }
117 /** `Cox2.fitCentral(mean, var, skew)`: the SECOND argument is the variance, not the SCV. */
118 static lang::Distrib<double> fit_central(double m, double var, double skew) {
119 return lang::cox2_fit_central<double>(m, var, skew);
120 }
121};
122
123/** `PH(alpha, A)`: a general phase-type law. */
125 PH(const std::vector<double>& alpha, const Matrix<double>& A) {
126 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::phase_type(alpha, A, false);
127 }
128};
129
130/** `APH(alpha, A)`: the acyclic phase-type law. */
132 APH(const std::vector<double>& alpha, const Matrix<double>& A) {
133 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::phase_type(alpha, A, true);
134 }
135 /** `APH.fitMeanAndSCV(mean, scv)`. */
136 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
137 return lang::aph_fit_mean_scv<double>(m, scv);
138 }
139 /** `APH.fitCentral(mean, var, skew)`: the SECOND argument is the variance, not the SCV. */
140 static lang::Distrib<double> fit_central(double m, double var, double skew) {
141 return lang::aph_fit_central<double>(m, var, skew);
142 }
143};
144
145/** `MAP(D0, D1)`. */
147 MAP(const Matrix<double>& D0, const Matrix<double>& D1) {
149 }
150};
151
152/** `MMPP(Q, lambda)`: the MAP with D0 = Q - diag(lambda), D1 = diag(lambda). */
154 MMPP(const Matrix<double>& Q, const std::vector<double>& lambda) {
155 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::mmpp(Q, lambda);
156 }
157};
158
159/** `Det(t)`: the deterministic law. */
161 explicit Det(double t) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::det(t); }
162};
163
164/** `Immediate()`: a zero-time transition. */
165LINE_DIST_CTOR(Immediate) {
166 Immediate() { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::immediate(); }
167};
168
169/** `Disabled()`: the class is not served here. */
170LINE_DIST_CTOR(Disabled) {
171 Disabled() { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::disabled_dist(); }
172};
173
174/** `Uniform(a, b)`. */
176 Uniform(double a, double b) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::uniform(a, b); }
177};
178
179/** `Pareto(shape, scale)`. */
181 Pareto(double shape, double scale) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::pareto(shape, scale); }
182 /** `Pareto.fitMeanAndSCV(mean, scv)`. */
183 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
185 }
186};
187
188/** `Gamma(shape, scale)`. */
190 Gamma(double shape, double scale) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::gamma_dist(shape, scale); }
191 /** `Gamma.fitMeanAndSCV(mean, scv)`. */
192 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
194 }
195};
196
197/** `Weibull(scale, shape)`. */
199 Weibull(double scale, double shape) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::weibull(scale, shape); }
200};
201
202/** `LogLogistic(shape, scale)`, stored as getParam {alpha = scale, beta = shape}. */
203LINE_DIST_CTOR(LogLogistic) {
204 LogLogistic(double shape, double scale) {
205 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::loglogistic(shape, scale);
206 }
207 /**
208 * `LogLogistic.fitMeanAndSCV(mean, scv)`: tan(b)/b - 1 rises monotonically
209 * from 0 to Inf on b in (0, pi/2), so b = pi/beta is bracketed and bisected
210 * and the scale follows from the mean, alpha = mean sin(b)/b.
211 */
212 static lang::Distrib<double> fit_mean_and_scv(double m, double scv) {
213 if (!(m > 0.0) || !(scv > 0.0) || !std::isfinite(scv))
214 throw InputError("LogLogistic.fitMeanAndSCV requires a positive mean and a positive finite SCV");
215 const double pi = 3.14159265358979323846;
216 double lo = 1e-8, hi = pi / 2.0 * (1.0 - 2.220446049250313e-16);
217 for (int i = 0; i < 200 && hi - lo > 1e-15 * hi; ++i) {
218 const double mid = 0.5 * (lo + hi);
219 if (std::tan(mid) / mid - 1.0 < scv) lo = mid; else hi = mid;
220 }
221 const double b = 0.5 * (lo + hi);
222 return lang::Distrib<double>::loglogistic(pi / b, m * std::sin(b) / b);
223 }
224};
225
226/** `MassAtOrigin(inner, p0)`: zero with probability p0, else `inner`. */
227LINE_DIST_CTOR(MassAtOrigin) {
228 MassAtOrigin(const lang::Distrib<double>& inner, double p0) {
229 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::mass_at_origin(inner, p0);
230 }
231};
232
233/**
234 * `Defective(inner, pInf)`: infinite with probability pInf, else `inner`. A
235 * lifetime law only: its mean is Inf, so no station accepts it.
236 */
237LINE_DIST_CTOR(Defective) {
238 Defective(const lang::Distrib<double>& inner, double pInf) {
239 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::defective(inner, pInf);
240 }
241};
242
243/** `Lognormal(logmean, logsigma)`. */
244LINE_DIST_CTOR(Lognormal) {
245 Lognormal(double logmean, double logsigma) {
246 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::lognormal(logmean, logsigma);
247 }
248};
249
250/** `Normal(mu, sigma)`. */
252 Normal(double mu, double sigma) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::normal(mu, sigma); }
253};
254
255/** `Geometric(p)`. */
256LINE_DIST_CTOR(Geometric) {
257 explicit Geometric(double p) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::geometric(p); }
258};
259
260/** `Bernoulli(p)`. */
261LINE_DIST_CTOR(Bernoulli) {
262 explicit Bernoulli(double p) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::bernoulli(p); }
263};
264
265/** `Binomial(n, p)`. */
266LINE_DIST_CTOR(Binomial) {
267 Binomial(double n, double p) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::binomial(n, p); }
268};
269
270/** `Poisson(lambda)`. */
272 explicit Poisson(double lambda) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::poisson(lambda); }
273};
274
275/** `DiscreteUniform(a, b)`. */
276LINE_DIST_CTOR(DiscreteUniform) {
277 DiscreteUniform(double a, double b) {
279 }
280};
281
282/** `Zipf(s, n)`. */
284 Zipf(double s, std::size_t n) { static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::zipf(s, n); }
285};
286
287/** `DiscreteSampler(p, x)`. */
288LINE_DIST_CTOR(DiscreteSampler) {
289 explicit DiscreteSampler(const std::vector<double>& p) {
290 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::discrete_sampler(p, std::vector<double>());
291 }
292 DiscreteSampler(const std::vector<double>& p, const std::vector<double>& x) {
294 }
295};
296
297/** `Replayer(samples)` / `Replayer(path)`: the trace-driven law. */
298LINE_DIST_CTOR(Replayer) {
299 explicit Replayer(const std::vector<double>& samples) {
300 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::replayer(samples);
301 }
302 explicit Replayer(const std::string& path) {
303 std::ifstream in(path.c_str());
304 if (!in) throw InputError("Replayer: cannot open the trace file '" + path + "'");
305 std::vector<double> samples;
306 double value = 0.0;
307 while (in >> value) samples.push_back(value);
308 if (!in.eof())
309 throw InputError("Replayer: the trace file '" + path + "' contains a non-number");
310 if (samples.empty())
311 throw InputError("Replayer: the trace file '" + path + "' has no samples");
312 static_cast<lang::Distrib<double>&>(*this) =
314 }
315 Replayer(const std::vector<double>& samples, const std::string& path) {
316 static_cast<lang::Distrib<double>&>(*this) = lang::Distrib<double>::replayer_from(samples, path);
317 }
318};
319
320#undef LINE_DIST_CTOR
321
322} // namespace line
323
324#endif // LINE_LANG_DISTRIBUTIONS_H
InputError(const std::string &what)
Definition error.h:105
The moment fitters the reference distributions carry as STATIC FACTORIES: Erlang.fitMeanAndOrder,...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Distrib< T > hyperexp_fit_mean_scv_balanced(const T &mean, const T &scv)
HyperExp.fitMeanAndSCVBalanced(MEAN, SCV): the balanced-means branch, p / mu1 = (1 - p) / mu2.
Distrib< T > aph_fit_central(const T &mean, const T &var, const T &skew)
APH.fitCentral(MEAN, VAR, SKEW): the three central moments converted to raw ones and matched by a can...
Distrib< T > erlang_fit_mean_order(const T &mean, std::size_t k)
Erlang.fitMeanAndOrder(MEAN, k): k phases, each of rate k / MEAN.
Distrib< T > coxian_fit_central(const T &mean, const T &var, const T &skew)
Coxian.fitCentral, which the reference forwards to Cox2.fitCentral.
Distrib< T > aph_fit_mean_scv(const T &mean, const T &scv)
APH.fitMeanAndSCV(MEAN, SCV), through mam::aph_fit_mean_scv.
Distrib< T > gamma_fit_mean_scv(const T &mean, const T &scv)
Gamma.fitMeanAndSCV(MEAN, SCV): shape 1/SCV, scale MEAN * SCV.
Distrib< T > pareto_fit_mean_scv(const T &mean, const T &scv)
Pareto.fitMeanAndSCV(MEAN, SCV): alpha = 1 + sqrt(1 + 1/SCV) and k = MEAN (alpha - 1) / alpha.
Distrib< T > coxian_fit_mean_scv(const T &mean, const T &scv)
Coxian.fitMeanAndSCV(MEAN, SCV), branch for branch.
Distrib< T > hyperexp_fit_mean_scv(const T &mean, const T &scv)
HyperExp.fitMeanAndSCV(MEAN, SCV), which is map_hyperexp at p = 0.99 read back as (p,...
Distrib< T > cox2_fit_central(const T &mean, const T &var, const T &skew)
Cox2.fitCentral(MEAN, VAR, SKEW): the two-phase Coxian matching three central moments exactly when th...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
lang::Distrib< double > Distribution
LINE_DIST_CTOR(Exp)
Exp(rate): the exponential law of the given RATE, as Python's Exp.
static Distrib loglogistic(const T &shape, const T &scale)
LogLogistic(shape, scale), stored in getParam order {alpha = scale, beta = shape}.
static Distrib replayer(const std::vector< T > &samples)
Replayer / Trace: the samples, with their empirical first two moments.
static Distrib normal(const T &mu, const T &sigma)
Normal(mu, sigma): the Gaussian, for use as a continuous Prior's parameter density.
static Distrib defective(const Distrib &in, const T &pInf)
Defective(inner, pInf): infinite with probability pInf, else inner.
static Distrib exp_rate(const T &r)
static Distrib phase_type(const std::vector< T > &alpha, const Matrix< T > &A, bool acyclic)
PH / APH given by (alpha, A): D0 = A and D1 = (-A e) alpha.
static Distrib weibull(const T &scale, const T &shape)
static Distrib replayer_from(const std::vector< T > &samples, const std::string &path)
Replayer / Trace read FROM A FILE, which keeps the path beside the samples.
static Distrib mass_at_origin(const Distrib &in, const T &p0)
MassAtOrigin(inner, p0): zero with probability p0, else inner.
static Distrib disabled_dist()
static Distrib erlang_fit(const T &m, const T &c2)
Erlang fitted to a mean and an SCV, as MATLAB's Erlang.fitMeanAndSCV.
static Distrib pareto(const T &shape, const T &scale)
Pareto(shape, scale), with the MATLAB parameter order (alpha, k).
static Distrib gamma_dist(const T &shape, const T &scale)
Gamma(shape, scale), Weibull(scale, shape) and Lognormal(mu, sigma).
static Distrib cox2(const T &mu1, const T &mu2, const T &phi1)
Cox2(mu1, mu2, phi1), MATLAB's two-phase Coxian constructor.
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.
static Distrib poisson(const T &lambda)
Poisson(lambda), whose SCV is 1/lambda – the count's variance is lambda and its mean is lambda,...
static Distrib bernoulli(const T &p)
Bernoulli(p): one trial, mean p and variance p(1-p).
static Distrib hyperexp_n(const std::vector< T > &p, const std::vector< T > &lambda)
The same for any number of branches, which is what MATLAB HyperExp accepts and what the writers emit ...
static Distrib discrete_uniform(const T &a, const T &b)
DiscreteUniform(a, b) over the integers a..b inclusive.
static Distrib geometric(const T &p)
Geometric(p) on the MATLAB convention: the NUMBER OF TRIALS to the first success, support {1,...
static Distrib det(const T &m)
static Distrib uniform(const T &a, const T &b)
Uniform(a, b).
static Distrib lognormal(const T &logmean, const T &logsigma)
static Distrib hyperexp(const T &p, const T &lambda1, const T &lambda2)
HyperExp(p, lambda1, lambda2): phase i chosen with probability p_i.
static Distrib immediate()
The point mass at zero.
static Distrib erlang(const T &phase_rate, std::size_t r)
Erlang(alpha, r): r phases of rate alpha, as MATLAB's Erlang(phaseRate, nphases).
static Distrib coxian(const std::vector< T > &mu, const std::vector< T > &phi)
Coxian(mu, phi): phase i completes with probability phi(i) and otherwise moves to phase i+1.
static Distrib discrete_sampler(const std::vector< T > &p, const std::vector< T > &x)
DiscreteSampler(p, x): the pmf p over the points x.
static Distrib mmpp(const Matrix< T > &Q, const std::vector< T > &lambda)
MMPP(Q, lambda): the Markov-modulated Poisson process, i.e.
static Distrib binomial(const T &n, const T &p)
Binomial(n, p).
static Distrib zipf(const T &s, std::size_t n)
Zipf(s, n) over the ranks 1..n, with the generalized harmonic moments H(s-1,n)/H(s,...