LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
fj_cox_fit.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_FJ_COX_FIT_H
6#define LINE_API_FJ_COX_FIT_H
7
8/**
9 * @file
10 * @ingroup api_fj
11 * Two-stage Coxian fit of a mean and a squared coefficient of variation.
12 *
13 * Templated port of matlab/src/api/fj/fj_cox_fit.m.
14 *
15 * Marie's balanced-stage condition 1/mu1 = q/mu2 closes the system of two
16 * moment equations in three unknowns and gives
17 *
18 * mu1 = 2 mu, q = 1/(2 c2), mu2 = 2 mu q = mu/c2,
19 *
20 * which needs q <= 1, hence c2 >= 0.5. The Erlang stage count representing the
21 * same target is bracketed by ceil(1/c2) <= k <= floor(1/c2) + 1.
22 */
23
24#include <cmath>
25
27#include "line/num/number.h"
28#include "line/util/error.h"
29
30namespace line {
31namespace fj {
32
33/** [mu1, mu2, q, kmin, kmax] of fj_cox_fit. */
34template <class T>
36 T mu1;
37 T mu2;
38 T q;
39 unsigned kmin;
40 unsigned kmax;
41};
42
43/**
44 * @brief Two-stage Coxian fit of a mean and a squared coefficient of
45 * variation.
46 *
47 * @param m1 target mean, m1 > 0
48 * @param c2 target squared coefficient of variation, c2 >= 0.5
49 * @return the two stage rates, the branching probability and the Erlang bracket
50 */
51template <class T>
52FJCoxFitResult<T> fj_cox_fit(const T& m1, const T& c2) {
53 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1),
55 if (!(m1 > zero)) throw InputError("fj_cox_fit: the target mean must be positive");
56 if (c2 < one / two)
57 throw InputError("fj_cox_fit: the balanced-stage Coxian fit needs c2 >= 0.5");
58
59 const T mu = one / m1;
61 // Both stages contribute half of the mean
62 out.mu1 = two * mu;
63 out.q = one / (two * c2);
64 out.mu2 = mu / c2;
65
66 const double inv = 1.0 / num_traits<T>::to_double(c2);
67 long lo = static_cast<long>(std::ceil(inv - 1e-12));
68 long hi = static_cast<long>(std::floor(inv + 1e-12)) + 1;
69 if (lo < 1) lo = 1;
70 if (hi < lo) hi = lo;
71 out.kmin = static_cast<unsigned>(lo);
72 out.kmax = static_cast<unsigned>(hi);
73 return out;
74}
75
76} // namespace fj
77} // namespace line
78
79#endif // LINE_API_FJ_COX_FIT_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Shared return types and arithmetic helpers for the templated fork-join port.
FJCoxFitResult< T > fj_cox_fit(const T &m1, const T &c2)
Two-stage Coxian fit of a mean and a squared coefficient of variation.
Definition fj_cox_fit.h:52
Number-type abstraction for the templated API port.
[mu1, mu2, q, kmin, kmax] of fj_cox_fit.
Definition fj_cox_fit.h:35